kivymd / KivyMD

KivyMD is a collection of Material Design compliant widgets for use with Kivy, a framework for cross-platform, touch-enabled graphical applications. https://youtube.com/c/KivyMD https://twitter.com/KivyMD https://habr.com/ru/users/kivymd https://stackoverflow.com/tags/kivymd
https://kivymd.readthedocs.io
MIT License
2.25k stars 674 forks source link

MDDropdownMenu Acts Incorrectly When `show_duration`<0.3 (With Solution) #1745

Open Chitaoji opened 1 month ago

Chitaoji commented 1 month ago

MDDropdownMenu Acts Incorrectly When show_duration<0.3

I tried to create a MDDropdownMenu object with show_duration=0.0, but the result seemed funny to me (see the screenshot below).

I think i've already found the reason why this happened, which was in the class MotionDropDownMenuBehavior:

class MotionDropDownMenuBehavior(MotionBase):
    def on_open(self, *args):
        anim = Animation(
            _scale_y=1,
            duration=self.show_duration,
            transition=self.show_transition,
        )
        anim &= Animation(
            _scale_x=1,
            duration=self.show_duration - 0.3, # HERE!!
            transition="out_quad",
        )
        anim.start(self)

It seems that when duration=self.show_duration - 0.3 is less than 0, the animation will become strange and never-ending, so a single change may fix the problem:

class MotionDropDownMenuBehavior(MotionBase):
    def on_open(self, *args):
        anim = Animation(
            _scale_y=1,
            duration=self.show_duration,
            transition=self.show_transition,
        )
        anim &= Animation(
            _scale_x=1,
            duration=max(self.show_duration - 0.3, 0.0), # HERE!!
            transition="out_quad",
        )
        anim.start(self)

It works good with me, and will cause no other problems as far as I can see. But I'm not sure whether the program was meant to be like before.

Code and Logs

from kivy.lang import Builder
from kivymd.app import MDApp
from kivymd.uix.menu import MDDropdownMenu

KV = """
MDScreen:

    MDIconButton:
        id: button
        icon: "dots-vertical"
        pos_hint: {"center_x": .5, "center_y": .5}
        on_release: app.menu_open()
"""

class Test(MDApp):
    def menu_open(self):
        menu_items = [{"text": f"Item {i}"} for i in range(5)]
        MDDropdownMenu(
            caller=self.root.ids.button, items=menu_items, show_duration=0.0
        ).open()

    def build(self):
        return Builder.load_string(KV)

Test().run()

"""

class MainApp(App):
    def build(self):
        self.root = Builder.load_string(kv)

if __name__ == '__main__':
    MainApp().run()

Screenshots

c34a73a1147836b8d6d0ba78062f042

Versions