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

Use app-model keybindings internally #5103

Merged
merged 17 commits into from Oct 27, 2022

Conversation

kne42
Copy link
Member

@kne42 kne42 commented Sep 19, 2022

Description

Converts the internal representation of keybindings from strings to app-model's KeyBinding.

see #4860

@github-actions github-actions bot added the tests Something related to our tests label Sep 19, 2022
@Czaki
Copy link
Collaborator

Czaki commented Sep 19, 2022

If we touch this, maybe remove the strict key bind for actions and allow configuring it through preferences?

@kne42
Copy link
Member Author

kne42 commented Sep 19, 2022

If we touch this, maybe remove the strict key bind for actions and allow configuring it through preferences?

what are you referring to exactly? i may address this in a future PR

@Czaki
Copy link
Collaborator

Czaki commented Sep 19, 2022

For most actions we define shortcuts here:
https://github.com/napari/napari/blob/31ee23683e5b38b589cce9633e8aca00200e0905/napari/utils/shortcuts.py

and in proper files we define only Actions:

@register_label_mode_action(trans._('Pan/zoom mode'))
def activate_label_pan_zoom_mode(layer: Labels):
layer.mode = Mode.PAN_ZOOM

And this allows to set actions shortcuts to personal preferences:
obraz

So as we touch this, we may want to stop using hardcoded shortcuts.

@kne42
Copy link
Member Author

kne42 commented Sep 19, 2022

ah, thank you for pointing this out! ill replace those with app-model keybindings in this PR too then

else:
# Shift is consumed to transform key

# bug found on OSX: Command will cause Shift to not
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you add a few test cases to show this?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is deleted code that was re-added. i dont know a good way to test this and i dont think its worth my time since the code for handling this will change in the next PR i work on related to handling keypress events

@github-actions
Copy link
Contributor

Click here to download the docs artifacts
docs
(zip file)

@github-actions github-actions bot added the preferences Issues relating to the creation of new preference fields/panels label Sep 20, 2022
@github-actions
Copy link
Contributor

Click here to download the docs artifacts
docs
(zip file)

@codecov
Copy link

codecov bot commented Sep 20, 2022

Codecov Report

Merging #5103 (8811f6a) into main (d5ffbac) will increase coverage by 0.01%.
The diff coverage is 96.00%.

@@            Coverage Diff             @@
##             main    #5103      +/-   ##
==========================================
+ Coverage   88.76%   88.77%   +0.01%     
==========================================
  Files         579      579              
  Lines       49093    49109      +16     
==========================================
+ Hits        43576    43597      +21     
+ Misses       5517     5512       -5     
Impacted Files Coverage Δ
napari/utils/interactions.py 74.52% <86.20%> (+2.43%) ⬆️
napari/utils/key_bindings.py 95.89% <98.24%> (+2.64%) ⬆️
napari/_qt/dialogs/preferences_dialog.py 89.81% <100.00%> (+0.19%) ⬆️
napari/layers/image/_image_key_bindings.py 69.23% <100.00%> (+0.60%) ⬆️
napari/layers/labels/_labels_key_bindings.py 91.80% <100.00%> (+0.13%) ⬆️
napari/layers/points/_points_key_bindings.py 100.00% <100.00%> (ø)
napari/layers/shapes/_shapes_key_bindings.py 97.89% <100.00%> (+0.02%) ⬆️
napari/settings/_shortcuts.py 93.75% <100.00%> (+0.41%) ⬆️
napari/settings/_yaml.py 91.66% <100.00%> (+0.75%) ⬆️
napari/utils/_tests/test_interactions.py 100.00% <100.00%> (ø)
... and 6 more

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

@github-actions
Copy link
Contributor

Click here to download the docs artifacts
docs
(zip file)

@liu-ziyang
Copy link
Contributor

@kne42 can you take a look at the failed tests? thanks!

@kne42
Copy link
Member Author

kne42 commented Sep 23, 2022

There's an issue serializing KeyBinding to json, since to properly use the json_encoders config settings on other models, you must pass models_as_dict=False (see pydantic/pydantic#3542). However, this aspect doesn't seem to be passed between json calls, so neither overriding the method to change the default on ShortcutSettings nor calling get_settings().json(models_as_dict=False) fixes the problem.

As a workaround, it looks like we'll need to implement KeyBinding with a custom root type. If we want to address this later, I can revert the shortcuts changes. I may also want to fix this upstream in pydantic at some point as well.

@nclack
Copy link
Contributor

nclack commented Sep 23, 2022

I can't say I completely understand the issue but I do see this:

In [4]: KeyBinding.from_str('ctrl-c').json()
Out[4]: '{"parts": [{"ctrl": true, "shift": false, "alt": false, "meta": false, "key": 20}]}'

In [5]: KeyBinding.from_str('ctrl-c').dict()
Out[5]: 
{'parts': [{'ctrl': True,
   'shift': False,
   'alt': False,
   'meta': False,
   'key': <KeyCode.KeyC: 20>}]}

I suspect we need to add use_enum_values to the SimpleKeyBinding Config (StackOverflow).

@kne42
Copy link
Member Author

kne42 commented Sep 30, 2022

tests work on my machine with the version of app-model in pyapp-kit/app-model#65

@kne42 kne42 force-pushed the use-app-model-kb-internally branch from 1434d17 to 1cf54f3 Compare October 17, 2022 21:02
@github-actions github-actions bot removed the preferences Issues relating to the creation of new preference fields/panels label Oct 17, 2022
@kne42 kne42 force-pushed the use-app-model-kb-internally branch from 1cf54f3 to 7b60f46 Compare October 19, 2022 21:06
@github-actions github-actions bot added preferences Issues relating to the creation of new preference fields/panels qt Relates to qt labels Oct 19, 2022
@kne42
Copy link
Member Author

kne42 commented Oct 19, 2022

ppl were not in favour of the custom root type solution i proposed so we put a hack in here, enjoy

@kne42 kne42 force-pushed the use-app-model-kb-internally branch from 6adf0c5 to 18c8693 Compare October 24, 2022 18:07
Copy link
Collaborator

@Czaki Czaki left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Few comments from fast review

napari/utils/interactions.py Outdated Show resolved Hide resolved
napari/utils/interactions.py Show resolved Hide resolved
napari/utils/interactions.py Outdated Show resolved Hide resolved
napari/utils/interactions.py Outdated Show resolved Hide resolved
napari/utils/key_bindings.py Outdated Show resolved Hide resolved
napari/utils/key_bindings.py Outdated Show resolved Hide resolved
napari/utils/key_bindings.py Outdated Show resolved Hide resolved
napari/utils/key_bindings.py Outdated Show resolved Hide resolved
napari/utils/shortcuts.py Show resolved Hide resolved
@kne42 kne42 force-pushed the use-app-model-kb-internally branch from f6071a1 to 835a42b Compare October 24, 2022 21:34
@kne42 kne42 merged commit 6ba16c1 into napari:main Oct 27, 2022
@kne42 kne42 deleted the use-app-model-kb-internally branch October 27, 2022 18:51
@Czaki Czaki mentioned this pull request Jun 7, 2023
@Czaki Czaki added this to the 0.5.0 milestone Jun 7, 2023
@Czaki Czaki added maintenance PR with maintance changes, triaged-0.4.18 To mark PR that is triaged in 0.4.18 release process labels Jun 7, 2023
Carreau pushed a commit that referenced this pull request Sep 27, 2023
# Description
This PR restores the `V` keybinding for toggling the visibility of the
currently selected layers.

It looks like this was unintentionally changed in
#5103. I'm pretty sure it's
unintentional because the PR description talks about changing the
internal representation, not the user interaction. Also, the line above
the one affected also contains `KeyCode.KeyG]`, so I think that it was
copy-pasted as a template for the line below it, which was then only
partially edited by mistake.

# References and relevant issues

https://github.com/napari/napari/blob/bf211d7c8c5582c0fe8cc8a09a9e51f31a230e5c/napari/utils/shortcuts.py#L23
```
 'napari:toggle_selected_visibility': [KeyCode.KeyG], 
```
Reference: #5103
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
maintenance PR with maintance changes, preferences Issues relating to the creation of new preference fields/panels qt Relates to qt tests Something related to our tests triaged-0.4.18 To mark PR that is triaged in 0.4.18 release process
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants