From 1e21b929f1289058214945b2cc676aea1f9d1e7c Mon Sep 17 00:00:00 2001 From: Tristan Simas Date: Tue, 17 Nov 2026 04:10:27 -0500 Subject: [PATCH] Qt sidebar toggle: rotate the heading on vertical edges MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Left and right sidebars are vertical columns, so their toggle bar's heading now rotates 90° (book-spine, bottom-to-top) to read along the bar's long axis instead of overflowing it. Top and bottom bars stay horizontal, unchanged. The orientation is not chosen inside the toggle: each PlacementEdge owns `title_rotation_degrees()` as part of its nominal contract (0 for horizontal strips, -90 for vertical columns). SidebarToggle reads that one method and rebuilds its own layout to match — no isinstance or per-edge name branches. Changes: - PlacementEdge protocol: add title_rotation_degrees(). - _RotatableTitle (new): ElidedLine with a rotation property. When rotated, sizeHint/minimumSizeHint swap axes and paintEvent rotates the painter, so the elided text still measures and lays out as a normal QWidget. - SidebarToggle: layout is now rebuilt by _apply_orientation() each present, mirroring how SidebarPane rebuilds its own axis. On a vertical edge the three children stack top-to-bottom and the bar's cross-axis minimum moves from height to width; on a horizontal edge the single-row layout is the old behaviour. Co-Authored-By: Claude Opus 4.7 Claude-Session: https://claude.ai/code/session_016gZLdupVQyBzHTmHBC9Cyn --- src/formbridge/widgets/sidebar.py | 117 +++++++++++++++++++++++++++--- 1 file changed, 106 insertions(+), 11 deletions(-) diff --git a/src/formbridge/widgets/sidebar.py b/src/formbridge/widgets/sidebar.py index a5e7b0e..61a4191 100644 --- a/src/formbridge/widgets/sidebar.py +++ b/src/formbridge/widgets/sidebar.py @@ -20,7 +20,8 @@ from collections.abc import Callable, Iterable from typing import Protocol, runtime_checkable -from PySide6.QtCore import Qt +from PySide6.QtCore import QRect, QSize, Qt +from PySide6.QtGui import QPainter, QPalette from PySide6.QtWidgets import QHBoxLayout, QLabel, QToolButton, QVBoxLayout, QWidget from pyqt_reactive.protocols.widget_adapters import PyQtWidgetMeta from pyqt_reactive.widgets.fitted_text import ElidedLine @@ -29,11 +30,15 @@ @runtime_checkable class PlacementEdge(Protocol): """Where a pane sits: an edge of the workspace. The widgets only need to know which end - of the pane is interior-facing and the arrow character for the toggle bar.""" + of the pane is interior-facing, the arrow character for the toggle bar, and how far the + toggle's heading is rotated so it reads along the bar.""" @classmethod def toggle_symbol(cls, collapsed: bool) -> str: ... + @classmethod + def title_rotation_degrees(cls) -> int: ... + @runtime_checkable class LeadingPlacementEdge(PlacementEdge, Protocol): @@ -203,9 +208,57 @@ def _controls_leading(placement: Placement | None) -> bool: ) +class _RotatableTitle(ElidedLine): + """An elided one-line heading that can be painted rotated 90° either way, so the same + widget reads correctly on a horizontal strip (top/bottom bar, rotation=0) or a vertical + column (left/right bar, rotation=-90, book-spine convention). + + Rotation is a property on the title itself, not on the surrounding layout: a nominal + axis each edge owns, read from the placement, not branched on the edge's identity.""" + + def __init__(self, text: str = "") -> None: + super().__init__(text) + self._rotation = 0 + + def set_rotation(self, degrees: int) -> None: + if self._rotation != degrees: + self._rotation = degrees + self.updateGeometry() + self.update() + + def sizeHint(self) -> QSize: # noqa: N802 - Qt virtual method name + hint = super().sizeHint() + return QSize(hint.height(), hint.width()) if self._rotation % 180 else hint + + def minimumSizeHint(self) -> QSize: # noqa: N802 - Qt virtual method name + hint = super().minimumSizeHint() + return QSize(hint.height(), hint.width()) if self._rotation % 180 else hint + + def paintEvent(self, event) -> None: # noqa: N802 - Qt virtual method name + if not self._rotation % 360: + super().paintEvent(event) + return + painter = QPainter(self) + painter.setPen(self.palette().color(QPalette.ColorRole.WindowText)) + # Qt's rotate() is clockwise; translate the origin to the corner the rotated text + # starts from so a paint rectangle of (widget.height × widget.width) maps onto the + # widget without clipping. + if self._rotation % 360 == 270 or self._rotation == -90: + painter.translate(0, self.height()) + else: # 90 or -270 + painter.translate(self.width(), 0) + painter.rotate(self._rotation) + paint_rect = QRect(0, 0, self.height(), self.width()) + flags, text = self.drawn(paint_rect) + painter.drawText(paint_rect, flags, text) + + class SidebarToggle(QWidget): """A sidebar's bar: an arrow and the pane's heading, nothing else. A press shows or hides - the pane; the arrow points the way the pane opens.""" + the pane; the arrow points the way the pane opens. On a vertical edge (left/right) the + heading rotates to read along the bar's long axis and the arrows stack above and below + it; on a horizontal edge they lie across a single row. The axis flip is driven by the + placement edge's own `title_rotation_degrees()` — no per-edge branches here.""" def __init__(self, layout: PlacementLayout, identity: object, *, heading: Callable[[], str], shown: Callable[[], bool], @@ -218,17 +271,21 @@ def __init__(self, layout: PlacementLayout, identity: object, *, self._heading = heading self._shown = shown self._toggled = toggled - self.setMinimumHeight(40) - row = QHBoxLayout(self) - row.setContentsMargins(4, 0, 4, 0) - row.setSpacing(4) self._left = QLabel(self) + self._left.setAlignment(Qt.AlignmentFlag.AlignCenter) self._right = QLabel(self) - self._title = ElidedLine("") + self._right.setAlignment(Qt.AlignmentFlag.AlignCenter) + self._title = _RotatableTitle("") self._title.setAlignment(Qt.AlignmentFlag.AlignCenter) - row.addWidget(self._left) - row.addWidget(self._title, 1) - row.addWidget(self._right) + # Outer is always QVBoxLayout; a horizontal arrangement is a QHBoxLayout nested + # inside it. Qt forbids replacing a widget's top-level layout, so the axis change + # happens by rebuilding the outer layout's contents, not by swapping the layout + # itself (mirroring SidebarPane._apply_layout_for). + self._outer = QVBoxLayout(self) + self._outer.setContentsMargins(4, 0, 4, 0) + self._outer.setSpacing(4) + self._current_rotation: int | None = None + self._apply_orientation(None) def mousePressEvent(self, event) -> None: # type: ignore[override] if event.button() == Qt.MouseButton.LeftButton: @@ -237,8 +294,46 @@ def mousePressEvent(self, event) -> None: # type: ignore[override] else: super().mousePressEvent(event) + def _apply_orientation(self, placement: Placement | None) -> None: + """Rebuild the bar's arrangement for the placement's edge. `None` is the pre-placement + default (initial build): horizontal, no rotation.""" + rotation = placement.side.title_rotation_degrees() if placement is not None else 0 + if rotation == self._current_rotation: + return + self._current_rotation = rotation + self._title.set_rotation(rotation) + # Reparent the three children back onto self; QLayout.takeAt() leaves the widgets + # alive but detaches them from the layout, so nothing is destroyed here. + for child in (self._left, self._title, self._right): + child.setParent(self) + while self._outer.count(): + item = self._outer.takeAt(0) + sub = item.layout() + if sub is not None: + while sub.count(): + sub.takeAt(0) + if rotation: + # Vertical bar — arrows stack above and below the rotated heading, and the + # bar's cross-axis minimum moves from height to width so it draws as a strip. + self._outer.addWidget(self._left) + self._outer.addWidget(self._title, 1) + self._outer.addWidget(self._right) + self.setMinimumHeight(0) + self.setMinimumWidth(40) + else: + row = QHBoxLayout() + row.setContentsMargins(0, 0, 0, 0) + row.setSpacing(4) + row.addWidget(self._left) + row.addWidget(self._title, 1) + row.addWidget(self._right) + self._outer.addLayout(row) + self.setMinimumWidth(0) + self.setMinimumHeight(40) + def present(self) -> None: placement = self._layout.get(self._identity) + self._apply_orientation(placement) shown = self._shown() heading = self._heading() arrow = placement.side.toggle_symbol(not shown)