Make switch knob smaller than path (#2008)

to ensure contrast when we move to a lighter window-bg color.
This commit is contained in:
Matthias Metelka 2022-08-15 05:05:20 +02:00 committed by GitHub
parent 21812556a6
commit 99f4f68aca
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -14,8 +14,6 @@ class Switch(QAbstractButton):
The suppoorted slots are toggle(), for an animated transition, and setChecked(). The suppoorted slots are toggle(), for an animated transition, and setChecked().
""" """
_margin: int = 2
def __init__( def __init__(
self, self,
radius: int = 10, radius: int = 10,
@ -33,9 +31,10 @@ class Switch(QAbstractButton):
self._right_label = right_label self._right_label = right_label
self._left_color = left_color self._left_color = left_color
self._right_color = right_color self._right_color = right_color
self._path_radius = radius - self._margin self._path_radius = radius
self._knob_radius = self._left_position = self._position = radius self._knob_radius = radius - 1
self._right_position = 3 * self._path_radius + self._margin self._left_position = self._position = radius
self._right_position = 3 * self._path_radius
@pyqtProperty(int) # type: ignore @pyqtProperty(int) # type: ignore
def position(self) -> int: def position(self) -> int:
@ -65,8 +64,8 @@ class Switch(QAbstractButton):
def sizeHint(self) -> QSize: def sizeHint(self) -> QSize:
return QSize( return QSize(
4 * self._path_radius + 2 * self._margin, 4 * self._path_radius,
2 * self._path_radius + 2 * self._margin, 2 * self._path_radius,
) )
def setChecked(self, checked: bool) -> None: def setChecked(self, checked: bool) -> None:
@ -85,17 +84,17 @@ class Switch(QAbstractButton):
def _paint_path(self, painter: QPainter) -> None: def _paint_path(self, painter: QPainter) -> None:
painter.setBrush(QBrush(self.path_color)) painter.setBrush(QBrush(self.path_color))
rectangle = QRectF( rectangle = QRectF(
self._margin, 0,
self._margin, 0,
self.width() - 2 * self._margin, self.width(),
self.height() - 2 * self._margin, self.height(),
) )
painter.drawRoundedRect(rectangle, self._path_radius, self._path_radius) painter.drawRoundedRect(rectangle, self._path_radius, self._path_radius)
def _current_knob_rectangle(self) -> QRectF: def _current_knob_rectangle(self) -> QRectF:
return QRectF( return QRectF(
self.position - self._knob_radius, # type: ignore self.position - self._knob_radius, # type: ignore
0, 1,
2 * self._knob_radius, 2 * self._knob_radius,
2 * self._knob_radius, 2 * self._knob_radius,
) )