Bläddra i källkod

feat(on_scroll): broken attempt at scroll event for review

Eli Khazan 1 år sedan
förälder
incheckning
f96a8b9151
2 ändrade filer med 28 tillägg och 1 borttagningar
  1. 12 1
      nicegui/elements/scroll_area.py
  2. 16 0
      nicegui/events.py

+ 12 - 1
nicegui/elements/scroll_area.py

@@ -1,12 +1,23 @@
+from typing import Optional, Callable, Any
+
 from ..element import Element
+from ..events import handle_event, ScrollEventArguments
 
 
 class ScrollArea(Element):
 
-    def __init__(self) -> None:
+    def __init__(self, *,
+                 on_scroll: Optional[Callable[..., Any]] = None) -> None:
         """Scroll Area
 
         A way of customizing the scrollbars by encapsulating your content.
         """
         super().__init__('q-scroll-area')
         self._classes = ['nicegui-scroll']
+
+        def scroll_handle(info):
+            handle_event(on_scroll, ScrollEventArguments(
+                sender=self, client=self.client, info=info))
+
+        if on_scroll:
+            self.on('scroll', scroll_handle)

+ 16 - 0
nicegui/events.py

@@ -268,6 +268,22 @@ class KeyEventArguments(EventArguments):
     modifiers: KeyboardModifiers
 
 
+@dataclass(**KWONLY_SLOTS)
+class ScrollInfo:
+    verticalPosition: int
+    verticalPercentage: float
+    verticalSize: int
+    verticalContainerSize: int
+    horizontalPosition: int
+    horizontalPercentage: float
+    horizontalSize: int
+    horizontalContainerSize: int
+
+
+@dataclass(**KWONLY_SLOTS)
+class ScrollEventArguments(EventArguments):
+    info: ScrollInfo
+
 def handle_event(handler: Optional[Callable[..., Any]],
                  arguments: Union[EventArguments, Dict], *,
                  sender: Optional['Element'] = None) -> None: