Hookspecs for layer keybindings
See original GitHub issueš Feature
Plugin developers are beginning to use our reader/ writer hookspecs with success, but are looking to add additional functionality like custom key bindings, mouse functions, or GUI elements (see https://github.com/hci-unihd/covid-if-annotations/issues/3).
I propose that the next hookspecs we add are for our key bindings for our layers. We can then grow to other hookspecs after.
Motivation
I think we should continue with our functional approach, and our approach of not providing ānapariā objects to plugins but instead providing things like layer data tuples, e.g. (data, meta, layer_type). The advantage of this approach is that it enforces a āloose couplingā with napari, our plugins canāt just do anything like nuke the whole viewer or layer, and it gives us a lot of control over what a plugin does as napari calls the plugin code. The downsides are some reductions in flexibility, and potentially performance, but I think they are well worth it, if they are even really there.
Pitch
Itās unclear if hookspecs are the natural thing for what I have in mind, but I think each plugin should be able to provide one dictionary per layer type. The keys of that dictionary need to valid key press combinations for napari, and the values of that dictionary need to be functions with the following signature:
image_key_bindings_func(data: Any, meta: dict) -> (Any, dict)
Note that this is different from the API of our current keybindings functions which take in a layer and can then do anything. We could keep that API around or eventually deprecate it for this one.
How these keybindings would work is that when the key was pressed weād generate the (data, meta) tuple pass those to the function, and then get back a (new_data, new_meta) tuple. If new_data is not None weād set layer.data=new_data
, and then weād go through every element of new_meta
and update the layer property accordingly. This is a massive reduction in what keybindings can do, but I think it will actually make them much easier to understand and make this whole system more robust.
One question would be do we ācopyā the data when we pass it to the keybindings function, Iām not sure if weād want to do that as it might trigger some big delays, but it might make the whole system much more āsafeā as no in place modifications would be possible.
Curious what do @jni @tlambert03 @kne42 think of this approach? and how to best integrate it with the napari_plugin_engine? We could add a special āhookspecā where people are able to provide dictionaries instead of functions.
An interesting exercise would be how many of our current layer keybindings could be rewritten to conform to this API? Unfortunately not all of them as we have keybindings do things like change mode
(like painting
/ pan_zoom
) amongst other things, which arenāt inside our meta
dict, but maybe we could figure out how to fix that.
Issue Analytics
- State:
- Created 3 years ago
- Reactions:1
- Comments:21 (20 by maintainers)
I agree with lots of ideas that have been stated. Due to an abundance of laziness, I wonāt attribute the idea to the original author (sorry!), but hereās my random thoughts:
import napari
and going to town. If you want a constrained plugin interface with good isolation, python is probably the wrong language[1].[1] It would be super fascinating to explore a plugin interface based on python multiprocessing + shared memory, as that would deliver the constrained plugin interface with good isolation.
and Iāll add that Iām definitely not totally opposed to the pattern you just proposed there š
I certainly agree there are code safety concerns, and it would be nice to be able to prevent something like this:
at the same time, thereāa a good percentage of our current keybindings that we wouldnāt be able to implement with this pattern.
which gets me thinking, what about a proxy object? Something that behaves very much like the original object API, passing through āapprovedā methods and attributes, but preventing things like deletion, or any other unapproved methodsā¦