Change how the switch is animated

This commit is contained in:
Veronica Berglyd Olsen
2025-05-11 17:33:04 +02:00
parent 746c6f79d2
commit e200e6b900
+15 -14
View File
@@ -23,12 +23,12 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
"""
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