From 15949962fb5730ac6f2a0b3ffcab4c90f6fd713d Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Sat, 5 Dec 2026 14:18:05 -0800 Subject: [PATCH 1/2] Swatches: Shift+F10, the Menu key or a screen reader sets the stroke from a Swatch Strip chip A right-click or an Alt-click on a Swatch Strip chip sets the stroke, but keyboard and screen-reader users had no way to do the same. The strip now reads the swatch's context request (SwatchOutput::context), so Shift+F10, or the Menu key off macOS, on the focused chip sets the stroke as a right-click does. Each chip offers AccessKit's ShowContextMenu action, which sets the stroke too. The swatch's request also counts any right-button release over it, wherever the press began. The strip keeps its own press tracking for the pointer, so a right-drag from elsewhere, a press on a layer over the chip or a release in another document still sets nothing, and it takes the context request only in a frame without a right-button release. Each chip's control is now keyed by its document and the paint it sends rather than by its place in the list, so a click whose press lands on one swatch sets that swatch even when the list reorders before the release, and a release after the active document changes sets nothing there. The panel test harness gains offers_context_menu and show_context_menu for a control's AccessKit context menu action. --- crates/ui-egui/src/panels/swatch_strip.rs | 98 ++++++++++++++++++++--- crates/ui-egui/src/tests/panel_harness.rs | 21 +++++ docs/control-protocol.md | 5 +- 3 files changed, 113 insertions(+), 11 deletions(-) diff --git a/crates/ui-egui/src/panels/swatch_strip.rs b/crates/ui-egui/src/panels/swatch_strip.rs index c91b5b3b7a..720b41163e 100644 --- a/crates/ui-egui/src/panels/swatch_strip.rs +++ b/crates/ui-egui/src/panels/swatch_strip.rs @@ -4,11 +4,14 @@ //! across in one line; in a dock column or a narrow floating window they wrap down the column, and scroll when they //! do not fit. //! -//! A click on a chip sets the fill (`paint.setFill`); a right-click or an Alt-click sets the stroke -//! (`paint.setStroke`). Neither changes which of the Fill and Stroke proxies is active. +//! A click on a chip, or Space or Enter on the focused chip, sets the fill (`paint.setFill`); a right-click, an +//! Alt-click, Shift+F10 (or the Menu key off macOS) on the focused chip, or a screen reader's context menu action +//! sets the stroke (`paint.setStroke`). +//! Neither changes which of the Fill and Stroke proxies is active. use craft::theme::Srgba; use craft::ui::choice::Swatch; +use craft::ui::contract::overlay::offer_context_menu; use craft::ui::contract::{Props as _, Scroll, Show as _, ShowContainer as _}; use craft::ui::layout_components::{Row, Stack}; use egui::{Event, PointerButton, Rect, Ui}; @@ -56,33 +59,42 @@ pub fn show(app: &mut VectorcraftApp, ui: &mut Ui) { return; } let chips = chips(app); + // Each chip is keyed by its document and the paint it sends, so a swatch that moves in the list between a press + // and its release keeps its control, and a release in another document reaches none of this one's chips. + let uid = app.session.active().map_or(0, |st| st.uid); + let keys: Vec = chips.iter().map(|c| format!("{uid}:{}", c.params)).collect(); let mut picked = None; + // A secondary-button release in this frame: the chips' context requests then come from the pointer, which the + // press tracking below handles, rather than from Shift+F10, the Menu key or a screen reader. + let secondary_release = + ui.input(|i| i.events.iter().any(|e| matches!(e, Event::PointerButton { button: PointerButton::Secondary, pressed: false, .. }))); // Where each chip shows (clipped to the scroll area), for placing the frame's right-button events. // A sizing pass draws the chips first; the drawing pass after it writes over its rects. let mut hits: Vec> = vec![None; chips.len()]; Stack::new().id_salt(ID).fill().scroll(Scroll::vertical()).show(ui, |ui, _| { Row::new().fill_width().wrap(true).show(ui, |ui, _| { - for (at, chip) in chips.iter().enumerate() { + for (at, (chip, key)) in chips.iter().zip(&keys).enumerate() { let swatch = match chip.color { Some(color) => Swatch::new(chip.name.clone(), color), None => Swatch::optional(chip.name.clone(), None::), }; - let out = swatch.id_salt(at).tooltip(chip.name.clone()).show(ui); + let out = swatch.id_salt(key).tooltip(chip.name.clone()).show(ui); + // A screen reader's context menu action on the chip sets the stroke, as a right-click does. + offer_context_menu(ui.ctx(), out.response.id); if let Some(hit) = hits.get_mut(at) { *hit = Some(out.response.rect.intersect(ui.clip_rect())); } if out.clicked { picked = Some((at, out.activation.is_some_and(|a| a.mods.alt))); + } else if out.context && !secondary_release { + picked = Some((at, true)); } } }); }); - // A right-click sets the stroke when its press and its release both land on the same chip. + // A right-click sets the stroke when its press and its release both land on the same chip. The swatch's context + // request counts any secondary release over it, wherever the press began, so the strip tracks the press itself. let press_id = ui.scope_id().with((ID, "secondary-press")); - // The chip is kept by its document and the paint it sends, so a swatch that moves in the list between press and - // release still counts, and a release in another document does not. - let uid = app.session.active().map_or(0, |st| st.uid); - let keys: Vec = chips.iter().map(|c| format!("{uid}:{}", c.params)).collect(); let laid_out: Vec<(Rect, String)> = hits.iter().zip(&keys).filter_map(|(r, k)| r.map(|r| (r, k.clone()))).collect(); // A container's measuring run shows the strip in an invisible sizing pass with the same id and input before the // frame's real run; it leaves the remembered press alone, so the real run's release still finds it. @@ -174,6 +186,32 @@ mod tests { assert_eq!(panel.app().session.fill_active, fill_active, "the active proxy stays as it was"); } + #[test] + fn shift_f10_on_the_focused_chip_sets_the_stroke() { + let mut panel = Panel::new(app(), show); + let before = paints(&panel); + panel.focus("Strip Red"); + assert!(panel.focused("Strip Red"), "the chip takes the focus"); + assert_eq!(paints(&panel), before, "focus alone sets nothing"); + + panel.key(egui::Key::F10, Modifiers::SHIFT); + panel.frame(); + let (fill, stroke) = paints(&panel); + assert_eq!(swatch_of(&stroke).as_deref(), Some("Strip Red"), "Shift+F10 sets the stroke"); + assert_eq!(fill, before.0, "Shift+F10 leaves the fill"); + } + + #[test] + fn a_screen_readers_context_menu_action_sets_the_stroke() { + let mut panel = Panel::new(app(), show); + assert!(panel.offers_context_menu("Strip Green"), "the chip offers the context menu action"); + let before = paints(&panel).0; + panel.show_context_menu("Strip Green"); + let (fill, stroke) = paints(&panel); + assert_eq!(swatch_of(&stroke).as_deref(), Some("Strip Green"), "the action sets the stroke"); + assert_eq!(fill, before, "the action leaves the fill"); + } + #[test] fn a_right_drag_from_outside_onto_a_chip_leaves_the_stroke() { let mut panel = Panel::new(app(), show); @@ -219,6 +257,40 @@ mod tests { assert_eq!(swatch_of(&paints(&panel).1).as_deref(), Some("Strip Red"), "the release on the same swatch sets the stroke"); } + #[test] + fn a_click_counts_for_the_swatch_pressed_when_another_moves_in_under_it() { + let mut panel = Panel::new(app(), show); + let from = panel.rect("Strip Red").center(); + let button = |pressed| egui::Event::PointerButton { pos: from, button: egui::PointerButton::Primary, pressed, modifiers: Modifiers::NONE }; + panel.frame_with(vec![egui::Event::PointerMoved(from)]); + panel.frame_with(vec![button(true)]); + panel.app().run("swatch.move", json!({"names": ["Strip Blue"], "to": 0})).unwrap(); + panel.frame(); + assert!(!panel.rect("Strip Red").contains(from), "the move takes the pressed chip from under the pointer"); + panel.frame_with(vec![button(false)]); + panel.frame(); + assert_eq!(swatch_of(&paints(&panel).0).as_deref(), Some("Strip Red"), "the click sets the pressed swatch, not the one that moved in"); + } + + #[test] + fn a_click_released_in_another_document_leaves_its_fill() { + let mut app = app(); + app.run("file.new", json!({"width": 200, "height": 200})).unwrap(); + app.run("swatch.new", json!({"name": "Strip Green", "color": "#22aa44", "global": true})).unwrap(); + app.run("shape.rectangle", json!({"x": 10, "y": 10, "width": 50, "height": 50})).unwrap(); + let mut panel = Panel::new(app, show); + let at = panel.rect("Strip Green").center(); + let button = |pressed| egui::Event::PointerButton { pos: at, button: egui::PointerButton::Primary, pressed, modifiers: Modifiers::NONE }; + panel.frame_with(vec![egui::Event::PointerMoved(at)]); + panel.frame_with(vec![button(true)]); + assert!(panel.app().session.set_active(0), "the first document becomes active"); + panel.frame(); + let before = paints(&panel).0; + panel.frame_with(vec![button(false)]); + panel.frame(); + assert_eq!(paints(&panel).0, before, "a press in one document and a release in another set nothing"); + } + #[test] fn a_right_click_in_the_frame_the_strip_remeasures_still_sets_the_stroke() { let mut panel = Panel::new(app(), show); @@ -274,6 +346,14 @@ mod tests { panel.frame_with(vec![button(false)]); panel.frame(); assert_eq!(swatch_of(&paints(&panel).1).as_deref(), Some("Strip Green"), "the shown strip's right-click sets the stroke"); + + let steps = |panel: &Panel| panel.app().session.active().map_or(0, |st| st.history.undo.len()); + let before = steps(&panel); + panel.focus("Strip Blue"); + panel.key(egui::Key::F10, Modifiers::SHIFT); + panel.frame(); + assert_eq!(swatch_of(&paints(&panel).1).as_deref(), Some("Strip Blue"), "Shift+F10 on the shown strip sets the stroke"); + assert_eq!(steps(&panel), before + 1, "the sizing pass and the shown strip set it once between them"); } thread_local! { diff --git a/crates/ui-egui/src/tests/panel_harness.rs b/crates/ui-egui/src/tests/panel_harness.rs index e8c2ca4c21..fdd6924d86 100644 --- a/crates/ui-egui/src/tests/panel_harness.rs +++ b/crates/ui-egui/src/tests/panel_harness.rs @@ -307,6 +307,27 @@ impl Panel { self.after(); } + /// Whether the control named `name` offers a screen reader its context menu (AccessKit `ShowContextMenu`). + pub(crate) fn offers_context_menu(&mut self, name: &str) -> bool { + let target = self.find(name); + let id = egui::accesskit::NodeId(target.node.raw()); + let tree = self.fixture.tree(); + tree.update().nodes.iter().any(|(node, n)| *node == id && n.supports_action(egui::accesskit::Action::ShowContextMenu)) + } + + /// A screen reader's `ShowContextMenu` on the control named `name`. + pub(crate) fn show_context_menu(&mut self, name: &str) { + let target = self.find(name); + let request = egui::accesskit::ActionRequest { + action: egui::accesskit::Action::ShowContextMenu, + target_tree: egui::accesskit::TreeId::ROOT, + target_node: egui::accesskit::NodeId(target.node.raw()), + data: None, + }; + self.frame_with(vec![Event::AccessKitActionRequest(request)]); + self.after(); + } + /// Turns the wheel by `dy` points over `at`. pub(crate) fn scroll_at(&mut self, at: Pos2, dy: f32) { self.frame_with(vec![Event::PointerMoved(at)]); diff --git a/docs/control-protocol.md b/docs/control-protocol.md index 2e62cd2de8..e40422e645 100644 --- a/docs/control-protocol.md +++ b/docs/control-protocol.md @@ -293,8 +293,9 @@ Swatch Strip panel (`window.panel {panel: "swatchStrip"}`): a None chip, then th swatches outside any color group, then each group's; gradient and pattern swatches are left out), in a row of chips that wraps to the panel's room and scrolls when they do not fit. `window.panel.dock {panel: "swatchStrip", anchor: "canvas", placement: {"Edge": "Bottom"}}` docks it along the window's bottom edge, where the chips run across in one -line. A click on a chip runs `paint.setFill {swatch}` (`{none: true}` for None) and a right-click or an Alt-click -runs `paint.setStroke`, both with `focus: false`, so the active proxy stays as it was. +line. A click on a chip runs `paint.setFill {swatch}` (`{none: true}` for None) and a right-click, an Alt-click, +Shift+F10 or the Menu key on the focused chip, or a screen reader's context menu action runs `paint.setStroke`, both +with `focus: false`, so the active proxy stays as it was. Color Themes panel (`window.panel {panel: "colorThemes"}`): its Create tab makes a theme on a harmony wheel and its My Themes tab lists the saved themes; every action runs a command (`colorTheme.save`, `colorTheme.delete`, From 9f6e817063812d91665e2dc300b94d1a340a64e7 Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Sat, 5 Dec 2026 16:47:26 -0800 Subject: [PATCH 2/2] Swatches: two swatches with one name keep a Swatch Strip chip each Each chip's control was keyed by its document and the paint it sends, so two swatches with the same name (which an opened file can hold) gave two chips one control, sharing hover, press, focus and one accessibility node. Chips that send the same paint are now numbered in list order, so each keeps a control of its own; distinct swatches keep their keys. docs/control-protocol.md now says the Menu key on a focused chip works off macOS only. --- crates/ui-egui/src/panels/swatch_strip.rs | 26 +++++++++++++++++++++-- crates/ui-egui/src/tests/panel_harness.rs | 5 +++++ docs/control-protocol.md | 2 +- 3 files changed, 30 insertions(+), 3 deletions(-) diff --git a/crates/ui-egui/src/panels/swatch_strip.rs b/crates/ui-egui/src/panels/swatch_strip.rs index 720b41163e..8c0fb5cf4a 100644 --- a/crates/ui-egui/src/panels/swatch_strip.rs +++ b/crates/ui-egui/src/panels/swatch_strip.rs @@ -60,9 +60,21 @@ pub fn show(app: &mut VectorcraftApp, ui: &mut Ui) { } let chips = chips(app); // Each chip is keyed by its document and the paint it sends, so a swatch that moves in the list between a press - // and its release keeps its control, and a release in another document reaches none of this one's chips. + // and its release keeps its control, and a release in another document reaches none of this one's chips. Chips + // that send the same paint (two swatches with one name, which an opened file can hold) are numbered in list order, + // so each keeps a control of its own. let uid = app.session.active().map_or(0, |st| st.uid); - let keys: Vec = chips.iter().map(|c| format!("{uid}:{}", c.params)).collect(); + let mut seen = std::collections::HashMap::::new(); + let keys: Vec = chips + .iter() + .map(|c| { + let paint = c.params.to_string(); + let n = seen.entry(paint.clone()).or_default(); + let key = format!("{uid}:{paint}:{n}"); + *n += 1; + key + }) + .collect(); let mut picked = None; // A secondary-button release in this frame: the chips' context requests then come from the pointer, which the // press tracking below handles, rather than from Shift+F10, the Menu key or a screen reader. @@ -414,6 +426,16 @@ mod tests { assert!(!panel.shows("Strip Ramp"), "a gradient swatch has none"); } + #[test] + fn two_swatches_with_one_name_keep_a_chip_each() { + let mut app = app(); + let st = app.session.doc_mut().expect("a document"); + let green = std::sync::Arc::make_mut(&mut st.doc).swatch_mut("Strip Green").expect("the green swatch"); + green.name = "Strip Red".to_owned(); + let panel = Panel::new(app, show); + assert_eq!(panel.count_named("Strip Red"), 2, "each of the two swatches named Strip Red has a control of its own"); + } + #[test] fn the_chips_run_along_a_wide_strip_and_wrap_down_a_narrow_one() { let mut panel = Panel::new(app(), show); diff --git a/crates/ui-egui/src/tests/panel_harness.rs b/crates/ui-egui/src/tests/panel_harness.rs index fdd6924d86..c549c50d46 100644 --- a/crates/ui-egui/src/tests/panel_harness.rs +++ b/crates/ui-egui/src/tests/panel_harness.rs @@ -406,6 +406,11 @@ impl Panel { self.fixture.advance(seconds); } + /// How many nodes the latest frame shows named `name`. + pub(crate) fn count_named(&self, name: &str) -> usize { + self.names().iter().filter(|n| *n == name).count() + } + /// Whether the latest frame shows a control named `name`. pub(crate) fn shows(&self, name: &str) -> bool { self.fixture.by_label(name).is_some() diff --git a/docs/control-protocol.md b/docs/control-protocol.md index e40422e645..9e7a300231 100644 --- a/docs/control-protocol.md +++ b/docs/control-protocol.md @@ -294,7 +294,7 @@ swatches outside any color group, then each group's; gradient and pattern swatch that wraps to the panel's room and scrolls when they do not fit. `window.panel.dock {panel: "swatchStrip", anchor: "canvas", placement: {"Edge": "Bottom"}}` docks it along the window's bottom edge, where the chips run across in one line. A click on a chip runs `paint.setFill {swatch}` (`{none: true}` for None) and a right-click, an Alt-click, -Shift+F10 or the Menu key on the focused chip, or a screen reader's context menu action runs `paint.setStroke`, both +Shift+F10 (or the Menu key off macOS) on the focused chip, or a screen reader's context menu action runs `paint.setStroke`, both with `focus: false`, so the active proxy stays as it was. Color Themes panel (`window.panel {panel: "colorThemes"}`): its Create tab makes a theme on a harmony wheel and its