Coverage for graphica/gui/shell/rail.py: 95.5%
114 statements
« prev ^ index » next coverage.py v7.17.5, created at 2026-11-25 19:22 +0000
« prev ^ index » next coverage.py v7.17.5, created at 2026-11-25 19:22 +0000
1"""左端のアイコンの帯。押すと左パネルのページを替える。下にはダークモードと設定。"""
2from PySide6.QtCore import QRectF, QSize, Qt, Signal
3from PySide6.QtGui import QColor, QPainter
4from PySide6.QtWidgets import QButtonGroup, QToolButton, QVBoxLayout, QWidget
6from graphica.gui import motion, theme
7from graphica.gui.shell.icons import checkable_icon, themed_icon
9RAIL_WIDTH = 56
10RAIL_BUTTON_SIZE = 38
13def _rail_button(icon_name, tooltip, checkable):
14 button = QToolButton()
15 button.setObjectName("rail_button")
16 button.setToolTip(tooltip)
17 button.setCheckable(checkable)
18 button.setFixedSize(RAIL_BUTTON_SIZE, RAIL_BUTTON_SIZE)
19 button.setIconSize(QSize(20, 20))
20 button.setFocusPolicy(Qt.FocusPolicy.NoFocus)
21 button.setCursor(Qt.CursorShape.PointingHandCursor)
22 button.setProperty("iconName", icon_name)
23 return button
26# 左の帯の選んだ四角の動き: 全体の長さ(ms)、進む側の辺がその何割で着くか、後ろの辺がどれだけ遅れて動き出すか
27RAIL_SLIDE_MS = 340
28RAIL_LEAD_SHARE = 0.5
29RAIL_TRAIL_DELAY = 0.4
32def _ease_out(t):
33 return 1 - (1 - t) ** 3
36class ActivityRail(QWidget):
37 page_requested = Signal(str)
39 def __init__(self, parent=None):
40 super().__init__(parent)
41 self.setObjectName("activity_rail")
42 self.setAttribute(Qt.WidgetAttribute.WA_StyledBackground, True)
43 self.setFixedWidth(RAIL_WIDTH)
44 layout = QVBoxLayout(self)
45 layout.setContentsMargins(9, 8, 9, 8)
46 layout.setSpacing(4)
47 self._group = QButtonGroup(self)
48 self._group.setExclusive(True)
49 self._page_buttons = {}
50 self._top_count = 0
51 layout.addStretch(1)
52 # 選んだ四角が動いている間の位置(止まっていれば選んだボタンの位置に描く)と、四角が今いるボタン。
53 # ボタンを押すと、こちらが呼ばれる前に QButtonGroup がもう選び替えているので、選び替えからは動き出せない
54 self._sliding = None
55 self._plate_button = None
57 def add_page(self, key, icon_name, tooltip):
58 """上の並びにページのボタンを足す。最初のものを選んだ状態にする。"""
59 button = _rail_button(icon_name, tooltip, checkable=True)
60 button.setIcon(checkable_icon(icon_name))
61 button.clicked.connect(lambda _checked=False, k=key: self.select_page(k))
62 self._group.addButton(button)
63 self.layout().insertWidget(self._top_count, button, 0, Qt.AlignmentFlag.AlignHCenter)
64 self._top_count += 1
65 self._page_buttons[key] = button
66 if len(self._page_buttons) == 1:
67 button.setChecked(True)
68 self._plate_button = button
69 return button
71 def add_bottom_button(self, icon_name, tooltip, slot):
72 button = _rail_button(icon_name, tooltip, checkable=False)
73 button.setIcon(themed_icon(icon_name, size=20))
74 button.clicked.connect(slot)
75 self.layout().addWidget(button, 0, Qt.AlignmentFlag.AlignHCenter)
76 return button
78 def page_button(self, key):
79 return self._page_buttons.get(key)
81 def current_page(self):
82 for key, button in self._page_buttons.items(): 82 ↛ 85line 82 didn't jump to line 85 because the loop on line 82 didn't complete
83 if button.isChecked():
84 return key
85 return None
87 def select_page(self, key):
88 button = self._page_buttons[key]
89 button.setChecked(True)
90 start = self._plate_rect() if button is not self._plate_button else None
91 self._plate_button = button
92 # ページの切り替え(重い)を先に済ませ、描けてから四角を動かす
93 self.page_requested.emit(key)
94 if start is not None:
95 self._slide_plate(start, self._plate_rect())
96 self.update()
98 def _plate_rect(self):
99 button = self._plate_button
100 if button is None or not button.isVisible():
101 return None
102 return QRectF(button.geometry())
104 def _slide_plate(self, start, end):
105 """先に進む側の辺を先に、後ろの辺を遅れて動かす(四角が伸びてから縮み、にゅるっと移る)。"""
106 if start is None or end is None or start == end or not motion.is_enabled() or not self.isVisible():
107 self._sliding = None
108 return
109 downward = end.top() > start.top()
110 self._sliding = QRectF(start)
112 def step(progress):
113 lead = _ease_out(min(1.0, progress / RAIL_LEAD_SHARE))
114 trail = _ease_out(max(0.0, (progress - RAIL_TRAIL_DELAY) / (1.0 - RAIL_TRAIL_DELAY)))
115 top_t, bottom_t = (trail, lead) if downward else (lead, trail)
116 top = start.top() + (end.top() - start.top()) * top_t
117 bottom = start.bottom() + (end.bottom() - start.bottom()) * bottom_t
118 self._sliding = QRectF(start.left(), top, start.width(), bottom - top)
119 self.update()
121 def finish():
122 self._sliding = None
123 self.update()
125 motion.animate_value(self, "_motion_plate", 0.0, 1.0, step, finish, RAIL_SLIDE_MS, after_paint=True)
127 def paintEvent(self, event):
128 super().paintEvent(event)
129 rect = self._sliding if self._sliding is not None else self._plate_rect()
130 if rect is None: 130 ↛ 131line 130 didn't jump to line 131 because the condition on line 130 was never true
131 return
132 painter = QPainter(self)
133 painter.setRenderHint(QPainter.RenderHint.Antialiasing, True)
134 painter.setPen(Qt.PenStyle.NoPen)
135 painter.setBrush(QColor(theme.current_tokens()["accent"]))
136 painter.drawRoundedRect(rect, theme.RADIUS_LG, theme.RADIUS_LG)
137 painter.end()
139 def refresh_icons(self):
140 for button in self.findChildren(QToolButton, "rail_button"):
141 name = button.property("iconName")
142 if not name: 142 ↛ 143line 142 didn't jump to line 143 because the condition on line 142 was never true
143 continue
144 button.setIcon(checkable_icon(name) if button.isCheckable() else themed_icon(name, size=20))