Skip to content

Commit

Permalink
wfeat(core): 🎸 add PartState to partial state even if it's a value …
Browse files Browse the repository at this point in the history
…type; closes #521 #507
  • Loading branch information
M-Adoo committed May 5, 2024
1 parent e7afc97 commit 8164ff1
Show file tree
Hide file tree
Showing 21 changed files with 641 additions and 587 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ Please only add new entries below the [Unreleased](#unreleased---releasedate) he

### Features

- **core**: The split functions in `StateReader::map_reader`, `StateWriter::map_writer`, and `StateWriter::split_writer` no longer need to return a reference. (#568 @M-Adoo)
- **core**: Introduced `StateWatcher` for watching state modifies, which was previously the responsibility of `StateReader`. This results in a cleaner and more compact `StateReader` implementation. (#556, @M-Adoo)
- **gpu**: Introduced `GPUBackendImpl::max_textures_per_draw` to set a limit on textures per draw phase (#562 @M-Adoo)

Expand All @@ -41,6 +42,8 @@ Please only add new entries below the [Unreleased](#unreleased---releasedate) he

### Breaking

- **macros**: removed `map_writer!` and `split_writer!` macros. (#568, @M-Adoo)
- **ribir**: `StateWriter::map_writer` and `StateWriter::split_writer` now only require a writer split function, enhancing both reader and writer split operations. (#568, @M-Adoo)
- **core**: The `StateReader` no longer supports watching its modifications. Use the `StateWatcher` trait instead for this functionality. (#556 @M-Adoo)
- **painter**: Changes to `BackendPainter` APIs. This only affects you if you've implemented a custom painter. (#562 @M-Adoo)

Expand Down
33 changes: 25 additions & 8 deletions core/src/animation/stagger.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
//! A stagger animation is a series of animations that run one by one.
//! The next animation will start after a delay after the previous animation
//! starts and not care about if the previous animation ends.
//! A stagger animation consists of a sequence of animations that execute
//! consecutively. Each subsequent animation commences following a delay from
//! the start of the preceding animation, regardless of whether the preceding
//! animation has concluded.
//!
//! # Example
//!
Expand All @@ -22,14 +23,21 @@
//!
//! let mut first = @Text { text: "first" };
//! let mut second = @Text { text: "second" };
//! let first_opacity = first
//! .get_opacity_widget()
//! .map_writer(|w| PartData::from_ref_mut(&mut w.opacity));
//! let second_opacity = second
//! .get_opacity_widget()
//! .map_writer(|w| PartData::from_ref_mut(&mut w.opacity));
//!
//!
//! let first_fade_in = @Animate {
//! transition: transitions::EASE_IN.of(ctx!()),
//! state: map_writer!($first.opacity),
//! state: first_opacity,
//! };
//!
//! stagger.write().push_animation(first_fade_in);
//! stagger.write().push_state(map_writer!($second.opacity), 0., ctx!());
//! stagger.write().push_state(second_opacity, 0., ctx!());
//!
//! @Column {
//! on_mounted: move |_| stagger.run(),
Expand Down Expand Up @@ -225,15 +233,18 @@ mod tests {
fn_widget! {
let stagger = Stagger::new(Duration::from_millis(100), transitions::EASE_IN.of(ctx!()));
let mut mock_box = @MockBox { size: Size::new(100., 100.) };
let opacity = mock_box
.get_opacity_widget()
.map_writer(|w| PartData::from_ref_mut(&mut w.opacity));
let animate = @Animate {
transition: transitions::EASE_IN.of(ctx!()),
state: map_writer!($mock_box.opacity),
state: opacity,
from: 0.,
};

stagger.write().push_animation(animate);
stagger.write().push_state(
map_writer!($mock_box.size),
mock_box.map_writer(|w| PartData::from_ref_mut(&mut w.size)),
Size::new(200., 200.),
ctx!()
);
Expand All @@ -260,7 +271,13 @@ mod tests {
let c_stagger = stagger.clone_writer().into_inner();
let w = fn_widget! {
let mut mock_box = @MockBox { size: Size::new(100., 100.) };
$stagger.write().push_state(map_writer!($mock_box.opacity), 0., ctx!());
$stagger.write().push_state(
mock_box
.get_opacity_widget()
.map_writer(|w| PartData::from_ref_mut(&mut w.opacity)),
0.,
ctx!()
);
stagger.run();

mock_box
Expand Down
1 change: 1 addition & 0 deletions core/src/context/app_ctx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,7 @@ impl Drop for AppCtxScopeGuard {
fn drop(&mut self) {
// Safety: this guard guarantee only one thread can access the `AppCtx`.
unsafe {
AppCtx::shared_mut().windows.borrow_mut().clear();
APP_CTX = None;
INIT_THREAD_ID = None;
APP_CTX_INIT = Once::new();
Expand Down
6 changes: 6 additions & 0 deletions core/src/render_helper.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::cell::RefCell;

use ribir_algo::Sc;
use state_cell::StateCell;

use crate::prelude::*;

Expand Down Expand Up @@ -60,6 +61,11 @@ impl<R: Render> RenderTarget for RefCell<R> {
fn proxy<V>(&self, f: impl FnOnce(&Self::Target) -> V) -> V { f(&*self.borrow()) }
}

impl<R: Render> RenderTarget for StateCell<R> {
type Target = R;
fn proxy<V>(&self, f: impl FnOnce(&Self::Target) -> V) -> V { f(&*self.read()) }
}

impl<R: RenderTarget> RenderTarget for Sc<R> {
type Target = R::Target;
fn proxy<V>(&self, f: impl FnOnce(&Self::Target) -> V) -> V { self.deref().proxy(f) }
Expand Down
Loading

0 comments on commit 8164ff1

Please sign in to comment.