Skip to main content

egui_pop_api/
sliders.rs

1//! Sliders' steps (ADR 0011). egui-pop's sliders
2//! (`egui_pop_core::slider`) are egui widgets: they see their changes
3//! through a hook ([`Slider::on_change`](egui_pop_core::slider::Slider::on_change)),
4//! and [`hook`] makes that hook run each finished change as the
5//! `slider.change` step of the application:
6//!
7//! | Step | Arguments | Default |
8//! |---|---|---|
9//! | `slider.change` | `slider` (the name given to [`hook`]), `thumb` (`value`, `low`, `high`), `from`, `to`, `cause` (`drag`, `key`, `wheel`, `typed`, `reset`, `access`) | writes `to` |
10//!
11//! A change is finished at the end of its gesture: a drag's release
12//! (from the value before the drag), a key, a wheel notch, a typed value,
13//! a reset. A before-hook vetoes it (the slider keeps or goes back to
14//! `from`) or redirects it (another `to`: snap, clamp); a run-hook gives
15//! the value to write (its result); an after-hook follows it. Every run
16//! publishes `slider.change` on the journal, so the value-change event
17//! is observed there. The frames of a drag are not steps: the slider
18//! follows the pointer, and the step decides at the release.
19//!
20//! ```
21//! use egui_pop_api::sliders;
22//! use egui_pop_core::slider::Slider;
23//!
24//! fn exposure(ui: &mut egui::Ui, app: &egui_pop_api::AppHandle, ev: &mut f64) {
25//!     ui.add(Slider::new(ev, "Exposure", -3.0..=4.0).on_change(sliders::hook(app, "exposure")));
26//! }
27//! ```
28
29use egui_pop_core::slider::{Change, Verdict};
30use egui_pop_model::ids::StepId;
31use serde_json::json;
32
33use crate::app::AppHandle;
34use crate::ctx::{Ctx, CtxError};
35use crate::steps::{Outcome, StepSpec};
36
37/// A slider's value changes, at the end of a gesture.
38pub const STEP_CHANGE: StepId = StepId::from_static("slider.change");
39
40pub(crate) fn register(ctx: &mut Ctx<'_>) -> Result<(), CtxError> {
41    ctx.step(
42        StepSpec::new(
43            STEP_CHANGE,
44            "A slider's value changes at the end of a gesture (`slider`, `thumb`, `from`, `to`, `cause`); a veto keeps `from`, a redirect or a run-hook gives another value.",
45        )
46        .args(json!({ "slider": "exposure", "thumb": "value", "from": 0.0, "to": 0.5, "cause": "drag" })),
47    )?;
48    Ok(())
49}
50
51/// A slider hook ([`Slider::on_change`](egui_pop_core::slider::Slider::on_change),
52/// [`RangeSlider::on_change`](egui_pop_core::slider::RangeSlider::on_change))
53/// running each finished change as the `slider.change` step, the slider
54/// named `slider` in its arguments. A drag's frames go through.
55pub fn hook<'a>(app: &'a AppHandle, slider: &'a str) -> impl FnMut(&Change) -> Verdict + 'a {
56    move |c: &Change| {
57        if !c.done {
58            return Verdict::Go;
59        }
60        let args = json!({
61            "slider": slider,
62            "thumb": c.thumb.name(),
63            "from": c.from,
64            "to": c.to,
65            "cause": c.cause.name(),
66        });
67        match app.step(&STEP_CHANGE, args, |_, args| Ok(args["to"].clone())) {
68            Outcome::Done(Ok(v)) => match v.as_f64() {
69                Some(to) if to == c.to => Verdict::Go,
70                Some(to) => Verdict::Redirect(to),
71                None => Verdict::Veto,
72            },
73            Outcome::Done(Err(_)) | Outcome::Vetoed { .. } => Verdict::Veto,
74        }
75    }
76}