-
-
Notifications
You must be signed in to change notification settings - Fork 411
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into use-app-model-kb-internally
- Loading branch information
Showing
19 changed files
with
448 additions
and
114 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
"""For testing one off action callbacks""" | ||
from napari._app_model.actions._view_actions import _tooltip_visibility_toggle | ||
from napari.settings import get_settings | ||
|
||
|
||
def test_tooltip_visibility_toggle(): | ||
settings = get_settings().appearance | ||
assert settings.layer_tooltip_visibility is False | ||
_tooltip_visibility_toggle() | ||
assert settings.layer_tooltip_visibility is True | ||
_tooltip_visibility_toggle() | ||
assert settings.layer_tooltip_visibility is False |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
from napari import Viewer | ||
from napari._app_model._app import get_app | ||
from napari._app_model.actions._toggle_action import ViewerToggleAction | ||
from napari.components import ViewerModel | ||
|
||
|
||
def test_viewer_toggler(): | ||
viewer = ViewerModel() | ||
action = ViewerToggleAction( | ||
id='some.command.id', | ||
title='Toggle Axis Visibility', | ||
viewer_attribute='axes', | ||
sub_attribute='visible', | ||
) | ||
app = get_app() | ||
app.register_action(action) | ||
|
||
with app.injection_store.register(providers={Viewer: lambda: viewer}): | ||
assert viewer.axes.visible is False | ||
app.commands.execute_command('some.command.id') | ||
assert viewer.axes.visible is True | ||
app.commands.execute_command('some.command.id') | ||
assert viewer.axes.visible is False |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
from __future__ import annotations | ||
|
||
from typing import TYPE_CHECKING | ||
|
||
from app_model.types import Action, ToggleRule | ||
|
||
if TYPE_CHECKING: | ||
from ...viewer import Viewer | ||
|
||
|
||
class ViewerToggleAction(Action): | ||
"""Action subclass that toggles a boolean viewer (sub)attribute on trigger. | ||
Parameters | ||
---------- | ||
id : str | ||
The command id of the action. | ||
title : str | ||
The title of the action. Prefer capital case. | ||
viewer_attribute : str | ||
The attribute of the viewer to toggle. (e.g. 'axes') | ||
sub_attribute : str | ||
The attribute of the viewer attribute to toggle. (e.g. 'visible') | ||
**kwargs | ||
Additional keyword arguments to pass to the Action constructor. | ||
Examples | ||
-------- | ||
>>> action = ViewerToggleAction( | ||
... id='some.command.id', | ||
... title='Toggle Axis Visibility', | ||
... viewer_attribute='axes', | ||
... sub_attribute='visible', | ||
... ) | ||
""" | ||
|
||
def __init__( | ||
self, | ||
*, | ||
id: str, | ||
title: str, | ||
viewer_attribute: str, | ||
sub_attribute: str, | ||
**kwargs, | ||
): | ||
def get_current(viewer: Viewer): | ||
"""return the current value of the viewer attribute""" | ||
attr = getattr(viewer, viewer_attribute) | ||
return getattr(attr, sub_attribute) | ||
|
||
def toggle(viewer: Viewer): | ||
"""toggle the viewer attribute""" | ||
attr = getattr(viewer, viewer_attribute) | ||
setattr(attr, sub_attribute, not getattr(attr, sub_attribute)) | ||
|
||
super().__init__( | ||
id=id, | ||
title=title, | ||
toggled=ToggleRule(get_current=get_current), | ||
callback=toggle, | ||
**kwargs, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
"""Actions related to the view that require Qt. | ||
View actions that do not require Qt should go in | ||
napari/_app_model/actions/_view_actions.py. | ||
""" | ||
|
||
from typing import List | ||
|
||
from app_model.types import Action, ToggleRule | ||
|
||
from ...settings import get_settings | ||
from ..constants import CommandId, MenuId | ||
from ._toggle_action import ViewerToggleAction | ||
|
||
VIEW_ACTIONS: List[Action] = [] | ||
|
||
for cmd, viewer_attr, sub_attr in ( | ||
(CommandId.TOGGLE_VIEWER_AXES, 'axes', 'visible'), | ||
(CommandId.TOGGLE_VIEWER_AXES_COLORED, 'axes', 'colored'), | ||
(CommandId.TOGGLE_VIEWER_AXES_LABELS, 'axes', 'labels'), | ||
(CommandId.TOGGLE_VIEWER_AXES_DASHED, 'axes', 'dashed'), | ||
(CommandId.TOGGLE_VIEWER_AXES_ARROWS, 'axes', 'arrows'), | ||
(CommandId.TOGGLE_VIEWER_SCALE_BAR, 'scale_bar', 'visible'), | ||
(CommandId.TOGGLE_VIEWER_SCALE_BAR_COLORED, 'scale_bar', 'colored'), | ||
(CommandId.TOGGLE_VIEWER_SCALE_BAR_TICKS, 'scale_bar', 'ticks'), | ||
): | ||
menu = MenuId.VIEW_AXES if viewer_attr == 'axes' else MenuId.VIEW_SCALEBAR | ||
VIEW_ACTIONS.append( | ||
ViewerToggleAction( | ||
id=cmd, | ||
title=cmd.title, | ||
viewer_attribute=viewer_attr, | ||
sub_attribute=sub_attr, | ||
menus=[{'id': menu}], | ||
) | ||
) | ||
|
||
|
||
def _tooltip_visibility_toggle(): | ||
settings = get_settings().appearance | ||
settings.layer_tooltip_visibility = not settings.layer_tooltip_visibility | ||
|
||
|
||
# this can be generalised for all boolean settings, similar to `ViewerToggleAction` | ||
def _get_current_tooltip_visibility(): | ||
return get_settings().appearance.layer_tooltip_visibility | ||
|
||
|
||
VIEW_ACTIONS.extend( | ||
[ | ||
# TODO: this could be made into a toggle setting Action subclass | ||
# using a similar pattern to the above ViewerToggleAction classes | ||
Action( | ||
id=CommandId.TOGGLE_LAYER_TOOLTIPS, | ||
title=CommandId.TOGGLE_LAYER_TOOLTIPS.title, | ||
menus=[ | ||
{'id': MenuId.MENUBAR_VIEW, 'group': '1_render', 'order': 10} | ||
], | ||
callback=_tooltip_visibility_toggle, | ||
toggled=ToggleRule(get_current=_get_current_tooltip_visibility), | ||
), | ||
] | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,25 @@ | ||
from unittest.mock import MagicMock | ||
|
||
import pytest | ||
|
||
from napari import viewer | ||
from napari._app_model import constants, get_app | ||
from napari._qt._qapp_model import build_qmodel_menu | ||
from napari._qt._qapp_model.qactions import init_qactions | ||
from napari._qt.qt_main_window import Window | ||
|
||
|
||
@pytest.mark.parametrize('menu_id', list(constants.MenuId)) | ||
def test_build_qmodel_menu(qtbot, menu_id): | ||
"""Test that we can build qmenus for all registered menu IDs""" | ||
app = get_app() | ||
menu = build_qmodel_menu(menu_id) | ||
qtbot.addWidget(menu) | ||
assert len(menu.actions()) >= len(app.menus.get_menu(menu_id)) | ||
|
||
mock = MagicMock() | ||
with app.injection_store.register( | ||
providers={viewer.Viewer: lambda: mock, Window: lambda: mock} | ||
): | ||
init_qactions() | ||
menu = build_qmodel_menu(menu_id) | ||
qtbot.addWidget(menu) | ||
# `>=` because separator bars count as actions | ||
assert len(menu.actions()) >= len(app.menus.get_menu(menu_id)) |
Oops, something went wrong.