Skip to content

Commit

Permalink
refactor(core): 💡 remove all widget builder traits
Browse files Browse the repository at this point in the history
  • Loading branch information
M-Adoo committed Aug 1, 2024
1 parent ae63d2f commit 2968317
Show file tree
Hide file tree
Showing 91 changed files with 485 additions and 773 deletions.
10 changes: 5 additions & 5 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,11 @@ Please only add new entries below the [Unreleased](#unreleased---releasedate) he

### Breaking

- Remove `ChildFrom` and `FromAnother` traits (#pr, @M-Adoo)
- Remove `SingleParent` and `MultiParent` traits. (#pr, @M-Adoo)
- Remove `PairChild` and `PairWithChild` traits. User can use a generic type instead. (#pr, @M-Adoo)
- Allow only the child to be converted to a widget or a type that implements the Into trait. (#pr, @M-Adoo)

- Removed `ChildFrom` and `FromAnother` traits (#pr @M-Adoo)
- Removed `SingleParent` and `MultiParent` traits. (#pr @M-Adoo)
- Removed `PairChild` and `PairWithChild` traits. User can use a generic type instead. (#pr @M-Adoo)
- Allow only the child to be converted to a widget or a type that implements the Into trait. (#pr @M-Adoo)
- Removed the all builder traits such as WidgetBuilder and ComposeBuilder and so on. (#pr @M-Adoo)

## [0.4.0-alpha.3] - 2024-06-26

Expand Down
2 changes: 1 addition & 1 deletion core/src/animation/stagger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ mod tests {
use super::*;
use crate::{reset_test_env, test_helper::*};

fn stagger_run_and_stop() -> impl WidgetBuilder {
fn stagger_run_and_stop() -> impl IntoWidgetStrict<FN> {
fn_widget! {
let stagger = Stagger::new(Duration::from_millis(100), transitions::EASE_IN.of(ctx!()));
let mut mock_box = @MockBox { size: Size::new(100., 100.) };
Expand Down
45 changes: 18 additions & 27 deletions core/src/builtin_widgets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ pub struct LazyWidgetId(Sc<Cell<Option<WidgetId>>>);
/// let w = multi.get_margin_widget().clone_writer();
/// multi
/// .on_tap(move |_| w.write().margin = EdgeInsets::all(20.))
/// .build(ctx)
/// .into_widget(ctx)
/// };
/// ```
pub struct FatObj<T> {
Expand Down Expand Up @@ -845,21 +845,6 @@ impl<T> ObjDeclarer for FatObj<T> {
fn finish(self, _: &BuildCtx) -> Self::Target { self }
}

crate::widget::multi_build_replace_impl! {
impl<T: {#} > {#} for FatObj<T> {
#[track_caller]
fn build(self, ctx: &BuildCtx) -> Widget {
self.map(|host| host.build(ctx)).build(ctx)
}
}
}

impl WidgetBuilder for FatObj<Widget> {
#[inline]
#[track_caller]
fn build(self, ctx: &BuildCtx) -> Widget { self.into_widget_strict(ctx) }
}

impl<T, const M: usize> IntoWidgetStrict<M> for FatObj<T>
where
T: IntoWidget<M>,
Expand All @@ -869,19 +854,23 @@ where
let mut host = self.host.into_widget(ctx);
self.host_id.set(host.id());
if let Some(mix_builtin) = self.mix_builtin {
host = mix_builtin.with_child(host, ctx).build(ctx)
host = mix_builtin.with_child(host, ctx).into_widget(ctx)
}
if let Some(request_focus) = self.request_focus {
host = request_focus.with_child(host, ctx).build(ctx);
host = request_focus
.with_child(host, ctx)
.into_widget(ctx);
}
if let Some(has_focus) = self.has_focus {
host = has_focus.with_child(host, ctx).build(ctx);
host = has_focus.with_child(host, ctx).into_widget(ctx);
}
if let Some(mouse_hover) = self.mouse_hover {
host = mouse_hover.with_child(host, ctx).build(ctx);
host = mouse_hover.with_child(host, ctx).into_widget(ctx);
}
if let Some(pointer_pressed) = self.pointer_pressed {
host = pointer_pressed.with_child(host, ctx).build(ctx);
host = pointer_pressed
.with_child(host, ctx)
.into_widget(ctx);
}
if let Some(fitted_box) = self.fitted_box {
host = fitted_box.with_child(host, ctx).into_widget(ctx);
Expand All @@ -895,16 +884,16 @@ where
host = padding.with_child(host, ctx).into_widget(ctx);
}
if let Some(layout_box) = self.layout_box {
host = layout_box.with_child(host, ctx).build(ctx);
host = layout_box.with_child(host, ctx).into_widget(ctx);
}
if let Some(cursor) = self.cursor {
host = cursor.with_child(host, ctx).build(ctx);
host = cursor.with_child(host, ctx).into_widget(ctx);
}
if let Some(margin) = self.margin {
host = margin.with_child(host, ctx).into_widget(ctx);
}
if let Some(scrollable) = self.scrollable {
host = scrollable.with_child(host, ctx).build(ctx);
host = scrollable.with_child(host, ctx).into_widget(ctx);
}
if let Some(transform) = self.transform {
host = transform.with_child(host, ctx).into_widget(ctx);
Expand All @@ -921,16 +910,18 @@ where
.into_widget(ctx);
}
if let Some(global_anchor) = self.global_anchor {
host = global_anchor.with_child(host, ctx).build(ctx);
host = global_anchor
.with_child(host, ctx)
.into_widget(ctx);
}
if let Some(visibility) = self.visibility {
host = visibility.with_child(host, ctx).build(ctx);
host = visibility.with_child(host, ctx).into_widget(ctx);
}
if let Some(opacity) = self.opacity {
host = opacity.with_child(host, ctx).into_widget(ctx);
}
if let Some(keep_alive) = self.keep_alive {
host = keep_alive.with_child(host, ctx).build(ctx);
host = keep_alive.with_child(host, ctx).into_widget(ctx);
}
if let Some(h) = self.keep_alive_unsubscribe_handle {
let arena = &mut ctx.tree.borrow_mut().arena;
Expand Down
20 changes: 10 additions & 10 deletions core/src/builtin_widgets/align.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,47 +168,47 @@ mod tests {
const CHILD_SIZE: Size = Size::new(10., 10.);
const WND_SIZE: Size = Size::new(100., 100.);

fn h_align(h_align: HAlign) -> impl WidgetBuilder {
fn h_align(h_align: HAlign) -> impl IntoWidgetStrict<FN> {
fn_widget! {
@HAlignWidget {
h_align,
@MockBox { size: CHILD_SIZE }
}
}
}
fn left_align() -> impl WidgetBuilder { h_align(HAlign::Left) }
fn left_align() -> impl IntoWidgetStrict<FN> { h_align(HAlign::Left) }
widget_layout_test!(
left_align,
wnd_size = WND_SIZE,
{ path = [0], width == 100., height == 10.,}
{ path = [0, 0], size == CHILD_SIZE, }
);

fn h_center_align() -> impl WidgetBuilder { h_align(HAlign::Center) }
fn h_center_align() -> impl IntoWidgetStrict<FN> { h_align(HAlign::Center) }
widget_layout_test!(
h_center_align,
wnd_size = WND_SIZE,
{ path = [0], width == 100., height == 10.,}
{ path = [0, 0], x == 45., size == CHILD_SIZE,}
);

fn right_align() -> impl WidgetBuilder { h_align(HAlign::Right) }
fn right_align() -> impl IntoWidgetStrict<FN> { h_align(HAlign::Right) }
widget_layout_test!(
right_align,
wnd_size = WND_SIZE,
{ path = [0], width == 100., height == 10.,}
{ path = [0, 0], x == 90., size == CHILD_SIZE,}
);

fn h_stretch_algin() -> impl WidgetBuilder { h_align(HAlign::Stretch) }
fn h_stretch_algin() -> impl IntoWidgetStrict<FN> { h_align(HAlign::Stretch) }
widget_layout_test!(
h_stretch_algin,
wnd_size = WND_SIZE,
{ path = [0], width == 100., height == 10.,}
{ path = [0, 0], x == 0., width == 100., height == 10.,}
);

fn v_align(v_align: VAlign) -> impl WidgetBuilder {
fn v_align(v_align: VAlign) -> impl IntoWidgetStrict<FN> {
fn_widget! {
@VAlignWidget {
v_align,
Expand All @@ -217,31 +217,31 @@ mod tests {
}
}

fn top_align() -> impl WidgetBuilder { v_align(VAlign::Top) }
fn top_align() -> impl IntoWidgetStrict<FN> { v_align(VAlign::Top) }
widget_layout_test!(
top_align,
wnd_size = WND_SIZE,
{ path = [0], width == 10., height == 100.,}
{ path = [0, 0], size == CHILD_SIZE,}
);

fn v_center_align() -> impl WidgetBuilder { v_align(VAlign::Center) }
fn v_center_align() -> impl IntoWidgetStrict<FN> { v_align(VAlign::Center) }
widget_layout_test!(
v_center_align,
wnd_size = WND_SIZE,
{ path = [0], width == 10., height == 100.,}
{ path = [0, 0], y == 45., size == CHILD_SIZE,}
);

fn bottom_align() -> impl WidgetBuilder { v_align(VAlign::Bottom) }
fn bottom_align() -> impl IntoWidgetStrict<FN> { v_align(VAlign::Bottom) }
widget_layout_test!(
bottom_align,
wnd_size = WND_SIZE,
{ path = [0], width == 10., height == 100.,}
{ path = [0, 0], y == 90., size == CHILD_SIZE,}
);

fn v_stretch_align() -> impl WidgetBuilder { v_align(VAlign::Stretch) }
fn v_stretch_align() -> impl IntoWidgetStrict<FN> { v_align(VAlign::Stretch) }
widget_layout_test!(
v_stretch_align,
wnd_size = WND_SIZE,
Expand Down
8 changes: 4 additions & 4 deletions core/src/builtin_widgets/anchor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ mod test {
const CHILD_SIZE: Size = Size::new(50., 50.);
const WND_SIZE: Size = Size::new(100., 100.);

fn pixel_left_top() -> impl WidgetBuilder {
fn pixel_left_top() -> impl IntoWidgetStrict<FN> {
fn_widget! {
@MockBox {
size: CHILD_SIZE,
Expand All @@ -220,7 +220,7 @@ mod test {
{ path = [0, 0], x == 1., }
);

fn pixel_left_bottom() -> impl WidgetBuilder {
fn pixel_left_bottom() -> impl IntoWidgetStrict<FN> {
fn_widget! {
@MockBox {
size: CHILD_SIZE,
Expand All @@ -235,7 +235,7 @@ mod test {
{ path = [0, 0], x == 1., }
);

fn pixel_top_right() -> impl WidgetBuilder {
fn pixel_top_right() -> impl IntoWidgetStrict<FN> {
fn_widget! {
@MockBox {
size: CHILD_SIZE,
Expand All @@ -250,7 +250,7 @@ mod test {
{ path = [0, 0], x == 49.,}
);

fn pixel_bottom_right() -> impl WidgetBuilder {
fn pixel_bottom_right() -> impl IntoWidgetStrict<FN> {
fn_widget! {
@MockBox {
size: CHILD_SIZE,
Expand Down
2 changes: 1 addition & 1 deletion core/src/builtin_widgets/box_decoration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ mod tests {
}

const SIZE: Size = Size::new(100., 100.);
fn with_border() -> impl WidgetBuilder {
fn with_border() -> impl IntoWidgetStrict<FN> {
fn_widget! {
@MockBox {
size: SIZE,
Expand Down
2 changes: 1 addition & 1 deletion core/src/builtin_widgets/container.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ mod tests {
use crate::test_helper::*;

const SIZE: Size = Size::new(100., 100.);
fn smoke() -> impl WidgetBuilder {
fn smoke() -> impl IntoWidgetStrict<FN> {
fn_widget! { @Container { size: SIZE }}
}
widget_layout_test!(smoke, size == SIZE,);
Expand Down
2 changes: 1 addition & 1 deletion core/src/builtin_widgets/cursor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ impl Declare for Cursor {

impl ComposeChild for Cursor {
type Child = Widget;
fn compose_child(this: impl StateWriter<Value = Self>, child: Self::Child) -> impl WidgetBuilder {
fn compose_child(this: impl StateWriter<Value = Self>, child: Self::Child) -> impl IntoWidgetStrict<FN> {
fn_widget! {
let save_cursor: Stateful<Option<CursorIcon>> = Stateful::new(None);
@$child {
Expand Down
2 changes: 1 addition & 1 deletion core/src/builtin_widgets/fitted_box.rs
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ mod tests {
.test();
}

fn as_builtin_field() -> impl WidgetBuilder {
fn as_builtin_field() -> impl IntoWidgetStrict<FN> {
fn_widget! {
@MockBox {
size: Size::new(200., 200.),
Expand Down
4 changes: 2 additions & 2 deletions core/src/builtin_widgets/focus_node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@ impl Declare for RequestFocus {

impl ComposeChild for RequestFocus {
type Child = Widget;
fn compose_child(this: impl StateWriter<Value = Self>, child: Self::Child) -> impl WidgetBuilder {
fn compose_child(this: impl StateWriter<Value = Self>, child: Self::Child) -> impl IntoWidgetStrict<FN> {
fn_widget! {
@$child {
on_mounted: move |e| {
let handle = e.window().focus_mgr.borrow().focus_handle(e.id);
$this.silent().handle = Some(handle);
}
}
.build(ctx!())
.into_widget(ctx!())
.try_unwrap_state_and_attach(this, ctx!())
}
}
Expand Down
6 changes: 4 additions & 2 deletions core/src/builtin_widgets/focus_scope.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,15 @@ pub struct FocusScope {

impl ComposeChild for FocusScope {
type Child = Widget;
fn compose_child(this: impl StateWriter<Value = Self>, child: Self::Child) -> impl WidgetBuilder {
fn compose_child(
this: impl StateWriter<Value = Self>, child: Self::Child,
) -> impl IntoWidgetStrict<FN> {
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),
}
.build(ctx!())
.into_widget(ctx!())
.try_unwrap_state_and_attach(this, ctx!())
}
}
Expand Down
7 changes: 4 additions & 3 deletions core/src/builtin_widgets/global_anchor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@ impl Declare for GlobalAnchor {

impl ComposeChild for GlobalAnchor {
type Child = Widget;
#[inline]
fn compose_child(this: impl StateWriter<Value = Self>, child: Self::Child) -> impl WidgetBuilder {
fn compose_child(
this: impl StateWriter<Value = Self>, child: Self::Child,
) -> impl IntoWidgetStrict<FN> {
fn_widget! {
let wnd = ctx!().window();
let tick_of_layout_ready = wnd
Expand Down Expand Up @@ -177,7 +178,7 @@ mod tests {
use crate::test_helper::*;

const WND_SIZE: Size = Size::new(100., 100.);
fn global_anchor() -> impl WidgetBuilder {
fn global_anchor() -> impl IntoWidgetStrict<FN> {
fn_widget! {
let parent = @MockBox {
anchor: Anchor::left_top(10., 10.),
Expand Down
2 changes: 1 addition & 1 deletion core/src/builtin_widgets/has_focus.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ impl Declare for HasFocus {

impl ComposeChild for HasFocus {
type Child = Widget;
fn compose_child(this: impl StateWriter<Value = Self>, child: Self::Child) -> impl WidgetBuilder {
fn compose_child(this: impl StateWriter<Value = Self>, child: Self::Child) -> impl IntoWidgetStrict<FN> {
fn_widget! {
@ $child {
on_focus_in: move|_| $this.write().focused = true,
Expand Down
8 changes: 5 additions & 3 deletions core/src/builtin_widgets/keep_alive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ impl Declare for KeepAlive {
impl ComposeChild for KeepAlive {
type Child = Widget;
#[inline]
fn compose_child(this: impl StateWriter<Value = Self>, child: Self::Child) -> impl WidgetBuilder {
fn compose_child(
this: impl StateWriter<Value = Self>, child: Self::Child,
) -> impl IntoWidgetStrict<FN> {
fn_widget! {
let modifies = this.raw_modifies();
child.try_unwrap_state_and_attach(this, ctx!()).dirty_subscribe(modifies, ctx!())
Expand All @@ -52,11 +54,11 @@ mod tests {
let mut wnd = TestWindow::new(fn_widget! {
pipe! {
if *$remove_widget {
Void.build(ctx!())
Void.into_widget(ctx!())
} else {
FatObj::new(Void)
.keep_alive(pipe!(*$keep_alive))
.build(ctx!())
.into_widget(ctx!())
}
}
});
Expand Down
6 changes: 4 additions & 2 deletions core/src/builtin_widgets/key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,10 +124,12 @@ where
impl<V: 'static + Default + Clone + PartialEq> ComposeChild for KeyWidget<V> {
type Child = Widget;
#[inline]
fn compose_child(this: impl StateWriter<Value = Self>, child: Self::Child) -> impl WidgetBuilder {
fn compose_child(
this: impl StateWriter<Value = Self>, child: Self::Child,
) -> impl IntoWidgetStrict<FN> {
fn_widget! {
let data: Box<dyn AnyKey> = Box::new(this);
child.attach_data(Queryable(data), ctx!()).build(ctx!())
child.attach_data(Queryable(data), ctx!()).into_widget(ctx!())
}
}
}
Expand Down
Loading

0 comments on commit 2968317

Please sign in to comment.