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
|
||||
# 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
|
||||
|
||||
|
||||
Reference in new issue
Block a user