Skip to content

Commit

Permalink
refactor(core): 💡 KeyWidget and Expanded are not declared as `Fat…
Browse files Browse the repository at this point in the history
…Obj`
  • Loading branch information
M-Adoo committed Nov 3, 2024
1 parent b267756 commit 634a9f3
Show file tree
Hide file tree
Showing 8 changed files with 47 additions and 49 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ Please only add new entries below the [Unreleased](#unreleased---releasedate) he
- **core**: The `Provider` might be missing in a pipe class. (#648 @M-Adoo)
- **core**: The child generated by the class may not be mounted. (#648 @M-Adoo)

### Breaking

- **core**: `Expanded` and `KeyWidget` are not declared with `FatObj`, so they do not currently support built-in widgets. (#648 @M-Adoo)

## [0.4.0-alpha.14] - 2024-10-30

### Features
Expand Down
18 changes: 12 additions & 6 deletions core/src/builtin_widgets/key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,15 +69,21 @@ impl<V: Default> Default for KeyChange<V> {
/// // generate `KeyWidget`, because the root of generated widget is `Margin`.
/// fn_widget!{
/// @ {
/// pipe!($trigger;).map(move |_| @KeyWidget {
/// pipe!($trigger;).map(move |_|
/// @Margin {
/// margin: EdgeInsets::all(10.),
/// key: "key",
/// value: (),
/// @Void {}
/// })
/// @KeyWidget {
/// key: "key",
/// value: (),
/// @Void {}
/// }})
/// }
/// };
#[derive(Declare)]
// `KeyWidget` should not support `FatObj`, as this may cause the `KeyWidget` to be invisible to its
// parent. `@KeyWidget { margin: EdgeInsets::all(10.) }` actually expands as `@Margin { @KeyWidget {
// .. } }`.
#[simple_declare]

pub struct KeyWidget<V: 'static> {
pub key: Key,
#[declare(strict)]
Expand Down
8 changes: 0 additions & 8 deletions core/src/builtin_widgets/provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@ use crate::prelude::*;
/// ```
#[simple_declare]
pub struct Provider {
#[declare(custom)]
pub provider: Box<dyn Query>,
}

Expand Down Expand Up @@ -107,13 +106,6 @@ impl Provider {
}
}

impl ProviderDeclarer {
pub fn provider(mut self, p: impl Query) -> Self {
self.provider = Some(Box::new(p));
self
}
}

impl<'c> ComposeChild<'c> for Provider {
type Child = FnWidget<'c>;
fn compose_child(this: impl StateWriter<Value = Self>, child: Self::Child) -> Widget<'c> {
Expand Down
2 changes: 1 addition & 1 deletion core/src/widget.rs
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ impl<'w> Widget<'w> {
/// Attach a state to a widget and try to unwrap it before attaching.
///
/// User can query the state or its value type.
pub(crate) fn try_unwrap_state_and_attach<D: Any>(
pub fn try_unwrap_state_and_attach<D: Any>(
self, data: impl StateWriter<Value = D> + 'static,
) -> Self {
let data: Box<dyn Query> = match data.try_into_value() {
Expand Down
6 changes: 1 addition & 5 deletions macros/src/declare_derive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -553,11 +553,7 @@ fn declarer_set_methods<'a>(
.filter(|f| f.need_set_method())
.map(move |f| {
let field_name = f.field.ident.as_ref().unwrap();
let doc = f
.field
.attrs
.iter()
.find(|attr| matches!(&attr.meta, syn::Meta::NameValue(nv) if nv.path.is_ident("doc")));
let doc = f.doc_attr();
let ty = &f.field.ty;
let set_method = f.set_method_name();
if f
Expand Down
15 changes: 13 additions & 2 deletions macros/src/simple_declare_attr.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use proc_macro2::TokenStream;
use quote::{ToTokens, quote, quote_spanned};
use syn::{
Fields, Ident, Result, Visibility,
Attribute, Fields, Ident, Result, Visibility,
parse::{Parse, discouraged::Speculative},
spanned::Spanned,
};
Expand Down Expand Up @@ -191,6 +191,14 @@ impl<'a> DeclareField<'a> {
.as_ref()
.map_or(true, |attr| attr.custom.is_none() && attr.skip.is_none())
}

pub fn doc_attr(&self) -> Option<&Attribute> {
self
.field
.attrs
.iter()
.find(|attr| matches!(&attr.meta, syn::Meta::NameValue(nv) if nv.path.is_ident("doc")))
}
}

impl Parse for DeclareAttr {
Expand Down Expand Up @@ -253,19 +261,21 @@ impl Parse for DefaultMeta {
}
}

fn declarer_set_methods<'a>(
pub fn declarer_set_methods<'a>(
fields: &'a [DeclareField], vis: &'a Visibility,
) -> impl Iterator<Item = TokenStream> + 'a {
fields
.iter()
.filter(|f| f.need_set_method())
.map(move |f| {
let field_name = f.field.ident.as_ref().unwrap();
let doc_attr = f.doc_attr();
let ty = &f.field.ty;
let set_method = f.set_method_name();
if f.is_strict() {
quote! {
#[inline]
#doc_attr
#vis fn #set_method(mut self, v: #ty) -> Self {
self.#field_name = Some(v);
self
Expand All @@ -274,6 +284,7 @@ fn declarer_set_methods<'a>(
} else {
quote! {
#[inline]
#doc_attr
#vis fn #set_method(mut self, v: impl Into<#ty>) -> Self
{
self.#field_name = Some(v.into());
Expand Down
22 changes: 6 additions & 16 deletions widgets/src/layout/expanded.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@ use ribir_core::prelude::*;
/// A widget that expanded a child of `Flex`, so that the child fills the
/// available space. If multiple children are expanded, the available space is
/// divided among them according to the flex factor.
#[derive(Clone, PartialEq, Declare)]
#[derive(Clone, PartialEq)]
// `Expand` should not support `FatObj`, as this may cause the `Expanded` to be invisible to its
// parent. `@Expanded { margin: EdgeInsets::all(10.) }` actually expands as `@Margin { @Expanded {
// .. } }`.
#[simple_declare]
pub struct Expanded {
#[declare(default = 1.)]
pub flex: f32,
Expand All @@ -13,21 +17,7 @@ impl<'c> ComposeChild<'c> for Expanded {
type Child = Widget<'c>;
#[inline]
fn compose_child(this: impl StateWriter<Value = Self>, child: Self::Child) -> Widget<'c> {
let data: Box<dyn Query> = match this.try_into_value() {
Ok(data) => Box::new(Queryable(data)),
Err(data) => Box::new(data),
};
Provider::new(data)
.with_child(fn_widget! {
@ConstrainedBox {
clamp: BoxClamp {
min: Size::new(0., 0.),
max: Size::new(f32::INFINITY, f32::INFINITY)
},
@{ child }
}
})
.into_widget()
child.try_unwrap_state_and_attach(this)
}
}

Expand Down
21 changes: 10 additions & 11 deletions widgets/src/tabs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,27 +184,26 @@ impl Tabs {
}
});
@ {
let mut tab_header = @Expanded {
let mut tab_header = @Flex {
align_items: Align::Center,
justify_content: JustifyContent::Center,
direction: match icon_pos {
Position::Left | Position::Right => Direction::Horizontal,
Position::Top | Position::Bottom => Direction::Vertical,
},
reverse: matches!(icon_pos, Position::Right | Position::Bottom),
on_tap: move |_| if $tabs.cur_idx != idx {
$tabs.write().cur_idx = idx;
},
};

let u = watch!(($tabs.cur_idx == idx, $tab_header.layout_rect()))
.filter_map(|(active, rect)| active.then_some(rect))
.subscribe(move |v| $indicator.write().rect = v);

@TabDecorator {
on_disposed: move |_| { u.unsubscribe(); },
@$tab_header {
@Flex {
align_items: Align::Center,
justify_content: JustifyContent::Center,
direction: match icon_pos {
Position::Left | Position::Right => Direction::Horizontal,
Position::Top | Position::Bottom => Direction::Vertical,
},
reverse: matches!(icon_pos, Position::Right | Position::Bottom),
@Expanded {
@$tab_header {
@ { icon_widget }
@ { label_widget }
}
Expand Down

0 comments on commit 634a9f3

Please sign in to comment.