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): 💡 merge all boolean status of widget into MixFlags #627

Merged
merged 1 commit into from
Sep 20, 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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ Please only add new entries below the [Unreleased](#unreleased---releasedate) he

### Changed

- **core**: Merged boolean status widgets into a single widget `MixFlags`, including `HasFocus`, `MouseHover` and `PointerPressed`. (#627 @M-Adoo)
- **core**: Reimplemented `HAlignWidget`, `VAlignWidget`, `RelativeAnchor`, `BoxDecoration`, `ConstrainedBox`, `IgnorePoint`, `Opacity`, `Padding`, `TransformWidget`, and `VisibilityRender` as `WrapRender`. (#626 @M-Adoo)


Expand Down
121 changes: 51 additions & 70 deletions core/src/builtin_widgets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,8 @@ pub mod fitted_box;
pub use fitted_box::*;
pub mod svg;

pub mod has_focus;
pub use has_focus::*;
pub mod mouse_hover;
pub use mouse_hover::*;
pub mod clip;
pub use clip::*;
pub mod pointer_pressed;
pub use pointer_pressed::*;
pub mod focus_node;
pub use focus_node::*;
pub mod focus_scope;
Expand Down Expand Up @@ -108,11 +102,8 @@ pub struct FatObj<T> {
host: T,
host_id: LazyWidgetId,
id: LazyWidgetId,
mix_builtin: Option<State<MixBuiltin>>,
mix_builtin: Option<MixBuiltin>,
request_focus: Option<State<RequestFocus>>,
has_focus: Option<State<HasFocus>>,
mouse_hover: Option<State<MouseHover>>,
pointer_pressed: Option<State<PointerPressed>>,
fitted_box: Option<State<FittedBox>>,
box_decoration: Option<State<BoxDecoration>>,
padding: Option<State<Padding>>,
Expand Down Expand Up @@ -176,9 +167,6 @@ impl<T> FatObj<T> {
id: self.id,
mix_builtin: self.mix_builtin,
request_focus: self.request_focus,
has_focus: self.has_focus,
mouse_hover: self.mouse_hover,
pointer_pressed: self.pointer_pressed,
fitted_box: self.fitted_box,
box_decoration: self.box_decoration,
padding: self.padding,
Expand Down Expand Up @@ -206,9 +194,6 @@ impl<T> FatObj<T> {
&& self.id.ref_count() == 1
&& self.mix_builtin.is_none()
&& self.request_focus.is_none()
&& self.has_focus.is_none()
&& self.mouse_hover.is_none()
&& self.pointer_pressed.is_none()
&& self.fitted_box.is_none()
&& self.box_decoration.is_none()
&& self.padding.is_none()
Expand Down Expand Up @@ -249,171 +234,173 @@ impl<T> FatObj<T> {
impl<T> FatObj<T> {
/// Returns the `State<Class>` widget from the FatObj. If it doesn't exist, a
/// new one is created.
pub fn get_class_widget(&mut self) -> &mut State<Class> {
pub fn get_class_widget(&mut self) -> &State<Class> {
self
.class
.get_or_insert_with(|| State::value(<_>::default()))
}

pub fn get_mix_builtin_widget(&mut self) -> &mut State<MixBuiltin> {
pub fn get_mix_builtin_widget(&mut self) -> &MixBuiltin {
self
.mix_builtin
.get_or_insert_with(|| State::value(<_>::default()))
.get_or_insert_with(MixBuiltin::default)
}

/// Returns the `State<RequestFocus>` widget from the FatObj. If it doesn't
/// exist, a new one is created.
pub fn get_request_focus_widget(&mut self) -> &mut State<RequestFocus> {
pub fn get_request_focus_widget(&mut self) -> &State<RequestFocus> {
self
.request_focus
.get_or_insert_with(|| State::value(<_>::default()))
}

/// Returns the `State<HasFocus>` widget from the FatObj. If it doesn't exist,
/// a new one is created.
pub fn get_has_focus_widget(&mut self) -> &mut State<HasFocus> {
/// Return the `State<MixFlags>` from the `MixBuiltin`. If the `MixBuiltin`
/// does not exist in the `FatObj`, a new one is created.
pub fn get_mix_flags_widget(&mut self) -> &State<MixFlags> {
self.get_mix_builtin_widget().mix_flags()
}

/// Begin tracing the focus status of this widget.
pub fn trace_focus(&mut self) -> &mut Self {
self.get_mix_builtin_widget().trace_focus();
self
.has_focus
.get_or_insert_with(|| State::value(<_>::default()))
}

/// Returns the `State<MouseHover>` widget from the FatObj. If it doesn't
/// exist, a new one is created.
pub fn get_mouse_hover_widget(&mut self) -> &mut State<MouseHover> {
/// Begin tracing the hover status of this widget.
pub fn trace_hover(&mut self) -> &mut Self {
self.get_mix_builtin_widget().trace_hover();
self
.mouse_hover
.get_or_insert_with(|| State::value(<_>::default()))
}

/// Returns the `State<PointerPressed>` widget from the FatObj. If it doesn't
/// exist, a new one is created.
pub fn get_pointer_pressed_widget(&mut self) -> &mut State<PointerPressed> {
/// Begin tracing if the pointer pressed on this widget
pub fn trace_pointer_pressed(&mut self) -> &mut Self {
self
.get_mix_builtin_widget()
.trace_pointer_pressed();
self
.pointer_pressed
.get_or_insert_with(|| State::value(<_>::default()))
}

/// Returns the `State<FittedBox>` widget from the FatObj. If it doesn't
/// exist, a new one is created.
pub fn get_fitted_box_widget(&mut self) -> &mut State<FittedBox> {
pub fn get_fitted_box_widget(&mut self) -> &State<FittedBox> {
self
.fitted_box
.get_or_insert_with(|| State::value(<_>::default()))
}

/// Returns the `State<BoxDecoration>` widget from the FatObj. If it doesn't
/// exist, a new one is created.
pub fn get_box_decoration_widget(&mut self) -> &mut State<BoxDecoration> {
pub fn get_box_decoration_widget(&mut self) -> &State<BoxDecoration> {
self
.box_decoration
.get_or_insert_with(|| State::value(<_>::default()))
}

/// Returns the `State<Padding>` widget from the FatObj. If it doesn't exist,
/// a new one is created.
pub fn get_padding_widget(&mut self) -> &mut State<Padding> {
pub fn get_padding_widget(&mut self) -> &State<Padding> {
self
.padding
.get_or_insert_with(|| State::value(<_>::default()))
}

/// Returns the `State<LayoutBox>` widget from the FatObj. If it doesn't
/// exist, a new one is created.
pub fn get_layout_box_widget(&mut self) -> &mut State<LayoutBox> {
pub fn get_layout_box_widget(&mut self) -> &State<LayoutBox> {
self
.layout_box
.get_or_insert_with(|| State::value(<_>::default()))
}

/// Returns the `State<Cursor>` widget from the FatObj. If it doesn't exist, a
/// new one is created.
pub fn get_cursor_widget(&mut self) -> &mut State<Cursor> {
pub fn get_cursor_widget(&mut self) -> &State<Cursor> {
self
.cursor
.get_or_insert_with(|| State::value(<_>::default()))
}

/// Returns the `State<Margin>` widget from the FatObj. If it doesn't exist, a
/// new one is created.
pub fn get_margin_widget(&mut self) -> &mut State<Margin> {
pub fn get_margin_widget(&mut self) -> &State<Margin> {
self
.margin
.get_or_insert_with(|| State::value(<_>::default()))
}

pub fn get_constrained_box_widget(&mut self) -> &mut State<ConstrainedBox> {
pub fn get_constrained_box_widget(&mut self) -> &State<ConstrainedBox> {
self
.constrained_box
.get_or_insert_with(|| State::value(<_>::default()))
}

/// Returns the `State<ScrollableWidget>` widget from the FatObj. If it
/// doesn't exist, a new one is created.
pub fn get_scrollable_widget(&mut self) -> &mut State<ScrollableWidget> {
pub fn get_scrollable_widget(&mut self) -> &State<ScrollableWidget> {
self
.scrollable
.get_or_insert_with(|| State::value(<_>::default()))
}

/// Returns the `State<TransformWidget>` widget from the FatObj. If it doesn't
/// exist, a new one is created.
pub fn get_transform_widget(&mut self) -> &mut State<TransformWidget> {
pub fn get_transform_widget(&mut self) -> &State<TransformWidget> {
self
.transform
.get_or_insert_with(|| State::value(<_>::default()))
}

/// Returns the `State<HAlignWidget>` widget from the FatObj. If it doesn't
/// exist, a new one is created.
pub fn get_h_align_widget(&mut self) -> &mut State<HAlignWidget> {
pub fn get_h_align_widget(&mut self) -> &State<HAlignWidget> {
self
.h_align
.get_or_insert_with(|| State::value(<_>::default()))
}

/// Returns the `State<VAlignWidget>` widget from the FatObj. If it doesn't
/// exist, a new one is created.
pub fn get_v_align_widget(&mut self) -> &mut State<VAlignWidget> {
pub fn get_v_align_widget(&mut self) -> &State<VAlignWidget> {
self
.v_align
.get_or_insert_with(|| State::value(<_>::default()))
}

/// Returns the `State<RelativeAnchor>` widget from the FatObj. If it doesn't
/// exist, a new one is created.
pub fn get_relative_anchor_widget(&mut self) -> &mut State<RelativeAnchor> {
pub fn get_relative_anchor_widget(&mut self) -> &State<RelativeAnchor> {
self
.relative_anchor
.get_or_insert_with(|| State::value(<_>::default()))
}

/// Returns the `State<GlobalAnchor>` widget from the FatObj. If it doesn't
/// exist, a new one is created.
pub fn get_global_anchor_widget(&mut self) -> &mut State<GlobalAnchor> {
pub fn get_global_anchor_widget(&mut self) -> &State<GlobalAnchor> {
self
.global_anchor
.get_or_insert_with(|| State::value(<_>::default()))
}

/// Returns the `State<Visibility>` widget from the FatObj. If it doesn't
/// exist, a new one is created.
pub fn get_visibility_widget(&mut self) -> &mut State<Visibility> {
pub fn get_visibility_widget(&mut self) -> &State<Visibility> {
self
.visibility
.get_or_insert_with(|| State::value(<_>::default()))
}

/// Returns the `State<Opacity>` widget from the FatObj. If it doesn't exist,
/// a new one is created.
pub fn get_opacity_widget(&mut self) -> &mut State<Opacity> {
pub fn get_opacity_widget(&mut self) -> &State<Opacity> {
self
.opacity
.get_or_insert_with(|| State::value(<_>::default()))
}

/// Returns the `State<KeepAlive>` widget from the FatObj. If it doesn't
/// exist, a new one is created.
pub fn get_keep_alive_widget(&mut self) -> &mut State<KeepAlive> {
pub fn get_keep_alive_widget(&mut self) -> &State<KeepAlive> {
self
.keep_alive
.get_or_insert_with(|| State::value(<_>::default()))
Expand All @@ -422,10 +409,7 @@ impl<T> FatObj<T> {

macro_rules! on_mixin {
($this:ident, $on_method:ident, $f:ident) => {{
$this
.get_mix_builtin_widget()
.read()
.$on_method($f);
$this.get_mix_builtin_widget().$on_method($f);
$this
}};
}
Expand Down Expand Up @@ -565,7 +549,6 @@ impl<T> FatObj<T> {
) -> Self {
self
.get_mix_builtin_widget()
.read()
.on_x_times_tap((times, f));
self
}
Expand All @@ -579,7 +562,6 @@ impl<T> FatObj<T> {
) -> Self {
self
.get_mix_builtin_widget()
.read()
.on_x_times_tap_capture((times, f));
self
}
Expand Down Expand Up @@ -715,9 +697,11 @@ impl<T> FatObj<T> {
/// position in the tree source. The maximum value for tab_index is 32767.
/// If not specified, it takes the default value 0.
pub fn tab_index<const M: u8>(self, tab_idx: impl DeclareInto<i16, M>) -> Self {
self.declare_builtin_init(tab_idx, Self::get_mix_builtin_widget, |mixin, v| {
mixin.set_tab_index(v);
})
self.declare_builtin_init(
tab_idx,
|this| this.get_mix_builtin_widget().mix_flags(),
|m, v| m.set_tab_index(v),
)
}

/// Initializes the `Class` that should be applied to the widget.
Expand All @@ -731,9 +715,11 @@ impl<T> FatObj<T> {
/// Only one widget should have this attribute specified. If there are
/// several, the widget nearest the root, get the initial focus.
pub fn auto_focus<const M: u8>(self, v: impl DeclareInto<bool, M>) -> Self {
self.declare_builtin_init(v, Self::get_mix_builtin_widget, |m, v| {
m.set_auto_focus(v);
})
self.declare_builtin_init(
v,
|this| this.get_mix_builtin_widget().mix_flags(),
|m, v| m.set_auto_focus(v),
)
}

/// Initializes how its child should be scale to fit its box.
Expand Down Expand Up @@ -839,17 +825,15 @@ impl<T> FatObj<T> {
}

fn declare_builtin_init<V: 'static, B: 'static, const M: u8>(
mut self, init: impl DeclareInto<V, M>, get_builtin: impl FnOnce(&mut Self) -> &mut State<B>,
mut self, init: impl DeclareInto<V, M>, get_builtin: impl FnOnce(&mut Self) -> &State<B>,
set_value: fn(&mut B, V),
) -> Self {
let builtin = get_builtin(&mut self);
let (v, o) = init.declare_into().unzip();
set_value(&mut *builtin.silent(), v);
if let Some(o) = o {
let c_builtin = builtin.clone_writer();
let u = o.subscribe(move |(_, v)| {
set_value(&mut *c_builtin.write(), v);
});
let u = o.subscribe(move |(_, v)| set_value(&mut *c_builtin.write(), v));
self.on_disposed(move |_| u.unsubscribe())
} else {
self
Expand Down Expand Up @@ -896,9 +880,6 @@ impl<'a> FatObj<Widget<'a>> {
layout_box,
mix_builtin,
request_focus,
has_focus,
mouse_hover,
pointer_pressed,
cursor,
margin,
transform,
Expand Down
2 changes: 1 addition & 1 deletion core/src/builtin_widgets/focus_node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ mod tests {
let mut cnt = 0;
id.query_all_iter::<MixBuiltin>(tree)
.for_each(|b| {
if b.contain_flag(BuiltinFlags::Focus) {
if b.contain_flag(MixFlags::Focus) {
cnt += 1;
}
});
Expand Down
28 changes: 0 additions & 28 deletions core/src/builtin_widgets/has_focus.rs

This file was deleted.

Loading
Loading