generic_events_documentation.py 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. from nicegui import globals, ui
  2. from ..documentation_tools import text_demo
  3. def main_demo() -> None:
  4. """Generic Events
  5. Most UI elements come with predefined events.
  6. For example, a `ui.button` like "A" in the demo has an `on_click` parameter that expects a coroutine or function.
  7. But you can also use the `on` method to register a generic event handler like for "B".
  8. This allows you to register handlers for any event that is supported by JavaScript and Quasar.
  9. For example, you can register a handler for the `mousemove` event like for "C", even though there is no `on_mousemove` parameter for `ui.button`.
  10. Some events, like `mousemove`, are fired very often.
  11. To avoid performance issues, you can use the `throttle` parameter to only call the handler every `throttle` seconds ("D").
  12. The generic event handler can be synchronous or asynchronous and optionally takes an event dictionary as argument ("E").
  13. You can also specify which attributes of the JavaScript or Quasar event should be passed to the handler ("F").
  14. This can reduce the amount of data that needs to be transferred between the server and the client.
  15. You can also include `key modifiers <https://vuejs.org/guide/essentials/event-handling.html#key-modifiers>`_ (shown in input "G"),
  16. modifier combinations (shown in input "H"),
  17. and `event modifiers <https://vuejs.org/guide/essentials/event-handling.html#mouse-button-modifiers>`_ (shown in input "I").
  18. """
  19. with ui.row():
  20. ui.button('A', on_click=lambda: ui.notify('You clicked the button A.'))
  21. ui.button('B').on('click', lambda: ui.notify('You clicked the button B.'))
  22. with ui.row():
  23. ui.button('C').on('mousemove', lambda: ui.notify('You moved on button C.'))
  24. ui.button('D').on('mousemove', lambda: ui.notify('You moved on button D.'), throttle=0.5)
  25. with ui.row():
  26. ui.button('E').on('mousedown', lambda e: ui.notify(str(e)))
  27. ui.button('F').on('mousedown', lambda e: ui.notify(str(e)), ['ctrlKey', 'shiftKey'])
  28. with ui.row():
  29. ui.input('G').classes('w-12').on('keydown.space', lambda: ui.notify('You pressed space.'))
  30. ui.input('H').classes('w-12').on('keydown.y.shift', lambda: ui.notify('You pressed Shift+Y'))
  31. ui.input('I').classes('w-12').on('keydown.once', lambda: ui.notify('You started typing.'))
  32. def more() -> None:
  33. @text_demo('Custom events', '''
  34. It's fairly easy to emit custom events from javascript which can be listened to with `element.on(...)`.
  35. This can be useful if you want to call Python code when something happens in javascript.
  36. Like in this example where we are listening to the `visibilitychange` event of the browser tab.
  37. ''')
  38. async def custom_events() -> None:
  39. tabwatch = ui.checkbox('Watch browser tab re-entering') \
  40. .on('tabvisible', lambda: ui.notify('welcome back') if tabwatch.value else None)
  41. ui.add_head_html(f'''<script>
  42. document.addEventListener('visibilitychange', function() {{
  43. if (document.visibilityState === 'visible')
  44. document.getElementById({tabwatch.id}).dispatchEvent(new CustomEvent('tabvisible'))
  45. }});
  46. </script>''')
  47. # END OF DEMO
  48. await globals.get_client().connected()
  49. await ui.run_javascript(f'''
  50. document.addEventListener('visibilitychange', function() {{
  51. if (document.visibilityState === 'visible')
  52. document.getElementById({tabwatch.id}).dispatchEvent(new CustomEvent('tabvisible'))
  53. }});
  54. ''', respond=False)