use std::fmt::{self, Debug};
use std::rc::Rc;
use std::time::Duration;
use waft_animation::{Curve, Curves};
use waft_embedder::Clip;
use waft_foundation::{App, Handle, Listener};
use waft_painting::AlignmentGeometry;
use waft_rendering::{
AnyRenderObject, RenderAligningShiftedBox, RenderAnimatedSize, RenderBox, RenderHandle,
TickerProviderRef,
};
use waft_scheduler::{Ticker, TickerCallback, TickerProviderObject};
use crate::framework::{
BuildContext, IntoWidget, KeyRef, RenderObjectWidget, SingleChildRenderObjectWidget, State,
StateData, StatefulWidget, WidgetRef,
};
use crate::widgets::basic::Directionality;
use crate::widgets::ticker_provider::{
SingleTickerProviderStateMixin, SingleTickerProviderStateMixinData,
};
use waft_foundation::Ref;
pub struct AnimatedSize {
pub key: Option<KeyRef>,
pub child: Option<WidgetRef>,
pub alignment: AlignmentGeometry,
pub curve: Rc<dyn Curve>,
pub duration: Duration,
pub reverse_duration: Option<Duration>,
pub clip_behavior: Clip,
pub on_end: Option<Listener>,
}
impl Debug for AnimatedSize {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("AnimatedSize").finish_non_exhaustive()
}
}
impl AnimatedSize {
pub fn new(duration: Duration) -> AnimatedSize {
AnimatedSize {
key: None,
child: None,
alignment: AlignmentGeometry::CENTER,
curve: Curves::linear(),
duration,
reverse_duration: None,
clip_behavior: Clip::HardEdge,
on_end: None,
}
}
pub fn key(mut self, key: KeyRef) -> AnimatedSize {
self.key = Some(key);
self
}
pub fn child<K>(mut self, child: impl IntoWidget<K>) -> AnimatedSize {
self.child = Some(child.into_widget());
self
}
pub fn alignment(mut self, alignment: AlignmentGeometry) -> AnimatedSize {
self.alignment = alignment;
self
}
pub fn curve(mut self, curve: Rc<dyn Curve>) -> AnimatedSize {
self.curve = curve;
self
}
pub fn reverse_duration(mut self, reverse_duration: Duration) -> AnimatedSize {
self.reverse_duration = Some(reverse_duration);
self
}
pub fn clip_behavior(mut self, clip_behavior: Clip) -> AnimatedSize {
self.clip_behavior = clip_behavior;
self
}
pub fn on_end(mut self, on_end: Listener) -> AnimatedSize {
self.on_end = Some(on_end);
self
}
}
impl StatefulWidget for AnimatedSize {
type State = AnimatedSizeState;
fn key(&self) -> Option<&KeyRef> {
self.key.as_ref()
}
fn create_state(&self) -> AnimatedSizeState {
AnimatedSizeState {
state: StateData::new(),
single_ticker_provider: SingleTickerProviderStateMixinData::new(),
}
}
}
pub struct AnimatedSizeState {
state: StateData<AnimatedSize>,
single_ticker_provider: SingleTickerProviderStateMixinData,
}
impl Debug for AnimatedSizeState {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("AnimatedSizeState").finish_non_exhaustive()
}
}
impl SingleTickerProviderStateMixin for AnimatedSizeState {
fn single_ticker_provider_data(
self: Handle<Self>,
app: &App,
) -> &SingleTickerProviderStateMixinData {
&app.get(self).single_ticker_provider
}
fn single_ticker_provider_data_mut(
self: Handle<Self>,
app: &mut App,
) -> &mut SingleTickerProviderStateMixinData {
&mut app.get_mut(self).single_ticker_provider
}
}
impl TickerProviderObject for AnimatedSizeState {
fn create_ticker(self: Handle<Self>, app: &mut App, on_tick: TickerCallback) -> Ref<Ticker> {
SingleTickerProviderStateMixin::create_ticker(self, app, on_tick)
}
}
impl State for AnimatedSizeState {
type Widget = AnimatedSize;
crate::state_accessors!();
fn activate(self: Handle<Self>, app: &mut App) {
SingleTickerProviderStateMixin::activate(self, app);
}
fn dispose(self: Handle<Self>, app: &mut App) {
SingleTickerProviderStateMixin::dispose(self, app);
}
fn build(self: Handle<Self>, app: &mut App, _context: BuildContext) -> WidgetRef {
let widget = self.widget(app);
AnimatedSizeRender {
key: None,
alignment: widget.alignment,
curve: widget.curve.clone(),
duration: widget.duration,
reverse_duration: widget.reverse_duration,
vsync: TickerProviderRef::new(self),
clip_behavior: widget.clip_behavior,
on_end: widget.on_end.clone(),
child: widget.child.clone(),
}
.into_widget()
}
}
struct AnimatedSizeRender {
key: Option<KeyRef>,
alignment: AlignmentGeometry,
curve: Rc<dyn Curve>,
duration: Duration,
reverse_duration: Option<Duration>,
vsync: TickerProviderRef,
clip_behavior: Clip,
on_end: Option<Listener>,
child: Option<WidgetRef>,
}
impl Debug for AnimatedSizeRender {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("_AnimatedSize").finish_non_exhaustive()
}
}
impl RenderObjectWidget for AnimatedSizeRender {
type RenderObject = RenderAnimatedSize;
fn key(&self) -> Option<&KeyRef> {
self.key.as_ref()
}
fn create_render_object(&self, app: &mut App, context: BuildContext) -> AnyRenderObject {
let text_direction = Directionality::maybe_of(app, context);
RenderAnimatedSize::new(
app,
self.vsync.clone(),
self.duration,
self.reverse_duration,
self.curve.clone(),
self.alignment,
text_direction,
self.clip_behavior,
self.on_end.clone(),
None,
)
.as_object()
}
fn update_render_object(
&self,
app: &mut App,
context: BuildContext,
render_object: RenderHandle<RenderAnimatedSize>,
) {
let text_direction = Directionality::maybe_of(app, context);
render_object.set_alignment(app, self.alignment);
render_object.set_duration(app, self.duration);
render_object.set_reverse_duration(app, self.reverse_duration);
render_object.set_curve(app, self.curve.clone());
render_object.set_vsync(app, self.vsync.clone());
render_object.set_text_direction(app, text_direction);
render_object.set_clip_behavior(app, self.clip_behavior);
render_object.set_on_end(app, self.on_end.clone());
}
}
impl SingleChildRenderObjectWidget for AnimatedSizeRender {
fn child(&self) -> Option<&WidgetRef> {
self.child.as_ref()
}
}