Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(core): 💡 GenWidget::gen_widget no longer requires a `&mut … #616

Merged
merged 1 commit into from
Aug 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ Please only add new entries below the [Unreleased](#unreleased---releasedate) he

## [@Unreleased] - @ReleaseDate

### Breaking

- **core**: `GenWidget::gen_widget` no longer requires a `&mut BuildCtx` parameter. (#616 @M-Adoo)

## [0.4.0-alpha.5] - 2024-08-14

### Features
Expand Down
6 changes: 2 additions & 4 deletions core/src/builtin_widgets/theme.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,7 @@ pub struct ThemeWidget {

impl ComposeChild<'static> for ThemeWidget {
type Child = GenWidget;
fn compose_child(
this: impl StateWriter<Value = Self>, mut child: Self::Child,
) -> Widget<'static> {
fn compose_child(this: impl StateWriter<Value = Self>, child: Self::Child) -> Widget<'static> {
use crate::prelude::*;
let f = move |ctx: &BuildCtx| {
let theme = this.read().theme.clone();
Expand All @@ -95,7 +93,7 @@ impl ComposeChild<'static> for ThemeWidget {

// shadow the context with the theme.
let ctx = BuildCtx::new_with_data(Some(p), ctx.tree, themes);
let child = child.gen_widget(&ctx).build(&ctx);
let child = child.gen_widget().build(&ctx);
p.append(child, &mut ctx.tree.borrow_mut());

p
Expand Down
17 changes: 8 additions & 9 deletions core/src/overlay.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ impl OverlayCloseHandle {
pub fn close(&self) { self.0.close() }
}

type Builder = Box<dyn FnMut(OverlayCloseHandle, &BuildCtx) -> Widget<'static>>;
type Builder = Box<dyn FnMut(OverlayCloseHandle) -> Widget<'static>>;
struct OverlayData {
builder: Builder,
style: Option<OverlayStyle>,
Expand Down Expand Up @@ -67,8 +67,8 @@ impl Overlay {
/// App::run(w);
/// ```
pub fn new(gen: impl Into<GenWidget>) -> Self {
let mut gen = gen.into();
Self::inner_new(Box::new(move |_, ctx| gen.gen_widget(ctx)))
let gen = gen.into();
Self::inner_new(Box::new(move |_| gen.gen_widget()))
}

/// Create overlay from a builder with a close_handle
Expand Down Expand Up @@ -100,9 +100,9 @@ impl Overlay {
/// App::run(w).with_size(Size::new(200., 200.));
/// ```
pub fn new_with_handle(
mut builder: impl FnMut(OverlayCloseHandle) -> Widget<'static> + 'static,
builder: impl FnMut(OverlayCloseHandle) -> Widget<'static> + 'static,
) -> Self {
Self::inner_new(Box::new(move |ctrl, _| builder(ctrl)))
Self::inner_new(Box::new(builder))
}

/// Overlay will show with the given style, if the overlay have not been set
Expand All @@ -115,10 +115,10 @@ impl Overlay {
if self.is_show() {
return;
}
let ctx = BuildCtx::new(None, &wnd.widget_tree);

let mut inner = self.0.borrow_mut();
let handle = inner.state.close_handle();
let w = (inner.builder)(handle, &ctx);
let w = (inner.builder)(handle);
let style = inner.style.clone();
inner.state.show(w, style, wnd);
}
Expand Down Expand Up @@ -168,10 +168,9 @@ impl Overlay {
return;
}

let ctx = BuildCtx::new(None, &wnd.widget_tree);
let mut inner = self.0.borrow_mut();
let close_handle = inner.state.close_handle();
let overlay = (inner.builder)(close_handle.clone(), &ctx);
let overlay = (inner.builder)(close_handle.clone());
let overlay = f(overlay, close_handle);
let style = inner.style.clone();
inner
Expand Down
16 changes: 12 additions & 4 deletions core/src/widget.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
use std::cell::RefCell;
#[doc(hidden)]
pub use std::{
any::{Any, TypeId},
marker::PhantomData,
ops::Deref,
};

use ribir_algo::Sc;
use widget_id::{new_node, RenderQueryable};

pub(crate) use crate::widget_tree::*;
Expand Down Expand Up @@ -82,7 +84,8 @@ impl<'w> LazyWidget<'w> {

/// A boxed function widget that can be called multiple times to regenerate
/// widget.
pub struct GenWidget(Box<dyn FnMut(&BuildCtx) -> Widget<'static>>);
pub struct GenWidget(InnerGenWidget);
type InnerGenWidget = Sc<RefCell<Box<dyn FnMut(&BuildCtx) -> Widget<'static>>>>;

// The widget type marker.
pub const COMPOSE: usize = 1;
Expand Down Expand Up @@ -147,7 +150,7 @@ where

impl IntoWidgetStrict<'static, FN> for GenWidget {
#[inline]
fn into_widget_strict(self) -> Widget<'static> { self.0.into_widget_strict() }
fn into_widget_strict(self) -> Widget<'static> { self.gen_widget() }
}

impl<'a> Widget<'a> {
Expand Down Expand Up @@ -180,10 +183,15 @@ impl<'a> Widget<'a> {
}
impl GenWidget {
#[inline]
pub fn new(f: impl FnMut(&BuildCtx) -> Widget<'static> + 'static) -> Self { Self(Box::new(f)) }
pub fn new(f: impl FnMut(&BuildCtx) -> Widget<'static> + 'static) -> Self {
Self(Sc::new(RefCell::new(Box::new(f))))
}

#[inline]
pub fn gen_widget(&mut self, ctx: &BuildCtx) -> Widget<'static> { (self.0)(ctx) }
pub fn gen_widget(&self) -> Widget<'static> {
let f = self.0.clone();
fn_widget! { f.borrow_mut()(ctx!()) }.into_widget()
}
}

impl<F: FnMut(&BuildCtx) -> Widget<'static> + 'static> From<F> for GenWidget {
Expand Down
3 changes: 2 additions & 1 deletion macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,8 @@ pub fn lerp_derive(input: TokenStream) -> TokenStream {
/// `declare!` to build the `XXX` widget.
/// - for every field of `XXXBuilder`
/// - implement method with same name of the field and use to init the field.
/// [declare]: ../ribir/declare/index.html
///
/// [declare]: ../ribir/declare/index.html
#[proc_macro_derive(Declare, attributes(declare))]
pub fn declare_trait_macro_derive(input: TokenStream) -> TokenStream {
let mut input = parse_macro_input!(input as DeriveInput);
Expand Down
2 changes: 1 addition & 1 deletion widgets/src/tabs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,7 @@ impl ComposeChild<'static> for Tabs {
},
@ { header }
@Expanded {
@ { pipe!($this.cur_idx).map(move |idx| panes[idx].gen_widget(ctx!())) }
@ { pipe!($this.cur_idx).map(move |idx| panes[idx].gen_widget()) }
}
}
}
Expand Down
Loading