Advertisement
ElliotDG

Untitled

Mar 12th, 2025
121
0
179 days
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.93 KB | None | 0 0
  1. from kivy.app import App
  2. from kivy.lang import Builder
  3. from kivy.uix.boxlayout import BoxLayout
  4. from kivy.animation import Animation
  5. from kivy.core.window import Window
  6. from kivy.metrics import dp
  7. from kivy.clock import mainthread
  8. from kivy.properties import BooleanProperty, ListProperty
  9.  
  10. Window.size = 400, 600
  11.  
  12. kv = r'''
  13. PullDown:
  14.    orientation:"vertical"
  15.    Label:
  16.        id:btn
  17.        text:"Expandable Label"
  18.        size_hint_y: None
  19.        height:0
  20.        opacity:0
  21.    RecycleView:
  22.        id:rv
  23.        data: root.data
  24.        viewclass:"Button"
  25.        effect_cls: 'ScrollEffect'
  26.        RecycleGridLayout:
  27.            id:bl
  28.            cols:3
  29.            default_size_hint: 1, None
  30.            default_height: dp(101)
  31.            size_hint_y: None
  32.            size: self.minimum_size
  33. '''
  34.  
  35.  
  36. class PullDown(BoxLayout):
  37.     rv_open = BooleanProperty(False)  # used to create an event
  38.     data = ListProperty([{'text': str(x)} for x in range(18)])
  39.  
  40.     def __init__(self, **kwargs):
  41.         super().__init__(**kwargs)
  42.         self._animation_ongoing = False
  43.  
  44.     def on_touch_down(self, touch):
  45.         if self.collide_point(*touch.pos):
  46.             touch.grab(self)
  47.         if self.rv_open:
  48.             self.collapse(touch)
  49.         return super().on_touch_down(touch)
  50.  
  51.     def on_touch_move(self, touch):
  52.         if (touch.grab_current is self) and (not self._animation_ongoing):
  53.             if touch.dy < 5: # and self.ids.rv.effect_y.overscroll < -dp(150):
  54.                 self.expand(touch)
  55.                 return True
  56.         return super().on_touch_move(touch)
  57.  
  58.     def on_touch_up(self, touch):
  59.         if touch.grab_current is self:
  60.             touch.ungrab(self)
  61.         return super().on_touch_up(touch)
  62.  
  63.     def on_rv_open(self, instance, button_open):
  64.         print(f'{self.rv_open=}')
  65.  
  66.     @mainthread
  67.     def expand(self, touch):
  68.         self._animation_ongoing = True
  69.         # super().on_touch_up(touch)
  70.         anim = Animation(
  71.             height=dp(100),
  72.             opacity=1,
  73.             d=0.3, t="out_cubic"
  74.         )
  75.  
  76.         def on_animation_complete(*args):
  77.             self._animation_ongoing = False
  78.             self.rv_open = True
  79.  
  80.         anim.bind(on_complete=on_animation_complete)
  81.         anim.start(self.ids.btn)
  82.         self.ids.rv.scroll_y = 1
  83.  
  84.     @mainthread
  85.     def collapse(self, touch):
  86.         self._animation_ongoing = True
  87.         anim = Animation(
  88.             height=0,
  89.             opacity=0,
  90.             d=0.3, t="out_cubic"
  91.         )
  92.  
  93.         def on_animation_complete(*args):
  94.             self._animation_ongoing = False
  95.             self.rv_open = False
  96.             self.ids.rv.scroll_y = 1  # set scroll_y to None
  97.  
  98.         anim.bind(on_complete=on_animation_complete)
  99.         anim.start(self.ids.btn)
  100.  
  101.  
  102. class Test(App):
  103.     def build(self):
  104.         self.title = 'Pull down touch'
  105.         return Builder.load_string(kv)
  106.  
  107.  
  108. Test().run()
  109.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement