Jelajahi Sumber

feat(set_scroll): exposed set methods of the element

Eli Khazan 1 tahun lalu
induk
melakukan
1687ee5c5b
1 mengubah file dengan 26 tambahan dan 1 penghapusan
  1. 26 1
      nicegui/elements/scroll_area.py

+ 26 - 1
nicegui/elements/scroll_area.py

@@ -1,5 +1,5 @@
 from dataclasses import fields
 from dataclasses import fields
-from typing import Optional, Callable, Any, Dict
+from typing import Optional, Callable, Any, Dict, Union, Literal
 
 
 from ..element import Element
 from ..element import Element
 from ..events import ScrollEventArguments, ScrollInfo
 from ..events import ScrollEventArguments, ScrollInfo
@@ -27,3 +27,28 @@ class ScrollArea(Element):
             client=self.client,
             client=self.client,
             info=ScrollInfo(**msg['args'])
             info=ScrollInfo(**msg['args'])
         ))
         ))
+
+    def set_scroll_position(self, offset: Union[int, float], *,
+                            axis: Literal['vertical', 'horizontal'] = 'vertical', duration_ms: int = 0
+                            ) -> None:
+        """
+
+        :param offset: Scroll position offset from top in pixels or percentage (0.0 <= x <= 1.0) of the total scrolling
+                        size
+        :param axis: Scroll axis to set
+        :param duration_ms: Duration (in milliseconds) enabling animated scroll
+        """
+        if offset < 0:
+            raise ValueError(f'scroll offset must be positive. Got: {offset}')
+
+        if type(offset) == int:
+            self.run_method('setScrollPosition', axis, offset, duration_ms)
+
+        elif type(offset) == float and offset > 1.0:
+            raise ValueError(f'a percentage scroll offset must be 0.0 <= x <= 1.0. Got: {offset}')
+
+        elif type(offset) == float and offset <= 1.0:
+            self.run_method('setScrollPercentage', axis, offset, duration_ms)
+
+        else:
+            raise ValueError(f'Got unsupported offset: {offset}')