# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

# # Mouse and Keyboard Events
#
# ipyevents documentation: https://github.com/mwcraig/ipyevents/blob/master/doc/Widget%20DOM%20Events.ipynb
#
# Event handling is not yet incorported into the image widget class.  This is a work in progress.
#

# +
import pathlib
import time

import numpy as np
import ipywidgets
import IPython
import image_attendant as ia

import ipyevents
import numpy_image_widget as niw
# -

# # Load image data

folder = pathlib.Path('./images/').resolve()

# +
files = list(folder.glob('*_small.jpg'))
files = sorted(files)
files = files + files[::-1]

images = []
for f in files:

    images.append(ia.read(f))
# -

# # Display

# +
wid_img = niw.NumpyImage(images[0])

wid_lab = ipywidgets.Text(description='info:')

wid_box = ipywidgets.VBox(children=[wid_img, wid_lab])

wid_box


# -

# # Event Handler

# +
def handle(ev):
    xy = ev['arrayX'], ev['arrayY'], ev['type']
    wid_lab.value = str(xy)
    
events = ipyevents.Event(source=wid_img, 
                         prevent_default_action=True,
                         watched_events=['click', 'contextmenu', 'mousemove'])

events.on_dom_event(handle)

_no_drag = ipyevents.Event(source=wid_img, watched_events=['dragstart'], prevent_default_action=True)
# -

events.supported_key_events

events.supported_mouse_events





StarcoderdataJupyterScriptsDedupFiltered