Skip to content
Merged
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
117 changes: 106 additions & 11 deletions src/formbridge/widgets/sidebar.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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):
Expand Down Expand Up @@ -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],
Expand All @@ -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:
Expand All @@ -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)
Expand Down
Loading