mirror of
https://github.com/bec-project/bec_widgets.git
synced 2025-07-13 19:21:50 +02:00
fix(dark mode button): fixed dark mode button state for external updates, including auto
This commit is contained in:
@ -8,7 +8,7 @@ import pyqtgraph as pg
|
||||
from bec_qthemes._os_appearance.listener import OSThemeSwitchListener
|
||||
from pydantic_core import PydanticCustomError
|
||||
from qtpy.QtGui import QColor
|
||||
from qtpy.QtWidgets import QApplication
|
||||
from qtpy.QtWidgets import QApplication, QPushButton, QToolButton
|
||||
|
||||
|
||||
def get_theme_palette():
|
||||
@ -56,6 +56,17 @@ def apply_theme(theme: Literal["dark", "light"]):
|
||||
for pg_widget in children:
|
||||
pg_widget.setBackground("k" if theme == "dark" else "w")
|
||||
|
||||
dark_mode_buttons = [
|
||||
button
|
||||
for button in app.topLevelWidgets()
|
||||
if hasattr(button, "dark_mode_enabled")
|
||||
and hasattr(button, "mode_button")
|
||||
and isinstance(button.mode_button, (QPushButton, QToolButton))
|
||||
]
|
||||
|
||||
for button in dark_mode_buttons:
|
||||
button.dark_mode_enabled = theme == "dark"
|
||||
button.update_mode_button()
|
||||
# now define stylesheet according to theme and apply it
|
||||
style = bec_qthemes.load_stylesheet(theme)
|
||||
app.setStyleSheet(style)
|
||||
|
@ -1,6 +1,6 @@
|
||||
from bec_qthemes import material_icon
|
||||
from qtpy.QtCore import Property, Qt, Slot
|
||||
from qtpy.QtWidgets import QHBoxLayout, QPushButton, QToolButton, QWidget
|
||||
from qtpy.QtWidgets import QApplication, QHBoxLayout, QPushButton, QToolButton, QWidget
|
||||
|
||||
from bec_widgets.utils.bec_widget import BECWidget
|
||||
from bec_widgets.utils.colors import set_theme
|
||||
@ -27,17 +27,31 @@ class DarkModeButton(BECWidget, QWidget):
|
||||
self.layout.setContentsMargins(0, 0, 0, 0)
|
||||
self.layout.setAlignment(Qt.AlignmentFlag.AlignVCenter)
|
||||
|
||||
icon = material_icon("dark_mode", size=(20, 20), convert_to_pixmap=False)
|
||||
if toolbar:
|
||||
self.mode_button = QToolButton(icon=icon)
|
||||
self.mode_button = QToolButton()
|
||||
else:
|
||||
self.mode_button = QPushButton(icon=icon)
|
||||
self.mode_button = QPushButton()
|
||||
|
||||
self.dark_mode_enabled = self._get_qapp_dark_mode_state()
|
||||
self.update_mode_button()
|
||||
self.mode_button.clicked.connect(self.toggle_dark_mode)
|
||||
self.layout.addWidget(self.mode_button)
|
||||
self.setLayout(self.layout)
|
||||
self.setFixedSize(40, 40)
|
||||
|
||||
def _get_qapp_dark_mode_state(self) -> bool:
|
||||
"""
|
||||
Get the dark mode state from the QApplication.
|
||||
|
||||
Returns:
|
||||
bool: True if dark mode is enabled, False otherwise.
|
||||
"""
|
||||
qapp = QApplication.instance()
|
||||
if hasattr(qapp, "theme") and qapp.theme["theme"] == "dark":
|
||||
return True
|
||||
|
||||
return False
|
||||
|
||||
@Property(bool)
|
||||
def dark_mode_enabled(self) -> bool:
|
||||
"""
|
||||
@ -72,8 +86,10 @@ class DarkModeButton(BECWidget, QWidget):
|
||||
if __name__ == "__main__":
|
||||
from qtpy.QtWidgets import QApplication
|
||||
|
||||
app = QApplication([])
|
||||
from bec_widgets.utils.colors import set_theme
|
||||
|
||||
app = QApplication([])
|
||||
set_theme("auto")
|
||||
w = DarkModeButton()
|
||||
w.show()
|
||||
|
||||
|
@ -3,6 +3,7 @@ from unittest import mock
|
||||
import pytest
|
||||
from qtpy.QtCore import Qt
|
||||
|
||||
from bec_widgets.utils.colors import set_theme
|
||||
from bec_widgets.widgets.dark_mode_button.dark_mode_button import DarkModeButton
|
||||
|
||||
# pylint: disable=unused-import
|
||||
@ -19,6 +20,7 @@ def dark_mode_button(qtbot, mocked_client):
|
||||
button = DarkModeButton(client=mocked_client)
|
||||
qtbot.addWidget(button)
|
||||
qtbot.waitExposed(button)
|
||||
set_theme("light")
|
||||
yield button
|
||||
|
||||
|
||||
|
Reference in New Issue
Block a user