Skip to content

Commit

Permalink
refactor: [tmp] state split as reader and writer
Browse files Browse the repository at this point in the history
  • Loading branch information
M-Adoo committed Sep 4, 2023
1 parent 11217c9 commit 87a4e98
Show file tree
Hide file tree
Showing 20 changed files with 397 additions and 491 deletions.
14 changes: 7 additions & 7 deletions core/src/animation/animate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ where
<S::State as Share>::V: Clone,
{
pub fn run(&mut self) {
let mut animate_ref = self.state_ref();
let mut animate_ref = self.read();
let this = &mut *animate_ref;
let wnd_id = this.window_id;
let new_to = this.state.state().state_ref().clone();
Expand All @@ -69,24 +69,24 @@ where
} else if let Some(wnd) = AppCtx::get_window(wnd_id) {
drop(animate_ref);

let animate = self.clone_state();
let animate = self.clone_writer();
let ticker = wnd.frame_ticker.frame_tick_stream();
let unsub = ticker.subscribe(move |msg| {
match msg {
FrameMsg::NewFrame(time) => {
let p = animate.state_ref().shallow().rate_at_instant(time);
let p = animate.write().shallow().rate_at_instant(time);
if matches!(p, AnimateProgress::Finish) {
let wnd = AppCtx::get_window(wnd_id).unwrap();
let animate = animate.clone_state();
let animate = animate.clone_writer();
wnd
.frame_spawn(async move { animate.state_ref().silent().stop() })
.frame_spawn(async move { animate.write().silent().stop() })
.unwrap();
}
}
FrameMsg::LayoutReady(_) => {}
// use silent_ref because the state of animate change, bu no need to effect the framework.
FrameMsg::Finish(_) => {
let mut animate = animate.state_ref();
let mut animate = animate.write();
let animate = &mut **animate.silent();
let info = animate.running_info.as_mut().unwrap();
**animate.state.state().state_ref().shallow() = info.to.clone();
Expand All @@ -95,7 +95,7 @@ where
}
});
let guard = BoxSubscription::new(unsub).unsubscribe_when_dropped();
let animate = &mut *self.state_ref();
let animate = &mut *self.write();
animate.running_info = Some(AnimateInfo {
from: animate.from.clone(),
to: new_to,
Expand Down
8 changes: 4 additions & 4 deletions core/src/animation/transition.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ pub trait TransitionState: Sized + 'static {
mut self,
transition: T,
ctx: &BuildCtx,
) -> State<Animate<T, Self>>
) -> Writer<Animate<T, Self>>
where
Self: AnimateState,
<Self::State as Share>::V: Clone,
Expand All @@ -36,13 +36,13 @@ pub trait TransitionState: Sized + 'static {
.state(self)
.build_declare(ctx);

let c_animate = animate.clone_state();
let c_animate = animate.clone_writer();
let h = state
.modifies()
.map(move |_| state.state_ref().clone())
.pairwise()
.subscribe(move |(old, _)| {
animate.state_ref().from = old;
animate.write().from = old;
animate.run();
})
.unsubscribe_when_dropped();
Expand All @@ -56,7 +56,7 @@ pub trait TransitionState: Sized + 'static {
transition: T,
lerp_fn: F,
ctx: &BuildCtx,
) -> State<Animate<T, LerpFnState<Self, F>>>
) -> Writer<Animate<T, LerpFnState<Self, F>>>
where
Self: Share,
Self::V: Clone,
Expand Down
6 changes: 3 additions & 3 deletions core/src/builtin_widgets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ impl<T: ComposeChild + 'static> ComposeChild for FatObj<State<T>> {
type Child = T::Child;

fn compose_child(this: State<Self>, child: Self::Child) -> Widget {
let this = this.into_inner_value();
let this = this.into_value();
let Self { host, builtin } = this;
FnWidget::new(move |ctx| {
let this = host.with_child(child, ctx);
Expand All @@ -212,7 +212,7 @@ impl<T: ComposeChild + 'static> ComposeChild for FatObj<T> {
type Child = T::Child;

fn compose_child(this: State<Self>, child: Self::Child) -> Widget {
let this = this.into_inner_value();
let this = this.into_value();
let Self { host, builtin } = this;
FnWidget::new(move |ctx| {
let this = host.with_child(child, ctx);
Expand Down Expand Up @@ -242,7 +242,7 @@ impl ComposeChild for BuiltinObj {
type Child = Widget;

fn compose_child(this: State<Self>, child: Self::Child) -> Widget {
let this = this.into_inner_value();
let this = this.into_value();
fn_widget! { this.compose_with_host(child, ctx!()) }.into()
}
}
Expand Down
10 changes: 5 additions & 5 deletions core/src/builtin_widgets/focus_node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,18 +59,18 @@ impl ComposeChild for FocusNode {
subject
});

fn subscribe_fn(this: Stateful<FocusNode>) -> impl FnMut(&'_ mut AllLifecycle) + 'static {
fn subscribe_fn(this: Reader<FocusNode>) -> impl FnMut(&'_ mut AllLifecycle) + 'static {
move |e| match e {
AllLifecycle::Mounted(e) => {
let auto_focus = this.state_ref().auto_focus;
let auto_focus = this.read().auto_focus;
e.window().add_focus_node(e.id, auto_focus, FocusType::Node)
}
AllLifecycle::PerformedLayout(_) => {}
AllLifecycle::Disposed(e) => e.window().remove_focus_node(e.id, FocusType::Node),
}
}
let h = subject
.subscribe(subscribe_fn(this.clone()))
.subscribe(subscribe_fn(this.clone_reader()))
.unsubscribe_when_dropped();

attach_to_id(id, &mut *ctx.tree.borrow_mut(), |child| {
Expand Down Expand Up @@ -104,7 +104,7 @@ pub struct RequestFocus {
impl ComposeChild for RequestFocus {
type Child = Widget;
fn compose_child(this: State<Self>, child: Self::Child) -> Widget {
let this2 = this.clone_state();
let this2 = this.clone_reader();
let w: Widget = fn_widget! {
@$child {
on_mounted: move |e| {
Expand All @@ -114,7 +114,7 @@ impl ComposeChild for RequestFocus {
}
}
.into();
DataWidget::attach_state(w, this2)
DataWidget::attach(w, this2)
}
}
impl RequestFocus {
Expand Down
9 changes: 4 additions & 5 deletions core/src/builtin_widgets/key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,20 +69,19 @@ where
V: Default + Clone + PartialEq,
Self: Any,
{
fn key(&self) -> Key { self.state_ref().key.clone() }
fn key(&self) -> Key { self.write().key.clone() }

fn record_prev_key_widget(&self, key: &dyn AnyKey) {
assert_eq!(self.key(), key.key());
let Some(key) = key.as_any().downcast_ref::<Self>() else {
log::warn!("Different value type for same key.");
return;
};
self
.state_ref()
.record_before_value(key.state_ref().value.clone());
let mut this = self.write();
this.record_before_value(this.value.clone());
}

fn record_next_key_widget(&self, _: &dyn AnyKey) { self.state_ref().has_successor.set(true); }
fn record_next_key_widget(&self, _: &dyn AnyKey) { self.write().has_successor.set(true); }

fn as_any(&self) -> &dyn Any { self }
}
Expand Down
2 changes: 1 addition & 1 deletion core/src/builtin_widgets/theme.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ impl ComposeChild for ThemeWidget {
use crate::prelude::*;
FnWidget::new(move |ctx| {
let ctx = ctx.force_as_mut();
let theme = this.state_ref().theme.clone();
let theme = this.read().theme.clone();

AppCtx::load_font_from_theme(&theme);
ctx.push_theme(theme.clone());
Expand Down
2 changes: 1 addition & 1 deletion core/src/context/build_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ impl<'a> BuildCtx<'a> {
/// and fire mount events.
pub(crate) fn on_widget_mounted(&self, id: WidgetId) {
self.assert_get(id).query_all_type(
|notifier: &Modifier| {
|notifier: &Notifier| {
let state_changed = self.tree.borrow().dirty_set.clone();
notifier
.raw_modifies()
Expand Down
8 changes: 2 additions & 6 deletions core/src/data_widget.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,8 @@ impl<D: Query + 'static> DataWidget<D> {
pub fn attach_state(widget: Widget, data: State<D>) -> Widget {
match data.0.into_inner() {
InnerState::Data(data) => {
let (data, slot) = data.into_inner();
let mut widget = DataWidget::attach(widget, data);
if let Some(slot) = slot {
widget = DataWidget::attach(widget, AnonymousData::new(Box::new(slot)));
}
widget
let data = data.into_inner();
DataWidget::attach(widget, data)
}
InnerState::Stateful(data) => DataWidget::attach(widget, data),
}
Expand Down
6 changes: 6 additions & 0 deletions core/src/declare.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,13 @@ pub trait Declare2 {
/// document](declare) to know how to use it.
pub trait DeclareBuilder {
type Target;
/// build the object with the given context, return the object and not care
/// about if this object is subscribed to other or not.
fn build_declare(self, ctx: &BuildCtx) -> Self::Target;
/// build the object with the given context, return the object and a
/// subscription if the object is subscribed anything. You can use it to
/// control when to `unsubscribe` this object to its upstream.
fn build_declare_with_handle(self, ctx: &BuildCtx) -> (Self::Target, Option<()>);
}

/// The type use to store the init value of the field when declare a object.
Expand Down
Loading

0 comments on commit 87a4e98

Please sign in to comment.