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)