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

Update theme/style sheet handling to enable napari-console to use napari font_size setting #33

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
4 changes: 3 additions & 1 deletion napari_console/_tests/test_qt_console.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,12 @@ def actual_factory(*model_args, viewer_class=Viewer, **model_kwargs):
def test_console(qtbot, make_test_viewer):
"""Test creating the console."""
viewer = make_test_viewer()
console = QtConsole(viewer)
style_sheet = viewer.window._qt_window.styleSheet()
console = QtConsole(viewer, style_sheet)
qtbot.addWidget(console)
assert console.kernel_client is not None
assert console.viewer is viewer
assert console.style_sheet == style_sheet


def test_ipython_console(qtbot, make_test_viewer):
Expand Down
42 changes: 24 additions & 18 deletions napari_console/qt_console.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,13 +73,10 @@ class QtConsole(RichJupyterWidget):
Shell for the kernel if it exists, None otherwise.
"""

def __init__(self, viewer: 'napari.viewer.Viewer'):
def __init__(self, viewer: 'napari.viewer.Viewer', style_sheet: str = ''):
super().__init__()

self.viewer = viewer

# Connect theme update
self.viewer.events.theme.connect(self._update_theme)
user_variables = {'viewer': self.viewer}

# this makes calling `setFocus()` on a QtConsole give keyboard focus to
Expand Down Expand Up @@ -135,29 +132,38 @@ def __init__(self, viewer: 'napari.viewer.Viewer'):
self.enable_calltips = False

# Set stylings
self._update_theme()
self._update_theme(style_sheet=style_sheet)

# TODO: Try to get console from jupyter to run without a shift click
# self.execute_on_complete_input = True

def _update_theme(self, event=None):
def _update_theme(self, event=None, style_sheet=''):
"""Update the napari GUI theme."""
from napari.utils.theme import get_theme, template
from napari.qt import get_stylesheet
from napari.utils.theme import get_theme, template

# qtconsole unfortunately won't inherit the parent stylesheet
# so it needs to be directly set
raw_stylesheet = get_stylesheet()
# template and apply the primary stylesheet
# (should probably be done by napari)
# After napari 0.4.11, themes are evented models rather than
# dicts.
# After napari 0.5.0 the `as_dict` kwarg has been deprecated
# and will be removed in a future version.
theme = get_theme(self.viewer.theme, as_dict=True)
self.style_sheet = template(raw_stylesheet, **theme)

# After napari 0.4.6 the following syntax will be allowed
# self.style_sheet = get_stylesheet(self.viewer.theme)
# In the future, with napari 0.5.0+, the following syntax will be allowed
# theme = get_theme(self.viewer.theme).to_rgb_dict()

# qtconsole unfortunately won't inherit the parent stylesheet
# so it needs to be directly set when required.
if style_sheet:
# napari 0.5.x uses the `style_sheet` kwarg
self.style_sheet = style_sheet
else:
# napari 0.4.x doesn't use the `style_sheet` kwarg
raw_stylesheet = get_stylesheet()
# template and apply the primary stylesheet
# (should probably be done by napari)
# After napari 0.4.11, themes are evented models rather than
# dicts.
self.style_sheet = template(raw_stylesheet, **theme)

# After napari 0.4.6 the following syntax will be allowed
# self.style_sheet = get_stylesheet(self.viewer.theme)

# Set syntax styling and highlighting using theme
self.syntax_style = theme['syntax_style']
Expand Down
Loading