feat(tab-bar): add clickable close buttons to tabs
Tabs could only be closed with the keyboard or the middle mouse button, which is not discoverable. Draw a small cross at the start of each horizontal tab, close the tab when it is clicked, and avoid starting a drag from the button.
This commit is contained in:
3 files changed
+64
-6
No files matched your search
+28
-1
@@ -348,6 +348,10 @@ def template_uses_max_title_length(template: str) -> bool:
|
|||||||
# Not across updates, as templates can use state not in TabBarData, such as
|
# Not across updates, as templates can use state not in TabBarData, such as
|
||||||
# the progress of the windows in the tab.
|
# the progress of the windows in the tab.
|
||||||
title_cache: dict[tuple[DrawData, TabBarData, int, int], str] | None = None
|
title_cache: dict[tuple[DrawData, TabBarData, int, int], str] | None = None
|
||||||
|
# Cells occupied by the close buttons drawn in the last layout pass, mapped to
|
||||||
|
# the id of the tab they close. None while titles are being measured, so that
|
||||||
|
# only the cells of the final drawing are kept.
|
||||||
|
close_button_cells: dict[tuple[int, int], int] | None = None
|
||||||
|
|
||||||
|
|
||||||
@contextmanager
|
@contextmanager
|
||||||
@@ -582,9 +586,14 @@ def draw_title(draw_data: DrawData, screen: Screen, tab: TabBarData, index: int,
|
|||||||
# Mark the truncation, since the caller's ellipsis logic only fires
|
# Mark the truncation, since the caller's ellipsis logic only fires
|
||||||
# when the final line reaches the full width of the tab.
|
# when the final line reaches the full width of the tab.
|
||||||
del lines[limit:]
|
del lines[limit:]
|
||||||
lines[-1] = truncate_line(lines[-1], draw_data.wrap_width or max_title_length)
|
lines[-1] = truncate_line(lines[-1], draw_data.wrap_width or title_width)
|
||||||
title = '\n'.join(lines)
|
title = '\n'.join(lines)
|
||||||
before_draw = screen.cursor.x
|
before_draw = screen.cursor.x
|
||||||
|
if show_close:
|
||||||
|
if close_button_cells is not None:
|
||||||
|
close_button_cells[(before_draw, screen.cursor.y)] = tab.tab_id
|
||||||
|
close_button_cells[(before_draw + 1, screen.cursor.y)] = tab.tab_id
|
||||||
|
screen.draw('× ')
|
||||||
draw_attributed_string(title, screen)
|
draw_attributed_string(title, screen)
|
||||||
if draw_data.max_tab_title_length > 0:
|
if draw_data.max_tab_title_length > 0:
|
||||||
x_limit = before_draw + draw_data.max_tab_title_length
|
x_limit = before_draw + draw_data.max_tab_title_length
|
||||||
@@ -882,6 +891,7 @@ class TabBar:
|
|||||||
self.data_buffer_size = 0
|
self.data_buffer_size = 0
|
||||||
self.blank_rects: tuple[Border, ...] = ()
|
self.blank_rects: tuple[Border, ...] = ()
|
||||||
self.tab_extents: Sequence[TabExtent] = ()
|
self.tab_extents: Sequence[TabExtent] = ()
|
||||||
|
self.close_button_cells: dict[tuple[int, int], int] = {}
|
||||||
self.window_drop_insert_before: int | None = None
|
self.window_drop_insert_before: int | None = None
|
||||||
self.tab_drop_insert_before: int | None = None
|
self.tab_drop_insert_before: int | None = None
|
||||||
self.laid_out_once = False
|
self.laid_out_once = False
|
||||||
@@ -1067,6 +1077,7 @@ class TabBar:
|
|||||||
self.blank_rects = tuple(blank_rects)
|
self.blank_rects = tuple(blank_rects)
|
||||||
|
|
||||||
def layout(self) -> None:
|
def layout(self) -> None:
|
||||||
|
self.close_button_cells = {}
|
||||||
central, tab_bar, vw, vh, cell_width, cell_height = viewport_for_window(self.os_window_id)
|
central, tab_bar, vw, vh, cell_width, cell_height = viewport_for_window(self.os_window_id)
|
||||||
if self.is_vertical:
|
if self.is_vertical:
|
||||||
if tab_bar.width < cell_width or tab_bar.height < cell_height:
|
if tab_bar.width < cell_width or tab_bar.height < cell_height:
|
||||||
@@ -1220,11 +1231,15 @@ class TabBar:
|
|||||||
s.erase_in_line(2, False)
|
s.erase_in_line(2, False)
|
||||||
cr: list[TabExtent] = []
|
cr: list[TabExtent] = []
|
||||||
ed.for_layout = False
|
ed.for_layout = False
|
||||||
|
global close_button_cells
|
||||||
|
close_button_cells = {}
|
||||||
for i, t in enumerate(data):
|
for i, t in enumerate(data):
|
||||||
try:
|
try:
|
||||||
draw_tab(i, t, cr, max_tab_lengths[i])
|
draw_tab(i, t, cr, max_tab_lengths[i])
|
||||||
except StopIteration:
|
except StopIteration:
|
||||||
break
|
break
|
||||||
|
self.close_button_cells = close_button_cells
|
||||||
|
close_button_cells = None
|
||||||
self.tab_extents = cr
|
self.tab_extents = cr
|
||||||
s.erase_in_line(0, False) # Ensure no long titles bleed after the last tab
|
s.erase_in_line(0, False) # Ensure no long titles bleed after the last tab
|
||||||
self.align()
|
self.align()
|
||||||
@@ -1235,6 +1250,7 @@ class TabBar:
|
|||||||
s = self.screen
|
s = self.screen
|
||||||
self.last_laid_out_tabs = data
|
self.last_laid_out_tabs = data
|
||||||
self.tab_extents = ()
|
self.tab_extents = ()
|
||||||
|
self.close_button_cells = {}
|
||||||
s.cursor.x = s.cursor.y = 0
|
s.cursor.x = s.cursor.y = 0
|
||||||
s.erase_in_display(2, False)
|
s.erase_in_display(2, False)
|
||||||
if not data:
|
if not data:
|
||||||
@@ -1383,6 +1399,17 @@ class TabBar:
|
|||||||
return te.tab_id
|
return te.tab_id
|
||||||
return 0
|
return 0
|
||||||
|
|
||||||
|
def close_tab_id_at(self, x: int, y: int) -> int:
|
||||||
|
"The id of the tab whose close button is at the specified pixel, or zero."
|
||||||
|
if not self.laid_out_once or not self.close_button_cells:
|
||||||
|
return 0
|
||||||
|
g = self.window_geometry
|
||||||
|
if not (g.left <= x < g.right and g.top <= y < g.bottom):
|
||||||
|
return 0
|
||||||
|
x = (x - g.left) // self.cell_width
|
||||||
|
y = (y - g.top) // self.cell_height
|
||||||
|
return self.close_button_cells.get((x, y), 0)
|
||||||
|
|
||||||
def drag_axis_coordinate(self, x: int, y: int) -> int:
|
def drag_axis_coordinate(self, x: int, y: int) -> int:
|
||||||
return y if self.is_vertical else x
|
return y if self.is_vertical else x
|
||||||
|
|
||||||
|
|||||||
@@ -2093,9 +2093,19 @@ class TabManager: # {{{
|
|||||||
self.recent_tab_bar_mouse_events.clear()
|
self.recent_tab_bar_mouse_events.clear()
|
||||||
return
|
return
|
||||||
if button == GLFW_MOUSE_BUTTON_LEFT:
|
if button == GLFW_MOUSE_BUTTON_LEFT:
|
||||||
|
close_tab_id = self.tab_bar.close_tab_id_at(int(x), int(y))
|
||||||
if action == GLFW_PRESS:
|
if action == GLFW_PRESS:
|
||||||
|
if close_tab_id:
|
||||||
|
return # clicking the close button must not start a drag
|
||||||
set_tab_being_dragged(tab.id, False, x, y)
|
set_tab_being_dragged(tab.id, False, x, y)
|
||||||
return
|
return
|
||||||
|
if close_tab_id:
|
||||||
|
if self.recent_tab_bar_mouse_events.click_count(GLFW_MOUSE_BUTTON_LEFT) == 1:
|
||||||
|
set_tab_being_dragged()
|
||||||
|
self.recent_tab_bar_mouse_events.clear()
|
||||||
|
if (tab_to_close := self.tab_for_id(close_tab_id)) is not None:
|
||||||
|
get_boss().close_tab(tab_to_close)
|
||||||
|
return
|
||||||
match self.recent_tab_bar_mouse_events.click_count(GLFW_MOUSE_BUTTON_LEFT):
|
match self.recent_tab_bar_mouse_events.click_count(GLFW_MOUSE_BUTTON_LEFT):
|
||||||
case 2:
|
case 2:
|
||||||
self.set_active_tab(tab)
|
self.set_active_tab(tab)
|
||||||
|
|||||||
+26
-5
@@ -677,17 +677,38 @@ class TestTabBar(BaseTest):
|
|||||||
def horizontal_tab_bar_line(self, titles: list[str], columns: int, boss: object | None = None, **opts: object) -> str:
|
def horizontal_tab_bar_line(self, titles: list[str], columns: int, boss: object | None = None, **opts: object) -> str:
|
||||||
return str(self.horizontal_tab_bar(titles, columns, boss, **opts).screen.line(0)).rstrip()
|
return str(self.horizontal_tab_bar(titles, columns, boss, **opts).screen.line(0)).rstrip()
|
||||||
|
|
||||||
|
def test_horizontal_tab_bar_close_buttons(self) -> None:
|
||||||
|
tb = self.horizontal_tab_bar(['one', 'two'], 40, tab_bar_style='separator')
|
||||||
|
line = str(tb.screen.line(0))
|
||||||
|
self.ae(line.count('×'), 2)
|
||||||
|
# each close button is two cells wide, at the start of its tab
|
||||||
|
self.ae(set(tb.close_button_cells.values()), {1, 2})
|
||||||
|
for (cell_x, row), tab_id in tb.close_button_cells.items():
|
||||||
|
px = tb.window_geometry.left + cell_x * tb.cell_width + 1
|
||||||
|
py = tb.window_geometry.top + row * tb.cell_height + 1
|
||||||
|
self.ae(tb.close_tab_id_at(px, py), tab_id)
|
||||||
|
# clicking the title itself does not close the tab
|
||||||
|
for te in tb.tab_extents:
|
||||||
|
px = tb.window_geometry.left + te.x.end * tb.cell_width
|
||||||
|
py = tb.window_geometry.top + 1
|
||||||
|
self.ae(tb.close_tab_id_at(px, py), 0)
|
||||||
|
self.ae(tb.close_tab_id_at(0, 0), 0)
|
||||||
|
|
||||||
|
# vertical tab bars keep closing with the middle mouse button
|
||||||
|
self.ae(self.vertical_tab_bar().close_button_cells, {})
|
||||||
|
|
||||||
def test_horizontal_tab_bar_uses_free_space(self) -> None:
|
def test_horizontal_tab_bar_uses_free_space(self) -> None:
|
||||||
line = self.horizontal_tab_bar_line
|
line = self.horizontal_tab_bar_line
|
||||||
# space not needed by short tabs goes to the long ones, instead of
|
# space not needed by short tabs goes to the long ones, instead of
|
||||||
# being left unused at the end of the tab bar
|
# being left unused at the end of the tab bar
|
||||||
long_title = 'abcdefghijklmn'
|
long_title = 'abcdefghijklmn'
|
||||||
|
# two extra cells per tab are needed for its close button
|
||||||
for style, columns, titles in (
|
for style, columns, titles in (
|
||||||
('fade', 25, ['1', long_title]),
|
('fade', 29, ['1', long_title]),
|
||||||
('fade', 35, ['1'] * 2 + [long_title]),
|
('fade', 41, ['1'] * 2 + [long_title]),
|
||||||
('separator', 60, ['1'] * 8 + [long_title] * 2),
|
('separator', 80, ['1'] * 8 + [long_title] * 2),
|
||||||
('powerline', 80, ['1'] * 5 + [long_title] * 3),
|
('powerline', 96, ['1'] * 5 + [long_title] * 3),
|
||||||
('slant', 126, ['1'] * 9 + [long_title] * 4),
|
('slant', 152, ['1'] * 9 + [long_title] * 4),
|
||||||
):
|
):
|
||||||
text = line(titles, columns, tab_bar_style=style)
|
text = line(titles, columns, tab_bar_style=style)
|
||||||
self.ae(text.count(long_title), titles.count(long_title), (style, text))
|
self.ae(text.count(long_title), titles.count(long_title), (style, text))
|
||||||
|
|||||||
Reference in new issue
Block a user