diff --git a/core/src/animation/animate.rs b/core/src/animation/animate.rs index 19e7e1cd9..0d69707ed 100644 --- a/core/src/animation/animate.rs +++ b/core/src/animation/animate.rs @@ -14,7 +14,7 @@ where pub from: ::Value, #[declare(skip)] running_info: Option::Value>>, - #[declare(skip, default = ctx.window().id())] + #[declare(skip, default = ctx!().window().id())] window_id: WindowId, } diff --git a/core/src/builtin_widgets.rs b/core/src/builtin_widgets.rs index 1e56fcf3d..2fc94e47b 100644 --- a/core/src/builtin_widgets.rs +++ b/core/src/builtin_widgets.rs @@ -223,7 +223,7 @@ impl ComposeChild for FatObj { } impl SingleParent for FatObj { - fn append_child(self, child: WidgetId, ctx: &mut BuildCtx) -> WidgetId { + fn append_child(self, child: WidgetId, ctx: &BuildCtx) -> WidgetId { let Self { host, builtin } = self; let p = host.append_child(child, ctx); builtin.compose_with_host(p.into(), ctx).build(ctx) @@ -231,7 +231,7 @@ impl SingleParent for FatObj { } impl MultiParent for FatObj { - fn append_children(self, children: Vec, ctx: &mut BuildCtx) -> WidgetId { + fn append_children(self, children: Vec, ctx: &BuildCtx) -> WidgetId { let Self { host, builtin } = self; let host = host.append_children(children, ctx); builtin.compose_with_host(host.into(), ctx).build(ctx) diff --git a/core/src/builtin_widgets/theme.rs b/core/src/builtin_widgets/theme.rs index dfe1776a8..946655908 100644 --- a/core/src/builtin_widgets/theme.rs +++ b/core/src/builtin_widgets/theme.rs @@ -2,6 +2,7 @@ //! to app-wide or particular part of the application. use crate::{fill_svgs, impl_query_self_only, prelude::*, widget::StrictBuilder}; +use ribir_algo::Sc; pub use ribir_algo::{CowArc, ShareResource}; use ribir_geom::Size; use ribir_macros::Declare2; @@ -68,7 +69,7 @@ pub enum Theme { #[derive(Declare2)] pub struct ThemeWidget { - pub theme: Rc, + pub theme: Sc, } impl ComposeChild for ThemeWidget { @@ -77,17 +78,18 @@ impl ComposeChild for ThemeWidget { fn compose_child(this: State, child: Self::Child) -> Widget { use crate::prelude::*; FnWidget::new(move |ctx| { - let ctx = ctx.force_as_mut(); let theme = this.read().theme.clone(); - AppCtx::load_font_from_theme(&theme); - ctx.push_theme(theme.clone()); + + let mut themes = ctx.themes().clone(); + themes.push(theme.clone()); + let p = DataWidget::new(Box::new(Void), theme).strict_build(ctx); - let old = ctx.force_as_mut().reset_ctx_from(Some(p)); - let c = child.build(ctx); + // shadow the context with the theme. + let ctx = BuildCtx::new_with_data(Some(p), ctx.tree, themes); + let c = child.build(&ctx); ctx.append_child(p, c); - ctx.force_as_mut().reset_ctx_from(old); - ctx.pop_theme(); + p }) .into() diff --git a/core/src/context.rs b/core/src/context.rs index 7b3ae1032..4a0ce8c79 100644 --- a/core/src/context.rs +++ b/core/src/context.rs @@ -4,7 +4,7 @@ mod layout_ctx; mod widget_ctx; pub use layout_ctx::*; pub use widget_ctx::*; -pub(crate) mod build_context; -pub use build_context::BuildCtx; +pub(crate) mod build_ctx; +pub use build_ctx::BuildCtx; pub mod app_ctx; pub use app_ctx::*; diff --git a/core/src/context/build_context.rs b/core/src/context/build_ctx.rs similarity index 78% rename from core/src/context/build_context.rs rename to core/src/context/build_ctx.rs index 11f9eb8ef..9c2108e8e 100644 --- a/core/src/context/build_context.rs +++ b/core/src/context/build_ctx.rs @@ -1,18 +1,19 @@ +use ribir_algo::Sc; + use crate::{ prelude::*, widget::{widget_id::new_node, WidgetTree}, window::{DelayEvent, WindowId}, }; use std::{ - cell::{Ref, RefCell, UnsafeCell}, + cell::{OnceCell, Ref, RefCell}, ops::Deref, rc::Rc, }; /// A context provide during build the widget tree. pub struct BuildCtx<'a> { - // tmp `UnsafeCell` before use `BuildCtx` as mutable reference. - pub(crate) themes: UnsafeCell>>>, + pub(crate) themes: OnceCell>>, /// The widget which this `BuildCtx` is created from. It's not means this /// is the parent of the widget which is builded by this `BuildCtx`. ctx_from: Option, @@ -43,19 +44,27 @@ impl<'a> BuildCtx<'a> { } } - pub fn reset_ctx_from(&mut self, reset: Option) -> Option { - std::mem::replace(&mut self.ctx_from, reset) - } - #[inline] pub(crate) fn new(from: Option, tree: &'a RefCell) -> Self { Self { - themes: UnsafeCell::new(None), + themes: OnceCell::new(), ctx_from: from, tree, } } + pub(crate) fn new_with_data( + from: Option, + tree: &'a RefCell, + data: Vec>, + ) -> Self { + let themes: OnceCell>> = OnceCell::new(); + // Safety: we just create the `OnceCell` and it's empty. + unsafe { themes.set(data).unwrap_unchecked() }; + + Self { themes, ctx_from: from, tree } + } + pub(crate) fn find_cfg(&self, f: impl Fn(&Theme) -> Option<&T>) -> Option<&T> { for t in self.themes().iter().rev() { let v = f(t); @@ -77,12 +86,12 @@ impl<'a> BuildCtx<'a> { new_node(&mut self.tree.borrow_mut().arena, widget) } - pub(crate) fn append_child(&mut self, parent: WidgetId, child: WidgetId) { + pub(crate) fn append_child(&self, parent: WidgetId, child: WidgetId) { parent.append(child, &mut self.tree.borrow_mut().arena); } /// Insert `next` after `prev` - pub(crate) fn insert_after(&mut self, prev: WidgetId, next: WidgetId) { + pub(crate) fn insert_after(&self, prev: WidgetId, next: WidgetId) { prev.insert_after(next, &mut self.tree.borrow_mut().arena); } @@ -124,43 +133,23 @@ impl<'a> BuildCtx<'a> { .add_delay_event(DelayEvent::Disposed { id, parent }); } - pub(crate) fn mark_dirty(&mut self, id: WidgetId) { self.tree.borrow_mut().mark_dirty(id); } - - #[inline] - pub(crate) fn push_theme(&self, theme: Rc) { self.themes().push(theme); } - - #[inline] - pub(crate) fn pop_theme(&self) { self.themes().pop(); } - - /// todo: tmp code - /// because we use `BuildCtx` as reference now, but we need to use it as - /// mutable reference. Do a unsafe cast here, and remove it when we use - /// `BuildCtx` as mutable reference in `Widget` - #[allow(clippy::mut_from_ref)] - pub(crate) fn force_as_mut(&self) -> &mut Self { - #[allow(clippy::cast_ref_to_mut)] - unsafe { - &mut *(self as *const Self as *mut Self) - } - } + pub(crate) fn mark_dirty(&self, id: WidgetId) { self.tree.borrow_mut().mark_dirty(id); } - #[allow(clippy::mut_from_ref)] - fn themes(&self) -> &mut Vec> { - let this = self.force_as_mut(); - unsafe { &mut *this.themes.get() }.get_or_insert_with(|| { + pub(crate) fn themes(&self) -> &Vec> { + self.themes.get_or_init(|| { let mut themes = vec![]; let Some(p) = self.ctx_from else { return themes }; - let arena = &this.tree.borrow().arena; + let arena = &self.tree.borrow().arena; p.ancestors(arena).any(|p| { p.assert_get(arena).query_all_type( - |t: &Rc| { + |t: &Sc| { themes.push(t.clone()); matches!(t.deref(), Theme::Inherit(_)) }, QueryOrder::InnerFirst, ); - matches!(themes.last().map(Rc::deref), Some(Theme::Full(_))) + matches!(themes.last().map(Sc::deref), Some(Theme::Full(_))) }); themes }) @@ -191,7 +180,7 @@ mod tests { #[derive(Default, Clone)] struct LightDarkThemes(Rc>>); - let themes: Stateful>> = Stateful::new(vec![]); + let themes: Stateful>> = Stateful::new(vec![]); let c_themes = themes.clone_writer(); let light_palette = Palette { brightness: Brightness::Light, @@ -203,7 +192,7 @@ mod tests { }; let light_dark = fn_widget! { @ThemeWidget { - theme: Rc::new(Theme::Inherit(InheritTheme { + theme: Sc::new(Theme::Inherit(InheritTheme { palette: Some(Rc::new(light_palette)), ..<_>::default() @@ -211,7 +200,7 @@ mod tests { @MockBox { size: INFINITY_SIZE, @ThemeWidget { - theme: Rc::new(Theme::Inherit(InheritTheme { + theme: Sc::new(Theme::Inherit(InheritTheme { palette: Some(Rc::new(dark_palette)), ..<_>::default() })), diff --git a/core/src/lib.rs b/core/src/lib.rs index 12c93d958..48d127454 100644 --- a/core/src/lib.rs +++ b/core/src/lib.rs @@ -1,4 +1,4 @@ -#![feature(decl_macro)] +#![feature(decl_macro, once_cell)] #![cfg_attr(test, feature(mutex_unpoison, test))] #[macro_use] diff --git a/core/src/pipe.rs b/core/src/pipe.rs index 7268a76d9..4200f680d 100644 --- a/core/src/pipe.rs +++ b/core/src/pipe.rs @@ -120,7 +120,6 @@ impl + 'static> StrictBuilder for Pipe { let wnd = ctx.window(); AppCtx::spawn_local(async move { wnd.remove_regenerating_mark(id) }).unwrap(); - let ctx = ctx.force_as_mut(); if !ctx.window().is_in_another_regenerating(id) { let new_id = v.into().build(ctx); @@ -171,7 +170,7 @@ impl + 'static> WidgetBuilder for Pipe> { } impl SingleParent for Pipe { - fn append_child(self, child: WidgetId, ctx: &mut BuildCtx) -> WidgetId { + fn append_child(self, child: WidgetId, ctx: &BuildCtx) -> WidgetId { let (v, modifies) = self.unzip(); let p = v.append_child(child, ctx); let rg = half_to_close_interval(p..child, &ctx.tree.borrow()); @@ -184,7 +183,7 @@ impl SingleParent for Pipe { } impl MultiParent for Pipe { - fn append_children(self, children: Vec, ctx: &mut BuildCtx) -> WidgetId { + fn append_children(self, children: Vec, ctx: &BuildCtx) -> WidgetId { // if children is empty, we can let the pipe parent as the whole subtree. if children.is_empty() { self.strict_build(ctx) @@ -203,7 +202,7 @@ impl MultiParent for Pipe { } impl + 'static> SingleParent for Pipe> { - fn append_child(self, child: WidgetId, ctx: &mut BuildCtx) -> WidgetId { + fn append_child(self, child: WidgetId, ctx: &BuildCtx) -> WidgetId { self .map(|p| -> Box { if let Some(p) = p { @@ -225,8 +224,8 @@ fn update_pipe_parent( parent: Range, // transplant the children of the old parent to the new widget. modifies: BoxOp<'static, (ModifyScope, W), Infallible>, - ctx: &mut BuildCtx, - transplant: impl Fn(W, WidgetId, &mut BuildCtx) -> WidgetId + 'static, + ctx: &BuildCtx, + transplant: impl Fn(W, WidgetId, &BuildCtx) -> WidgetId + 'static, ) { let id_share = Sc::new(RefCell::new(parent.clone())); let id_share2 = id_share.clone(); @@ -243,7 +242,6 @@ fn update_pipe_parent( .sample(new_frame_sampler!(ctx)) .subscribe(move |(_, v)| { handle.with_ctx(|ctx| { - let ctx = ctx.force_as_mut(); let rg = id_share.borrow().clone(); let wnd = ctx.window(); @@ -253,7 +251,7 @@ fn update_pipe_parent( if !ctx.window().is_in_another_regenerating(rg.start) { let first_child = rg.end.first_child(&ctx.tree.borrow().arena).unwrap(); - let p = transplant(v, rg.end, ctx.force_as_mut()); + let p = transplant(v, rg.end, ctx); let new_rg = half_to_close_interval(p..first_child, &ctx.tree.borrow()); update_key_status_single(rg.start, new_rg.start, ctx); @@ -276,14 +274,14 @@ fn update_pipe_parent( } impl Pipe { - pub(crate) fn build_multi(self, vec: &mut Vec, ctx: &mut BuildCtx) + pub(crate) fn build_multi(self, vec: &mut Vec, ctx: &BuildCtx) where W: IntoIterator, W::Item: WidgetBuilder, { fn build_multi( v: impl IntoIterator, - ctx: &mut BuildCtx, + ctx: &BuildCtx, ) -> Vec { let mut ids = v.into_iter().map(|w| w.build(ctx)).collect::>(); @@ -320,7 +318,6 @@ impl Pipe { .box_it() .subscribe(move |(_, m)| { handle.with_ctx(|ctx| { - let ctx = ctx.force_as_mut(); let mut old = ids_share.borrow_mut(); let removed_subtree = old.clone(); diff --git a/core/src/widget.rs b/core/src/widget.rs index e8df779b1..c9e389280 100644 --- a/core/src/widget.rs +++ b/core/src/widget.rs @@ -1,6 +1,6 @@ pub(crate) use crate::widget_tree::*; use crate::{context::*, prelude::*}; -use ribir_algo::ShareResource; +use ribir_algo::{Sc, ShareResource}; use rxrust::subscription::{BoxSubscription, SubscriptionGuard}; use std::rc::Rc; @@ -372,6 +372,8 @@ impl_proxy_query!(paths [deref()], ShareResource, , where T: Render + 'st impl_proxy_render!(proxy deref(), ShareResource, , where T: Render + 'static); impl_proxy_query!(paths [deref()], Rc, , where W: Query + 'static); impl_proxy_render!(proxy deref(), Rc, , where W: Render + 'static); +impl_proxy_query!(paths [deref()], Sc, , where W: Query + 'static); +impl_proxy_render!(proxy deref(), Sc, , where W: Render + 'static); impl_query_self_only!(Vec>>); diff --git a/core/src/widget_children.rs b/core/src/widget_children.rs index 1335fa6ce..b99d00646 100644 --- a/core/src/widget_children.rs +++ b/core/src/widget_children.rs @@ -47,8 +47,8 @@ pub trait SingleChild {} /// A boxed render widget that support accept one child. pub trait BoxedSingleParent { - fn boxed_append_child(self: Box, child: WidgetId, ctx: &mut BuildCtx) -> WidgetId; - fn boxed_build(self: Box, ctx: &mut BuildCtx) -> WidgetId; + fn boxed_append_child(self: Box, child: WidgetId, ctx: &BuildCtx) -> WidgetId; + fn boxed_build(self: Box, ctx: &BuildCtx) -> WidgetId; } /// Trait to tell Ribir a object that has multi children. @@ -56,12 +56,8 @@ pub trait MultiChild {} /// A boxed render widget that support accept multi children. pub trait BoxedMultiParent { - fn boxed_append_children( - self: Box, - children: Vec, - ctx: &mut BuildCtx, - ) -> WidgetId; - fn boxed_build(self: Box, ctx: &mut BuildCtx) -> WidgetId; + fn boxed_append_children(self: Box, children: Vec, ctx: &BuildCtx) -> WidgetId; + fn boxed_build(self: Box, ctx: &BuildCtx) -> WidgetId; } /// Trait mark widget can have one child and also have compose logic for widget @@ -80,38 +76,38 @@ impl MultiChild for Box {} impl StrictBuilder for Box { #[inline] - fn strict_build(self, ctx: &BuildCtx) -> WidgetId { self.boxed_build(ctx.force_as_mut()) } + fn strict_build(self, ctx: &BuildCtx) -> WidgetId { self.boxed_build(ctx) } } impl SingleParent for Box { #[inline] - fn append_child(self, child: WidgetId, ctx: &mut BuildCtx) -> WidgetId { + fn append_child(self, child: WidgetId, ctx: &BuildCtx) -> WidgetId { self.boxed_append_child(child, ctx) } } impl StrictBuilder for Box { #[inline] - fn strict_build(self, ctx: &BuildCtx) -> WidgetId { self.boxed_build(ctx.force_as_mut()) } + fn strict_build(self, ctx: &BuildCtx) -> WidgetId { self.boxed_build(ctx) } } impl MultiParent for Box { #[inline] - fn append_children(self, children: Vec, ctx: &mut BuildCtx) -> WidgetId { + fn append_children(self, children: Vec, ctx: &BuildCtx) -> WidgetId { self.boxed_append_children(children, ctx) } } pub(crate) trait SingleParent { - fn append_child(self, child: WidgetId, ctx: &mut BuildCtx) -> WidgetId; + fn append_child(self, child: WidgetId, ctx: &BuildCtx) -> WidgetId; } pub(crate) trait MultiParent { - fn append_children(self, children: Vec, ctx: &mut BuildCtx) -> WidgetId; + fn append_children(self, children: Vec, ctx: &BuildCtx) -> WidgetId; } impl> + SingleChild> SingleParent for T { - fn append_child(self, child: WidgetId, ctx: &mut BuildCtx) -> WidgetId { + fn append_child(self, child: WidgetId, ctx: &BuildCtx) -> WidgetId { let p = ctx.alloc_widget(self.into()); ctx.append_child(p, child); p @@ -120,7 +116,7 @@ impl> + SingleChild> SingleParent for T { impl> + MultiChild> MultiParent for T { #[inline] - fn append_children(self, children: Vec, ctx: &mut BuildCtx) -> WidgetId { + fn append_children(self, children: Vec, ctx: &BuildCtx) -> WidgetId { let p = ctx.alloc_widget(self.into()); for c in children { ctx.append_child(p, c); @@ -131,26 +127,22 @@ impl> + MultiChild> MultiParent for T { impl BoxedSingleParent for W { #[inline] - fn boxed_append_child(self: Box, child: WidgetId, ctx: &mut BuildCtx) -> WidgetId { + fn boxed_append_child(self: Box, child: WidgetId, ctx: &BuildCtx) -> WidgetId { (*self).append_child(child, ctx) } #[inline] - fn boxed_build(self: Box, ctx: &mut BuildCtx) -> WidgetId { (*self).build(ctx) } + fn boxed_build(self: Box, ctx: &BuildCtx) -> WidgetId { (*self).build(ctx) } } impl BoxedMultiParent for W { #[inline] - fn boxed_append_children( - self: Box, - children: Vec, - ctx: &mut BuildCtx, - ) -> WidgetId { + fn boxed_append_children(self: Box, children: Vec, ctx: &BuildCtx) -> WidgetId { (*self).append_children(children, ctx) } #[inline] - fn boxed_build(self: Box, ctx: &mut BuildCtx) -> WidgetId { (*self).build(ctx) } + fn boxed_build(self: Box, ctx: &BuildCtx) -> WidgetId { (*self).build(ctx) } } #[cfg(test)] diff --git a/core/src/widget_children/multi_child_impl.rs b/core/src/widget_children/multi_child_impl.rs index 11ba2db1d..1a0e9dde7 100644 --- a/core/src/widget_children/multi_child_impl.rs +++ b/core/src/widget_children/multi_child_impl.rs @@ -42,9 +42,7 @@ where D: IntoIterator + 'static, D::Item: WidgetBuilder, { - fn fill_vec(self, vec: &mut Vec, ctx: &BuildCtx) { - self.build_multi(vec, ctx.force_as_mut()); - } + fn fill_vec(self, vec: &mut Vec, ctx: &BuildCtx) { self.build_multi(vec, ctx); } } impl MultiWithChild for P @@ -76,6 +74,6 @@ where impl StrictBuilder for MultiPair

{ fn strict_build(self, ctx: &BuildCtx) -> WidgetId { let MultiPair { parent, children } = self; - parent.append_children(children, ctx.force_as_mut()) + parent.append_children(children, ctx) } } diff --git a/core/src/widget_children/single_child_impl.rs b/core/src/widget_children/single_child_impl.rs index 7e32683ea..9dc7cb2f0 100644 --- a/core/src/widget_children/single_child_impl.rs +++ b/core/src/widget_children/single_child_impl.rs @@ -55,7 +55,7 @@ where fn strict_build(self, ctx: &BuildCtx) -> WidgetId { let Self { widget, child } = self; let child = child.build(ctx); - widget.append_child(child, ctx.force_as_mut()) + widget.append_child(child, ctx) } } diff --git a/core/src/widget_tree.rs b/core/src/widget_tree.rs index b96bb09aa..61d8d8d2a 100644 --- a/core/src/widget_tree.rs +++ b/core/src/widget_tree.rs @@ -531,8 +531,8 @@ mod tests { @ { (0..100).map(|_| @MockBox { - size: Size::new(150., 50.), - background: Color::BLUE, + size: Size::new(150., 50.), + background: Color::BLUE, }) } }}; diff --git a/examples/messages/src/messages.rs b/examples/messages/src/messages.rs index 58b11aef7..58450ede3 100644 --- a/examples/messages/src/messages.rs +++ b/examples/messages/src/messages.rs @@ -43,10 +43,8 @@ pub fn messages() -> impl Into { impl Compose for MessageList { fn compose(this: State) -> Widget { fn_widget! { - let palette = Palette::of(ctx!()); - @Column { - background: palette.surface(), + background: Palette::of(ctx!()).surface(), @Row { justify_content: JustifyContent::SpaceBetween, padding: EdgeInsets::new(8., 16., 8., 16.), @@ -56,7 +54,7 @@ impl Compose for MessageList { @TinyIcon { @{ svgs::MENU } } @Text { text: "Message", - foreground: palette.on_surface(), + foreground: Palette::of(ctx!()).on_surface(), text_style: TypographyTheme::of(ctx!()).title_large.text.clone(), } } @@ -77,7 +75,7 @@ impl Compose for MessageList { @VScrollBar { @Lists { @{ - let message_gen = |message: Message| { + let message_gen = move |message: Message| { @Column { @ListItem { line_number: 1usize, diff --git a/macros/src/declare_derive2.rs b/macros/src/declare_derive2.rs index b27f67edb..fc40f7c23 100644 --- a/macros/src/declare_derive2.rs +++ b/macros/src/declare_derive2.rs @@ -264,8 +264,7 @@ fn struct_with_fields_gen( type Target = State<#name #g_ty>; #[inline] - fn build_declare(mut self, ctx: &BuildCtx) -> Self::Target { - set_build_ctx!(ctx); + fn build_declare(mut self, ctx!(): &BuildCtx) -> Self::Target { #(#fill_default)* #(#unzip_fields)* diff --git a/macros/src/fn_widget_macro.rs b/macros/src/fn_widget_macro.rs index 6a41786ab..d14a7f62e 100644 --- a/macros/src/fn_widget_macro.rs +++ b/macros/src/fn_widget_macro.rs @@ -23,8 +23,7 @@ impl ToTokens for FnWidgetMacro { fn to_tokens(&self, tokens: &mut proc_macro2::TokenStream) { let Self(stmts) = self; quote! { - FnWidget::new(move |ctx: &BuildCtx| { - set_build_ctx!(ctx); + FnWidget::new(move |ctx!(): &BuildCtx| { #[allow(unused_mut)] { #(#stmts)* } }) diff --git a/widgets/src/avatar.rs b/widgets/src/avatar.rs index e5c2266a0..0b44bef7f 100644 --- a/widgets/src/avatar.rs +++ b/widgets/src/avatar.rs @@ -24,7 +24,7 @@ use ribir_core::prelude::*; /// ``` #[derive(Declare2, Default, Clone)] pub struct Avatar { - #[declare(default=Palette::of(ctx).primary())] + #[declare(default=Palette::of(ctx!()).primary())] pub color: Color, } diff --git a/widgets/src/buttons/button.rs b/widgets/src/buttons/button.rs index 35d10ed47..d3c0d46b9 100644 --- a/widgets/src/buttons/button.rs +++ b/widgets/src/buttons/button.rs @@ -64,7 +64,7 @@ impl ComposeDecorator for ButtonDecorator { /// ``` #[derive(Default, Declare2)] pub struct Button { - #[declare(default=Palette::of(ctx).primary())] + #[declare(default=Palette::of(ctx!()).primary())] color: Color, } @@ -89,8 +89,8 @@ impl ComposeChild for Button { icon_pos, label_style, padding_style, - } = ButtonStyle::of(ctx); - let palette = Palette::of(ctx).clone(); + } = ButtonStyle::of(ctx!()); + let palette = Palette::of(ctx!()).clone(); @ButtonDecorator { button_type, diff --git a/widgets/src/buttons/fab_button.rs b/widgets/src/buttons/fab_button.rs index f8f789a87..074d7ddec 100644 --- a/widgets/src/buttons/fab_button.rs +++ b/widgets/src/buttons/fab_button.rs @@ -75,7 +75,7 @@ impl ComposeDecorator for FabButtonDecorator { /// ``` #[derive(Default, Declare2)] pub struct FabButton { - #[declare(default=Palette::of(ctx).primary())] + #[declare(default=Palette::of(ctx!()).primary())] color: Color, } @@ -101,9 +101,9 @@ impl ComposeChild for FabButton { label_style, radius, padding_style, - } = FabButtonStyle::of(ctx); - let palette1 = Palette::of(ctx).clone(); - let palette2 = Palette::of(ctx).clone(); + } = FabButtonStyle::of(ctx!()); + let palette1 = Palette::of(ctx!()).clone(); + let palette2 = Palette::of(ctx!()).clone(); @FabButtonDecorator { button_type, diff --git a/widgets/src/buttons/filled_button.rs b/widgets/src/buttons/filled_button.rs index 83e42c4bd..46fc39516 100644 --- a/widgets/src/buttons/filled_button.rs +++ b/widgets/src/buttons/filled_button.rs @@ -75,7 +75,7 @@ impl ComposeDecorator for FilledButtonDecorator { /// ``` #[derive(Declare2, Default)] pub struct FilledButton { - #[declare(default=Palette::of(ctx).primary())] + #[declare(default=Palette::of(ctx!()).primary())] color: Color, } @@ -101,9 +101,7 @@ impl ComposeChild for FilledButton { label_style, radius, padding_style, - } = FilledButtonStyle::of(ctx); - let palette1 = Palette::of(ctx).clone(); - let palette2 = Palette::of(ctx).clone(); + } = FilledButtonStyle::of(ctx!()); @FilledButtonDecorator { button_type, @@ -114,8 +112,11 @@ impl ComposeChild for FilledButton { label_gap, icon_pos, label_style, - background_color: pipe!(Brush::from(palette1.base_of(&$this.color))), - foreground_color: pipe!(Brush::from(palette2.on_of(&palette2.base_of(&$this.color)))), + background_color: pipe!(Palette::of(ctx!()).base_of(&$this.color)), + foreground_color: pipe! { + let palette = Palette::of(ctx!()); + palette.on_of(&palette.base_of(&$this.color)) + }, radius, border_style: None, padding_style, diff --git a/widgets/src/buttons/outlined_button.rs b/widgets/src/buttons/outlined_button.rs index c55e39f5a..74e80e4d3 100644 --- a/widgets/src/buttons/outlined_button.rs +++ b/widgets/src/buttons/outlined_button.rs @@ -77,7 +77,7 @@ impl ComposeDecorator for OutlinedButtonDecorator { /// ``` #[derive(Default, Declare2)] pub struct OutlinedButton { - #[declare(default=Palette::of(ctx).primary())] + #[declare(default=Palette::of(ctx!()).primary())] color: Color, } @@ -104,9 +104,7 @@ impl ComposeChild for OutlinedButton { radius, padding_style, border_width, - } = OutlinedButtonStyle::of(ctx); - let palette1 = Palette::of(ctx).clone(); - let palette2 = Palette::of(ctx).clone(); + } = OutlinedButtonStyle::of(ctx!()); @OutlinedButtonDecorator { button_type, @@ -118,11 +116,11 @@ impl ComposeChild for OutlinedButton { icon_pos, label_style, background_color: None, - foreground_color: pipe!(Brush::from(palette1.base_of(&$this.color))), + foreground_color: pipe!(Palette::of(ctx!()).base_of(&$this.color)), radius, border_style: pipe!(Border::all(BorderSide { width: border_width, - color: Brush::from(palette2.base_of(&$this.color)) + color: Palette::of(ctx!()).base_of(&$this.color).into() })), padding_style, diff --git a/widgets/src/checkbox.rs b/widgets/src/checkbox.rs index 0af1adea9..06f9a789c 100644 --- a/widgets/src/checkbox.rs +++ b/widgets/src/checkbox.rs @@ -11,7 +11,7 @@ pub struct Checkbox { pub checked: bool, #[declare(default)] pub indeterminate: bool, - #[declare(default=Palette::of(ctx).primary())] + #[declare(default=Palette::of(ctx!()).primary())] pub color: Color, } @@ -27,7 +27,7 @@ pub struct CheckBoxStyle { #[derive(Clone, Declare2)] pub struct CheckBoxDecorator { - #[declare(default=Palette::of(ctx).primary())] + #[declare(default=Palette::of(ctx!()).primary())] pub color: Color, } diff --git a/widgets/src/divider.rs b/widgets/src/divider.rs index 525da2fc1..e17a20920 100644 --- a/widgets/src/divider.rs +++ b/widgets/src/divider.rs @@ -42,7 +42,7 @@ pub struct Divider { // Extent of divider pub extent: f32, // Color of divider - #[declare(default=Palette::of(ctx).outline_variant())] + #[declare(default=Palette::of(ctx!()).outline_variant())] pub color: Brush, // Direction of divider #[declare(default=Direction::Horizontal)] diff --git a/widgets/src/icon.rs b/widgets/src/icon.rs index 4d6334dd7..c6a83059c 100644 --- a/widgets/src/icon.rs +++ b/widgets/src/icon.rs @@ -7,7 +7,7 @@ use ribir_core::prelude::*; /// its child. So you can declare any widget as its child to display as a icon. #[derive(Declare2, Default, Clone, Copy)] pub struct Icon { - #[declare(default = IconSize::of(ctx).small)] + #[declare(default = IconSize::of(ctx!()).small)] pub size: Size, } @@ -38,7 +38,7 @@ macro_rules! define_fixed_size_icon { type Child = Widget; fn compose_child(_: State, child: Self::Child) -> Widget { fn_widget! { - let icon = @Icon { size: IconSize::of(ctx).$field }; + let icon = @Icon { size: IconSize::of(ctx!()).$field }; @ $icon { @ { child } } } .into() diff --git a/widgets/src/input.rs b/widgets/src/input.rs index fd3134de4..d25c9a64e 100644 --- a/widgets/src/input.rs +++ b/widgets/src/input.rs @@ -35,19 +35,19 @@ impl CustomStyle for InputStyle { #[derive(Declare2)] pub struct Input { - #[declare(default = TypographyTheme::of(ctx).body_large.text.clone())] + #[declare(default = TypographyTheme::of(ctx!()).body_large.text.clone())] pub style: CowArc, #[declare(skip)] text: CowArc, #[declare(skip)] caret: CaretState, - #[declare(default = InputStyle::of(ctx).size)] + #[declare(default = InputStyle::of(ctx!()).size)] size: Option, } #[derive(Declare2)] pub struct TextArea { - #[declare(default = TypographyTheme::of(ctx).body_large.text.clone())] + #[declare(default = TypographyTheme::of(ctx!()).body_large.text.clone())] pub style: CowArc, #[declare(default = true)] pub auto_wrap: bool, @@ -55,9 +55,9 @@ pub struct TextArea { text: CowArc, #[declare(skip)] caret: CaretState, - #[declare(default = TextAreaStyle::of(ctx).rows)] + #[declare(default = TextAreaStyle::of(ctx!()).rows)] rows: Option, - #[declare(default = TextAreaStyle::of(ctx).cols)] + #[declare(default = TextAreaStyle::of(ctx!()).cols)] cols: Option, } diff --git a/widgets/src/input/editarea.rs b/widgets/src/input/editarea.rs index 22ac91114..b2fc591c4 100644 --- a/widgets/src/input/editarea.rs +++ b/widgets/src/input/editarea.rs @@ -64,7 +64,7 @@ impl ComposeChild for TextEditorArea { } }); - let tick_of_layout_ready = ctx.window() + let tick_of_layout_ready = ctx!().window() .frame_tick_stream() .filter(|msg| matches!(msg, FrameMsg::LayoutReady(_))); diff --git a/widgets/src/lists.rs b/widgets/src/lists.rs index 70fba1049..2d5883120 100644 --- a/widgets/src/lists.rs +++ b/widgets/src/lists.rs @@ -270,7 +270,6 @@ impl ComposeChild for ListItem { } = child; fn_widget! { - let palette = Palette::of(ctx!()); let ListItemStyle { padding_style, label_gap, @@ -279,7 +278,7 @@ impl ComposeChild for ListItem { leading_config, trailing_config, item_align, - } = ListItemStyle::of(ctx); + } = ListItemStyle::of(ctx!()); let padding = padding_style.map(|padding| Padding { padding }); let label_gap = label_gap.map(|padding| Padding { padding }); @@ -291,7 +290,7 @@ impl ComposeChild for ListItem { @Row { align_items: pipe!(item_align($this.line_number)), @{ - leading.map(|w| { + leading.map(move |w| { let (leading, widget) = w.unzip(); let (_, builtin) = leading.unzip(); builtin.compose_with_host(widget.compose_with_style(leading_config), ctx!()) @@ -303,7 +302,7 @@ impl ComposeChild for ListItem { @Column { @Text { text: pipe!($headline.0.0.clone()), - foreground: palette.on_surface(), + foreground: Palette::of(ctx!()).on_surface(), text_style: headline_style, } @{ supporting.map(|mut supporting| { @@ -318,7 +317,7 @@ impl ComposeChild for ListItem { } , @Text { text: pipe!($supporting.0.0.clone()), - foreground: palette.on_surface_variant(), + foreground: Palette::of(ctx!()).on_surface_variant(), text_style: supporting_style, } } @@ -343,7 +342,7 @@ impl ComposeChild for ListItem { pub struct ListItem { #[declare(default = 1usize)] pub line_number: usize, - #[declare(default = Palette::of(ctx).primary())] + #[declare(default = Palette::of(ctx!()).primary())] pub active_background: Color, } diff --git a/widgets/src/scrollbar.rs b/widgets/src/scrollbar.rs index 504a1d2f9..684ed1dcc 100644 --- a/widgets/src/scrollbar.rs +++ b/widgets/src/scrollbar.rs @@ -189,7 +189,7 @@ impl Compose for HRawScrollbar { thickness, thumb_min_size, track_brush, - } = ScrollBarStyle::of(ctx); + } = ScrollBarStyle::of(ctx!()); let mut track_box = @Container { size: Size::new(f32::MAX, 0.), @@ -251,7 +251,7 @@ impl Compose for VRawScrollbar { thickness, thumb_min_size, ref track_brush - } = ScrollBarStyle::of(ctx); + } = ScrollBarStyle::of(ctx!()); let mut track_box = @Container { size: Size::new(0., f32::MAX), diff --git a/widgets/src/text.rs b/widgets/src/text.rs index e7a65e624..735cbceb9 100644 --- a/widgets/src/text.rs +++ b/widgets/src/text.rs @@ -10,9 +10,9 @@ use ribir_core::{ #[derive(Debug, Declare2, Clone, PartialEq)] pub struct Text { pub text: CowArc, - #[declare(default = Palette::of(ctx).on_surface_variant())] + #[declare(default = Palette::of(ctx!()).on_surface_variant())] pub foreground: Brush, - #[declare(default = TypographyTheme::of(ctx).body_medium.text.clone())] + #[declare(default = TypographyTheme::of(ctx!()).body_medium.text.clone())] pub text_style: CowArc, #[declare(default)] pub path_style: PathPaintStyle, @@ -132,7 +132,7 @@ macro_rules! define_text_with_theme_style { #[derive(Declare2)] pub struct $name { pub text: CowArc, - #[declare(default = Palette::of(ctx).on_surface_variant())] + #[declare(default = Palette::of(ctx!()).on_surface_variant())] pub foreground: Brush, #[declare(default)] pub overflow: Overflow, @@ -144,7 +144,7 @@ macro_rules! define_text_with_theme_style { @Text { text: pipe!($this.text.clone()), foreground: pipe!($this.foreground.clone()), - text_style: TypographyTheme::of(ctx).$style.text.clone(), + text_style: TypographyTheme::of(ctx!()).$style.text.clone(), overflow: pipe!($this.overflow), } }