Merge pull request #11004 from dotCooCoo/fix/swatch-strip-context-menu

Swatches: Shift+F10, the Menu key or a screen reader sets the stroke from a Swatch Strip chip
This commit is contained in:
.CooCoo authored and GitHub committed 2026-12-05 19:35:22 -08:00
commit 6123cd0e10
3 files changed
+140 -11

No files matched your search

+111 -9
View File
@@ -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,54 @@ 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. 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 mut seen = std::collections::HashMap::<String, usize>::new();
let keys: Vec<String> = 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.
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<Option<Rect>> = 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::<Srgba>),
};
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<String> = 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 +198,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 +269,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 +358,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! {
@@ -334,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);
+26
View File
@@ -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)]);
@@ -385,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()
+3 -2
View File
@@ -295,8 +295,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 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
My Themes tab lists the saved themes; every action runs a command (`colorTheme.save`, `colorTheme.delete`,