mirror of
https://github.com/bec-project/bec_widgets.git
synced 2025-07-14 03:31:50 +02:00
feat(darkmodebutton): added button to toggle between dark and light mode
This commit is contained in:
@ -21,6 +21,7 @@ class Widgets(str, enum.Enum):
|
||||
BECQueue = "BECQueue"
|
||||
BECStatusBox = "BECStatusBox"
|
||||
BECWaveformWidget = "BECWaveformWidget"
|
||||
DarkModeButton = "DarkModeButton"
|
||||
DeviceBrowser = "DeviceBrowser"
|
||||
DeviceComboBox = "DeviceComboBox"
|
||||
DeviceLineEdit = "DeviceLineEdit"
|
||||
@ -2289,6 +2290,14 @@ class BECWaveformWidget(RPCBase):
|
||||
"""
|
||||
|
||||
|
||||
class DarkModeButton(RPCBase):
|
||||
@rpc_call
|
||||
def set_dark_mode_enabled(self) -> None:
|
||||
"""
|
||||
None
|
||||
"""
|
||||
|
||||
|
||||
class DeviceBrowser(RPCBase):
|
||||
@property
|
||||
@rpc_call
|
||||
|
0
bec_widgets/widgets/dark_mode_button/__init__.py
Normal file
0
bec_widgets/widgets/dark_mode_button/__init__.py
Normal file
67
bec_widgets/widgets/dark_mode_button/dark_mode_button.py
Normal file
67
bec_widgets/widgets/dark_mode_button/dark_mode_button.py
Normal file
@ -0,0 +1,67 @@
|
||||
from bec_qthemes import material_icon
|
||||
from qtpy.QtCore import Property, Qt, Slot
|
||||
from qtpy.QtWidgets import QHBoxLayout, QPushButton, QWidget
|
||||
|
||||
from bec_widgets.utils.bec_widget import BECWidget
|
||||
from bec_widgets.utils.colors import set_theme
|
||||
|
||||
|
||||
class DarkModeButton(BECWidget, QWidget):
|
||||
USER_ACCESS = ["set_dark_mode_enabled"]
|
||||
|
||||
def __init__(
|
||||
self, parent: QWidget | None = None, client=None, gui_id: str | None = None
|
||||
) -> None:
|
||||
super().__init__(client=client, gui_id=gui_id)
|
||||
QWidget.__init__(self, parent)
|
||||
|
||||
self._dark_mode_enabled = False
|
||||
self.layout = QHBoxLayout(self)
|
||||
self.layout.setSpacing(0)
|
||||
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)
|
||||
self.mode_button = QPushButton(icon=icon)
|
||||
self.update_mode_button()
|
||||
self.mode_button.clicked.connect(self.set_dark_mode_enabled)
|
||||
self.layout.addWidget(self.mode_button)
|
||||
self.setLayout(self.layout)
|
||||
self.setFixedSize(40, 40)
|
||||
|
||||
@Property(bool)
|
||||
def dark_mode_enabled(self) -> bool:
|
||||
"""
|
||||
The dark mode state. If True, dark mode is enabled. If False, light mode is enabled.
|
||||
"""
|
||||
return self._dark_mode_enabled
|
||||
|
||||
@dark_mode_enabled.setter
|
||||
def dark_mode_enabled(self, state: bool) -> None:
|
||||
self._dark_mode_enabled = state
|
||||
|
||||
@Slot()
|
||||
def set_dark_mode_enabled(self) -> None:
|
||||
self.dark_mode_enabled = not self.dark_mode_enabled
|
||||
self.update_mode_button()
|
||||
set_theme("dark" if self.dark_mode_enabled else "light")
|
||||
|
||||
def update_mode_button(self):
|
||||
icon = material_icon(
|
||||
"light_mode" if self.dark_mode_enabled else "dark_mode",
|
||||
size=(20, 20),
|
||||
convert_to_pixmap=False,
|
||||
)
|
||||
self.mode_button.setIcon(icon)
|
||||
self.mode_button.setToolTip("Set Light Mode" if self.dark_mode_enabled else "Set Dark Mode")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
from qtpy.QtWidgets import QApplication
|
||||
|
||||
app = QApplication([])
|
||||
|
||||
w = DarkModeButton()
|
||||
w.show()
|
||||
|
||||
app.exec_()
|
@ -0,0 +1 @@
|
||||
{'files': ['dark_mode_button.py']}
|
@ -0,0 +1,54 @@
|
||||
# Copyright (C) 2022 The Qt Company Ltd.
|
||||
# SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
|
||||
|
||||
from qtpy.QtDesigner import QDesignerCustomWidgetInterface
|
||||
|
||||
from bec_widgets.utils.bec_designer import designer_material_icon
|
||||
from bec_widgets.widgets.dark_mode_button.dark_mode_button import DarkModeButton
|
||||
|
||||
DOM_XML = """
|
||||
<ui language='c++'>
|
||||
<widget class='DarkModeButton' name='dark_mode_button'>
|
||||
</widget>
|
||||
</ui>
|
||||
"""
|
||||
|
||||
|
||||
class DarkModeButtonPlugin(QDesignerCustomWidgetInterface): # pragma: no cover
|
||||
def __init__(self):
|
||||
super().__init__()
|
||||
self._form_editor = None
|
||||
|
||||
def createWidget(self, parent):
|
||||
t = DarkModeButton(parent)
|
||||
return t
|
||||
|
||||
def domXml(self):
|
||||
return DOM_XML
|
||||
|
||||
def group(self):
|
||||
return "BEC Buttons"
|
||||
|
||||
def icon(self):
|
||||
return designer_material_icon("dark_mode")
|
||||
|
||||
def includeFile(self):
|
||||
return "dark_mode_button"
|
||||
|
||||
def initialize(self, form_editor):
|
||||
self._form_editor = form_editor
|
||||
|
||||
def isContainer(self):
|
||||
return False
|
||||
|
||||
def isInitialized(self):
|
||||
return self._form_editor is not None
|
||||
|
||||
def name(self):
|
||||
return "DarkModeButton"
|
||||
|
||||
def toolTip(self):
|
||||
return "Button to toggle between dark and light mode."
|
||||
|
||||
def whatsThis(self):
|
||||
return self.toolTip()
|
@ -0,0 +1,15 @@
|
||||
def main(): # pragma: no cover
|
||||
from qtpy import PYSIDE6
|
||||
|
||||
if not PYSIDE6:
|
||||
print("PYSIDE6 is not available in the environment. Cannot patch designer.")
|
||||
return
|
||||
from PySide6.QtDesigner import QPyDesignerCustomWidgetCollection
|
||||
|
||||
from bec_widgets.widgets.dark_mode_button.dark_mode_button_plugin import DarkModeButtonPlugin
|
||||
|
||||
QPyDesignerCustomWidgetCollection.addCustomWidget(DarkModeButtonPlugin())
|
||||
|
||||
|
||||
if __name__ == "__main__": # pragma: no cover
|
||||
main()
|
Reference in New Issue
Block a user