Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 54d330c

Browse filesBrowse files
authored
better event docs, better docstring too (#654)
1 parent eb4b3f8 commit 54d330c
Copy full SHA for 54d330c

File tree

2 files changed

+144
-39
lines changed
Filter options

2 files changed

+144
-39
lines changed

‎docs/source/user_guide/guide.rst

Copy file name to clipboardExpand all lines: docs/source/user_guide/guide.rst
+111-30Lines changed: 111 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -249,13 +249,11 @@ Let's take a look at how we can define events to link ``Graphics`` and their pro
249249
Events
250250
------
251251

252-
Events can be a multitude of things: traditional events such as mouse or keyboard events, but they can also be
253-
You can use renderer events, such as mouse or keyboard events, or events related to ``Graphic`` properties.
254-
252+
Events can be a multitude of things: traditional events such as mouse or keyboard events, or events related to ``Graphic`` properties.
255253

256254
There are two ways to add events in ``fastplotlib``.
257255

258-
1) Use the method ::
256+
1) Use the method `add_event_handler()` ::
259257

260258
def event_handler(ev):
261259
pass
@@ -275,29 +273,16 @@ There are two ways to add events in ``fastplotlib``.
275273
276274

277275
The ``event_handler`` is a user-defined function that accepts an event instance as the first and only positional argument.
278-
Information about the structure of event instances are described below. The `"event_type"`
276+
Information about the structure of event instances are described below. The ``"event_type"``
279277
is a string that identifies the type of event; this can be either a ``pygfx.Event`` or a ``Graphic`` property event.
280-
See the above graphic-specific properties that can be used for events and below for the available ``pygfx`` events.
281-
282-
Rendering engine (``pygfx``) events:
283-
- "key_down"
284-
- "key_up"
285-
- "pointer_down"
286-
- "pointer_move"
287-
- "pointer_up"
288-
- "pointer_enter"
289-
- "pointer_leave"
290-
- "click"
291-
- "double_click"
292-
- "wheel"
293-
- "close"
294-
- "resize"
295-
296-
When an event occurs, the user-defined event handler will receive and event object. Depending on the type of event, the
278+
279+
``graphic.supported_events`` will return a tuple of all ``event_type`` strings that this graphic supports.
280+
281+
When an event occurs, the user-defined event handler will receive an event object. Depending on the type of event, the
297282
event object will have relevant information that can be used in the callback. See below for event tables.
298283

299-
Event Attributes
300-
^^^^^^^^^^^^^^^^
284+
Graphic property events
285+
^^^^^^^^^^^^^^^^^^^^^^^
301286

302287
All ``Graphic`` events have the following attributes:
303288

@@ -427,13 +412,107 @@ event_type: "selection"
427412
| value | np.ndarray | new [min, max] of selection |
428413
+----------+------------+-----------------------------+
429414

430-
Renderer Events
431-
^^^^^^^^^^^^^^^
415+
Rendering engine events from a Graphic
416+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
417+
418+
Rendering engine event handlers can be added to a graphic or to a Figure (see next section).
419+
Here is a description of all rendering engine events and their attributes.
420+
421+
* **pointer_down**: emitted when the user interacts with mouse,
422+
touch or other pointer devices, by pressing it down.
423+
424+
* *x*: horizontal position of the pointer within the widget.
425+
* *y*: vertical position of the pointer within the widget.
426+
* *button*: the button to which this event applies. See "Mouse buttons" section below for details.
427+
* *buttons*: a tuple of buttons being pressed down.
428+
* *modifiers*: a tuple of modifier keys being pressed down. See section below for details.
429+
* *ntouches*: the number of simultaneous pointers being down.
430+
* *touches*: a dict with int keys (pointer id's), and values that are dicts
431+
that contain "x", "y", and "pressure".
432+
* *time_stamp*: a timestamp in seconds.
433+
434+
* **pointer_up**: emitted when the user releases a pointer.
435+
This event has the same keys as the pointer down event.
436+
437+
* **pointer_move**: emitted when the user moves a pointer.
438+
This event has the same keys as the pointer down event.
439+
This event is throttled.
440+
441+
* **double_click**: emitted on a double-click.
442+
This event looks like a pointer event, but without the touches.
443+
444+
* **wheel**: emitted when the mouse-wheel is used (scrolling),
445+
or when scrolling/pinching on the touchpad/touchscreen.
446+
447+
Similar to the JS wheel event, the values of the deltas depend on the
448+
platform and whether the mouse-wheel, trackpad or a touch-gesture is
449+
used. Also, scrolling can be linear or have inertia. As a rule of
450+
thumb, one "wheel action" results in a cumulative ``dy`` of around
451+
100. Positive values of ``dy`` are associated with scrolling down and
452+
zooming out. Positive values of ``dx`` are associated with scrolling
453+
to the right. (A note for Qt users: the sign of the deltas is (usually)
454+
reversed compared to the QWheelEvent.)
455+
456+
On MacOS, using the mouse-wheel while holding shift results in horizontal
457+
scrolling. In applications where the scroll dimension does not matter,
458+
it is therefore recommended to use `delta = event['dy'] or event['dx']`.
459+
460+
* *dx*: the horizontal scroll delta (positive means scroll right).
461+
* *dy*: the vertical scroll delta (positive means scroll down or zoom out).
462+
* *x*: the mouse horizontal position during the scroll.
463+
* *y*: the mouse vertical position during the scroll.
464+
* *buttons*: a tuple of buttons being pressed down.
465+
* *modifiers*: a tuple of modifier keys being pressed down.
466+
* *time_stamp*: a timestamp in seconds.
467+
468+
* **key_down**: emitted when a key is pressed down.
469+
470+
* *key*: the key being pressed as a string. See section below for details.
471+
* *modifiers*: a tuple of modifier keys being pressed down.
472+
* *time_stamp*: a timestamp in seconds.
432473

433-
You can also add events to a ``Figure`` object's renderer. This is useful for defining click events where
434-
you want to map your click position to the nearest graphic object for example.
474+
* **key_up**: emitted when a key is released.
475+
This event has the same keys as the key down event.
435476

436-
Renderer events can be added using either method mentioned above (i.e. using the method or via a decorator).
477+
478+
Time stamps
479+
~~~~~~~~~~~
480+
481+
Since the time origin of ``time_stamp`` values is undefined,
482+
time stamp values only make sense in relation to other time stamps.
483+
484+
485+
Mouse buttons
486+
~~~~~~~~~~~~~
487+
488+
* 0: No button.
489+
* 1: Left button.
490+
* 2: Right button.
491+
* 3: Middle button
492+
* 4-9: etc.
493+
494+
495+
Keys
496+
~~~~
497+
498+
The key names follow the `browser spec <https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent>`_.
499+
500+
* Keys that represent a character are simply denoted as such. For these the case matters:
501+
"a", "A", "z", "Z" "3", "7", "&", " " (space), etc.
502+
* The modifier keys are:
503+
"Shift", "Control", "Alt", "Meta".
504+
* Some example keys that do not represent a character:
505+
"ArrowDown", "ArrowUp", "ArrowLeft", "ArrowRight", "F1", "Backspace", etc.
506+
507+
508+
Add renderer event handlers to a Figure
509+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
510+
511+
You can add event handlers to a ``Figure`` object's renderer. For example, this is useful for defining click events
512+
where you want to map click positions to the nearest graphic object. See the previous section for a description
513+
of all the renderer events.
514+
515+
Renderer event handlers can be added using a method or a decorator.
437516

438517
For example: ::
439518

@@ -484,7 +563,9 @@ ImageWidget
484563

485564
Often times, developing UIs for interacting with multi-dimension image data can be tedious and repetitive.
486565
In order to aid with common image and video visualization requirements the ``ImageWidget`` automatically generates sliders
487-
to easily navigate through different dimensions of your data. Let's look at an example: ::
566+
to easily navigate through different dimensions of your data. The image widget supports 2D, 3D and 4D arrays.
567+
568+
Let's look at an example: ::
488569

489570
import fastplotlib as fpl
490571
import imageio.v3 as iio

‎fastplotlib/graphics/_base.py

Copy file name to clipboardExpand all lines: fastplotlib/graphics/_base.py
+33-9Lines changed: 33 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -220,22 +220,18 @@ def event_handlers(self) -> list[tuple[str, callable, ...]]:
220220

221221
def add_event_handler(self, *args):
222222
"""
223-
Register an event handler.
223+
Register an event handler. Can also be used as a decorator.
224224
225225
Parameters
226226
----------
227227
callback: callable, the first argument
228228
Event handler, must accept a single event argument
229-
*types: list of strings
230-
A list of event types, ex: "click", "data", "colors", "pointer_down"
231229
232-
For the available renderer event types, see
233-
https://jupyter-rfb.readthedocs.io/en/stable/events.html
230+
*types: strings
231+
event types, ex: "click", "data", "colors", "pointer_down"
234232
235-
All feature support events, i.e. ``graphic.features`` will give a set of
236-
all features that are evented
237-
238-
Can also be used as a decorator.
233+
``supported_events`` will return a tuple of all event type strings that this graphic supports.
234+
See the user guide in the documentation for more information on events.
239235
240236
Example
241237
-------
@@ -316,6 +312,34 @@ def _handle_event(self, callback, event: pygfx.Event):
316312
callback(event)
317313

318314
def remove_event_handler(self, callback, *types):
315+
"""
316+
remove a registered event handler
317+
318+
Parameters
319+
----------
320+
callback: callable
321+
event handler that has been added
322+
323+
*types: strings
324+
event types that were registered with the given callback
325+
326+
Example
327+
-------
328+
329+
.. code-block:: py
330+
331+
# define event handler
332+
def my_handler(event):
333+
print(event)
334+
335+
# add event handler
336+
graphic.add_event_handler(my_handler, "pointer_up", "pointer_down")
337+
338+
# remove event handler
339+
graphic.remove_event_handler(my_handler, "pointer_up", "pointer_down")
340+
341+
"""
342+
319343
# remove from our record first
320344
for t in types:
321345
for wrapper_map in self._event_handler_wrappers[t]:

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.