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

rename get update to DerivedRw #697

Merged
merged 1 commit into from
Nov 21, 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
4 changes: 2 additions & 2 deletions examples/timer/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::time::{Duration, Instant};
use floem::{
action::exec_after,
reactive::{
create_effect, create_get_update, create_rw_signal, SignalGet, SignalTrack, SignalUpdate,
create_effect, create_rw_signal, DerivedRwSignal, SignalGet, SignalTrack, SignalUpdate,
},
unit::{Pct, UnitExt},
views::{button, container, label, slider, stack, text, v_stack, Decorators},
Expand Down Expand Up @@ -47,7 +47,7 @@ fn app_view() -> impl IntoView {
});
});

let progress = create_get_update(
let progress = DerivedRwSignal::new(
target_duration,
move |val| Pct(elapsed_time.get().as_secs_f64() / val.0 * 100.),
|val| *val,
Expand Down
4 changes: 2 additions & 2 deletions examples/tokio-timer/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::time::Duration;

use floem::ext_event::create_signal_from_stream;
use floem::prelude::*;
use floem::reactive::create_get_update;
use floem::reactive::DerivedRwSignal;
use floem::unit::Pct;
use tokio::runtime::Runtime;
use tokio::time::Instant;
Expand Down Expand Up @@ -41,7 +41,7 @@ fn app_view() -> impl IntoView {
)
});

let progress = create_get_update(
let progress = DerivedRwSignal::new(
target_duration,
move |val| Pct(elapsed_time().as_secs_f64() / val.0 * 100.),
|val| *val,
Expand Down
4 changes: 2 additions & 2 deletions examples/widget-gallery/src/slider.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use floem::{
reactive::{create_get_update, create_rw_signal, SignalGet},
reactive::{create_rw_signal, DerivedRwSignal, SignalGet},
unit::UnitExt,
views::{label, slider, stack, text_input, Decorators},
IntoView,
Expand All @@ -9,7 +9,7 @@ use crate::form::{self, form_item};

pub fn slider_view() -> impl IntoView {
let input = create_rw_signal(String::from("50"));
let slider_state = create_get_update(
let slider_state = DerivedRwSignal::new(
input,
|val| val.parse::<f64>().unwrap_or_default().pct(),
|val| val.0.to_string(),
Expand Down
41 changes: 31 additions & 10 deletions reactive/src/get_update_fn.rs → reactive/src/derived.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,28 @@ use std::marker::PhantomData;

use crate::{read::SignalTrack, RwSignal, SignalGet, SignalUpdate, SignalWith};

pub struct GetUpdateFn<T, O, GF: Fn(&T) -> O + Clone + 'static, UF: Fn(&O) -> T + 'static> {
/// A signal that is derived from an [RwSignal](super::RwSignal) but lets you specify getters and setters for the signal.
///
/// This is useful when you want a single state variable and don't want to use effects to synchronize multiple signals.
///
/// This is also useful when you want a derived signal that implements the [SignalGet], [SignalWith], etc. traits.
pub struct DerivedRwSignal<T, O, GF: Fn(&T) -> O + Clone + 'static, UF: Fn(&O) -> T + 'static> {
signal: RwSignal<T>,
getter: RwSignal<Box<GF>>,
setter: RwSignal<Box<UF>>,
ty: PhantomData<T>,
}

impl<T, O, GF: Fn(&T) -> O + Copy, UF: Fn(&O) -> T + Copy> Clone for GetUpdateFn<T, O, GF, UF> {
impl<T, O, GF: Fn(&T) -> O + Copy, UF: Fn(&O) -> T + Copy> Clone for DerivedRwSignal<T, O, GF, UF> {
fn clone(&self) -> Self {
*self
}
}

impl<T, O, GF: Fn(&T) -> O + Copy, UF: Fn(&O) -> T + Copy> Copy for GetUpdateFn<T, O, GF, UF> {}
impl<T, O, GF: Fn(&T) -> O + Copy, UF: Fn(&O) -> T + Copy> Copy for DerivedRwSignal<T, O, GF, UF> {}

impl<T: Clone + 'static, O: Clone, GF: Fn(&T) -> O + Copy, UF: Fn(&O) -> T + Copy> SignalGet<O>
for GetUpdateFn<T, O, GF, UF>
for DerivedRwSignal<T, O, GF, UF>
{
fn id(&self) -> crate::id::Id {
self.signal.id
Expand Down Expand Up @@ -48,7 +53,7 @@ impl<T: Clone + 'static, O: Clone, GF: Fn(&T) -> O + Copy, UF: Fn(&O) -> T + Cop
}

impl<T: Clone + 'static, O: Clone, GF: Fn(&T) -> O + Copy, UF: Fn(&O) -> T + Copy> SignalWith<O>
for GetUpdateFn<T, O, GF, UF>
for DerivedRwSignal<T, O, GF, UF>
{
fn id(&self) -> crate::id::Id {
self.signal.id
Expand Down Expand Up @@ -99,7 +104,7 @@ impl<T: Clone + 'static, O: Clone, GF: Fn(&T) -> O + Copy, UF: Fn(&O) -> T + Cop
}

impl<T: Clone + 'static, O: Clone, GF: Fn(&T) -> O + Copy, UF: Fn(&O) -> T + Copy> SignalTrack<O>
for GetUpdateFn<T, O, GF, UF>
for DerivedRwSignal<T, O, GF, UF>
{
fn id(&self) -> crate::id::Id {
self.signal.id
Expand All @@ -116,7 +121,7 @@ impl<T: Clone + 'static, O: Clone, GF: Fn(&T) -> O + Copy, UF: Fn(&O) -> T + Cop
}

impl<T: 'static, O, GF: Fn(&T) -> O + Copy, UF: Fn(&O) -> T + Copy> SignalUpdate<O>
for GetUpdateFn<T, O, GF, UF>
for DerivedRwSignal<T, O, GF, UF>
{
fn id(&self) -> crate::id::Id {
self.signal.id
Expand Down Expand Up @@ -168,19 +173,35 @@ impl<T: 'static, O, GF: Fn(&T) -> O + Copy, UF: Fn(&O) -> T + Copy> SignalUpdate
})
}
}
impl<T, O, GF, UF> DerivedRwSignal<T, O, GF, UF>
where
GF: Fn(&T) -> O + Clone + 'static,
UF: Fn(&O) -> T + 'static,
{
pub fn new(signal: RwSignal<T>, getter: GF, setter: UF) -> Self {
let getter = RwSignal::new(Box::new(getter));
let setter = RwSignal::new(Box::new(setter));
DerivedRwSignal {
signal,
getter,
setter,
ty: PhantomData,
}
}
}

pub fn create_get_update<T, O, GF, UF>(
pub fn create_derived_rw_signal<T, O, GF, UF>(
signal: RwSignal<T>,
getter: GF,
setter: UF,
) -> GetUpdateFn<T, O, GF, UF>
) -> DerivedRwSignal<T, O, GF, UF>
where
GF: Fn(&T) -> O + Clone + 'static,
UF: Fn(&O) -> T + 'static,
{
let getter = RwSignal::new(Box::new(getter));
let setter = RwSignal::new(Box::new(setter));
GetUpdateFn {
DerivedRwSignal {
signal,
getter,
setter,
Expand Down
4 changes: 2 additions & 2 deletions reactive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@

mod base;
mod context;
mod derived;
mod effect;
mod get_update_fn;
mod id;
mod impls;
mod memo;
Expand All @@ -20,8 +20,8 @@ mod write;

pub use base::{create_base_signal, BaseSignal};
pub use context::{provide_context, use_context};
pub use derived::{create_derived_rw_signal, DerivedRwSignal};
pub use effect::{batch, create_effect, create_stateful_updater, create_updater, untrack};
pub use get_update_fn::{create_get_update, GetUpdateFn};
pub use memo::{create_memo, Memo};
pub use read::{ReadSignalValue, SignalGet, SignalRead, SignalTrack, SignalWith};
pub use scope::{as_child_of_current_scope, with_scope, Scope};
Expand Down