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:
BlackAngelTV committed 2026-11-09 11:14:07 +01:00
1 parent 0a15964b3e
commit a8129b1d5d
3 files changed
+64 -6

No files matched your search

+28 -1
View File
@@ -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
# the progress of the windows in the tab.
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
@@ -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
# when the final line reaches the full width of the tab.
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)
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)
if draw_data.max_tab_title_length > 0:
x_limit = before_draw + draw_data.max_tab_title_length
@@ -882,6 +891,7 @@ class TabBar:
self.data_buffer_size = 0
self.blank_rects: tuple[Border, ...] = ()
self.tab_extents: Sequence[TabExtent] = ()
self.close_button_cells: dict[tuple[int, int], int] = {}
self.window_drop_insert_before: int | None = None
self.tab_drop_insert_before: int | None = None
self.laid_out_once = False
@@ -1067,6 +1077,7 @@ class TabBar:
self.blank_rects = tuple(blank_rects)
def layout(self) -> None:
self.close_button_cells = {}
central, tab_bar, vw, vh, cell_width, cell_height = viewport_for_window(self.os_window_id)
if self.is_vertical:
if tab_bar.width < cell_width or tab_bar.height < cell_height:
@@ -1220,11 +1231,15 @@ class TabBar:
s.erase_in_line(2, False)
cr: list[TabExtent] = []
ed.for_layout = False
global close_button_cells
close_button_cells = {}
for i, t in enumerate(data):
try:
draw_tab(i, t, cr, max_tab_lengths[i])
except StopIteration:
break
self.close_button_cells = close_button_cells
close_button_cells = None
self.tab_extents = cr
s.erase_in_line(0, False) # Ensure no long titles bleed after the last tab
self.align()
@@ -1235,6 +1250,7 @@ class TabBar:
s = self.screen
self.last_laid_out_tabs = data
self.tab_extents = ()
self.close_button_cells = {}
s.cursor.x = s.cursor.y = 0
s.erase_in_display(2, False)
if not data:
@@ -1383,6 +1399,17 @@ class TabBar:
return te.tab_id
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:
return y if self.is_vertical else x
+10
View File
@@ -2093,9 +2093,19 @@ class TabManager: # {{{
self.recent_tab_bar_mouse_events.clear()
return
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 close_tab_id:
return # clicking the close button must not start a drag
set_tab_being_dragged(tab.id, False, x, y)
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):
case 2:
self.set_active_tab(tab)
+26 -5
View File
@@ -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:
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:
line = self.horizontal_tab_bar_line
# space not needed by short tabs goes to the long ones, instead of
# being left unused at the end of the tab bar
long_title = 'abcdefghijklmn'
# two extra cells per tab are needed for its close button
for style, columns, titles in (
('fade', 25, ['1', long_title]),
('fade', 35, ['1'] * 2 + [long_title]),
('separator', 60, ['1'] * 8 + [long_title] * 2),
('powerline', 80, ['1'] * 5 + [long_title] * 3),
('slant', 126, ['1'] * 9 + [long_title] * 4),
('fade', 29, ['1', long_title]),
('fade', 41, ['1'] * 2 + [long_title]),
('separator', 80, ['1'] * 8 + [long_title] * 2),
('powerline', 96, ['1'] * 5 + [long_title] * 3),
('slant', 152, ['1'] * 9 + [long_title] * 4),
):
text = line(titles, columns, tab_bar_style=style)
self.ae(text.count(long_title), titles.count(long_title), (style, text))