Skip to content

Commit

Permalink
fix(core): 🐛 state notifies
Browse files Browse the repository at this point in the history
  • Loading branch information
M-Adoo committed Aug 18, 2023
1 parent 231b78c commit 5dd524e
Show file tree
Hide file tree
Showing 23 changed files with 536 additions and 401 deletions.
36 changes: 18 additions & 18 deletions core/src/animation/animate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ where
pub transition: T,
#[declare(strict)]
pub state: S,
pub from: <S::State as Share>::S,
pub from: <S::State as Share>::V,
#[declare(skip)]
running_info: Option<AnimateInfo<<S::State as Share>::S>>,
running_info: Option<AnimateInfo<<S::State as Share>::V>>,
#[declare(skip, default = ctx.window().id())]
window_id: WindowId,
}
Expand All @@ -24,10 +24,10 @@ pub trait AnimateState {

fn calc_lerp_value(
&mut self,
from: &<Self::State as Share>::S,
to: &<Self::State as Share>::S,
from: &<Self::State as Share>::V,
to: &<Self::State as Share>::V,
rate: f32,
) -> <Self::State as Share>::S;
) -> <Self::State as Share>::V;
}

/// A state with a lerp function as an animation state that use the `lerp_fn`
Expand Down Expand Up @@ -55,7 +55,7 @@ impl<T, S> State<Animate<T, S>>
where
T: Roc + 'static,
S: AnimateState + 'static,
<S::State as Share>::S: Clone,
<S::State as Share>::V: Clone,
{
pub fn run(&mut self) {
let mut animate_ref = self.state_ref();
Expand All @@ -69,7 +69,7 @@ where
} else if let Some(wnd) = AppCtx::get_window(wnd_id) {
drop(animate_ref);

let mut animate = self.clone();
let animate = self.clone_state();
let ticker = wnd.frame_ticker.frame_tick_stream();
let unsub = ticker.subscribe(move |msg| {
match msg {
Expand All @@ -78,7 +78,7 @@ where
let p = animate.state_ref().shallow().rate_at_instant(time);
if matches!(p, AnimateProgress::Finish) {
let wnd = AppCtx::get_window(wnd_id).unwrap();
let mut animate = animate.clone();
let animate = animate.clone();
observable::timer((), Duration::ZERO, wnd.frame_scheduler()).subscribe(move |_| {
animate.state_ref().silent().stop();
});
Expand Down Expand Up @@ -116,7 +116,7 @@ where
{
fn rate_at_instant(&mut self, now: Instant) -> AnimateProgress
where
<S::State as Share>::S: Clone,
<S::State as Share>::V: Clone,
{
let AnimateInfo {
from,
Expand Down Expand Up @@ -184,45 +184,45 @@ where
impl<S> AnimateState for S
where
S: Share,
S::S: Lerp,
S::V: Lerp,
{
type State = S;

fn state(&mut self) -> &mut Self::State { self }

fn calc_lerp_value(
&mut self,
from: &<Self::State as Share>::S,
to: &<Self::State as Share>::S,
from: &<Self::State as Share>::V,
to: &<Self::State as Share>::V,
rate: f32,
) -> <Self::State as Share>::S {
) -> <Self::State as Share>::V {
from.lerp(to, rate)
}
}

impl<S, F> AnimateState for LerpFnState<S, F>
where
S: Share,
F: FnMut(&<S as Share>::S, &<S as Share>::S, f32) -> <S as Share>::S,
F: FnMut(&<S as Share>::V, &<S as Share>::V, f32) -> <S as Share>::V,
{
type State = S;

fn state(&mut self) -> &mut Self::State { &mut self.state }

fn calc_lerp_value(
&mut self,
from: &<Self::State as Share>::S,
to: &<Self::State as Share>::S,
from: &<Self::State as Share>::V,
to: &<Self::State as Share>::V,
rate: f32,
) -> <Self::State as Share>::S {
) -> <Self::State as Share>::V {
(self.lerp_fn)(from, to, rate)
}
}

impl<S, F> LerpFnState<S, F>
where
S: Share,
F: FnMut(&<S as Share>::S, &<S as Share>::S, f32) -> <S as Share>::S,
F: FnMut(&<S as Share>::V, &<S as Share>::V, f32) -> <S as Share>::V,
{
#[inline]
pub fn new(state: S, lerp_fn: F) -> Self { Self { state, lerp_fn } }
Expand Down
12 changes: 6 additions & 6 deletions core/src/animation/transition.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ pub trait TransitionState: Sized + 'static {
fn transition<T: Roc + 'static>(mut self, transition: T, ctx: &BuildCtx)
where
Self: AnimateState,
<Self::State as Share>::S: Clone,
<Self::State as Share>::V: Clone,
{
let mut c_state = self.state().clone();
let mut c_state = self.state().clone_state();

let from = c_state.state_ref().clone();
let mut animate: State<Animate<T, Self>> = Animate::declare2_builder()
Expand All @@ -30,7 +30,7 @@ pub trait TransitionState: Sized + 'static {
.state(self)
.build(ctx);

let mut c_animate = animate.clone();
let mut c_animate = animate.clone_state();
let h = c_state.modifies().subscribe(move |_| {
animate.state_ref().from = c_state.state_ref().clone();
animate.run();
Expand All @@ -42,11 +42,11 @@ pub trait TransitionState: Sized + 'static {
fn transition_with<T: Roc + 'static>(
self,
transition: T,
lerp_fn: impl FnMut(&Self::S, &Self::S, f32) -> Self::S + 'static,
lerp_fn: impl FnMut(&Self::V, &Self::V, f32) -> Self::V + 'static,
ctx: &BuildCtx,
) where
Self: Share,
Self::S: Clone,
Self::V: Clone,
{
let animate_state = LerpFnState::new(self, lerp_fn);
animate_state.transition(transition, ctx)
Expand Down Expand Up @@ -90,7 +90,7 @@ impl<S: Share + 'static> TransitionState for S {}
impl<S, F> TransitionState for LerpFnState<S, F>
where
S: Share + 'static,
F: FnMut(&<S as Share>::S, &<S as Share>::S, f32) -> <S as Share>::S + 'static,
F: FnMut(&<S as Share>::V, &<S as Share>::V, f32) -> <S as Share>::V + 'static,
{
}

Expand Down
16 changes: 5 additions & 11 deletions core/src/builtin_widgets/key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,12 +91,7 @@ where
V: Clone + PartialEq,
Self: Any,
{
fn key(&self) -> Key {
match self {
State::Stateless(this) => this.key.clone(),
State::Stateful(this) => this.state_ref().key.clone(),
}
}
fn key(&self) -> Key { self.as_ref().key.clone() }

fn record_before_value(&self, key: &dyn AnyKey) {
assert_eq!(self.key(), key.key());
Expand All @@ -107,12 +102,11 @@ where
match self {
// stateless key widget needn't record before value.
State::Stateless(_) => {}
State::Stateful(this) => match key {
State::Stateless(key) => this.state_ref().record_before_value(key.value.clone()),
State::Stateful(key) => this
State::Stateful(this) => {
this
.state_ref()
.record_before_value(key.state_ref().value.clone()),
},
.record_before_value(key.as_ref().value.clone());
}
}
}

Expand Down
8 changes: 2 additions & 6 deletions core/src/builtin_widgets/theme.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,15 +81,11 @@ pub struct ThemeWidget {
impl ComposeChild for ThemeWidget {
type Child = Widget;
#[inline]
fn compose_child(this: State<Self>, child: Self::Child) -> Widget {
fn compose_child(mut this: State<Self>, child: Self::Child) -> Widget {
use crate::prelude::*;
FnWidget::new(move |ctx| {
let ctx = ctx.force_as_mut();

let theme = match this {
State::Stateless(t) => t.theme,
State::Stateful(s) => s.state_ref().theme.clone(),
};
let theme = this.state_ref().theme.clone();

AppCtx::load_font_from_theme(&theme);
ctx.push_theme(theme.clone());
Expand Down
4 changes: 2 additions & 2 deletions core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ pub mod prelude {
pub use ribir_geom::*;
#[doc(no_inline)]
pub use ribir_macros::{
ctx, fn_widget, include_svg, orphan_partial_state, partial_state, pipe, rdl, set_build_ctx,
watch, widget, Declare, Declare2, Lerp, MultiChild, SingleChild, Template, _dollar_ಠ_ಠ,
ctx, fn_widget, include_svg, partial_state, pipe, rdl, route_state, set_build_ctx, watch,
widget, Declare, Declare2, Lerp, MultiChild, SingleChild, Template, _dollar_ಠ_ಠ,
ribir_expanded_ಠ_ಠ,
};
#[doc(no_inline)]
Expand Down
Loading

0 comments on commit 5dd524e

Please sign in to comment.