예제 #1
0
def test_register_theme():
    # Check that blue theme is not listed in available themes
    themes = available_themes()
    assert 'test_blue' not in themes

    # Create new blue theme based on dark theme
    blue_theme = get_theme('dark')
    blue_theme.update(
        background='rgb(28, 31, 48)',
        foreground='rgb(45, 52, 71)',
        primary='rgb(80, 88, 108)',
        current='rgb(184, 112, 0)',
    )

    # Register blue theme
    register_theme('test_blue', blue_theme)

    # Check that blue theme is listed in available themes
    themes = available_themes()
    assert 'test_blue' in themes

    # Check that the dark theme has not been overwritten
    dark_theme = get_theme('dark')
    assert not dark_theme['background'] == blue_theme['background']

    # Check that blue theme can be gotten from available themes
    theme = get_theme('test_blue')
    assert theme['background'] == blue_theme['background']
예제 #2
0
def test_get_theme():
    theme = get_theme('dark')
    assert isinstance(theme, dict)
    assert theme['name'] == 'dark'

    # get theme in the old-style dict format
    theme = get_theme("dark")
    assert isinstance(theme, dict)

    # get theme in the new model-based format
    theme = get_theme("dark", False)
    assert isinstance(theme, Theme)
예제 #3
0
def test_update_theme(
    mock_add_theme,
    mock_remove_theme,
    mock_icon_changed,
    make_napari_viewer,
    qapp,
):
    viewer = make_napari_viewer()

    blue = get_theme("dark", False)
    blue.name = "blue"
    register_theme("blue", blue)

    # triggered when theme was added
    mock_add_theme.assert_called()
    mock_remove_theme.assert_not_called()

    unregister_theme("blue")
    # triggered when theme was removed
    mock_remove_theme.assert_called()

    mock_icon_changed.assert_not_called()
    viewer.theme = "light"
    theme = _themes["light"]
    theme.icon = "#FF0000"
    mock_icon_changed.assert_called()
예제 #4
0
def test_provide_theme_hook_registered_correctly(
    mock_add_theme,
    mock_remove_theme,
    make_napari_viewer,
    napari_plugin_manager,
):
    with pytest.warns(FutureWarning):
        dark = get_theme("dark")
        dark["name"] = "dark-test-2"

    class TestPlugin:
        @napari_hook_implementation
        def napari_experimental_provide_theme():
            return {"dark-test-2": dark}

    # create instance of viewer to make sure
    # registration and unregistration methods are called
    viewer = make_napari_viewer()

    # register theme
    napari_plugin_manager.register(TestPlugin)
    reg = napari_plugin_manager._theme_data["TestPlugin"]
    assert isinstance(reg["dark-test-2"], Theme)

    viewer.theme = "dark-test-2"

    # triggered when theme was added
    mock_add_theme.assert_called()
    mock_remove_theme.assert_not_called()

    # now, lets unregister the theme
    with pytest.warns(UserWarning, match="The current theme "):
        napari_plugin_manager.unregister("TestPlugin")
    mock_remove_theme.assert_called()
예제 #5
0
def test_provide_theme_hook_unregister(
    napari_plugin_manager: "NapariPluginManager", ):
    with pytest.warns(FutureWarning):
        dark = get_theme("dark")
        dark["name"] = "dark-test"

    class TestPlugin:
        @napari_hook_implementation
        def napari_experimental_provide_theme():
            return {"dark-test": dark}

    napari_plugin_manager.discover_themes()
    napari_plugin_manager.register(TestPlugin)

    # make sure theme was registered
    assert "TestPlugin" in napari_plugin_manager._theme_data
    reg = napari_plugin_manager._theme_data["TestPlugin"]
    assert isinstance(reg, dict)
    assert len(reg) == 1
    assert "dark-test" in available_themes()
    get_settings().appearance.theme = "dark-test"

    with pytest.warns(UserWarning, match="The current theme "):
        napari_plugin_manager.unregister("TestPlugin")

    # make sure that plugin-specific data was removed
    assert "TestPlugin" not in napari_plugin_manager._theme_data
    # since the plugin was unregistered, the current theme cannot
    # be the theme registered by the plugin
    assert get_settings().appearance.theme != "dark-test"
    assert "dark-test" not in available_themes()
예제 #6
0
def test_rebuild_theme_settings():
    settings = get_settings()
    assert "another-theme" not in available_themes()
    # theme is not updated
    with pytest.raises(ValidationError):
        settings.appearance.theme = "another-theme"
    blue_theme = get_theme("dark")
    register_theme("another-theme", blue_theme)
    settings.appearance.theme = "another-theme"
예제 #7
0
def test_custom_theme_settings(settings):
    # See: https://github.com/napari/napari/issues/2340
    custom_theme_name = "blue"

    # No theme registered yet, this should fail
    with pytest.raises(pydantic.error_wrappers.ValidationError):
        settings.appearance.theme = custom_theme_name

    blue_theme = get_theme('dark')
    blue_theme.update(
        background='rgb(28, 31, 48)',
        foreground='rgb(45, 52, 71)',
        primary='rgb(80, 88, 108)',
        current='rgb(184, 112, 0)',
    )
    register_theme(custom_theme_name, custom_theme_name)

    # Theme registered, should pass validation
    settings.appearance.theme = custom_theme_name
예제 #8
0
    def _update_theme(self, event=None):
        """Update the napari GUI theme."""
        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)
        theme = get_theme(self.viewer.theme)
        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"]
        bracket_color = QColor(*str_to_rgb(theme["highlight"]))
        self._bracket_matcher.format.setBackground(bracket_color)
예제 #9
0
def test_unregister_theme():
    # Create new blue theme based on dark theme
    blue_theme = get_theme('dark')
    blue_theme.update(
        background='rgb(28, 31, 48)',
        foreground='rgb(45, 52, 71)',
        primary='rgb(80, 88, 108)',
        current='rgb(184, 112, 0)',
    )

    # Register blue theme
    register_theme('test_blue', blue_theme)

    # Check that blue theme is listed in available themes
    themes = available_themes()
    assert 'test_blue' in themes

    # Remove theme from available themes
    unregister_theme("test_blue")
    themes = available_themes()
    assert 'test_blue' not in themes
예제 #10
0
    def _update_theme(self, theme_name):
        """
        Update styling based on the napari theme dictionary and any other attributes

        Parameters
        ----------
        theme : str
            name of napari theme
        """
        from napari.utils.theme import get_theme, template

        qss_template = """
        QListView::item:deselected {
            background-color: {{ foreground }};
        }
        QListView::item:selected {
            background-color: {{ current }};
        }
        QListView {
            background: transparent;
        }
        QListView::item {
            margin: 0px;
            padding: 0px;
            min-height:
            32px;
            max-height: 32px;
        }
        QImage {
            margin: 0px;
            padding: 0px;
            qproperty-alignment: AlignLeft;
        }
        """

        self.setStyleSheet(template(qss_template, **get_theme(theme_name)))
        self.setIconSize(QSize(64, 64))
        self.setSpacing(2)
예제 #11
0
def test_provide_theme_hook(napari_plugin_manager: "NapariPluginManager"):
    with pytest.warns(FutureWarning):
        dark = get_theme("dark")
        dark["name"] = "dark-test"

    class TestPlugin:
        @napari_hook_implementation
        def napari_experimental_provide_theme():
            return {"dark-test": dark}

    viewer = ViewerModel()
    napari_plugin_manager.discover_themes()
    napari_plugin_manager.register(TestPlugin)

    # make sure theme data is present in the plugin
    reg = napari_plugin_manager._theme_data["TestPlugin"]
    assert isinstance(reg, dict)
    assert len(reg) == 1
    assert isinstance(reg["dark-test"], Theme)

    # make sure theme was registered
    assert "dark-test" in available_themes()
    viewer.theme = "dark-test"
예제 #12
0
    def _update_theme(self, event=None):
        """Update the napari GUI theme."""
        from napari.utils.theme import get_theme, template
        from napari.qt import get_stylesheet

        # 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.
        with warnings.catch_warnings():
            warnings.simplefilter("ignore", FutureWarning)
            theme = get_theme(self.viewer.theme)
        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']
        bracket_color = QColor(*str_to_rgb(theme['highlight']))
        self._bracket_matcher.format.setBackground(bracket_color)
예제 #13
0
def test_provide_theme_hook_bad(napari_plugin_manager: "NapariPluginManager"):
    napari_plugin_manager.discover_themes()

    with pytest.warns(FutureWarning):
        dark = get_theme("dark")
        dark.pop("foreground")
        dark["name"] = "dark-bad"

    class TestPluginBad:
        @napari_hook_implementation
        def napari_experimental_provide_theme():
            return {"dark-bad": dark}

    with pytest.warns(
            UserWarning,
            match=", plugin 'TestPluginBad' provided an invalid dict object",
    ):
        napari_plugin_manager.register(TestPluginBad)

    # make sure theme data is present in the plugin but the theme is not there
    reg = napari_plugin_manager._theme_data["TestPluginBad"]
    assert isinstance(reg, dict)
    assert len(reg) == 0
    assert "dark-bad" not in available_themes()
예제 #14
0
"""
Displays an image and sets the theme to new custom theme.
"""

from skimage import data
import napari
from napari.utils.theme import available_themes, get_theme, register_theme

with napari.gui_qt():
    # create the viewer with an image
    viewer = napari.view_image(data.astronaut(), rgb=True, name='astronaut')

    # List themes
    print('Originally themes', available_themes())

    blue_theme = get_theme('dark')
    blue_theme.update(
        background='rgb(28, 31, 48)',
        foreground='rgb(45, 52, 71)',
        primary='rgb(80, 88, 108)',
        current='rgb(184, 112, 0)',
    )

    register_theme('blue', blue_theme)

    # List themes
    print('New themes', available_themes())

    # Set theme
    viewer.theme = 'blue'
예제 #15
0
def test_theme_syntax_highlight():
    theme = get_theme("dark", False)
    with pytest.raises(ValidationError):
        theme.syntax_style = "invalid"
예제 #16
0
Displays an image and sets the theme to new custom theme.

"""

from skimage import data
import napari
from napari.utils.theme import available_themes, get_theme, register_theme

# create the viewer with an image
viewer = napari.view_image(data.astronaut(), rgb=True, name='astronaut')

# List themes
print('Originally themes', available_themes())

blue_theme = get_theme('dark', False)
blue_theme.name = "blue"
blue_theme.icon = (
    'rgb(0, 255, 255)'  # you can provide colors as rgb(XXX, YYY, ZZZ)
)
blue_theme.background = 28, 31, 48  # or as tuples
blue_theme.foreground = [45, 52, 71]  # or as list
blue_theme.primary = '#50586c'  # or as hexes
blue_theme.current = 'orange'  # or as color name

register_theme('blue', blue_theme)

# List themes
print('New themes', available_themes())

# Set theme
예제 #17
0
def test_get_theme():
    theme = get_theme('dark')
    assert theme['folder'] == 'dark'
예제 #18
0
def test_theme(color):
    theme = get_theme("dark", False)
    theme.background = color