Skip to main content

Module group

Module group 

Source
Expand description

One undo step for many writes: AppHandle::group (and Call::group in a command) opens a history group through the host (Host::begin_group), runs a closure, and ends the group when the closure succeeds or cancels it (every write put back) when it fails or panics.

Experimental: may change in any release (docs/stability.md).

The group goes through the host, so a host keeping its own history sees it. While the host dispatches a command it is out of reach; a command’s group then goes through the host’s Host::groups, or the store’s groups when the host gives none.

use egui_pop_api::{App, Ctx, Plugin, PluginError};
use egui_pop_model::commands::{CommandSpec, Origin};
use egui_pop_model::ids::{CommandId, PluginId};
use egui_pop_model::values::{Change, Descriptor, Value};

const RESET: CommandId = CommandId::from_static("swatch.reset");

struct Swatch;
impl Plugin for Swatch {
    fn id(&self) -> PluginId { PluginId::from_static("swatch") }
    fn connect(&mut self, ctx: &mut Ctx<'_>) -> Result<(), PluginError> {
        ctx.value("swatch/hue", Descriptor::float("Hue", 0.0, 360.0, 200.0))?;
        ctx.value("swatch/size", Descriptor::float("Size", 40.0, 300.0, 160.0))?;
        ctx.command(CommandSpec::new(RESET, "Reset", "Swatch"), |call| {
            call.group("Reset the swatch", |g| {
                g.set("swatch/hue", Change::Reset)?;
                g.set("swatch/size", Change::Reset)?;
                Ok(serde_json::Value::Null)
            })
        })?;
        Ok(())
    }
}

let mut app = App::builder().plugin(Swatch).build(&egui::Context::default()).unwrap();
let h = app.handle().clone();
h.group(&Origin::User, "Move", |g| {
    g.set("swatch/hue", Change::To(Value::Float(30.0)))?;
    g.set("swatch/size", Change::To(Value::Float(80.0)))
}).unwrap();
app.run(&RESET, serde_json::Value::Null, Origin::User);
assert_eq!(h.value::<f64>("swatch/hue"), Some(200.0));
let store = h.store();
let steps = egui_pop_core::lock(&store).undo_steps().to_vec();
assert_eq!(steps.iter().map(|s| s.label.as_str()).collect::<Vec<_>>(), ["Move", "Reset the swatch"]);

Structs§

Group
A history group being written: what is written through it until the closure returns is one undo step, labelled and with its origin.