0c340c4f74
* Prevent multiple inclusion of variables in CSS files * Use dict instead of tuple for variables * Add comments to variables * Improve appearance of main window * Tweak main window styles * Use json.dumps over pprint.format * Make study button primary * Improve header margin * Make bottom toolbar slimmer * Make congrats page more balanced * Fix type issue * Replace day/night with light/dark * Exclude top-level-drag-row from hover effect * Create dataclass for variables * Run formatter * Apply CSS variables from Python side Why go full-circle with the Sass variables? This way we only need one interface for add-on authors to interact with. It also makes it easier for us to apply additional themes in the future. * Fix typing * Fix rgba values in Qt * Darken button background * Fix palette not being applied in light theme For some odd reason this problem arose much later than #2016. * Tweak default button look * Reformat * Apply CSS vars to ts pages * Include elevation in button_mixins_lib * Cast opacity to int * Add some margin to studiedToday info * Tweak light theme button gradient * Tweak highlight-bg for light theme * Add back default button color as it made the browser sidebar tool icons dark in light theme. * Reformat * Tweak light theme buttons once more Sorry for the back-and-forth. Sass only compiles when there are changes in user files, not when I only change the vars. * Fix bottom toolbar button indicators * Make buttons more clicky * Fix button padding * Handle macOS separately again * Decrease elevation effect for main window buttons to 1 * Imitate box-shadow for Qt elements * Adjust shadow vars * Adjust primary border color because the save button in the deck options had a lighter color than its background gradient. * Boost box-shadow color of primary buttons * Format * Adjust Qt box-shadow imitation and shadow colors * Use more subtle default shadow color * Add some more padding to top toolbar * Revert "Apply CSS vars to ts pages" This reverts commit 5d8e7f6b7ffc8894b6517ecbb8cfba35407fc69a. * Revert "Apply CSS variables from Python side" This reverts commit 87db774412fd2bfd75e2630d2c5e782daef96b5f. * Better match the standard macOS buttons In the dark theme the standard color is a lighter grey, but at least the size/shape is similar again. This doesn't work for the editor buttons. * Reduce the top margin of the congrats screen * Fix illegible buttons when changing theme on macOS; match dark button style
181 lines
5.8 KiB
Python
181 lines
5.8 KiB
Python
# Copyright: Ankitects Pty Ltd and contributors
|
|
# License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
|
|
from typing import cast
|
|
|
|
from aqt import colors
|
|
from aqt.qt import *
|
|
from aqt.theme import theme_manager
|
|
|
|
|
|
class Switch(QAbstractButton):
|
|
"""A horizontal slider to toggle between two states which can be denoted by strings and/or QIcons.
|
|
|
|
The left state is the default and corresponds to isChecked()=False.
|
|
The suppoorted slots are toggle(), for an animated transition, and setChecked().
|
|
"""
|
|
|
|
def __init__(
|
|
self,
|
|
radius: int = 10,
|
|
left_label: str = "",
|
|
right_label: str = "",
|
|
left_color: dict[str, str] = colors.ACCENT_CARD | {},
|
|
right_color: dict[str, str] = colors.ACCENT_NOTE | {},
|
|
parent: QWidget = None,
|
|
) -> None:
|
|
super().__init__(parent=parent)
|
|
self.setCheckable(True)
|
|
super().setChecked(False)
|
|
self.setSizePolicy(QSizePolicy.Policy.Fixed, QSizePolicy.Policy.Fixed)
|
|
self._left_label = left_label
|
|
self._right_label = right_label
|
|
self._left_color = left_color
|
|
self._right_color = right_color
|
|
self._path_radius = radius
|
|
self._knob_radius = radius - 2
|
|
self._label_padding = 4
|
|
self._left_knob_position = self._position = radius
|
|
self._right_knob_position = self.width - self._path_radius
|
|
self._left_label_position = self._label_padding / 2
|
|
self._right_label_position = 2 * self._knob_radius
|
|
|
|
@pyqtProperty(int) # type: ignore
|
|
def position(self) -> int:
|
|
return self._position
|
|
|
|
@position.setter # type: ignore
|
|
def position(self, position: int) -> None:
|
|
self._position = position
|
|
self.update()
|
|
|
|
@property
|
|
def start_position(self) -> int:
|
|
return (
|
|
self._left_knob_position if self.isChecked() else self._right_knob_position
|
|
)
|
|
|
|
@property
|
|
def end_position(self) -> int:
|
|
return (
|
|
self._right_knob_position if self.isChecked() else self._left_knob_position
|
|
)
|
|
|
|
@property
|
|
def label(self) -> str:
|
|
return self._right_label if self.isChecked() else self._left_label
|
|
|
|
@property
|
|
def path_color(self) -> QColor:
|
|
color = self._right_color if self.isChecked() else self._left_color
|
|
return theme_manager.qcolor(color)
|
|
|
|
@property
|
|
def label_width(self) -> int:
|
|
font = QFont()
|
|
font.setPixelSize(int(self._knob_radius))
|
|
font.setWeight(QFont.Weight.Bold)
|
|
fm = QFontMetrics(font)
|
|
return (
|
|
max(
|
|
fm.horizontalAdvance(self._left_label),
|
|
fm.horizontalAdvance(self._right_label),
|
|
)
|
|
+ 2 * self._label_padding
|
|
)
|
|
|
|
@property
|
|
def width(self) -> int:
|
|
return self.label_width + 2 * self._path_radius
|
|
|
|
@property
|
|
def height(self) -> int:
|
|
return 2 * self._path_radius
|
|
|
|
def sizeHint(self) -> QSize:
|
|
return QSize(
|
|
self.width,
|
|
self.height,
|
|
)
|
|
|
|
def setChecked(self, checked: bool) -> None:
|
|
super().setChecked(checked)
|
|
self._position = self.end_position
|
|
self.update()
|
|
|
|
def paintEvent(self, _event: QPaintEvent) -> None:
|
|
painter = QPainter(self)
|
|
painter.setRenderHint(QPainter.RenderHint.Antialiasing, True)
|
|
painter.setPen(Qt.PenStyle.NoPen)
|
|
self._paint_path(painter)
|
|
self._paint_label(painter)
|
|
self._paint_knob(painter)
|
|
|
|
def _current_path_rectangle(self) -> QRectF:
|
|
return QRectF(
|
|
0,
|
|
0,
|
|
self.width,
|
|
self.height,
|
|
)
|
|
|
|
def _current_label_rectangle(self) -> QRectF:
|
|
return QRectF(
|
|
self._left_label_position
|
|
if self.isChecked()
|
|
else self._right_label_position,
|
|
0,
|
|
self.label_width,
|
|
self.height,
|
|
)
|
|
|
|
def _current_knob_rectangle(self) -> QRectF:
|
|
return QRectF(
|
|
self.position - self._knob_radius, # type: ignore
|
|
2,
|
|
2 * self._knob_radius,
|
|
2 * self._knob_radius,
|
|
)
|
|
|
|
def _paint_path(self, painter: QPainter) -> None:
|
|
painter.setBrush(QBrush(self.path_color))
|
|
painter.drawRoundedRect(
|
|
self._current_path_rectangle(), self._path_radius, self._path_radius
|
|
)
|
|
|
|
def _paint_knob(self, painter: QPainter) -> None:
|
|
color = theme_manager.qcolor(colors.BUTTON_GRADIENT_START)
|
|
painter.setPen(Qt.PenStyle.NoPen)
|
|
painter.setBrush(QBrush(color))
|
|
painter.drawEllipse(self._current_knob_rectangle())
|
|
|
|
def _paint_label(self, painter: QPainter) -> None:
|
|
painter.setPen(theme_manager.qcolor(colors.CANVAS))
|
|
font = painter.font()
|
|
font.setPixelSize(int(self._knob_radius))
|
|
font.setWeight(QFont.Weight.Bold)
|
|
painter.setFont(font)
|
|
painter.drawText(
|
|
self._current_label_rectangle(), Qt.AlignmentFlag.AlignCenter, self.label
|
|
)
|
|
|
|
def mouseReleaseEvent(self, event: QMouseEvent) -> None:
|
|
super().mouseReleaseEvent(event)
|
|
if event.button() == Qt.MouseButton.LeftButton:
|
|
self._animate_toggle()
|
|
|
|
def enterEvent(self, event: QEnterEvent) -> None:
|
|
self.setCursor(Qt.CursorShape.PointingHandCursor)
|
|
super().enterEvent(event)
|
|
|
|
def toggle(self) -> None:
|
|
super().toggle()
|
|
self._animate_toggle()
|
|
|
|
def _animate_toggle(self) -> None:
|
|
animation = QPropertyAnimation(self, cast(QByteArray, b"position"), self)
|
|
animation.setDuration(100)
|
|
animation.setStartValue(self.start_position)
|
|
animation.setEndValue(self.end_position)
|
|
# make triggered events execute first so the animation runs smoothly afterwards
|
|
QTimer.singleShot(50, animation.start)
|