Skip to content

Commit

Permalink
Merge pull request nexpy#420 from rayosborn:simplify-mode-detection
Browse files Browse the repository at this point in the history
Uses PyQt signals and palettes to trigger changes in dark/light mode
  • Loading branch information
rayosborn authored Oct 6, 2023
2 parents 584aea4 + 389c846 commit 94b12da
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 42 deletions.
1 change: 0 additions & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,6 @@ lmfit https://lmfit.github.io/lmfit-py/
pylatexenc https://pylatexenc.readthedocs.io/
pillow https://pillow.readthedocs.io/
ansi2html https://pypi.python.org/pypi/ansi2html/
darkdetect https://github.com/albertosottile/darkdetect/
mplcursors https://mplcursors.readthedocs.io/
================= =================================================

Expand Down
1 change: 0 additions & 1 deletion conda-recipe/meta.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ requirements:
- pylatexenc
- pillow
- mplcursors
- darkdetect

test:
imports:
Expand Down
1 change: 0 additions & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ install_requires =
ansi2html
pillow
mplcursors
darkdetect
importlib-metadata; python_version<"3.10"

[options.packages.find]
Expand Down
15 changes: 6 additions & 9 deletions src/nexpy/gui/mainwindow.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,9 @@
from .pyqt import QtCore, QtGui, QtWidgets, getOpenFileName, getSaveFileName
from .scripteditor import NXScriptWindow
from .treeview import NXTreeView
from .utils import (NXListener, confirm_action, define_mode, display_message,
get_colors, get_name, import_plugin, is_file_locked,
load_image, mode_listener, natural_sort, report_error,
timestamp)
from .utils import (confirm_action, define_mode, display_message, get_colors,
get_name, import_plugin, is_file_locked, load_image,
natural_sort, report_error, timestamp)


class NXRichJupyterWidget(RichJupyterWidget):
Expand Down Expand Up @@ -196,9 +195,7 @@ def new_stb(etype, evalue, stb):
self.setWindowTitle('NeXpy v'+__version__)
self.statusBar().showMessage('Ready')

listener = NXListener()
listener.change_signal.connect(self.change_mode)
listener.start(mode_listener)
self.app.app.paletteChanged.connect(self.change_mode)

self.treeview.selection_changed()
self.shellview.setFocus()
Expand All @@ -213,8 +210,8 @@ def active_plotview(self):
from .plotview import active_plotview
return active_plotview

def change_mode(self, mode=None):
define_mode(mode)
def change_mode(self):
define_mode()

# Populate the menu bar with common actions and shortcuts
def add_menu_action(self, menu, action, defer_shortcut=False):
Expand Down
45 changes: 15 additions & 30 deletions src/nexpy/gui/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
from threading import Thread

import numpy as np
from darkdetect import isDark, theme
from IPython.core.ultratb import ColorTB
from matplotlib import __version__ as mplversion
from matplotlib import rcParams
Expand Down Expand Up @@ -704,50 +703,36 @@ def set_style(style=None):


def in_dark_mode():
if sys.version_info < (3, 9):
return False
try:
if isDark():
return True
else:
return False
except ImportError:
from .consoleapp import _mainwindow
app = _mainwindow.app.app
return (app.palette().window().color().value() <
app.palette().windowText().color().value())
except Exception:
return False


def mode_listener(listener):
old_mode = theme()
while True:
current_mode = theme()
if current_mode != old_mode:
old_mode = current_mode
listener.respond(current_mode)
time.sleep(5)

def define_mode():

def define_mode(mode=None):
from .consoleapp import _mainwindow
if parse_version(QtCore.__version__) <= parse_version('5.15'):
legacy_pyqt = True
else:
legacy_pyqt = False

if mode is None:
mode = theme()
if mode == 'Dark':
if in_dark_mode():
_mainwindow.console.set_default_style('linux')
if legacy_pyqt:
_mainwindow.statusBar().setStyleSheet('color: black')
_mainwindow.statusBar().setPalette(_mainwindow.app.app.palette())
else:
_mainwindow.console.set_default_style()
_mainwindow.statusBar().setPalette(_mainwindow.app.app.palette())

for dialog in _mainwindow.dialogs:
if dialog.windowTitle() == 'Script Editor':
for tab in [dialog.tabs[t] for t in dialog.tabs]:
tab.define_style()
elif dialog.windowTitle().startswith('Log File'):
dialog.format_log()
if legacy_pyqt:
for plotview in _mainwindow.plotviews.values():

for plotview in _mainwindow.plotviews.values():
if in_dark_mode():
plotview.otab.setStyleSheet('color: white')
else:
plotview.otab.setStyleSheet('color: black')


Expand Down

0 comments on commit 94b12da

Please sign in to comment.