mirror of
https://github.com/ankitects/anki.git
synced 2025-09-19 14:32:22 -04:00
Merge pull request #1188 from RumovZ/switch-color-2
Change switch color depending on state
This commit is contained in:
commit
47076b9ac5
2 changed files with 25 additions and 8 deletions
|
@ -1,6 +1,7 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
# Copyright: Ankitects Pty Ltd and contributors
|
||||
# License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
|
||||
from typing import Tuple
|
||||
|
||||
from aqt import colors
|
||||
from aqt.qt import *
|
||||
|
@ -21,6 +22,8 @@ class Switch(QAbstractButton):
|
|||
radius: int = 10,
|
||||
left_label: str = "",
|
||||
right_label: str = "",
|
||||
left_color: Tuple[str, str] = colors.FLAG4_BG,
|
||||
right_color: Tuple[str, str] = colors.FLAG3_BG,
|
||||
parent: QWidget = None,
|
||||
) -> None:
|
||||
super().__init__(parent=parent)
|
||||
|
@ -29,9 +32,10 @@ class Switch(QAbstractButton):
|
|||
self.setSizePolicy(QSizePolicy.Fixed, QSizePolicy.Fixed)
|
||||
self._left_label = left_label
|
||||
self._right_label = right_label
|
||||
self._path_radius = radius
|
||||
self._knob_radius = radius - self._margin
|
||||
self._left_position = self._position = self._path_radius + self._margin
|
||||
self._left_color = left_color
|
||||
self._right_color = right_color
|
||||
self._path_radius = radius - self._margin
|
||||
self._knob_radius = self._left_position = self._position = radius
|
||||
self._right_position = 3 * self._path_radius + self._margin
|
||||
|
||||
@pyqtProperty(int) # type: ignore
|
||||
|
@ -55,6 +59,11 @@ class Switch(QAbstractButton):
|
|||
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)
|
||||
|
||||
def sizeHint(self) -> QSize:
|
||||
return QSize(
|
||||
4 * self._path_radius + 2 * self._margin,
|
||||
|
@ -75,7 +84,7 @@ class Switch(QAbstractButton):
|
|||
self._paint_label(painter)
|
||||
|
||||
def _paint_path(self, painter: QPainter) -> None:
|
||||
painter.setBrush(QBrush(theme_manager.qcolor(colors.FRAME_BG)))
|
||||
painter.setBrush(QBrush(self.path_color))
|
||||
rectangle = QRectF(
|
||||
self._margin,
|
||||
self._margin,
|
||||
|
@ -87,19 +96,23 @@ class Switch(QAbstractButton):
|
|||
def _current_knob_rectangle(self) -> QRectF:
|
||||
return QRectF(
|
||||
self.position - self._knob_radius, # type: ignore
|
||||
2 * self._margin,
|
||||
0,
|
||||
2 * self._knob_radius,
|
||||
2 * self._knob_radius,
|
||||
)
|
||||
|
||||
def _paint_knob(self, painter: QPainter) -> None:
|
||||
painter.setBrush(QBrush(theme_manager.qcolor(colors.LINK)))
|
||||
if theme_manager.night_mode:
|
||||
color = QColor(theme_manager.DARK_MODE_BUTTON_BG_MIDPOINT)
|
||||
else:
|
||||
color = theme_manager.qcolor(colors.FRAME_BG)
|
||||
painter.setBrush(QBrush(color))
|
||||
painter.drawEllipse(self._current_knob_rectangle())
|
||||
|
||||
def _paint_label(self, painter: QPainter) -> None:
|
||||
painter.setPen(QColor("white"))
|
||||
painter.setPen(theme_manager.qcolor(colors.SLIGHTLY_GREY_TEXT))
|
||||
font = painter.font()
|
||||
font.setPixelSize(int(1.5 * self._knob_radius))
|
||||
font.setPixelSize(int(1.2 * self._knob_radius))
|
||||
painter.setFont(font)
|
||||
painter.drawText(self._current_knob_rectangle(), Qt.AlignCenter, self.label)
|
||||
|
||||
|
|
|
@ -36,6 +36,10 @@ class ThemeManager:
|
|||
_icon_size = 128
|
||||
_dark_mode_available: Optional[bool] = None
|
||||
|
||||
# Qt applies a gradient to the buttons in dark mode
|
||||
# from about #505050 to #606060.
|
||||
DARK_MODE_BUTTON_BG_MIDPOINT = "#555555"
|
||||
|
||||
def macos_dark_mode(self) -> bool:
|
||||
"True if the user has night mode on, and has forced native widgets."
|
||||
if not isMac:
|
||||
|
|
Loading…
Reference in a new issue