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

add get/update from fn #559

Merged
merged 9 commits into from
Aug 27, 2024
Merged
Show file tree
Hide file tree
Changes from 7 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 examples/flight_booker/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ version.workspace = true
[dependencies]
floem = { path = "../.." }
time = { version = "0.3.31", features = ["parsing", "macros"] }
strum = { version = "0.25.0", features = ["derive"] }
38 changes: 19 additions & 19 deletions examples/flight_booker/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
use std::fmt::Display;

use floem::{
peniko::Color,
reactive::{create_rw_signal, create_signal, SignalGet, SignalUpdate},
reactive::{create_rw_signal, RwSignal, SignalGet, SignalUpdate},
unit::UnitExt,
views::{
button, dyn_container, empty, h_stack, labeled_radio_button, text, text_input, v_stack,
Decorators,
button, dyn_container, empty, labeled_radio_button, text, text_input, v_stack, Decorators,
StackExt,
},
IntoView,
};
use strum::IntoEnumIterator;
use time::Date;

fn oneway_message(start_text: String) -> String {
Expand All @@ -18,17 +21,25 @@ fn return_message(start_text: String, return_text: String) -> String {
format!("You have booked a flight on {start_text} and a return flight on {return_text}",)
}

#[derive(Eq, PartialEq, Clone)]
#[derive(Eq, PartialEq, Clone, Copy, strum::EnumIter)]
enum FlightMode {
OneWay,
Return,
}
impl Display for FlightMode {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
FlightMode::OneWay => f.write_str("One Way Flight"),
FlightMode::Return => f.write_str("Return Flight"),
}
}
}

static DATE_FORMAT: &[time::format_description::FormatItem<'_>] =
time::macros::format_description!("[day]-[month]-[year]");

pub fn app_view() -> impl IntoView {
let (flight_mode, flight_mode_set) = create_signal(FlightMode::OneWay);
let flight_mode = RwSignal::new(FlightMode::OneWay);

let start_text = create_rw_signal("24-02-2024".to_string());
let start_date = move || Date::parse(&start_text.get(), &DATE_FORMAT).ok();
Expand All @@ -55,20 +66,9 @@ pub fn app_view() -> impl IntoView {

let did_booking = create_rw_signal(false);

let mode_picker = h_stack((
labeled_radio_button(
FlightMode::OneWay,
move || flight_mode.get(),
|| "One way flight",
)
.on_update(move |v| flight_mode_set.set(v)),
labeled_radio_button(
FlightMode::Return,
move || flight_mode.get(),
|| "Return flight",
)
.on_update(move |v| flight_mode_set.set(v)),
));
let mode_picker = FlightMode::iter()
.map(move |fm| labeled_radio_button(fm, flight_mode, move || fm))
.h_stack();

let start_date_input = text_input(start_text)
.placeholder("Start date")
Expand Down
23 changes: 15 additions & 8 deletions examples/timer/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ use std::time::{Duration, Instant};

use floem::{
action::exec_after,
reactive::{create_effect, create_rw_signal, SignalGet, SignalUpdate, SignalWith},
reactive::{
create_effect, create_get_update, create_rw_signal, SignalGet, SignalUpdate, SignalWith,
},
unit::UnitExt,
views::{button, container, label, slider, stack, text, v_stack, Decorators},
IntoView,
Expand All @@ -16,8 +18,7 @@ fn app_view() -> impl IntoView {
// We take maximum duration as 100s for convenience so that
// one percent represents one second.
let target_duration = create_rw_signal(100.0);
let duration_slider = thin_slider(move || target_duration.get())
.on_change_pct(move |new| target_duration.set(new));
let duration_slider = thin_slider(target_duration);

let elapsed_time = create_rw_signal(Duration::ZERO);
let is_active = move || elapsed_time.get().as_secs_f32() < target_duration.get();
Expand Down Expand Up @@ -46,7 +47,11 @@ fn app_view() -> impl IntoView {
});
});

let progress = move || elapsed_time.get().as_secs_f32() / target_duration.get() * 100.0;
let progress = create_get_update(
target_duration,
move |val| elapsed_time.get().as_secs_f32() / val * 100.,
|val| *val,
);
let elapsed_time_bar = gauge(progress);

let reset_button = button(|| "Reset").on_click_stop(move |_| elapsed_time.set(Duration::ZERO));
Expand All @@ -68,15 +73,17 @@ fn app_view() -> impl IntoView {
}

/// A slider with a thin bar instead of the default thick bar.
fn thin_slider(fill_percent: impl Fn() -> f32 + 'static) -> slider::Slider {
slider::slider(fill_percent)
fn thin_slider(
fill_percent: impl SignalGet<f32> + SignalUpdate<f32> + Copy + 'static,
) -> slider::Slider {
slider::Slider::new_get_update(fill_percent)
.slider_style(|s| s.accent_bar_height(30.pct()).bar_height(30.pct()))
.style(|s| s.width(200))
}

/// A non-interactive slider that has been repurposed into a progress bar.
fn gauge(fill_percent: impl Fn() -> f32 + 'static) -> slider::Slider {
slider::slider(fill_percent)
fn gauge(fill_percent: impl SignalGet<f32> + Copy + 'static) -> slider::Slider {
slider::Slider::new_get(fill_percent)
.disabled(|| true)
.slider_style(|s| {
s.handle_radius(0)
Expand Down
21 changes: 13 additions & 8 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,
reactive::{create_rw_signal, SignalGet, SignalUpdate},
reactive::{create_get_update, create_rw_signal, SignalGet, SignalUpdate},
unit::UnitExt,
views::{button, container, label, slider, stack, text, v_stack, Decorators},
IntoView,
Expand All @@ -24,8 +24,7 @@ fn app_view() -> impl IntoView {
// We take maximum duration as 100s for convenience so that
// one percent represents one second.
let target_duration = create_rw_signal(100.0);
let duration_slider = thin_slider(move || target_duration.get())
.on_change_pct(move |new| target_duration.set(new));
let duration_slider = thin_slider(target_duration);

let stream = IntervalStream::new(tokio::time::interval(Duration::from_millis(100)));
let now = Instant::now();
Expand All @@ -45,7 +44,11 @@ fn app_view() -> impl IntoView {
)
});

let progress = move || elapsed_time().as_secs_f32() / target_duration.get() * 100.0;
let progress = create_get_update(
target_duration,
move |val| elapsed_time().as_secs_f32() / val * 100.,
|val| *val,
);
let elapsed_time_bar = gauge(progress);

let reset_button = button(|| "Reset").on_click_stop(move |_| started_at.set(Instant::now()));
Expand All @@ -67,15 +70,17 @@ fn app_view() -> impl IntoView {
}

/// A slider with a thin bar instead of the default thick bar.
fn thin_slider(fill_percent: impl Fn() -> f32 + 'static) -> slider::Slider {
slider::slider(fill_percent)
fn thin_slider(
fill_percent: impl SignalGet<f32> + SignalUpdate<f32> + Copy + 'static,
) -> slider::Slider {
slider::Slider::new_get_update(fill_percent)
.slider_style(|s| s.accent_bar_height(30.pct()).bar_height(30.pct()))
.style(|s| s.width(200))
}

/// A non-interactive slider that has been repurposed into a progress bar.
fn gauge(fill_percent: impl Fn() -> f32 + 'static) -> slider::Slider {
slider::slider(fill_percent)
fn gauge(fill_percent: impl SignalGet<f32> + Copy + 'static) -> slider::Slider {
slider::Slider::new_get(fill_percent)
.disabled(|| true)
.slider_style(|s| {
s.handle_radius(0)
Expand Down
22 changes: 6 additions & 16 deletions examples/widget-gallery/src/checkbox.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use floem::{
reactive::{create_signal, SignalGet, SignalUpdate},
reactive::RwSignal,
views::{checkbox, labeled_checkbox, Decorators},
IntoView,
};
Expand All @@ -8,34 +8,24 @@ use crate::form::{form, form_item};

pub fn checkbox_view() -> impl IntoView {
let width = 160.0;
let (is_checked, set_is_checked) = create_signal(true);
let is_checked = RwSignal::new(true);
form({
(
form_item("Checkbox:".to_string(), width, move || {
checkbox(move || is_checked.get())
.on_update(move |checked| {
set_is_checked.set(checked);
})
.style(|s| s.margin(5.0))
checkbox(is_checked).style(|s| s.margin(5.0))
}),
form_item("Disabled Checkbox:".to_string(), width, move || {
checkbox(move || is_checked.get())
checkbox(is_checked)
.style(|s| s.margin(5.0))
.disabled(|| true)
}),
form_item("Labelled Checkbox:".to_string(), width, move || {
labeled_checkbox(move || is_checked.get(), || "Check me!").on_update(
move |checked| {
set_is_checked.set(checked);
},
)
labeled_checkbox(is_checked, || "Check me!")
}),
form_item(
"Disabled Labelled Checkbox:".to_string(),
width,
move || {
labeled_checkbox(move || is_checked.get(), || "Check me!").disabled(|| true)
},
move || labeled_checkbox(is_checked, || "Check me!").disabled(|| true),
),
)
})
Expand Down
18 changes: 13 additions & 5 deletions examples/widget-gallery/src/dropdown.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ use strum::IntoEnumIterator;

use floem::{
peniko::Color,
reactive::{create_effect, RwSignal, SignalGet},
unit::UnitExt,
views::{container, dropdown::dropdown, label, stack, svg, Decorators},
views::{container, dropdown::Dropdown, label, stack, svg, Decorators},
IntoView,
};

Expand Down Expand Up @@ -45,12 +46,19 @@ pub fn dropdown_view() -> impl IntoView {
.into_any()
};

let dropdown_active_item = RwSignal::new(Values::Three);

create_effect(move |_| {
let active_item = dropdown_active_item.get();
println!("Selected: {active_item}");
});

form::form({
(form_item("Dropdown".to_string(), 120.0, move || {
dropdown(
// drivign function
move || Values::Three,
// main view
Dropdown::new_get_set(
// state
dropdown_active_item,
// main view function
main_drop_view,
// iterator to build list in dropdown
Values::iter(),
Expand Down
10 changes: 3 additions & 7 deletions examples/widget-gallery/src/lists.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use floem::{
peniko::Color,
reactive::{create_signal, SignalGet, SignalUpdate},
reactive::{create_signal, RwSignal, SignalGet, SignalUpdate},
style::JustifyContent,
text::Weight,
views::{
Expand Down Expand Up @@ -57,15 +57,11 @@ fn enhanced_list() -> impl IntoView {
move || long_list.get().enumerate(),
move |(_, item)| *item,
move |(index, item)| {
let (is_checked, set_is_checked) = create_signal(true);
let checkbox_state = RwSignal::new(true);
container({
stack({
(
checkbox(move || is_checked.get())
.style(|s| s.margin_left(6))
.on_update(move |checkd| {
set_is_checked.set(checkd);
}),
checkbox(checkbox_state).style(|s| s.margin_left(6)),
label(move || item.to_string()).style(|s| {
s.margin_left(6).height(32.0).font_size(22.0).items_center()
}),
Expand Down
Loading