Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dark mode for Qt application #2417

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
103 changes: 103 additions & 0 deletions glue/app/qt/application.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@

from qtpy import QtCore, QtWidgets, QtGui, compat
from qtpy.QtCore import Qt
from qtpy.QtGui import QColor, QPalette

from glue.config import settings
from glue.core.application_base import Application
from glue.core.message import ApplicationClosedMessage, DataCollectionMessage, SettingsChangeMessage
from glue.core import command, BaseData
Expand Down Expand Up @@ -44,6 +46,66 @@
__all__ = ['GlueApplication']
DOCS_URL = 'http://www.glueviz.org'

DARK_PALETTE = QPalette()
DARK_PALETTE.setColor(QPalette.Window, QColor(53, 53, 53))
DARK_PALETTE.setColor(QPalette.WindowText, Qt.white)
DARK_PALETTE.setColor(QPalette.Base, QColor(35, 35, 35))
DARK_PALETTE.setColor(QPalette.AlternateBase, QColor(53, 53, 53))
DARK_PALETTE.setColor(QPalette.ToolTipBase, QColor(25, 25, 25))
DARK_PALETTE.setColor(QPalette.ToolTipText, Qt.white)
DARK_PALETTE.setColor(QPalette.Text, Qt.white)
DARK_PALETTE.setColor(QPalette.Button, QColor(53, 53, 53))
DARK_PALETTE.setColor(QPalette.ButtonText, Qt.white)
DARK_PALETTE.setColor(QPalette.BrightText, Qt.red)
DARK_PALETTE.setColor(QPalette.Link, QColor(42, 130, 218))
DARK_PALETTE.setColor(QPalette.Highlight, QColor(42, 130, 218))
DARK_PALETTE.setColor(QPalette.HighlightedText, QColor(35, 35, 35))
DARK_PALETTE.setColor(QPalette.Active, QPalette.Button, QColor(53, 53, 53))
DARK_PALETTE.setColor(QPalette.Disabled, QPalette.ButtonText, Qt.darkGray)
DARK_PALETTE.setColor(QPalette.Disabled, QPalette.WindowText, Qt.darkGray)
DARK_PALETTE.setColor(QPalette.Disabled, QPalette.Text, Qt.darkGray)
DARK_PALETTE.setColor(QPalette.Disabled, QPalette.Light, QColor(53, 53, 53))

LIGHT_PALETTE = QPalette()
LIGHT_PALETTE.setColor(QPalette.Window, QColor(239, 239, 239))
LIGHT_PALETTE.setColor(QPalette.WindowText, Qt.black)
LIGHT_PALETTE.setColor(QPalette.Base, Qt.white)
LIGHT_PALETTE.setColor(QPalette.AlternateBase, QColor(247, 247, 247))
LIGHT_PALETTE.setColor(QPalette.ToolTipBase, QColor(255, 255, 220))
LIGHT_PALETTE.setColor(QPalette.ToolTipText, Qt.black)
LIGHT_PALETTE.setColor(QPalette.Text, Qt.black)
LIGHT_PALETTE.setColor(QPalette.Button, QColor(239, 239, 239))
LIGHT_PALETTE.setColor(QPalette.ButtonText, Qt.black)
LIGHT_PALETTE.setColor(QPalette.BrightText, Qt.red)
LIGHT_PALETTE.setColor(QPalette.Link, Qt.blue)
LIGHT_PALETTE.setColor(QPalette.Highlight, QColor(48, 140, 198))
LIGHT_PALETTE.setColor(QPalette.HighlightedText, Qt.white)
LIGHT_PALETTE.setColor(QPalette.Active, QPalette.Button, QColor(239, 239, 239))
LIGHT_PALETTE.setColor(QPalette.Disabled, QPalette.ButtonText, QColor(190, 190, 190))
LIGHT_PALETTE.setColor(QPalette.Disabled, QPalette.WindowText, QColor(190, 190, 190))
LIGHT_PALETTE.setColor(QPalette.Disabled, QPalette.Text, QColor(190, 190, 190))
LIGHT_PALETTE.setColor(QPalette.Disabled, QPalette.Light, Qt.white)


def _stylesheet(text_color='black', bg_color='white', in_prompt_color='deepskyblue', out_prompt_color='crimson'):
return f"""
QPlainTextEdit, QTextEdit {{
background-color: {bg_color};
background-clip: padding;
color: {text_color};
selection-background-color: #ccc;
}}
.inverted {{
background-color: {text_color};
color: {bg_color};
}}
.error {{ color: red; }}
.in-prompt-number {{ font-weight: bold; }}
.out-prompt-number {{ font-weight: bold; }}
.in-prompt {{ color: {in_prompt_color}; }}
.out-prompt {{ color: {out_prompt_color}; }}
"""


def _fix_ipython_pylab():

Expand Down Expand Up @@ -319,6 +381,7 @@ def __init__(self, data_collection=None, session=None):
self._connect()
self.new_tab()
self._update_viewer_in_focus()
self._on_ui_settings_change()

def _update_viewer_in_focus(self, *args):

Expand Down Expand Up @@ -492,9 +555,35 @@ def _on_data_collection_change(self, *event):
self._button_link_data.setEnabled(len(self.data_collection) > 1)
self._button_edit_components.setEnabled(len(self.data_collection) > 0)

def _use_dark_terminal(self):
if settings.APP_THEME == 'System default':
# Try to identify whether we think the system theme is light or dark
palette = self.app.palette()
window_color = palette.color(QPalette.Window)
text_color = palette.color(QPalette.WindowText)
return window_color.lightness() < text_color.lightness()
else:
return settings.APP_THEME == 'Dark'

def _on_ui_settings_change(self, *event):
update_global_font_size()

# Update the global app palette
if settings.APP_THEME == 'Light':
palette = LIGHT_PALETTE
elif settings.APP_THEME == 'Dark':
palette = DARK_PALETTE
else:
palette = self.app.style().standardPalette()
self.app.setPalette(palette)

# Update the background color of the data canvas on each tab
for i in range(self.tab_count):
tab = self.tab(i)
tab.setBackground(palette.color(QPalette.AlternateBase))

self._update_terminal_style()

def keyPressEvent(self, event):
if self.current_tab.activeSubWindow() and self.current_tab.activeSubWindow().widget():
active_window = self.current_tab.activeSubWindow().widget()
Expand Down Expand Up @@ -1162,6 +1251,7 @@ def _create_terminal(self):
self._button_ipython.setEnabled(False)
else:
self._terminal = self.add_widget(widget)
self._update_terminal_style()
self._terminal.closed.connect(self._on_terminal_close)
self._hide_terminal()

Expand Down Expand Up @@ -1192,6 +1282,19 @@ def _show_terminal(self):
self._terminal.show()
self._terminal.widget().show()

def _update_terminal_style(self):
if self.has_terminal(create_if_not=False):
dark = self._use_dark_terminal()
terminal = self._terminal.widget()
terminal_colors = dict(
text_color='white' if dark else 'black',
bg_color='#282828' if dark else 'white',
in_prompt_color='deepskyblue' if dark else 'navy',
out_prompt_color='crimson' if dark else 'darkred'
)
terminal.style_sheet = _stylesheet(**terminal_colors)
terminal.syntax_style = 'rrt' if dark else 'default'

def start(self, size=None, position=None, block=True, maximized=True):
"""
Show the GUI and start the application.
Expand Down
2 changes: 1 addition & 1 deletion glue/app/qt/mdi_area.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def __init__(self, application, parent=None):
self._application = weakref.ref(application)
self.setAcceptDrops(True)
self.setAttribute(Qt.WA_DeleteOnClose)
self.setBackground(QtGui.QBrush(QtGui.QColor(250, 250, 250)))
self.setBackground(QtGui.QBrush(application.app.palette().color(QtGui.QPalette.AlternateBase)))
self.setHorizontalScrollBarPolicy(Qt.ScrollBarAsNeeded)
self.setVerticalScrollBarPolicy(Qt.ScrollBarAsNeeded)

Expand Down
13 changes: 12 additions & 1 deletion glue/app/qt/preferences.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ def finalize(self):

class PreferencesDialog(QtWidgets.QDialog):

app_theme = CurrentComboTextProperty('ui.combo_app_theme')
theme = CurrentComboTextProperty('ui.combo_theme')
background = ColorProperty('ui.color_background')
foreground = ColorProperty('ui.color_foreground')
Expand All @@ -84,6 +85,7 @@ def __init__(self, application, parent=None):
self.ui.ok.clicked.connect(self.accept)

self.ui.combo_theme.currentIndexChanged.connect(self._update_colors_from_theme)
self.ui.combo_app_theme.currentIndexChanged.connect(self._update_app_theme)

self.ui.button_reset_dialogs.clicked.connect(self._reset_dialogs)

Expand All @@ -100,6 +102,7 @@ def __init__(self, application, parent=None):
self.data_color = settings.DATA_COLOR
self.data_alpha = settings.DATA_ALPHA
self.font_size = settings.FONT_SIZE
self.app_theme = settings.APP_THEME

self._update_theme_from_colors()

Expand Down Expand Up @@ -138,6 +141,13 @@ def _update_colors_from_theme(self, *args):
elif self.theme != 'Custom':
raise ValueError("Unknown theme: {0}".format(self.theme))

def _update_app_theme(self, *args):
if self.app_theme == 'Dark' and self.theme == 'Black on White':
self.theme = 'White on Black'
elif self.app_theme == 'Light' and self.theme == 'White on Black':
self.theme = 'Black on White'
self._update_colors_from_theme()

def _reset_dialogs(self, *args):
from glue.config import settings
for key, _, _ in settings:
Expand All @@ -154,6 +164,7 @@ def accept(self):
settings.DATA_COLOR = self.data_color
settings.DATA_ALPHA = self.data_alpha
settings.FONT_SIZE = self.font_size
settings.APP_THEME = self.app_theme

self._autolink_pane.finalize()

Expand All @@ -171,7 +182,7 @@ def accept(self):
app = self._app()

if app is not None:
app._hub.broadcast(SettingsChangeMessage(self, ('FOREGROUND_COLOR', 'BACKGROUND_COLOR', 'FONT_SIZE')))
app._hub.broadcast(SettingsChangeMessage(self, ('FOREGROUND_COLOR', 'BACKGROUND_COLOR', 'FONT_SIZE', 'APP_THEME')))
if self.data_apply: # If requested, trigger data to update color
app.set_data_color(settings.DATA_COLOR, settings.DATA_ALPHA)

Expand Down
Loading