Skip to content

Commit

Permalink
refactor(ribir): 💡 remove DynWidget
Browse files Browse the repository at this point in the history
  • Loading branch information
M-Adoo committed Aug 28, 2023
1 parent cc71c71 commit 7baecbf
Show file tree
Hide file tree
Showing 20 changed files with 423 additions and 509 deletions.
7 changes: 2 additions & 5 deletions core/src/builtin_widgets/fitted_box.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,11 +101,8 @@ mod tests {
} = 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 w = fn_widget! {
@$fit { @MockBox { size } }
};
let mut wnd = TestWindow::new_with_size(w, WND_SIZE);
wnd.draw_frame();
Expand Down
39 changes: 19 additions & 20 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: Stateful<FocusNode>) -> impl FnMut(&'_ mut AllLifecycle) + 'static {
move |e| match e {
Expand Down Expand Up @@ -103,19 +103,18 @@ pub struct RequestFocus {

impl ComposeChild for RequestFocus {
type Child = Widget;
fn compose_child(this: State<Self>, child: Self::Child) -> Widget {
let this = this.into_writable();
let w: Widget = widget! {
states { this: this.clone() }
DynWidget {
on_mounted: move |ctx| {
this.silent().handle = Some(ctx.window().focus_mgr.borrow().focus_handle(ctx.id));
},
dyns: child
fn compose_child(mut this: State<Self>, child: Self::Child) -> Widget {
let this2 = this.clone_state();
let w: Widget = fn_widget! {
@ $child {
on_mounted: move |e| {
let handle = e.window().focus_mgr.borrow().focus_handle(e.id);
$this.silent().handle = Some(handle);
}
}
}
.into();
DataWidget::attach(w, this)
DataWidget::attach(w, this2)
}
}
impl RequestFocus {
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
12 changes: 5 additions & 7 deletions core/src/builtin_widgets/has_focus.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,11 @@ 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 compose_child(mut this: State<Self>, child: Self::Child) -> Widget {
fn_widget! {
@ $child {
on_focus_in: move|_| $this.focused = true,
on_focus_out: move |_| $this.focused = false,
}
}
.into()
Expand Down
16 changes: 7 additions & 9 deletions core/src/builtin_widgets/layout_box.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,13 @@ 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 compose_child(mut this: State<Self>, child: Self::Child) -> Widget {
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
12 changes: 5 additions & 7 deletions core/src/builtin_widgets/mouse_hover.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,11 @@ 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 compose_child(mut this: State<Self>, child: Self::Child) -> Widget {
fn_widget! {
@ $child {
on_pointer_enter: move |_| $this.hover = true,
on_pointer_leave: move |_| $this.hover = false,
}
}
.into()
Expand Down
12 changes: 5 additions & 7 deletions core/src/builtin_widgets/pointer_pressed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,11 @@ 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 compose_child(mut this: State<Self>, child: Self::Child) -> Widget {
fn_widget! {
@ $child {
on_pointer_down: move|_| $this.pointer_pressed = true,
on_pointer_up: move |_| $this.pointer_pressed = false,
}
}
.into()
Expand Down
11 changes: 5 additions & 6 deletions core/src/test_helper.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::timer::Timer;
pub use crate::timer::Timer;
use std::{
rc::Rc,
sync::atomic::{AtomicU64, Ordering},
Expand All @@ -22,8 +22,8 @@ pub struct TestWindow(pub Rc<Window>);
#[macro_export]
macro_rules! reset_test_env {
() => {
let _ = rxrust::scheduler::NEW_TIMER_FN.set(Timer::new_timer_future);
let _guard = unsafe { AppCtx::new_lock_scope() };
let _ = $crate::prelude::NEW_TIMER_FN.set(Timer::new_timer_future);
let _guard = unsafe { $crate::prelude::AppCtx::new_lock_scope() };
};
}

Expand Down Expand Up @@ -76,7 +76,6 @@ impl TestWindow {
pub fn draw_frame(&mut self) {
// Test window not have a eventloop, manually wake-up every frame.
Timer::wake_timeout_futures();
AppCtx::run_until_stalled();
self.run_frame_tasks();

self.0.draw_frame();
Expand Down Expand Up @@ -176,10 +175,10 @@ impl Render for MockStack {

impl_query_self_only!(MockStack);

#[derive(Declare, MultiChild)]
#[derive(Declare, Declare2, MultiChild)]
pub struct MockMulti;

#[derive(Declare, Clone, SingleChild)]
#[derive(Declare, Declare2, Clone, SingleChild)]
pub struct MockBox {
pub size: Size,
}
Expand Down
32 changes: 15 additions & 17 deletions core/src/widget_tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -244,24 +244,22 @@ mod tests {
}

impl Compose for Embed {
fn compose(this: State<Self>) -> Widget {
widget! {
states { this: this.into_writable()}
MockMulti {
Multi::new((0..this.width - 1)
.map(move |_| {
MockBox { size: Size::new(10., 10.)}
}))
DynWidget {
dyns: if this.depth > 1 {
Widget::from(Embed {
width: this.width,
depth: this.depth - 1,
})
} else {
Widget::from(MockBox { size: Size::new(10., 10.)})
}
fn compose(mut this: State<Self>) -> Widget {
fn_widget! {
let recursive_child: Widget = if $this.depth > 1 {
let width = $this.width;
let depth = $this.depth - 1;
Embed { width, depth }.into()
} else {
MockBox { size: Size::new(10., 10.) }.into()
};
@MockMulti {
@{
let leafs = (0..$this.width - 1)
.map(|_| MockBox { size: Size::new(10., 10.)} );
Multi::new(leafs)
}
@{ recursive_child }
}
}
.into()
Expand Down
1 change: 0 additions & 1 deletion core/src/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,6 @@ impl Window {
pub fn processes_native_event(&self, event: WindowEvent) {
let ratio = self.device_pixel_ratio() as f64;
self.dispatcher.borrow_mut().dispatch(event, ratio);
self.run_frame_tasks();
}

/// Request switch the focus to next widget.
Expand Down
21 changes: 13 additions & 8 deletions macros/src/declare_derive2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,16 +124,21 @@ pub(crate) fn declare_derive(input: &mut syn::DeriveInput) -> syn::Result<TokenS
let stt = data_struct_unwrap(data, DECLARE)?;

if stt.fields.is_empty() {
let construct = match &stt.fields {
Fields::Named(_) => quote!(#name {}),
Fields::Unnamed(_) => quote!(#name()),
Fields::Unit => quote!(#name),
};
let tokens = quote! {
impl Declare2 for #name {
type Builder = #name;
fn declare2_builder() -> Self::Builder { #name }
}
impl Declare2 for #name {
type Builder = #name;
fn declare2_builder() -> Self::Builder { #construct }
}

impl DeclareBuilder for #name {
type Target = #name;
fn build(self, _: &BuildCtx) -> Self::Target { self }
}
impl DeclareBuilder for #name {
type Target = #name;
fn build(self, _: &BuildCtx) -> Self::Target { self }
}
};
Ok(tokens)
} else {
Expand Down
1 change: 1 addition & 0 deletions ribir/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ impl App {
wnd.processes_native_event(event);
}
}
wnd.run_frame_tasks();
}
Event::MainEventsCleared => {
AppCtx::run_until_stalled();
Expand Down
Loading

0 comments on commit 7baecbf

Please sign in to comment.