|
2 | 2 |
|
3 | 3 | import lvgl as lv |
4 | 4 | import mpos.apps |
| 5 | +from mpos.ui.anim import WidgetAnimator |
5 | 6 |
|
6 | 7 | th = None |
7 | 8 |
|
|
31 | 32 |
|
32 | 33 | foreground_app_name=None |
33 | 34 |
|
34 | | - |
35 | | -class WidgetAnimator: |
36 | | - def __init__(self): |
37 | | - self.animations = {} # Store animations for each widget |
38 | | - |
39 | | - # show_widget and hide_widget could have a (lambda) callback that sets the final state (eg: drawer_open) at the end |
40 | | - |
41 | | - def show_widget(self, widget, anim_type="fade", duration=500, delay=0): |
42 | | - """Show a widget with an animation (fade or slide).""" |
43 | | - # Clear HIDDEN flag to make widget visible for animation |
44 | | - widget.remove_flag(lv.obj.FLAG.HIDDEN) |
45 | | - |
46 | | - if anim_type == "fade": |
47 | | - # Create fade-in animation (opacity from 0 to 255) |
48 | | - anim = lv.anim_t() |
49 | | - anim.init() |
50 | | - anim.set_var(widget) |
51 | | - anim.set_values(0, 255) |
52 | | - anim.set_time(duration) |
53 | | - anim.set_delay(delay) |
54 | | - anim.set_custom_exec_cb(lambda anim, value: widget.set_style_opacity(value, 0)) |
55 | | - anim.set_path_cb(lv.anim_t.path_ease_in_out) |
56 | | - # Ensure opacity is reset after animation |
57 | | - anim.set_completed_cb(lambda *args: widget.set_style_opacity(255, 0)) |
58 | | - elif anim_type == "slide_down": |
59 | | - print("doing slide_down") |
60 | | - # Create slide-down animation (y from -height to original y) |
61 | | - original_y = widget.get_y() |
62 | | - height = widget.get_height() |
63 | | - anim = lv.anim_t() |
64 | | - anim.init() |
65 | | - anim.set_var(widget) |
66 | | - anim.set_values(original_y - height, original_y) |
67 | | - anim.set_time(duration) |
68 | | - anim.set_delay(delay) |
69 | | - anim.set_custom_exec_cb(lambda anim, value: widget.set_y(value)) |
70 | | - anim.set_path_cb(lv.anim_t.path_ease_in_out) |
71 | | - # Reset y position after animation |
72 | | - anim.set_completed_cb(lambda *args: widget.set_y(original_y)) |
73 | | - elif anim_type == "slide_up": |
74 | | - # Create slide-up animation (y from +height to original y) |
75 | | - original_y = widget.get_y() |
76 | | - height = widget.get_height() |
77 | | - anim = lv.anim_t() |
78 | | - anim.init() |
79 | | - anim.set_var(widget) |
80 | | - anim.set_values(original_y + height, original_y) |
81 | | - anim.set_time(duration) |
82 | | - anim.set_delay(delay) |
83 | | - anim.set_custom_exec_cb(lambda anim, value: widget.set_y(value)) |
84 | | - anim.set_path_cb(lv.anim_t.path_ease_in_out) |
85 | | - # Reset y position after animation |
86 | | - anim.set_completed_cb(lambda *args: widget.set_y(original_y)) |
87 | | - |
88 | | - # Store and start animation |
89 | | - self.animations[widget] = anim |
90 | | - anim.start() |
91 | | - |
92 | | - def hide_widget(self, widget, anim_type="fade", duration=500, delay=0): |
93 | | - """Hide a widget with an animation (fade or slide).""" |
94 | | - if anim_type == "fade": |
95 | | - # Create fade-out animation (opacity from 255 to 0) |
96 | | - anim = lv.anim_t() |
97 | | - anim.init() |
98 | | - anim.set_var(widget) |
99 | | - anim.set_values(255, 0) |
100 | | - anim.set_time(duration) |
101 | | - anim.set_delay(delay) |
102 | | - anim.set_custom_exec_cb(lambda anim, value: widget.set_style_opacity(value, 0)) |
103 | | - anim.set_path_cb(lv.anim_t.path_ease_in_out) |
104 | | - # Set HIDDEN flag after animation |
105 | | - anim.set_completed_cb(lambda *args: self.hide_complete_cb(widget, original_y)) |
106 | | - elif anim_type == "slide_down": |
107 | | - # Create slide-down animation (y from original y to +height) |
108 | | - original_y = widget.get_y() |
109 | | - height = widget.get_height() |
110 | | - anim = lv.anim_t() |
111 | | - anim.init() |
112 | | - anim.set_var(widget) |
113 | | - anim.set_values(original_y, original_y + height) |
114 | | - anim.set_time(duration) |
115 | | - anim.set_delay(delay) |
116 | | - anim.set_custom_exec_cb(lambda anim, value: widget.set_y(value)) |
117 | | - anim.set_path_cb(lv.anim_t.path_ease_in_out) |
118 | | - # Set HIDDEN flag after animation |
119 | | - anim.set_completed_cb(lambda *args: self.hide_complete_cb(widget, original_y)) |
120 | | - elif anim_type == "slide_up": |
121 | | - print("hide with slide_up") |
122 | | - # Create slide-up animation (y from original y to -height) |
123 | | - original_y = widget.get_y() |
124 | | - height = widget.get_height() |
125 | | - anim = lv.anim_t() |
126 | | - anim.init() |
127 | | - anim.set_var(widget) |
128 | | - anim.set_values(original_y, original_y - height) |
129 | | - anim.set_time(duration) |
130 | | - anim.set_delay(delay) |
131 | | - anim.set_custom_exec_cb(lambda anim, value: widget.set_y(value)) |
132 | | - anim.set_path_cb(lv.anim_t.path_ease_in_out) |
133 | | - # Set HIDDEN flag after animation |
134 | | - anim.set_completed_cb(lambda *args: self.hide_complete_cb(widget, original_y)) |
135 | | - |
136 | | - # Store and start animation |
137 | | - self.animations[widget] = anim |
138 | | - anim.start() |
139 | | - |
140 | | - def hide_complete_cb(self, widget, original_y): |
141 | | - #print("hide_complete_cb") |
142 | | - widget.add_flag(lv.obj.FLAG.HIDDEN) |
143 | | - widget.set_y(original_y) |
144 | | - |
145 | | - |
146 | | - def stop_animation(self, widget): |
147 | | - """Stop any running animation for the widget.""" |
148 | | - if widget in self.animations: |
149 | | - self.animations[widget].delete() |
150 | | - del self.animations[widget] |
151 | | - |
152 | 35 | animator = WidgetAnimator() |
153 | 36 |
|
154 | 37 | def get_pointer_xy(): |
|
0 commit comments