From e200e6b9007758d6423941b8a16cc380563e979d Mon Sep 17 00:00:00 2001 From: Veronica Berglyd Olsen <1619840+vkbo@users.noreply.github.com> Date: Sun, 11 May 2025 17:33:04 +0200 Subject: [PATCH] Change how the switch is animated --- novelwriter/extensions/switch.py | 29 +++++++++++++++-------------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/novelwriter/extensions/switch.py b/novelwriter/extensions/switch.py index 421fb712..b915622e 100644 --- a/novelwriter/extensions/switch.py +++ b/novelwriter/extensions/switch.py @@ -23,12 +23,12 @@ along with this program. If not, see . """ from __future__ import annotations -from PyQt6.QtCore import QPropertyAnimation, Qt, pyqtProperty # pyright: ignore -from PyQt6.QtGui import QEnterEvent, QMouseEvent, QPainter, QPaintEvent, QResizeEvent +from PyQt6.QtCore import QPropertyAnimation, Qt, pyqtProperty, pyqtSlot # pyright: ignore +from PyQt6.QtGui import QEnterEvent, QPainter, QPaintEvent, QResizeEvent from PyQt6.QtWidgets import QAbstractButton, QWidget from novelwriter import SHARED -from novelwriter.types import QtMouseLeft, QtNoPen, QtPaintAntiAlias, QtSizeFixed +from novelwriter.types import QtNoPen, QtPaintAntiAlias, QtSizeFixed class NSwitch(QAbstractButton): @@ -50,6 +50,8 @@ class NSwitch(QAbstractButton): self.setFixedHeight(self._xH) self._offset = self._xR + self.clicked.connect(self._onClick) + return ## @@ -106,19 +108,18 @@ class NSwitch(QAbstractButton): return - def mouseReleaseEvent(self, event: QMouseEvent) -> None: - """Animate the switch on mouse release.""" - super().mouseReleaseEvent(event) - if event.button() == QtMouseLeft: - anim = QPropertyAnimation(self, b"offset", self) - anim.setDuration(120) - anim.setStartValue(self._offset) - anim.setEndValue((self._xW - self._xR) if self.isChecked() else self._xR) - anim.start() - return - def enterEvent(self, event: QEnterEvent) -> None: """Change the cursor when hovering the button.""" self.setCursor(Qt.CursorShape.PointingHandCursor) super().enterEvent(event) return + + @pyqtSlot(bool) + def _onClick(self, checked: bool) -> None: + """Animate the toggle action.""" + anim = QPropertyAnimation(self, b"offset", self) + anim.setDuration(120) + anim.setStartValue(self._offset) + anim.setEndValue((self._xW - self._xR) if checked else self._xR) + anim.start() + return