diff --git a/crates/ui-egui/src/app/frame.rs b/crates/ui-egui/src/app/frame.rs index 151bd3f0ab..e15fcfeaac 100644 --- a/crates/ui-egui/src/app/frame.rs +++ b/crates/ui-egui/src/app/frame.rs @@ -162,11 +162,7 @@ impl PhotocraftApp { // A software adapter or CPU mode chosen at startup is every document's reason. crate::render_path::frame(self); // Every frame that started its stage record closes it, whichever way `ui_body` returns. - // Controls and panels write their values back on every pass; only the writes that change an option move its - // revision, which the options bar's version holds, so a pass that changed nothing leaves the bar settled. - let options = self.ui.tool_options.checkpoint(); self.ui_body(ui); - self.ui.tool_options.settle(options); // What an injected step changed is the target of the rest of its request: only a change // between passes withdraws queued input (`withdraw_stale_input`). #[cfg(not(target_arch = "wasm32"))] diff --git a/crates/ui-egui/src/chrome_settle_trials.rs b/crates/ui-egui/src/chrome_settle_trials.rs index b0ea8b1033..7241e312e2 100644 --- a/crates/ui-egui/src/chrome_settle_trials.rs +++ b/crates/ui-egui/src/chrome_settle_trials.rs @@ -254,16 +254,24 @@ fn a_tool_option_runs_the_options_bar_once() { } /// A bar that runs and writes every control's value back leaves the tool options' revision alone, so idle frames never -/// move what the bar's version holds. +/// move what the bar's version holds, whichever tool's bar is showing. #[test] fn an_idle_options_bar_leaves_the_tool_options_revision() { let mut h = harness(); + let mut moved = Vec::new(); + for tool in crate::state::Tool::ALL { + h.state_mut().ui.tool = tool; + h.run_steps(4); + let settled = h.state().ui.tool_options.revision(); + h.run_steps(6); + if h.state().ui.tool_options.revision() != settled { + moved.push(tool); + } + } + assert!(moved.is_empty(), "idle frames moved the tool options under {moved:?}"); + // A changed option runs the bar, whose own writes then change nothing more. h.state_mut().ui.tool = crate::state::Tool::Move; h.run_steps(4); - let settled = h.state().ui.tool_options.revision(); - h.run_steps(10); - assert_eq!(h.state().ui.tool_options.revision(), settled, "ten idle frames"); - // A changed option runs the bar, whose own writes then change nothing more. let on = h.state().ui.tool_options.move_auto_select; h.state_mut().ui.tool_options.move_auto_select = !on; let changed = h.state().ui.tool_options.revision(); diff --git a/crates/ui-egui/src/panels/options_bar.rs b/crates/ui-egui/src/panels/options_bar.rs index 961b2318ef..0dd011df4e 100644 --- a/crates/ui-egui/src/panels/options_bar.rs +++ b/crates/ui-egui/src/panels/options_bar.rs @@ -345,8 +345,8 @@ pub(super) fn labelled(ui: &mut egui::Ui, label: &str, mut add: impl FnMut(&m } /// A check box named `label` over `value`. -pub(super) fn check(ui: &mut egui::Ui, label: &str, value: &mut bool) { - Checkbox::new(Bind::bound(value)).label(label.to_owned()).show(ui); +pub(super) fn check(ui: &mut egui::Ui, label: &str, value: Bind<'_, bool>) { + Checkbox::new(value).label(label.to_owned()).show(ui); } /// A number field named `label` over `value` (in `unit` when it is not empty), limited to `range`. The events of this diff --git a/crates/ui-egui/src/panels/options_bar/fill.rs b/crates/ui-egui/src/panels/options_bar/fill.rs index 61e9ff4df5..f44cde0f6b 100644 --- a/crates/ui-egui/src/panels/options_bar/fill.rs +++ b/crates/ui-egui/src/panels/options_bar/fill.rs @@ -1,6 +1,6 @@ //! The Paint Bucket's members of the options bar, and the Mode list of the Paint Bucket and the stamp tools. -use craft::ui::contract::{Bind, Entry, Show as _}; +use craft::ui::contract::{Bind, Entry, Show as _, bind_tracked}; use craft::ui::patterns::blend_keys::{BlendKeyEntry, blend_entries}; use craft::ui::pickers::Select; use photocraft_doc::BlendMode; @@ -67,25 +67,34 @@ pub(super) fn members(app: &mut PhotocraftApp, ui: &mut egui::Ui) { } let entries = mode_entries(Tool::PaintBucket, true, false); let sources = [Entry::new(false, tl!("Foreground").to_owned()), Entry::new(true, tl!("Pattern").to_owned())]; - let o = &mut app.ui.tool_options; - labelled(ui, tl!("Fill"), |ui| Select::new(tl!("Source").to_owned(), Bind::bound(&mut o.bucket_fill_pattern), &sources).show(ui)); - let mut mode = if o.bucket_clear { PaintMode::Clear } else { PaintMode::Blend(o.bucket_blend_mode) }; + { + let mut pattern = Some(bind_tracked!(app.ui.tool_options => bucket_fill_pattern)); + labelled(ui, tl!("Fill"), |ui| pattern.take().map(|pattern| Select::new(tl!("Source").to_owned(), pattern, &sources).show(ui))); + } + let o = &app.ui.tool_options; + let shown = if o.bucket_clear { PaintMode::Clear } else { PaintMode::Blend(o.bucket_blend_mode) }; + let mut mode = shown; labelled(ui, tl!("Mode"), |ui| mode_select(Bind::bound(&mut mode), &entries).show(ui)); - match mode { - PaintMode::Clear => o.bucket_clear = true, - PaintMode::Blend(blend) => { - o.bucket_clear = false; - o.bucket_blend_mode = blend; + let (opacity, tolerance) = (o.fill_opacity, o.tolerance); + if mode != shown { + let o = &mut app.ui.tool_options; + match mode { + PaintMode::Clear => o.bucket_clear = true, + PaintMode::Blend(blend) => { + o.bucket_clear = false; + o.bucket_blend_mode = blend; + } + PaintMode::Behind => {} } - PaintMode::Behind => {} } - if let Some(opacity) = number_field(ui, tl!("Opacity"), o.fill_opacity, 1.0..=100.0, "%").landed() { - o.fill_opacity = *opacity; + if let Some(opacity) = number_field(ui, tl!("Opacity"), opacity, 1.0..=100.0, "%").landed() { + app.ui.tool_options.fill_opacity = *opacity; } - if let Some(tolerance) = number_field(ui, tl!("Tolerance"), o.tolerance, 0.0..=255.0, "").landed() { - o.tolerance = *tolerance; + if let Some(tolerance) = number_field(ui, tl!("Tolerance"), tolerance, 0.0..=255.0, "").landed() { + app.ui.tool_options.tolerance = *tolerance; } - check(ui, tl!("Anti-alias"), &mut o.anti_alias); - check(ui, tl!("Contiguous"), &mut o.contiguous); - check(ui, tl!("All Layers"), &mut o.sample_all.paint_bucket); + let (anti_alias, contiguous, all_layers) = bind_tracked!(app.ui.tool_options => anti_alias, contiguous, sample_all.paint_bucket); + check(ui, tl!("Anti-alias"), anti_alias); + check(ui, tl!("Contiguous"), contiguous); + check(ui, tl!("All Layers"), all_layers); } diff --git a/crates/ui-egui/src/panels/options_bar/navigate.rs b/crates/ui-egui/src/panels/options_bar/navigate.rs index d05faa82cf..ee64c9e385 100644 --- a/crates/ui-egui/src/panels/options_bar/navigate.rs +++ b/crates/ui-egui/src/panels/options_bar/navigate.rs @@ -1,7 +1,7 @@ //! The options bar's members of the Move, Eyedropper, Zoom, Rotate View and Hand tools. use craft::ui::canvas::{NavigateBar, NavigationOptions}; -use craft::ui::contract::{Bind, Entry, Show as _}; +use craft::ui::contract::{Bind, Entry, Show as _, bind_tracked}; use craft::ui::display::Divider; use craft::ui::pickers::Select; @@ -21,12 +21,12 @@ pub(super) fn members(app: &mut PhotocraftApp, ui: &mut egui::Ui) { /// Auto-Select, its target, Show Transform Controls, then the align strip. fn move_members(app: &mut PhotocraftApp, ui: &mut egui::Ui) { - let o = &mut app.ui.tool_options; - check(ui, tl!("Auto-Select:"), &mut o.move_auto_select); + let (auto_select, target, transform) = bind_tracked!(app.ui.tool_options => move_auto_select, move_target, move_show_transform); + check(ui, tl!("Auto-Select:"), auto_select); let targets = [Entry::new("group".to_owned(), tl!("Group").to_owned()), Entry::new("layer".to_owned(), tl!("Layer").to_owned())]; - Select::new(tl!("Auto-Select:").to_owned(), Bind::bound(&mut o.move_target), &targets).show(ui); + Select::new(tl!("Auto-Select:").to_owned(), target, &targets).show(ui); Divider::vertical().show(ui); - check(ui, tl!("Show Transform Controls"), &mut o.move_show_transform); + check(ui, tl!("Show Transform Controls"), transform); Divider::vertical().show(ui); crate::align_bar::show(app, ui); } @@ -60,7 +60,9 @@ fn view_members(app: &mut PhotocraftApp, ui: &mut egui::Ui) { // Zoom Out is the bar's `out`; the tool keeps the direction as `zoom_in`. let mut out = !app.ui.tool_options.zoom_in; let shown = NavigateBar::zoom(Bind::bound(&mut view), target, &mut options, Bind::bound(&mut out)).show(ui); - app.ui.tool_options.zoom_in = !out; + if app.ui.tool_options.zoom_in == out { + app.ui.tool_options.zoom_in = !out; + } crate::chrome_ui::zoom_alert(app, shown.invalid_zoom); write_view(app, index, &view, !shown.view.is_empty()); } else { diff --git a/crates/ui-egui/src/state.rs b/crates/ui-egui/src/state.rs index 5ad2b02ab1..4be51b6a79 100644 --- a/crates/ui-egui/src/state.rs +++ b/crates/ui-egui/src/state.rs @@ -599,7 +599,7 @@ mod tests { let mut s = SampleAllLayers::default(); assert!(s.slot(Tool::Brush).is_none()); for t in [Tool::MagicWand, Tool::PaintBucket, Tool::Blur, Tool::Sharpen, Tool::Smudge, Tool::Remove] { - *s.slot(t).unwrap() = true; + *s.slot_mut(t).unwrap() = true; assert!(s.get(t), "{t:?}"); } assert!(!s.get(Tool::QuickSelection)); diff --git a/crates/ui-egui/src/state/tool_options.rs b/crates/ui-egui/src/state/tool_options.rs index 4638ca23a0..344bedefc2 100644 --- a/crates/ui-egui/src/state/tool_options.rs +++ b/crates/ui-egui/src/state/tool_options.rs @@ -416,29 +416,41 @@ impl Default for SampleAllLayers { } } +/// The field of `SampleAllLayers` that `$tool` keeps, borrowed as `$borrow`. +macro_rules! sample_all_slot { + ($this:ident, $tool:ident, $($borrow:tt)+) => { + Some(match $tool { + Tool::MagicWand => $($borrow)+ $this.magic_wand, + Tool::QuickSelection => $($borrow)+ $this.quick_selection, + Tool::ObjectSelection => $($borrow)+ $this.object_selection, + Tool::PaintBucket => $($borrow)+ $this.paint_bucket, + Tool::MagicEraser => $($borrow)+ $this.magic_eraser, + Tool::SpotHealing => $($borrow)+ $this.spot_healing, + Tool::Patch => $($borrow)+ $this.patch, + Tool::ContentAwareMove => $($borrow)+ $this.content_aware_move, + Tool::Remove => $($borrow)+ $this.remove, + Tool::Blur => $($borrow)+ $this.blur, + Tool::Sharpen => $($borrow)+ $this.sharpen, + Tool::Smudge => $($borrow)+ $this.smudge, + _ => return None, + }) + }; +} + impl SampleAllLayers { /// `tool`'s setting; false for a tool without the option. pub fn get(mut self, tool: Tool) -> bool { - self.slot(tool).is_some_and(|v| *v) + self.slot(tool).unwrap_or(false) } /// `tool`'s setting, or `None` for a tool without the option. - pub fn slot(&mut self, tool: Tool) -> Option<&mut bool> { - Some(match tool { - Tool::MagicWand => &mut self.magic_wand, - Tool::QuickSelection => &mut self.quick_selection, - Tool::ObjectSelection => &mut self.object_selection, - Tool::PaintBucket => &mut self.paint_bucket, - Tool::MagicEraser => &mut self.magic_eraser, - Tool::SpotHealing => &mut self.spot_healing, - Tool::Patch => &mut self.patch, - Tool::ContentAwareMove => &mut self.content_aware_move, - Tool::Remove => &mut self.remove, - Tool::Blur => &mut self.blur, - Tool::Sharpen => &mut self.sharpen, - Tool::Smudge => &mut self.smudge, - _ => return None, - }) + pub fn slot(&self, tool: Tool) -> Option { + sample_all_slot!(self, tool, &).copied() + } + + /// `tool`'s setting to write, or `None` for a tool without the option. + pub fn slot_mut(&mut self, tool: Tool) -> Option<&mut bool> { + sample_all_slot!(self, tool, &mut) } } diff --git a/crates/ui-egui/src/tools/adjustment_brush.rs b/crates/ui-egui/src/tools/adjustment_brush.rs index 04b3cf28d2..e18c508d75 100644 --- a/crates/ui-egui/src/tools/adjustment_brush.rs +++ b/crates/ui-egui/src/tools/adjustment_brush.rs @@ -6,7 +6,7 @@ use craft::behavior::IconId; use craft::ui::buttons::{IconButton, ToggleButton}; use craft::ui::choice::{Checkbox, Segmented}; -use craft::ui::contract::{Bind, Entry, Props as _, Show as _}; +use craft::ui::contract::{Bind, Entry, Props as _, Show as _, bind_tracked}; use craft::ui::display::Divider; use craft::ui::pickers::Select; use photocraft_engine::adjustment_brush_cmds::{self as ab, ENTRIES, INDEXED_ALERT}; @@ -152,7 +152,7 @@ pub fn options_bar(ui: &mut egui::Ui, app: &mut PhotocraftApp, tool: Tool) -> bo let choose = tl!("Choose an object and have the adjustment applied to it"); IconButton::new(IconId::new("square-dashed-mouse-pointer")).label(choose).tooltip(choose).enabled(false).show(ui); // On, the canvas tints what the active adjustment layer's mask reveals (`raster_source::MaskTint`, #12520). - Checkbox::new(Bind::bound(&mut app.ui.tool_options.adjustment_brush_overlay)) + Checkbox::new(bind_tracked!(app.ui.tool_options => adjustment_brush_overlay)) .label(tl!("Overlay").to_owned()) .tooltip(tl!("Show or hide the painted mask areas").to_owned()) .show(ui); @@ -160,7 +160,7 @@ pub fn options_bar(ui: &mut egui::Ui, app: &mut PhotocraftApp, tool: Tool) -> bo let mut alerts = Vec::new(); let mut opacity = (brush.opacity * 100.0).round(); - let out = option_cells::number(tl!("Opacity:"), &mut opacity, 1.0..=100.0).whole().unit("%").show(ui); + let out = option_cells::number(tl!("Opacity:"), Bind::bound(&mut opacity), 1.0..=100.0).whole().unit("%").show(ui); key = out.events.iter().find_map(|e| crate::history_keys::coalesce("brush", e)).or(key); brush.opacity = opacity / 100.0; alerts.extend(out.out_of_range); @@ -173,7 +173,7 @@ pub fn options_bar(ui: &mut egui::Ui, app: &mut PhotocraftApp, tool: Tool) -> bo key = pressure.events.iter().find_map(|e| crate::history_keys::coalesce("brush", e)).or(key); } let mut flow = brush.flow * 100.0; - let out = option_cells::number(tl!("Flow:"), &mut flow, 1.0..=100.0).unit("%").show(ui); + let out = option_cells::number(tl!("Flow:"), Bind::bound(&mut flow), 1.0..=100.0).unit("%").show(ui); key = out.events.iter().find_map(|e| crate::history_keys::coalesce("brush", e)).or(key); brush.flow = flow / 100.0; alerts.extend(out.out_of_range); diff --git a/crates/ui-egui/src/tools/analysis/bars.rs b/crates/ui-egui/src/tools/analysis/bars.rs index 15d34f36e8..31d2f1d580 100644 --- a/crates/ui-egui/src/tools/analysis/bars.rs +++ b/crates/ui-egui/src/tools/analysis/bars.rs @@ -7,7 +7,7 @@ use craft::draw::ui::measure::{RulerBar, RulerLine}; use craft::theme::Srgba; use craft::ui::buttons::{Button, IconButton, ToggleButton}; use craft::ui::choice::Swatch; -use craft::ui::contract::{Bind, Entry, Props as _, Show as _, ShowContainer as _}; +use craft::ui::contract::{Bind, Entry, Props as _, Show as _, ShowContainer as _, bind_tracked}; use craft::ui::display::{Divider, Text}; use craft::ui::form::Field; use craft::ui::pickers::Select; @@ -161,8 +161,8 @@ fn count(app: &mut PhotocraftApp, ui: &mut egui::Ui, doc: &photocraft_doc::Docum Divider::vertical().show(ui); let (marker, label) = (g.marker_size as f32, g.label_size as f32); let (mut new_marker, mut new_label) = (marker, label); - let marker_out = option_cells::number(tl!("Marker Size"), &mut new_marker, MARKER_SIZE).whole().show(ui); - let label_out = option_cells::number(tl!("Label Size"), &mut new_label, LABEL_SIZE).whole().show(ui); + let marker_out = option_cells::number(tl!("Marker Size"), Bind::bound(&mut new_marker), MARKER_SIZE).whole().show(ui); + let label_out = option_cells::number(tl!("Label Size"), Bind::bound(&mut new_label), LABEL_SIZE).whole().show(ui); // A typed size out of range takes the closest value and says so once (#13530). for alert in [&marker_out.out_of_range, &label_out.out_of_range].into_iter().flatten() { option_cells::range_alert(app, ui, alert); @@ -207,7 +207,7 @@ fn sampler(app: &mut PhotocraftApp, ui: &mut egui::Ui) { let n = app.session.active().map_or(0, |d| d.color_samplers.len()); // One Sample Size setting, shared with the Eyedropper (#11732). // PLAN: assumed `SamplerOptions::show(ui)` returns an output with `clear_all: bool`. - let out = SamplerOptions::new(n, Bind::bound(&mut app.ui.tool_options.eyedropper.size)).show(ui); + let out = SamplerOptions::new(n, bind_tracked!(app.ui.tool_options => eyedropper.size)).show(ui); if out.clear_all { run(app, "view.colorSamplers.clear", json!({})); } diff --git a/crates/ui-egui/src/tools/eraser_ui.rs b/crates/ui-egui/src/tools/eraser_ui.rs index 12fd03a433..c3a701c0a9 100644 --- a/crates/ui-egui/src/tools/eraser_ui.rs +++ b/crates/ui-egui/src/tools/eraser_ui.rs @@ -7,7 +7,7 @@ use craft::behavior::IconId; use craft::ui::buttons::ToggleButton; use craft::ui::choice::{Checkbox, Segmented}; -use craft::ui::contract::{Bind, Entry, Props as _, Show as _, ShowContainer as _}; +use craft::ui::contract::{Bind, Entry, Props as _, Show as _, ShowContainer as _, bind_tracked}; use craft::ui::display::Divider; use craft::ui::form::Field; use craft::ui::pickers::Select; @@ -148,8 +148,8 @@ pub(crate) fn stored_tolerance(v: f32) -> f32 { } /// A check box named `label` over `value`. -fn check(ui: &mut egui::Ui, label: &str, value: &mut bool, tip: Option<&str>) { - let mut checkbox = Checkbox::new(Bind::bound(value)).label(label.to_owned()); +fn check(ui: &mut egui::Ui, label: &str, value: Bind<'_, bool>, tip: Option<&str>) { + let mut checkbox = Checkbox::new(value).label(label.to_owned()); if let Some(tip) = tip { checkbox = checkbox.tooltip(tip.to_owned()); } @@ -168,28 +168,27 @@ pub fn options_bar(ui: &mut egui::Ui, app: &mut PhotocraftApp, tool: Tool) -> bo } fn magic_eraser_members(ui: &mut egui::Ui, app: &mut PhotocraftApp) { - let o = &mut app.ui.tool_options; // The field clamps without an alert, as the reference's Magic Eraser Tolerance does. - let _ = option_cells::number(tl!("Tolerance:"), &mut o.tolerance, 0.0..=255.0).show(ui); - check(ui, tl!("Anti-alias"), &mut o.anti_alias, None); - check(ui, tl!("Contiguous"), &mut o.contiguous, None); - check(ui, tl!("Sample All Layers"), &mut o.sample_all.magic_eraser, None); + let _ = option_cells::number(tl!("Tolerance:"), bind_tracked!(app.ui.tool_options => tolerance), 0.0..=255.0).show(ui); + let (anti_alias, contiguous, sample_all) = bind_tracked!(app.ui.tool_options => anti_alias, contiguous, sample_all.magic_eraser); + check(ui, tl!("Anti-alias"), anti_alias, None); + check(ui, tl!("Contiguous"), contiguous, None); + check(ui, tl!("Sample All Layers"), sample_all, None); Divider::vertical().show(ui); - let _ = option_cells::number(tl!("Opacity:"), &mut o.magic_eraser_opacity, 1.0..=100.0).unit("%").show(ui); + let _ = option_cells::number(tl!("Opacity:"), bind_tracked!(app.ui.tool_options => magic_eraser_opacity), 1.0..=100.0).unit("%").show(ui); } fn background_eraser_members(ui: &mut egui::Ui, app: &mut PhotocraftApp) { let mut alerts = Vec::new(); { - let o = &mut app.ui.tool_options; let sampling = [ Entry::new("continuous".to_string(), tl!("Sampling: Continuous")).icon(IconId::new("eyedropper")), Entry::new("once".to_string(), tl!("Sampling: Once")).icon(IconId::new("target")), Entry::new("backgroundSwatch".to_string(), tl!("Sampling: Background Swatch")).icon(IconId::new("square")), ]; - let picked = Segmented::new(tl!("Sampling"), Bind::given(o.bg_sampling.clone()), &sampling).show(ui).value().cloned(); + let picked = Segmented::new(tl!("Sampling"), Bind::given(app.ui.tool_options.bg_sampling.clone()), &sampling).show(ui).value().cloned(); if let Some(sampling) = picked { - o.bg_sampling = sampling; + app.ui.tool_options.bg_sampling = sampling; } Divider::vertical().show(ui); @@ -201,7 +200,7 @@ fn background_eraser_members(ui: &mut egui::Ui, app: &mut PhotocraftApp) { // In a `Field`, as Color Replacement's Limits: a bare control's label only names it (#16338). let picked = Field::new(tl!("Limits:")) .show(ui, |ui, _| { - Select::new(tl!("Limits:"), Bind::given(o.bg_limits.clone()), &limits) + Select::new(tl!("Limits:"), Bind::given(app.ui.tool_options.bg_limits.clone()), &limits) .tooltip(tl!("How far to let the erasing spread").to_owned()) .show(ui) .events @@ -211,20 +210,20 @@ fn background_eraser_members(ui: &mut egui::Ui, app: &mut PhotocraftApp) { .inner .flatten(); if let Some(limits) = picked { - o.bg_limits = limits; + app.ui.tool_options.bg_limits = limits; } // Shown as the stroke reads it; written back only when the user edits it. - let shown = stored_tolerance(o.bg_tolerance); + let shown = stored_tolerance(app.ui.tool_options.bg_tolerance); let mut tolerance = shown; - let alert = option_cells::number(tl!("Tolerance:"), &mut tolerance, 1.0..=100.0) + let alert = option_cells::number(tl!("Tolerance:"), Bind::bound(&mut tolerance), 1.0..=100.0) .whole() .unit("%") .tooltip(tl!("Erase range of similar colors")) .show(ui) .out_of_range; if tolerance != shown { - o.bg_tolerance = tolerance; + app.ui.tool_options.bg_tolerance = tolerance; } alerts.extend(alert); } @@ -233,7 +232,7 @@ fn background_eraser_members(ui: &mut egui::Ui, app: &mut PhotocraftApp) { let before = (*app.session.tools.brush).clone(); let mut brush = before.clone(); let mut angle = brush.angle; - let angle_out = option_cells::number(tl!("Angle"), &mut angle, -180.0..=180.0) + let angle_out = option_cells::number(tl!("Angle"), Bind::bound(&mut angle), -180.0..=180.0) .whole() .unit("°") .icon(IconSpec::svg(IconId::new("rotate_cw"))) @@ -244,7 +243,7 @@ fn background_eraser_members(ui: &mut egui::Ui, app: &mut PhotocraftApp) { brush.angle = angle; alerts.extend(angle_out.out_of_range); - check(ui, tl!("Protect Foreground Color"), &mut app.ui.tool_options.bg_protect_fg, Some(tl!("Do not erase the foreground swatch color"))); + check(ui, tl!("Protect Foreground Color"), bind_tracked!(app.ui.tool_options => bg_protect_fg), Some(tl!("Do not erase the foreground swatch color"))); let pressure = ToggleButton::new(tl!("Always use pressure for size"), Bind::given(brush.pressure_size)) .icon(IconId::new("target")) diff --git a/crates/ui-egui/src/tools/eyedropper_ui.rs b/crates/ui-egui/src/tools/eyedropper_ui.rs index d9da168205..a745abf2a2 100644 --- a/crates/ui-egui/src/tools/eyedropper_ui.rs +++ b/crates/ui-egui/src/tools/eyedropper_ui.rs @@ -10,7 +10,11 @@ use crate::PhotocraftApp; /// The options bar for the Eyedropper tool. pub fn options(app: &mut PhotocraftApp, ui: &mut egui::Ui) { - EyedropperBar::new(&mut app.ui.tool_options.eyedropper).show(ui); + let mut options = app.ui.tool_options.eyedropper; + EyedropperBar::new(&mut options).show(ui); + if options != app.ui.tool_options.eyedropper { + app.ui.tool_options.eyedropper = options; + } } #[cfg(test)] diff --git a/crates/ui-egui/src/tools/gradient_ui/bar.rs b/crates/ui-egui/src/tools/gradient_ui/bar.rs index 0f9a30ed4e..2a389f1be8 100644 --- a/crates/ui-egui/src/tools/gradient_ui/bar.rs +++ b/crates/ui-egui/src/tools/gradient_ui/bar.rs @@ -3,7 +3,7 @@ use craft::draw::ui::gradient::{GradientFillWords, GradientStyle, GradientSwatch}; use craft::ui::choice::{Checkbox, Segmented}; -use craft::ui::contract::{Bind, Entry, LabelKind, Props as _, Show as _, ShowContainer as _, SideLabel}; +use craft::ui::contract::{Bind, Entry, LabelKind, Props as _, Show as _, ShowContainer as _, SideLabel, bind_tracked}; use craft::ui::display::Divider; use craft::ui::form::Field; use craft::ui::patterns::blend_keys::{BlendKeyEntry, blend_entries}; @@ -51,37 +51,40 @@ pub(crate) fn bar(app: &mut PhotocraftApp, ui: &mut egui::Ui) { let blends = blend_modes(); let open_editor; { - let o = &mut app.ui.tool_options; - let mut classic = o.gradient_classic && !locked_live; + let mut classic = app.ui.tool_options.gradient_classic && !locked_live; Field::new(tl!("Gradient mode").to_owned()).side(SideLabel::new().kind(LabelKind::Hidden)).show(ui, |ui, _| { Select::new(tl!("Gradient mode").to_owned(), Bind::bound(&mut classic), &modes).enabled(!locked_live).show(ui); }); - if !locked_live { - o.gradient_classic = classic; + if !locked_live && classic != app.ui.tool_options.gradient_classic { + app.ui.tool_options.gradient_classic = classic; } open_editor = GradientSwatch::new(tl!("Gradient picker").to_string(), &tool_stops).sampler(&sampler).show(ui).clicked; Divider::vertical().show(ui); - let style = stops::style_named(&o.gradient_style); + let style = stops::style_named(&app.ui.tool_options.gradient_style); if let Some(next) = Segmented::new(Label::from(tl!("Gradient style").to_string()), Bind::given(style), &styles).show(ui).value().copied() { - o.gradient_style = stops::style_name(next).to_owned(); + app.ui.tool_options.gradient_style = stops::style_name(next).to_owned(); } if classic { - labelled(ui, tl!("Mode"), |ui| { - Select::new(tl!("Blend Mode").to_owned(), Bind::bound(&mut o.gradient_blend_mode), &blends).show(ui); - }); + let fill_opacity = app.ui.tool_options.fill_opacity; + { + let mut blend = bind_tracked!(app.ui.tool_options => gradient_blend_mode); + labelled(ui, tl!("Mode"), |ui| { + Select::new(tl!("Blend Mode").to_owned(), blend.reborrowed(), &blends).show(ui); + }); + } labelled(ui, tl!("Opacity"), |ui| { let field = - NumberField::new(tl!("Opacity").to_owned(), Bind::given(Some(f64::from(o.fill_opacity)))).range(1.0..=100.0).unit("%").scrub_by_label(); + NumberField::new(tl!("Opacity").to_owned(), Bind::given(Some(f64::from(fill_opacity)))).range(1.0..=100.0).unit("%").scrub_by_label(); if let Some(next) = field.show(ui).value().copied().flatten() { - o.fill_opacity = next as f32; + app.ui.tool_options.fill_opacity = next as f32; } }); } - if let Some(on) = Checkbox::new(Bind::given(o.gradient_reverse)).label(tl!("Reverse").to_owned()).show(ui).value().copied() { - o.gradient_reverse = on; + if let Some(on) = Checkbox::new(Bind::given(app.ui.tool_options.gradient_reverse)).label(tl!("Reverse").to_owned()).show(ui).value().copied() { + app.ui.tool_options.gradient_reverse = on; } - if let Some(on) = Checkbox::new(Bind::given(o.gradient_dither)).label(tl!("Dither").to_owned()).show(ui).value().copied() { - o.gradient_dither = on; + if let Some(on) = Checkbox::new(Bind::given(app.ui.tool_options.gradient_dither)).label(tl!("Dither").to_owned()).show(ui).value().copied() { + app.ui.tool_options.gradient_dither = on; } } if open_editor { diff --git a/crates/ui-egui/src/tools/magnetic_lasso_ui.rs b/crates/ui-egui/src/tools/magnetic_lasso_ui.rs index e2a7bb9c9d..6d059cf504 100644 --- a/crates/ui-egui/src/tools/magnetic_lasso_ui.rs +++ b/crates/ui-egui/src/tools/magnetic_lasso_ui.rs @@ -68,7 +68,9 @@ pub(crate) fn prepare(app: &PhotocraftApp, tool: &mut MagneticLassoTool /// Writes the tool's settings back after the run, so `[` and `]` reach the options store. pub(crate) fn store(app: &mut PhotocraftApp, tool: &MagneticLassoTool) { - app.ui.tool_options.magnetic = *tool.settings(); + if app.ui.tool_options.magnetic != *tool.settings() { + app.ui.tool_options.magnetic = *tool.settings(); + } } /// Selects the closed border as drawn, in `mode` (the family's operation-to-mode name). Fewer than diff --git a/crates/ui-egui/src/tools/option_cells.rs b/crates/ui-egui/src/tools/option_cells.rs index 159203d9c7..f0db5cd1f2 100644 --- a/crates/ui-egui/src/tools/option_cells.rs +++ b/crates/ui-egui/src/tools/option_cells.rs @@ -18,7 +18,7 @@ use crate::PhotocraftApp; /// commits, so showing a bar never rounds a fraction another tool shares. pub(crate) struct NumberCell<'a> { caption: Label, - value: &'a mut f32, + value: Bind<'a, f32>, range: RangeInclusive, unit: Option