From 893f0db16a4090c1c67d5aaaa01b6ef16a77e29e Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Sat, 28 Nov 2026 13:53:53 -0800 Subject: [PATCH] UI: a highlighted Blend Mode, Library or font Style applies only once it is chosen The Transparency panel's Blend Mode, the Libraries panel's Library and the font fields' Style select are bound to a value the code compares with the document after each pass. Under the default write-through policy, an entry highlighted with the arrow keys was written to that value, so the code ran transparency.set, library.setCurrent or the font command before the entry was chosen, and Escape ran it again to put the value back. Each binding now writes only on commit (EditPolicy::OnCommit), so Enter or a click applies the entry. --- crates/ui-egui/src/font_menu.rs | 4 +- crates/ui-egui/src/font_menu/tests.rs | 40 ++++++++++++++++++++ crates/ui-egui/src/panels/libraries.rs | 4 +- crates/ui-egui/src/panels/libraries/tests.rs | 27 +++++++++++++ crates/ui-egui/src/panels/transparency.rs | 33 +++++++++++++++- 5 files changed, 103 insertions(+), 5 deletions(-) diff --git a/crates/ui-egui/src/font_menu.rs b/crates/ui-egui/src/font_menu.rs index d3ac851220..bc7d206428 100644 --- a/crates/ui-egui/src/font_menu.rs +++ b/crates/ui-egui/src/font_menu.rs @@ -16,7 +16,7 @@ use std::hash::{Hash, Hasher}; use std::sync::Arc; use std::time::SystemTime; -use craft::behavior::{EditEvent, Preview}; +use craft::behavior::{EditEvent, EditPolicy, Preview}; use craft::font_manager::picker::{FamilySet, FilterLayout, FontFilters, FontPicker, FontPickerSettings, FontPreviewSize, FontScript}; use craft::ui::contract::{Bind, Entry, Show as _}; use craft::ui::fonts::{FaceBytes, FaceState}; @@ -204,7 +204,7 @@ pub(crate) fn font_fields(app: &mut VectorcraftApp, ui: &mut Ui) { let styles = FontDb::global().styles(&s.font_family); let entries: Vec> = styles.iter().map(|name| Entry::new(name.clone(), name.clone())).collect(); let mut shown = s.font_style.clone(); - Select::new(tl!("Style"), Bind::bound(&mut shown), &entries).show(ui); + Select::new(tl!("Style"), Bind::bound(&mut shown).policy(EditPolicy::OnCommit), &entries).show(ui); if shown != s.font_style { apply_font(app, &s.font_family, Some(&shown), Live::Released); } diff --git a/crates/ui-egui/src/font_menu/tests.rs b/crates/ui-egui/src/font_menu/tests.rs index cd0362187a..333b56e123 100644 --- a/crates/ui-egui/src/font_menu/tests.rs +++ b/crates/ui-egui/src/font_menu/tests.rs @@ -270,3 +270,43 @@ fn a_preview_size_asked_for_sets_the_preference_and_keeps_the_preview() { "undo takes the type away: no step was added for the size" ); } + +#[test] +fn a_highlighted_font_style_applies_only_once_it_is_chosen() { + use craft::ui::contract::Role; + use egui::{Key, Modifiers}; + + use crate::tests::panel_harness::Panel; + + let styles = vectorcraft_text::FontDb::global().styles("Source Sans 3"); + assert!(styles.len() >= 2, "{styles:?}"); + let mut app = app_with_type(&[]); + let params = serde_json::json!({"x": 20, "y": 90, "text": "Style", "font": "Source Sans 3", "style": styles[0]}); + let id = app.run("text.create", params).unwrap()["id"].as_u64().unwrap(); + app.run("select.set", serde_json::json!({"ids": [id]})).unwrap(); + let mut panel = Panel::new(app, super::font_fields); + let state = |panel: &Panel| { + let app = panel.app(); + let style = match &app.session.active().unwrap().doc.node(vectorcraft_doc::NodeId(id)).unwrap().kind { + vectorcraft_doc::NodeKind::Text(t) => t.first_style().font_style, + _ => String::new(), + }; + (style, app.session.doc().unwrap().history.undo.len()) + }; + let before = state(&panel); + assert_eq!(before.0, styles[0]); + panel.click_role("Style", Role::COMBO_BOX); + panel.key(Key::ArrowDown, Modifiers::NONE); + panel.frame(); + assert_eq!(state(&panel), before, "a highlighted style changes nothing"); + panel.key(Key::Escape, Modifiers::NONE); + panel.frame(); + assert_eq!(state(&panel), before, "Escape leaves the style as it was"); + panel.click_role("Style", Role::COMBO_BOX); + panel.key(Key::ArrowDown, Modifiers::NONE); + panel.key(Key::Enter, Modifiers::NONE); + panel.frame(); + let (style, undo) = state(&panel); + assert_eq!(style, styles[1], "Enter applies the style"); + assert_eq!(undo, before.1 + 1, "the choice is one undo step"); +} diff --git a/crates/ui-egui/src/panels/libraries.rs b/crates/ui-egui/src/panels/libraries.rs index 0a1ff63e12..62e1da73ca 100644 --- a/crates/ui-egui/src/panels/libraries.rs +++ b/crates/ui-egui/src/panels/libraries.rs @@ -14,7 +14,7 @@ use std::cell::RefCell; use std::collections::{HashMap, HashSet}; use std::sync::Arc; -use craft::behavior::{Axis, EditEvent, IconId, keyed}; +use craft::behavior::{Axis, EditEvent, EditPolicy, IconId, keyed}; use craft::host::PickRequest; use craft::keymap::CommandRef; use craft::theme::variant::Tone; @@ -843,7 +843,7 @@ fn header(ui: &mut Ui, libraries: &Libraries, current: &str, list_view: &mut boo ]; let mut picked = current.to_string(); Row::new().fill_width().show(ui, |ui, _| { - Select::new(tl!("Library"), Bind::bound(&mut picked), &names).sizing(Sizing::FillWidth).show(ui); + Select::new(tl!("Library"), Bind::bound(&mut picked).policy(EditPolicy::OnCommit), &names).sizing(Sizing::FillWidth).show(ui); Segmented::new(tl!("View"), Bind::bound(&mut *list_view), &views).show(ui); }); if picked != current { diff --git a/crates/ui-egui/src/panels/libraries/tests.rs b/crates/ui-egui/src/panels/libraries/tests.rs index b730aab1aa..9af1be9d75 100644 --- a/crates/ui-egui/src/panels/libraries/tests.rs +++ b/crates/ui-egui/src/panels/libraries/tests.rs @@ -566,6 +566,33 @@ fn menu_rows_and_dialogs_run_library_commands() { } } +#[test] +fn a_highlighted_library_becomes_current_only_once_it_is_chosen() { + use craft::ui::contract::Role; + + let mut app = app(); + for name in ["Brand", "House"] { + app.run("library.create", json!({"name": name})).unwrap(); + } + let ids: Vec = app.session.libraries.all().iter().map(|(id, _)| id.clone()).collect(); + app.run("library.setCurrent", json!({"library": ids[0]})).unwrap(); + let mut panel = Harness::new(app, show); + let current = |panel: &Harness| panel.app().session.libraries.current().map(|id| id.to_string()); + assert_eq!(current(&panel).as_deref(), Some(ids[0].as_str())); + panel.click_role("Library", Role::COMBO_BOX); + panel.key(Key::ArrowDown, Modifiers::NONE); + panel.frame(); + assert_eq!(current(&panel).as_deref(), Some(ids[0].as_str()), "a highlighted library does not become current"); + panel.key(Key::Escape, Modifiers::NONE); + panel.frame(); + assert_eq!(current(&panel).as_deref(), Some(ids[0].as_str()), "Escape leaves the current library as it was"); + panel.click_role("Library", Role::COMBO_BOX); + panel.key(Key::ArrowDown, Modifiers::NONE); + panel.key(Key::Enter, Modifiers::NONE); + panel.frame(); + assert_eq!(current(&panel).as_deref(), Some(ids[1].as_str()), "Enter makes the chosen library current"); +} + /// Export Library… writes the library's file under its name and Import Library… adds it back as a /// new library. #[cfg(not(target_arch = "wasm32"))] diff --git a/crates/ui-egui/src/panels/transparency.rs b/crates/ui-egui/src/panels/transparency.rs index 4f3d67fd06..3d5579e40f 100644 --- a/crates/ui-egui/src/panels/transparency.rs +++ b/crates/ui-egui/src/panels/transparency.rs @@ -7,6 +7,7 @@ use std::cell::RefCell; use std::sync::Arc; +use craft::behavior::EditPolicy; use craft::behavior::IconId; use craft::behavior::Mixed; use craft::behavior::item_source::keyed; @@ -245,7 +246,7 @@ pub fn show(app: &mut VectorcraftApp, ui: &mut Ui) { let entries = blend_entries(); let mut opacity = None; Row::new().show(ui, |ui, _| { - Select::new(tl!("Blend Mode"), Bind::bound_mixed(&mut shown_blend), &entries).enabled(has).show(ui); + Select::new(tl!("Blend Mode"), Bind::bound_mixed(&mut shown_blend).policy(EditPolicy::OnCommit), &entries).enabled(has).show(ui); opacity = Some( NumberField::new(tl!("Opacity"), Bind::given(op.map(|o| f64::from(o) * 100.0))) .unit_kind(UnitKind::Percent) @@ -692,4 +693,34 @@ mod tests { assert_eq!((info.opacity, undone.opacity), (Some(100.0), 1.0)); assert!(Arc::ptr_eq(&undone, &held(&app, n.id)), "the node Undo put back"); } + + #[test] + fn a_highlighted_blend_mode_applies_only_once_it_is_chosen() { + use egui::{Key, Modifiers}; + + let mut app = new_app(); + let id = run_cmd(&mut app, "shape.rectangle", json!({"x": 10, "y": 10, "width": 40, "height": 40}))["id"].clone(); + run_cmd(&mut app, "select.set", json!({"ids": [id]})); + let mut panel = Panel::new(app, show); + let state = |panel: &Panel| { + let app = panel.app(); + (app.session.transparency_info().blend, app.session.active().unwrap().history.undo.len()) + }; + let before = state(&panel); + assert_eq!(before.0, Some(vectorcraft_color::BlendMode::Normal)); + panel.click_role("Blend Mode", Role::COMBO_BOX); + panel.key(Key::ArrowDown, Modifiers::NONE); + panel.frame(); + assert_eq!(state(&panel), before, "a highlighted blend mode changes nothing"); + panel.key(Key::Escape, Modifiers::NONE); + panel.frame(); + assert_eq!(state(&panel), before, "Escape leaves the blend mode as it was"); + panel.click_role("Blend Mode", Role::COMBO_BOX); + panel.key(Key::ArrowDown, Modifiers::NONE); + panel.key(Key::Enter, Modifiers::NONE); + panel.frame(); + let (blend, undo) = state(&panel); + assert_ne!(blend, before.0, "Enter applies the blend mode"); + assert_eq!(undo, before.1 + 1, "the choice is one undo step"); + } }