1use std::fmt::{self, Debug};
4use std::rc::Rc;
5use std::time::Duration;
6
7use waft_animation::{Curve, Curves};
8use waft_embedder::Clip;
9use waft_foundation::{
10 App, DiagnosticPropertiesBuilder, DiagnosticsProperty, Handle, IntProperty, Listener,
11};
12use waft_painting::AlignmentGeometry;
13use waft_rendering::{
14 AnyRenderObject, RenderAligningShiftedBox, RenderAnimatedSize, RenderBox, RenderHandle,
15 TickerProviderRef,
16};
17use waft_scheduler::{Ticker, TickerCallback, TickerProviderObject};
18
19use crate::framework::{
20 BuildContext, IntoWidget, KeyRef, RenderObjectWidget, SingleChildRenderObjectWidget, State,
21 StateData, StatefulWidget, WidgetRef,
22};
23use crate::widgets::basic::Directionality;
24use crate::widgets::ticker_provider::{
25 SingleTickerProviderStateMixin, SingleTickerProviderStateMixinData,
26};
27use waft_foundation::Ref;
28
29pub struct AnimatedSize {
32 pub key: Option<KeyRef>,
33 pub child: Option<WidgetRef>,
35 pub alignment: AlignmentGeometry,
38 pub curve: Rc<dyn Curve>,
41 pub duration: Duration,
44 pub reverse_duration: Option<Duration>,
47 pub clip_behavior: Clip,
49 pub on_end: Option<Listener>,
51}
52
53impl Debug for AnimatedSize {
54 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
55 f.debug_struct("AnimatedSize").finish_non_exhaustive()
56 }
57}
58
59impl AnimatedSize {
60 pub fn new(duration: Duration) -> AnimatedSize {
62 AnimatedSize {
63 key: None,
64 child: None,
65 alignment: AlignmentGeometry::CENTER,
66 curve: Curves::linear(),
67 duration,
68 reverse_duration: None,
69 clip_behavior: Clip::HardEdge,
70 on_end: None,
71 }
72 }
73
74 pub fn key(mut self, key: KeyRef) -> AnimatedSize {
76 self.key = Some(key);
77 self
78 }
79
80 #[cfg_attr(debug_assertions, track_caller)]
82 pub fn child<K>(mut self, child: impl IntoWidget<K>) -> AnimatedSize {
83 self.child = Some(child.into_widget());
84 self
85 }
86
87 pub fn alignment(mut self, alignment: AlignmentGeometry) -> AnimatedSize {
89 self.alignment = alignment;
90 self
91 }
92
93 pub fn curve(mut self, curve: Rc<dyn Curve>) -> AnimatedSize {
95 self.curve = curve;
96 self
97 }
98
99 pub fn reverse_duration(mut self, reverse_duration: Duration) -> AnimatedSize {
101 self.reverse_duration = Some(reverse_duration);
102 self
103 }
104
105 pub fn clip_behavior(mut self, clip_behavior: Clip) -> AnimatedSize {
107 self.clip_behavior = clip_behavior;
108 self
109 }
110
111 pub fn on_end(mut self, on_end: Listener) -> AnimatedSize {
113 self.on_end = Some(on_end);
114 self
115 }
116}
117
118impl StatefulWidget for AnimatedSize {
119 type State = AnimatedSizeState;
120
121 fn key(&self) -> Option<&KeyRef> {
122 self.key.as_ref()
123 }
124
125 fn debug_fill_properties(&self, _app: &App, properties: &mut DiagnosticPropertiesBuilder) {
126 properties.add(
127 DiagnosticsProperty::<AlignmentGeometry>::new("alignment", Some(self.alignment))
128 .default_value(Some(AlignmentGeometry::TOP_CENTER)),
129 );
130 properties.add(IntProperty::new("duration", self.duration.as_millis() as i64).unit("ms"));
131 properties.add(
132 IntProperty::<i64>::new(
133 "reverseDuration",
134 self.reverse_duration
135 .map(|duration| duration.as_millis() as i64),
136 )
137 .unit("ms")
138 .default_value(None),
139 );
140 }
141
142 fn create_state(&self) -> AnimatedSizeState {
143 AnimatedSizeState {
144 state: StateData::new(),
145 single_ticker_provider: SingleTickerProviderStateMixinData::new(),
146 }
147 }
148}
149
150pub struct AnimatedSizeState {
152 state: StateData<AnimatedSize>,
153 single_ticker_provider: SingleTickerProviderStateMixinData,
154}
155
156impl Debug for AnimatedSizeState {
157 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
158 f.debug_struct("AnimatedSizeState").finish_non_exhaustive()
159 }
160}
161
162impl SingleTickerProviderStateMixin for AnimatedSizeState {
163 fn single_ticker_provider_data(
164 self: Handle<Self>,
165 app: &App,
166 ) -> &SingleTickerProviderStateMixinData {
167 &app.get(self).single_ticker_provider
168 }
169
170 fn single_ticker_provider_data_mut(
171 self: Handle<Self>,
172 app: &mut App,
173 ) -> &mut SingleTickerProviderStateMixinData {
174 &mut app.get_mut(self).single_ticker_provider
175 }
176}
177
178impl TickerProviderObject for AnimatedSizeState {
179 fn create_ticker(self: Handle<Self>, app: &mut App, on_tick: TickerCallback) -> Ref<Ticker> {
180 SingleTickerProviderStateMixin::create_ticker(self, app, on_tick)
181 }
182}
183
184impl State for AnimatedSizeState {
185 type Widget = AnimatedSize;
186 crate::state_accessors!();
187
188 fn activate(self: Handle<Self>, app: &mut App) {
189 SingleTickerProviderStateMixin::activate(self, app);
190 }
191
192 fn dispose(self: Handle<Self>, app: &mut App) {
193 SingleTickerProviderStateMixin::dispose(self, app);
194 }
195
196 fn build(self: Handle<Self>, app: &mut App, _context: BuildContext) -> WidgetRef {
197 let widget = self.widget(app);
198 AnimatedSizeRender {
199 key: None,
200 alignment: widget.alignment,
201 curve: widget.curve.clone(),
202 duration: widget.duration,
203 reverse_duration: widget.reverse_duration,
204 vsync: TickerProviderRef::new(self),
205 clip_behavior: widget.clip_behavior,
206 on_end: widget.on_end.clone(),
207 child: widget.child.clone(),
208 }
209 .into_widget()
210 }
211}
212
213struct AnimatedSizeRender {
215 key: Option<KeyRef>,
216 alignment: AlignmentGeometry,
217 curve: Rc<dyn Curve>,
218 duration: Duration,
219 reverse_duration: Option<Duration>,
220 vsync: TickerProviderRef,
221 clip_behavior: Clip,
222 on_end: Option<Listener>,
223 child: Option<WidgetRef>,
224}
225
226impl Debug for AnimatedSizeRender {
227 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
228 f.debug_struct("_AnimatedSize").finish_non_exhaustive()
229 }
230}
231
232impl RenderObjectWidget for AnimatedSizeRender {
233 type RenderObject = RenderAnimatedSize;
234
235 fn key(&self) -> Option<&KeyRef> {
236 self.key.as_ref()
237 }
238
239 fn create_render_object(&self, app: &mut App, context: BuildContext) -> AnyRenderObject {
240 let text_direction = Directionality::maybe_of(app, context);
241 RenderAnimatedSize::new(
242 app,
243 self.vsync.clone(),
244 self.duration,
245 self.reverse_duration,
246 self.curve.clone(),
247 self.alignment,
248 text_direction,
249 self.clip_behavior,
250 self.on_end.clone(),
251 None,
252 )
253 .as_object()
254 }
255
256 fn update_render_object(
257 &self,
258 app: &mut App,
259 context: BuildContext,
260 render_object: RenderHandle<RenderAnimatedSize>,
261 ) {
262 let text_direction = Directionality::maybe_of(app, context);
263 render_object.set_alignment(app, self.alignment);
264 render_object.set_duration(app, self.duration);
265 render_object.set_reverse_duration(app, self.reverse_duration);
266 render_object.set_curve(app, self.curve.clone());
267 render_object.set_vsync(app, self.vsync.clone());
268 render_object.set_text_direction(app, text_direction);
269 render_object.set_clip_behavior(app, self.clip_behavior);
270 render_object.set_on_end(app, self.on_end.clone());
271 }
272}
273
274impl SingleChildRenderObjectWidget for AnimatedSizeRender {
275 fn child(&self) -> Option<&WidgetRef> {
276 self.child.as_ref()
277 }
278}