Skip to content

Commit

Permalink
refactor(ribir): 💡 migrate to new syntax without DynWidget.
Browse files Browse the repository at this point in the history
  • Loading branch information
M-Adoo committed Sep 12, 2023
1 parent 904ed6e commit 45e9357
Show file tree
Hide file tree
Showing 82 changed files with 2,041 additions and 2,469 deletions.
2 changes: 1 addition & 1 deletion core/src/builtin_widgets/anchor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ impl PositionUnit {
}
}

pub fn lerp_fn(from: &Self, to: &Self, rate: f32, self_size: f32) -> PositionUnit {
pub fn lerp(from: &Self, to: &Self, rate: f32, self_size: f32) -> PositionUnit {
let from = from.abs_value(self_size);
let to = to.abs_value(self_size);
PositionUnit::Pixel(from.lerp(&to, rate))
Expand Down
4 changes: 2 additions & 2 deletions core/src/builtin_widgets/cursor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ pub struct Cursor {

impl ComposeChild for Cursor {
type Child = Widget;
fn compose_child(mut this: State<Self>, child: Self::Child) -> Widget {
fn compose_child(this: State<Self>, child: Self::Child) -> Widget {
fn_widget! {
let mut save_cursor = Stateful::new(CursorIcon::Default);
@$child {
Expand All @@ -20,7 +20,7 @@ impl ComposeChild for Cursor {
&& e.mouse_buttons() == MouseButtons::empty()
{
let wnd = e.window();
*$save_cursor = wnd.get_cursor();
*$save_cursor.write() = wnd.get_cursor();
wnd.set_cursor($this.get_cursor());
}
},
Expand Down
11 changes: 4 additions & 7 deletions core/src/builtin_widgets/fitted_box.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,18 +100,15 @@ mod tests {
expected_scale,
} = self;
let fit = Stateful::new(FittedBox { box_fit, scale_cache: <_>::default() });
let c_fit = fit.clone();
let w = widget! {
DynWidget {
dyns: fit,
MockBox { size }
}
let c_fit = fit.clone_reader();
let w = fn_widget! {
@$fit { @MockBox { size } }
};
let mut wnd = TestWindow::new_with_size(w, WND_SIZE);
wnd.draw_frame();

assert_layout_result_by_path!(wnd, {path = [0], size == expect,} );
assert_eq!(c_fit.state_ref().scale_cache.get(), expected_scale);
assert_eq!(c_fit.read().scale_cache.get(), expected_scale);
}
}

Expand Down
20 changes: 10 additions & 10 deletions core/src/builtin_widgets/focus_node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,21 +43,21 @@ impl ComposeChild for FocusNode {
let this = this.into_writable();
FnWidget::new(|ctx| {
let id = child.build(ctx);

if !ctx.assert_get(id).contain_type::<FocusNode>() {
let has_focus_node = ctx.assert_get(id).contain_type::<FocusNode>();
if !has_focus_node {
let subject = ctx
.assert_get(id)
.query_on_first_type(QueryOrder::OutsideFirst, |l: &LifecycleListener| {
l.lifecycle_stream()
})
.unwrap_or_else(|| {
let listener = LifecycleListener::default();
let subject = listener.lifecycle_stream();
attach_to_id(id, &mut *ctx.tree.borrow_mut(), |child| {
Box::new(DataWidget::new(child, listener))
});
subject
});
let subject = subject.unwrap_or_else(|| {
let listener = LifecycleListener::default();
let subject = listener.lifecycle_stream();
attach_to_id(id, &mut *ctx.tree.borrow_mut(), |child| {
Box::new(DataWidget::new(child, listener))
});
subject
});

fn subscribe_fn(this: Reader<FocusNode>) -> impl FnMut(&'_ mut AllLifecycle) + 'static {
move |e| match e {
Expand Down
9 changes: 4 additions & 5 deletions core/src/builtin_widgets/focus_scope.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,10 @@ pub struct FocusScope {
impl ComposeChild for FocusScope {
type Child = Widget;
fn compose_child(this: State<Self>, child: Self::Child) -> Widget {
let w = widget! {
DynWidget {
dyns: child,
on_mounted: move |ctx| ctx.window().add_focus_node(ctx.id, false, FocusType::Scope),
on_disposed: move|ctx| ctx.window().remove_focus_node(ctx.id, FocusType::Scope),
let w = fn_widget! {
@ $child {
on_mounted: move |e| e.window().add_focus_node(e.id, false, FocusType::Scope),
on_disposed: move|e| e.window().remove_focus_node(e.id, FocusType::Scope),
}
};
DataWidget::attach_state(w.into(), this)
Expand Down
10 changes: 4 additions & 6 deletions core/src/builtin_widgets/has_focus.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,10 @@ impl HasFocus {
impl ComposeChild for HasFocus {
type Child = Widget;
fn compose_child(this: State<Self>, child: Self::Child) -> Widget {
widget! {
states {this: this.into_writable()}
DynWidget {
dyns: child,
on_focus_in: move|_| this.focused = true,
on_focus_out: move |_| this.focused = false,
fn_widget! {
@ $child {
on_focus_in: move|_| $this.write().focused = true,
on_focus_out: move |_| $this.write().focused = false,
}
}
.into()
Expand Down
14 changes: 6 additions & 8 deletions core/src/builtin_widgets/layout_box.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,12 @@ pub struct LayoutBox {
impl ComposeChild for LayoutBox {
type Child = Widget;
fn compose_child(this: State<Self>, child: Self::Child) -> Widget {
widget! {
states { this: this.into_writable() }
DynWidget {
dyns: child,
on_performed_layout: move |ctx| {
let new_rect = ctx.box_rect().unwrap();
if this.rect != new_rect {
this.silent().rect = new_rect;
fn_widget! {
@ $child {
on_performed_layout: move |e| {
let new_rect = e.box_rect().unwrap();
if $this.rect != new_rect {
$this.silent().rect = new_rect;
}
}
}
Expand Down
91 changes: 46 additions & 45 deletions core/src/builtin_widgets/lifecycle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,43 +133,47 @@ mod tests {

use crate::{
prelude::*,
reset_test_env,
test_helper::{MockBox, MockMulti, TestWindow},
};

#[test]
fn full_lifecycle() {
let _guard = unsafe { AppCtx::new_lock_scope() };
reset_test_env!();

let trigger = Stateful::new(true);
let lifecycle = Stateful::new(vec![]);
let c_lc = lifecycle.clone_reader();
let c_trigger = trigger.clone_writer();

let w = widget! {
states {
trigger: trigger.clone(),
lifecycle: lifecycle.clone()
}
MockBox {
let w = fn_widget! {
@MockBox {
size: Size::zero(),
on_mounted: move |_| lifecycle.silent().push("static mounted"),
on_performed_layout: move |_| lifecycle.silent().push("static performed layout"),
on_disposed: move |_| lifecycle.silent().push("static disposed"),
widget::then(*trigger, || widget! {
MockBox {
size: Size::zero(),
on_mounted: move |_| lifecycle.silent().push("dyn mounted"),
on_performed_layout: move |_| lifecycle.silent().push("dyn performed layout"),
on_disposed: move |_| lifecycle.silent().push("dyn disposed")
}
})
on_mounted: move |_| $lifecycle.write().push("static mounted"),
on_performed_layout: move |_| $lifecycle.write().push("static performed layout"),
on_disposed: move |_| $lifecycle.write().push("static disposed"),
@ {
pipe!(*$trigger).map(move |b| {
b.then(move || {
@MockBox {
size: Size::zero(),
on_mounted: move |_| $lifecycle.write().push("dyn mounted"),
on_performed_layout: move |_| $lifecycle.write().push("dyn performed layout"),
on_disposed: move |_| $lifecycle.write().push("dyn disposed")
}
})
})
}
}
};

let mut wnd = TestWindow::new_with_size(w, Size::new(100., 100.));
assert_eq!(&**lifecycle.state_ref(), ["static mounted"]);
assert_eq!(&**c_lc.read(), ["static mounted", "dyn mounted",]);

wnd.draw_frame();

assert_eq!(
&**lifecycle.state_ref(),
&**c_lc.read(),
[
"static mounted",
"dyn mounted",
Expand All @@ -178,11 +182,11 @@ mod tests {
]
);
{
*trigger.state_ref() = false;
*c_trigger.write() = false;
}
wnd.draw_frame();
assert_eq!(
&**lifecycle.state_ref(),
&**c_lc.read(),
[
"static mounted",
"dyn mounted",
Expand All @@ -196,42 +200,39 @@ mod tests {

#[test]
fn track_lifecycle() {
let _guard = unsafe { AppCtx::new_lock_scope() };
reset_test_env!();

let cnt = Stateful::new(3);
let mounted: Stateful<HashSet<WidgetId>> = Stateful::new(HashSet::default());
let disposed: Stateful<HashSet<WidgetId>> = Stateful::new(HashSet::default());
let w = widget! {
states {
cnt: cnt.clone(),
mounted: mounted.clone(),
disposed: disposed.clone(),
}
MockMulti {
Multi::new((0..*cnt).map(move |_| widget! {
MockBox {
size: Size::zero(),
on_mounted: move |ctx| {
mounted.insert(ctx.id);
},
on_disposed: move |ctx| {
disposed.insert(ctx.id);
},
}
}))

let c_cnt = cnt.clone_writer();
let c_mounted = mounted.clone_reader();
let c_disposed = disposed.clone_reader();
let w = fn_widget! {
@MockMulti {
@ {
pipe!(*$cnt).map(move |cnt| {
let iter = (0..cnt).map(move |_| @MockBox {
size: Size::zero(),
on_mounted: move |e| { $mounted.write().insert(e.id); },
on_disposed: move |e| { $disposed.write().insert(e.id); },
});
Multi::new(iter)
})
}
}
};

let mut wnd = TestWindow::new_with_size(w, Size::new(100., 100.));
wnd.draw_frame();
let mounted_ids = (*mounted.state_ref()).clone();
let mounted_ids = c_mounted.read().clone();

*cnt.state_ref() = 5;
*c_cnt.write() = 5;
wnd.on_wnd_resize_event(Size::zero());
wnd.draw_frame();

let disposed_ids = (*disposed.state_ref()).clone();
assert_eq!(mounted_ids.len(), 3);
assert_eq!(mounted_ids, disposed_ids);
assert_eq!(&mounted_ids, &*c_disposed.read());
}
}
10 changes: 4 additions & 6 deletions core/src/builtin_widgets/mouse_hover.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,10 @@ impl MouseHover {
impl ComposeChild for MouseHover {
type Child = Widget;
fn compose_child(this: State<Self>, child: Self::Child) -> Widget {
widget! {
states {this: this.into_writable()}
DynWidget {
dyns: child,
on_pointer_enter: move |_| this.hover = true,
on_pointer_leave: move |_| this.hover = false,
fn_widget! {
@ $child {
on_pointer_enter: move |_| $this.write().hover = true,
on_pointer_leave: move |_| $this.write().hover = false,
}
}
.into()
Expand Down
10 changes: 4 additions & 6 deletions core/src/builtin_widgets/pointer_pressed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,10 @@ impl PointerPressed {
impl ComposeChild for PointerPressed {
type Child = Widget;
fn compose_child(this: State<Self>, child: Self::Child) -> Widget {
widget! {
states { this: this.into_writable()}
DynWidget {
dyns: child,
on_pointer_down: move|_| this.pointer_pressed = true,
on_pointer_up: move |_| this.pointer_pressed = false,
fn_widget! {
@ $child {
on_pointer_down: move|_| $this.write().pointer_pressed = true,
on_pointer_up: move |_| $this.write().pointer_pressed = false,
}
}
.into()
Expand Down
24 changes: 12 additions & 12 deletions core/src/builtin_widgets/scrollable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ pub struct ScrollableWidget {

impl ComposeChild for ScrollableWidget {
type Child = Widget;
fn compose_child(mut this: State<Self>, child: Self::Child) -> Widget {
fn compose_child(this: State<Self>, child: Self::Child) -> Widget {
fn_widget! {
let mut view = @UnconstrainedBox {
dir: pipe!(match $this.get_scrollable() {
Expand All @@ -40,21 +40,21 @@ impl ComposeChild for ScrollableWidget {
clamp_dim: ClampDim::MAX_SIZE,
};

let mut child = @ $child {
left_anchor: pipe!($this.get_scroll_pos().x),
top_anchor: pipe!($this.get_scroll_pos().y),
};
let mut child = @ $child {
left_anchor: pipe!($this.get_scroll_pos().x),
top_anchor: pipe!($this.get_scroll_pos().y),
};

watch!($child.layout_size())
.distinct_until_changed()
.subscribe(move |v| $this.set_content_size(v));
watch!($view.layout_size())
.distinct_until_changed()
.subscribe(move |v| $this.set_page(v));
watch!($child.layout_size())
.distinct_until_changed()
.subscribe(move |v| $this.write().set_content_size(v));
watch!($view.layout_size())
.distinct_until_changed()
.subscribe(move |v| $this.write().set_page(v));

@Clip {
@ $view {
on_wheel: move |e| $this.validate_scroll(Point::new(e.delta_x, e.delta_y)),
on_wheel: move |e| $this.write().validate_scroll(Point::new(e.delta_x, e.delta_y)),
@ { child }
}
}
Expand Down
17 changes: 5 additions & 12 deletions core/src/builtin_widgets/theme.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,10 @@
//! Theme use to share visual config or style compose logic. It can be defined
//! to app-wide or particular part of the application.
use crate::{
declare::DeclareBuilder,
fill_svgs, impl_query_self_only,
prelude::include_svg,
prelude::{Any, BuildCtx, ComposeChild, Declare, Query, QueryFiler, QueryOrder, TypeId},
state::State,
widget::{Widget, WidgetBuilder},
};
use ribir_algo::CowArc;
pub use ribir_algo::ShareResource;
use crate::{fill_svgs, impl_query_self_only, prelude::*, widget::WidgetBuilder};
pub use ribir_algo::{CowArc, ShareResource};
use ribir_geom::Size;
use ribir_macros::Declare2;
use ribir_text::TextStyle;
use std::{collections::HashMap, rc::Rc};

Expand Down Expand Up @@ -73,15 +66,15 @@ pub enum Theme {
Inherit(InheritTheme),
}

#[derive(Declare)]
#[derive(Declare, Declare2)]
pub struct ThemeWidget {
pub theme: Rc<Theme>,
}

impl ComposeChild for ThemeWidget {
type Child = Widget;
#[inline]
fn compose_child(mut this: State<Self>, child: Self::Child) -> Widget {
fn compose_child(this: State<Self>, child: Self::Child) -> Widget {
use crate::prelude::*;
FnWidget::new(move |ctx| {
let ctx = ctx.force_as_mut();
Expand Down
Loading

0 comments on commit 45e9357

Please sign in to comment.