Advertisement
ElliotDG

Untitled

Mar 9th, 2025
134
0
179 days
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.10 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.uix.label import Label
  5. from kivy.animation import Animation
  6. from kivy.core.window import Window
  7. from kivy.metrics import dp
  8. from kivy.clock import mainthread
  9. from kivy.properties import BooleanProperty
  10.  
  11. Window.size = 400, 600
  12.  
  13. kv = r'''
  14. PullDown:
  15.    orientation:"vertical"
  16.    Button:
  17.        id:btn
  18.        size_hint:1,None
  19.        height:0
  20.        opacity:0
  21.    ScrollView:
  22.        id:sv
  23.        BoxLayout:
  24.            id:bl
  25.            orientation:"vertical"
  26.            size_hint:1,None
  27.            height: dp(600) if  dp(500) < self.minimum_height < dp(560) else self.minimum_height
  28. '''
  29.  
  30.  
  31. class PullDown(BoxLayout):
  32.     sv_open = BooleanProperty(False)  # used to create an event
  33.     animation_ongoing = False
  34.  
  35.     def on_kv_post(self, base_widget):
  36.         for i in range(5):  # <<<<<<<--------------- buggy for 5 or 6, all others work
  37.             l = Label(size_hint=(1, None), height=dp(110), text="test")
  38.             self.ids.bl.add_widget(l)
  39.         # return super().on_kv_post(base_widget)  #this is an event, not overloading
  40.  
  41.     def on_touch_down(self, touch):
  42.         if self.collide_point(*touch.pos):
  43.             touch.grab(self)
  44.         if self.sv_open:
  45.             self.collapse(touch)
  46.         return super().on_touch_down(touch)
  47.  
  48.     def on_touch_move(self, touch):
  49.         if (touch.grab_current is self) and (not self.animation_ongoing):
  50.             if touch.dy < 0 and self.ids.sv.effect_y.overscroll < -dp(150):
  51.                 self.expand(touch)
  52.                 return False
  53.         return super().on_touch_move(touch)
  54.  
  55.     def on_touch_up(self, touch):
  56.         if touch.grab_current is self:
  57.             touch.ungrab(self)
  58.         return super().on_touch_up(touch)
  59.  
  60.     def on_sv_open(self, instance, button_open):
  61.         print(f'{self.sv_open=}')
  62.         # if button_open:
  63.         #     scroll_anim = Animation(scroll_y=1, d=.9)  # this is a hack, might be platform dependent
  64.         #     scroll_anim.start(self.ids.sv)
  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.sv_open = True
  79.  
  80.         anim.bind(on_complete=on_animation_complete)
  81.         anim.start(self.ids.btn)
  82.  
  83.     @mainthread
  84.     def collapse(self, touch):
  85.         self.animation_ongoing = True
  86.         super().on_touch_up(touch)
  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.sv_open = False
  96.             self.ids.sv.scroll_y = 1
  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.         return Builder.load_string(kv)
  105.  
  106.  
  107. Test().run()
  108.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement