Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 44 additions & 31 deletions src/toad/qt/name_wave.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,36 @@ def band_ink(span: tuple[int, int], length: int, ink: QTextCharFormat) -> QTextL
return band


class QtNameWaveDelegate(BandCells, MultilinePreviewItemDelegate):
class QtWaveBandCache(BandCells):
"""Shared accent-band ink cache for Qt widgets waving a band across a bold title."""

KEPT = 256
"""How many rows' bold titles and band ink ranges are kept; past it they are cleared."""

def _init_wave_ink(self, accent: QColor) -> None:
self.accent = accent
self.accent_format = QTextCharFormat()
self.accent_format.setForeground(accent)
# Cached FormatRange sequences keyed by clamped (start, end) cell span so moving the
# accent band across rows or tabs allocates neither QTextCharFormat nor FormatRange per paint.
self.inks: dict[tuple[int, int], tuple[list[QTextLayout.FormatRange],]] = {}

def _set_wave_accent(self, accent: QColor) -> None:
self.accent = accent
self.accent_format.setForeground(accent)
self.inks.clear()

def cached_band_inks(self, span: tuple[int, int], length: int) -> tuple[list[QTextLayout.FormatRange],]:
key = (min(length, span[0]), min(length, span[1]))
kept = self.inks.get(key)
if kept is None:
if len(self.inks) >= self.KEPT:
self.inks.clear()
kept = self.inks[key] = ([band_ink(key, length, self.accent_format)],)
return kept


class QtNameWaveDelegate(QtWaveBandCache, MultilinePreviewItemDelegate):
"""The sidebar's row delegate: a line whose thread works (`SidebarItem.wave_span`) draws
its title bold with the band's cells in the accent, as Textual's `ViewLabel` paints its
first row. Other lines draw as pyqt-reactive prepares them.
Expand All @@ -43,21 +72,13 @@ class QtNameWaveDelegate(BandCells, MultilinePreviewItemDelegate):
2026-11-22), and the sidebar's rows were 28% of a Pixel 8a's GUI thread while a fleet
worked."""

KEPT = 256
"""How many rows' bold titles are kept; past it they are all dropped and made again."""

def __init__(self, accent: QColor, *args) -> None:
super().__init__(*args)
self.accent = accent
self.accent_format = QTextCharFormat()
self.accent_format.setForeground(accent)
self._init_wave_ink(accent)
# A row's prepared text and the same text with its title bold, by the prepared text's
# identity: pyqt-reactive hands the same prepared text back until the row's text,
# font or width changes, and a changed one is another object.
self.bold: dict[int, tuple[PreparedTextLayout, PreparedTextLayout, int]] = {}
# Cached FormatRange sequences keyed by clamped (start, end) cell span so moving the
# accent band across rows allocates neither QTextCharFormat nor FormatRange per paint.
self.inks: dict[tuple[int, int], tuple[list[QTextLayout.FormatRange],]] = {}
# How many characters the title of the row being drawn has, plus the row index and
# band span read during _prepared_text so text_inks does not re-query itemFromIndex
# or wave_running on the same paint.
Expand Down Expand Up @@ -92,6 +113,7 @@ def bold_titled(self, prepared: PreparedTextLayout) -> tuple[PreparedTextLayout,
font = QFont(title.font())
font.setBold(True)
bold = QTextLayout(title.text(), font)
bold.setCacheEnabled(True)
bold.setFormats(title.formats())
bold.setTextOption(title.textOption())
bold.beginLayout()
Expand All @@ -114,17 +136,7 @@ def text_inks(self, index):
span = item.band_drawn() if item is not None else None
if span is None:
return ()
length = self.title_length
key = (min(length, span[0]), min(length, span[1]))
kept = self.inks.get(key)
if kept is None:
if len(self.inks) >= self.KEPT:
self.inks.clear()
band = QTextLayout.FormatRange()
band.start, band.length = key[0], key[1] - key[0]
band.format = self.accent_format
kept = self.inks[key] = ([band],)
return kept
return self.cached_band_inks(span, self.title_length)


class QtClockedWave(NameWave):
Expand Down Expand Up @@ -238,7 +250,7 @@ def land(self) -> None:
self.picture.deleteLater()


class QtSessionTabBar(BandCells, TearOffTabBar):
class QtSessionTabBar(QtWaveBandCache, TearOffTabBar):
"""The workspace's tab bar: a busy tab's title waves as Textual's `SessionLabel` does.
The title's line is the label with its one cell of padding each side, as the TUI's tab
draws it, so the band covers the same characters. A tab that opens grows in and one that
Expand All @@ -251,19 +263,17 @@ class QtSessionTabBar(BandCells, TearOffTabBar):

def __init__(self, clock, accent: QColor, ink: QColor) -> None:
super().__init__()
self.clock, self.accent, self.ink = clock, accent, ink
self.accent_format = QTextCharFormat()
self.accent_format.setForeground(accent)
self.clock, self.ink = clock, ink
self._init_wave_ink(accent)
self.lines: dict[tuple[str, int], QTextLayout] = {}
self._shown_titles: dict[tuple[int, str, int], tuple[QRect, str]] = {}
self._tab_chrome: dict[tuple, QImage] = {}
self.waves: dict[str, QtTabWave] = {}
self.growing: dict[str, TabGrowth] = {}

def set_colours(self, accent: QColor, ink: QColor) -> None:
self.accent = accent
self.ink = ink
self.accent_format.setForeground(accent)
self._set_wave_accent(accent)
self._tab_chrome.clear()
self.update()

Expand Down Expand Up @@ -449,20 +459,23 @@ def paintEvent(self, event) -> None: # type: ignore[override]
line = layout.lineAt(0)
top = rect.top() + (rect.height() - line.height()) / 2
painter.setPen(self.ink)
band = band_ink((max(0, start - self.padding_cells), max(0, end - self.padding_cells)),
len(shown), self.accent_format)
layout.draw(painter, QPointF(rect.left() + (rect.width() - line.naturalTextWidth()) / 2, top), [band])
inks = self.cached_band_inks(
(max(0, start - self.padding_cells), max(0, end - self.padding_cells)),
len(shown),
)[0]
layout.draw(painter, QPointF(rect.left() + (rect.width() - line.naturalTextWidth()) / 2, top), inks)

def bold_line(self, shown: str, width: int) -> QTextLayout:
"""A working tab's title, bold, on one line ``width`` wide: laid out once for the
title and width, since a frame of its wave moves the band's ink and not the text."""
kept = self.lines.get((shown, width))
if kept is None:
if len(self.lines) >= QtNameWaveDelegate.KEPT:
if len(self.lines) >= self.KEPT:
self.lines.clear()
font = QFont(self.font())
font.setBold(True)
kept = self.lines[(shown, width)] = QTextLayout(shown, font)
kept.setCacheEnabled(True)
kept.beginLayout()
kept.createLine().setLineWidth(width)
kept.endLayout()
Expand Down
15 changes: 15 additions & 0 deletions tests/textual/test_name_wave_repaint.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,10 +122,25 @@ def paint_accent_xs() -> list[int]:

left = paint_accent_xs()
first_ink = delegate.inks[(0, 3)]
assert all(titled.paragraphs[0].cacheEnabled() for _orig, titled, _len in delegate.bold.values())
item.checks = 0
paint_accent_xs()
assert item.checks == 1
assert delegate.inks[(0, 3)] is first_ink
item.painted_span = (8, 11)
right = paint_accent_xs()
assert left and right and max(left) < min(right)

from toad.qt.name_wave import QtSessionTabBar
from ui_theme.clock import AnimationClock

bar = QtSessionTabBar(AnimationClock(lambda _dt, _cb: None), accent, QColor("#eeeeee"))
try:
layout = bar.bold_line("toad-qt", 120)
assert layout.cacheEnabled() is True
cached_inks = bar.cached_band_inks((1, 4), 7)
assert bar.cached_band_inks((1, 4), 7) is cached_inks
bar.set_colours(QColor("#00ff00"), QColor("#ffffff"))
assert bar.inks == {}
finally:
bar.deleteLater()
Loading