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 state #414

Merged
merged 10 commits into from
Sep 13, 2023
Merged

Refactor state #414

merged 10 commits into from
Sep 13, 2023

Conversation

M-Adoo
Copy link
Collaborator

@M-Adoo M-Adoo commented Aug 14, 2023

This pull request contains two parts refactoring and adding new features.

The new syntax

  1. Introduce the new syntax to declare objects. The new syntax is more concise and flexible. The old syntax is to use Rust expressions in a DSL structure, so it looks like Rust syntax, but it is not. The new syntax is based on several macros to expand the Rust syntax, so its writing and thinking logic are consistent with Rust.
  2. The old syntax is auto-subscribe to the state modified if an object uses a state(or id) in its expression. But the new syntax needs to use pipe! to wrap the expression to subscribe to the state modified. The old syntax is implicit, the new syntax must explicitly subscribe.

the old syntax:

widget! {
  // declare extra states(id) to auto subscribe.
  states { }
  init ctx => {
    // Rust expression here
  }
  // declare object
  Animate {
    id: animate
    ...
  }
  Row {
    on_mounted: move |_|  animate.run()
    SizedBox{
      id: box1,
      size: Size { width: 100.0, height: 100.0 },
    }
    SizedBox{ size: box1.size },
  }
  finally ctx => {
    // Rust expression here
  }
}

the new syntax:

fn_widget!{
  // the whole body is a Rust expression.
  let animate = @Animate { ... };
  let box1 = @SizedBox {
    size: Size { width: 100.0, height: 100.0 },
  };
  @Row {
    on_mounted: move |_| animate.run()
    @ { box1 }
    @ SizedBox{ size: pipe!($box1.size) },
  }
}

The key macros of the new syntax:

  • set_build_ctx!(ctx): use to set the build context, let the subsequent code can use ctx!() to access the build context.
  • ctx!(): use to access the build context anywhere if the build context is set by set_build_ctx!(ctx) in this scope.
  • rdl! {} macro and @ symbol: used to declare an object. must used after set_build_ctx!(ctx) called. @ is a shortcut for rdl! {} in fn_widget, rdl! and pipe!.
  • $ symbol: get a reference of the state, supported in fn_widget, rdl! and pipe!.
  • pipe!: use to create a continuous value, for a widget or a field of an object that wants to keep update with an expression.
  • fn_widget: a macro to create a FnWidget, and call set_build_ctx! at the beginning of the function body.

The state reader, writer and state hierarchy

We use into_readonly and into_writeable to do a little read-write distinction before, but it is not enough. Now, we introduced StateReader and StateWriter. A state without a writer will cancel all subscriptions on it because no one can modify it.

Also introduced the state hierarchy in this state. The state always thinks as a whole before, when we need part of the state, it needs a little trick: introduce a new state and subscribe to the original map in some way.

Now, we have three convenient methods to create a new state from the original state:

  • Use StateReader::map_reader to create a new read state from the original state, it shares the same data and notify with the original state.
  • Use StateReader::map_writer to create a new write state from the original state, it shares the same data and notify with the original state.
  • Use StateReader::split_writer to create a new read-write state from the original state, it shares the same data with the original but has its own notify.

@codecov
Copy link

codecov bot commented Aug 14, 2023

Codecov Report

Patch coverage: 86.79% and project coverage change: -1.00% ⚠️

Comparison is base (0f0f89e) 84.08% compared to head (7ebac04) 83.08%.

Additional details and impacted files
@@            Coverage Diff             @@
##           master     #414      +/-   ##
==========================================
- Coverage   84.08%   83.08%   -1.00%     
==========================================
  Files         178      185       +7     
  Lines       23183    24899    +1716     
==========================================
+ Hits        19494    20688    +1194     
- Misses       3689     4211     +522     
Files Changed Coverage Δ
algo/src/cow_rc.rs 78.33% <0.00%> (-0.66%) ⬇️
algo/src/lib.rs 100.00% <ø> (ø)
core/src/animation/lerp.rs 68.29% <0.00%> (+0.40%) ⬆️
core/src/builtin_widgets/theme/transition_theme.rs 87.50% <ø> (-1.08%) ⬇️
core/src/events/focus.rs 100.00% <ø> (+6.06%) ⬆️
core/src/ticker.rs 100.00% <ø> (ø)
dev-helper/src/example_framework.rs 0.00% <0.00%> (ø)
dev-helper/src/widget_test.rs 100.00% <ø> (ø)
macros/src/widget_macro/visit_mut.rs 72.86% <0.00%> (-12.40%) ⬇️
ribir/src/app.rs 0.00% <0.00%> (ø)
... and 112 more

... and 11 files with indirect coverage changes

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@M-Adoo M-Adoo force-pushed the refactor-state branch 6 times, most recently from 6707b1b to b0391a5 Compare August 21, 2023 07:11
@M-Adoo M-Adoo force-pushed the refactor-state branch 5 times, most recently from a06dc9a to 7baecbf Compare August 28, 2023 06:31
@M-Adoo M-Adoo force-pushed the refactor-state branch 8 times, most recently from 171892c to bee3286 Compare September 8, 2023 15:36
@M-Adoo M-Adoo force-pushed the refactor-state branch 5 times, most recently from dea0c1a to 98ca62c Compare September 11, 2023 07:52
@M-Adoo M-Adoo force-pushed the refactor-state branch 2 times, most recently from 6d38da8 to 95c3fc2 Compare September 12, 2023 01:57
@M-Adoo M-Adoo added this pull request to the merge queue Sep 13, 2023
Merged via the queue into master with commit 0271598 Sep 13, 2023
5 of 6 checks passed
@M-Adoo M-Adoo deleted the refactor-state branch September 13, 2023 03:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants