mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-12-05 19:51:26 -05:00
Swatch Strip: a right-click needs its press and release on the same chip
A right-button release on a chip set the stroke even when the press began elsewhere, so a right-drag from empty space onto a chip changed the stroke. The strip now remembers the chip (by document and paint) that a right press landed on and sets the stroke only when the release lands on that chip. Presses on a layer covering a chip, and the invisible sizing pass of a measuring container, do not count, and a chip that moves in the list between press and release still does.
This commit is contained in:
@@ -11,7 +11,7 @@ use craft::theme::Srgba;
|
||||
use craft::ui::choice::Swatch;
|
||||
use craft::ui::contract::{Props as _, Scroll, Show as _, ShowContainer as _};
|
||||
use craft::ui::layout_components::{Row, Stack};
|
||||
use egui::{PointerButton, Ui};
|
||||
use egui::{Event, PointerButton, Rect, Ui};
|
||||
use serde_json::{Value, json};
|
||||
use vectorcraft_color::Paint;
|
||||
|
||||
@@ -57,6 +57,9 @@ pub fn show(app: &mut VectorcraftApp, ui: &mut Ui) {
|
||||
}
|
||||
let chips = chips(app);
|
||||
let mut picked = None;
|
||||
// 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() {
|
||||
@@ -65,14 +68,42 @@ pub fn show(app: &mut VectorcraftApp, ui: &mut Ui) {
|
||||
None => Swatch::optional(chip.name.clone(), None::<Srgba>),
|
||||
};
|
||||
let out = swatch.id_salt(at).tooltip(chip.name.clone()).show(ui);
|
||||
// A right-click ends on the chip, as the framework's swatch palette reads one.
|
||||
let secondary = out.response.contains_pointer() && ui.input(|i| i.pointer.button_released(PointerButton::Secondary));
|
||||
if out.clicked || secondary {
|
||||
picked = Some((at, secondary || out.activation.is_some_and(|a| a.mods.alt)));
|
||||
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)));
|
||||
}
|
||||
}
|
||||
});
|
||||
});
|
||||
// A right-click sets the stroke when its press and its release both land on the same chip.
|
||||
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.
|
||||
if !ui.is_sizing_pass() {
|
||||
let mut pressed_on = ui.ctx().data(|d| d.get_temp::<Option<String>>(press_id)).flatten();
|
||||
for event in ui.input(|i| i.events.clone()) {
|
||||
if let Event::PointerButton { pos, button: PointerButton::Secondary, pressed, .. } = event {
|
||||
// A chip under another layer (a popup, a floating panel) does not take the event.
|
||||
let on_layer = ui.ctx().layer_id_at(pos) == Some(ui.layer_id());
|
||||
let key = if on_layer { laid_out.iter().find(|(r, _)| r.contains(pos)).map(|(_, k)| k.clone()) } else { None };
|
||||
if pressed {
|
||||
pressed_on = key;
|
||||
} else if let Some(k) = pressed_on.take()
|
||||
&& key.as_ref() == Some(&k)
|
||||
{
|
||||
picked = keys.iter().position(|x| *x == k).map(|at| (at, true));
|
||||
}
|
||||
}
|
||||
}
|
||||
ui.ctx().data_mut(|d| d.insert_temp(press_id, pressed_on));
|
||||
}
|
||||
if let Some((at, stroke)) = picked
|
||||
&& let Some(chip) = chips.get(at)
|
||||
{
|
||||
@@ -143,6 +174,145 @@ mod tests {
|
||||
assert_eq!(panel.app().session.fill_active, fill_active, "the active proxy stays as it was");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_right_drag_from_outside_onto_a_chip_leaves_the_stroke() {
|
||||
let mut panel = Panel::new(app(), show);
|
||||
let before = paints(&panel).1;
|
||||
let chip = panel.rect("Strip Green");
|
||||
let outside = egui::pos2(chip.center().x, chip.bottom() + chip.height() * 3.0);
|
||||
let button =
|
||||
|at, pressed| egui::Event::PointerButton { pos: at, button: egui::PointerButton::Secondary, pressed, modifiers: Modifiers::NONE };
|
||||
panel.frame_with(vec![egui::Event::PointerMoved(outside)]);
|
||||
panel.frame_with(vec![button(outside, true)]);
|
||||
panel.frame_with(vec![egui::Event::PointerMoved(chip.center())]);
|
||||
panel.frame_with(vec![button(chip.center(), false)]);
|
||||
panel.frame();
|
||||
assert_eq!(paints(&panel).1, before, "a right-button release on a chip whose press began elsewhere sets nothing");
|
||||
|
||||
panel.frame_with(vec![
|
||||
egui::Event::PointerMoved(outside),
|
||||
button(outside, true),
|
||||
egui::Event::PointerMoved(chip.center()),
|
||||
button(chip.center(), false),
|
||||
]);
|
||||
panel.frame();
|
||||
assert_eq!(paints(&panel).1, before, "the same gesture within one frame sets nothing either");
|
||||
|
||||
panel.secondary_click("Strip Green");
|
||||
assert_eq!(swatch_of(&paints(&panel).1).as_deref(), Some("Strip Green"), "a right-click on the chip still sets the stroke");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_right_click_counts_for_the_swatch_pressed_when_the_list_reorders_under_it() {
|
||||
let mut panel = Panel::new(app(), show);
|
||||
let from = panel.rect("Strip Red").center();
|
||||
let button =
|
||||
|at, pressed| egui::Event::PointerButton { pos: at, button: egui::PointerButton::Secondary, pressed, modifiers: Modifiers::NONE };
|
||||
panel.frame_with(vec![egui::Event::PointerMoved(from)]);
|
||||
panel.frame_with(vec![button(from, true)]);
|
||||
panel.app().run("swatch.move", json!({"names": ["Strip Blue"], "to": 0})).unwrap();
|
||||
panel.frame();
|
||||
let to = panel.rect("Strip Red").center();
|
||||
assert_ne!(from, to, "the move shifts the pressed chip: {from:?}");
|
||||
panel.frame_with(vec![egui::Event::PointerMoved(to), button(to, false)]);
|
||||
panel.frame();
|
||||
assert_eq!(swatch_of(&paints(&panel).1).as_deref(), Some("Strip Red"), "the release on the same swatch sets the stroke");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_right_click_in_the_frame_the_strip_remeasures_still_sets_the_stroke() {
|
||||
let mut panel = Panel::new(app(), show);
|
||||
panel.room(vec2(4000.0, 200.0)).frame();
|
||||
panel.frame();
|
||||
let at = panel.rect("Strip Green").center();
|
||||
let button = |pressed| egui::Event::PointerButton { pos: at, button: egui::PointerButton::Secondary, pressed, modifiers: Modifiers::NONE };
|
||||
panel.frame_with(vec![egui::Event::PointerMoved(at)]);
|
||||
// A new width makes the strip measure itself again in the frame that carries the press.
|
||||
panel.room(vec2(3000.0, 200.0));
|
||||
panel.frame_with(vec![button(true)]);
|
||||
panel.frame_with(vec![button(false)]);
|
||||
panel.frame();
|
||||
assert_eq!(swatch_of(&paints(&panel).1).as_deref(), Some("Strip Green"), "the right-click sets the stroke");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_right_release_in_another_document_leaves_its_stroke() {
|
||||
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 from = panel.rect("Strip Green").center();
|
||||
let button =
|
||||
|at, pressed| egui::Event::PointerButton { pos: at, button: egui::PointerButton::Secondary, pressed, modifiers: Modifiers::NONE };
|
||||
panel.frame_with(vec![egui::Event::PointerMoved(from)]);
|
||||
panel.frame_with(vec![button(from, true)]);
|
||||
assert!(panel.app().session.set_active(0), "the first document becomes active");
|
||||
panel.frame();
|
||||
let before = paints(&panel).1;
|
||||
let to = panel.rect("Strip Green").center();
|
||||
panel.frame_with(vec![egui::Event::PointerMoved(to), button(to, false)]);
|
||||
panel.frame();
|
||||
assert_eq!(paints(&panel).1, before, "a press in one document and a release in another set nothing");
|
||||
}
|
||||
|
||||
/// The strip measured first in an invisible sizing pass elsewhere on the screen, with the same scope id, then shown.
|
||||
fn measured(app: &mut VectorcraftApp, ui: &mut egui::Ui) {
|
||||
let away = ui.max_rect().translate(vec2(5000.0, 5000.0));
|
||||
let mut sizing = ui.new_child(egui::UiBuilder::new().scope_id(ui.scope_id()).max_rect(away).sizing_pass().invisible());
|
||||
show(app, &mut sizing);
|
||||
show(app, ui);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_sizing_pass_leaves_the_right_click_to_the_shown_strip() {
|
||||
let mut panel = Panel::new(app(), measured);
|
||||
let at = panel.rect("Strip Green").center();
|
||||
let button = |pressed| egui::Event::PointerButton { pos: at, button: egui::PointerButton::Secondary, pressed, modifiers: Modifiers::NONE };
|
||||
panel.frame_with(vec![egui::Event::PointerMoved(at)]);
|
||||
panel.frame_with(vec![button(true)]);
|
||||
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");
|
||||
}
|
||||
|
||||
thread_local! {
|
||||
/// Whether [`covered`] draws its cover.
|
||||
static COVER: std::cell::Cell<bool> = const { std::cell::Cell::new(true) };
|
||||
}
|
||||
|
||||
/// The strip with a foreground area covering the first 400 by 400 points while [`COVER`] is set.
|
||||
fn covered(app: &mut VectorcraftApp, ui: &mut egui::Ui) {
|
||||
show(app, ui);
|
||||
if COVER.with(|c| c.get()) {
|
||||
egui::Area::new(egui::Id::unique("strip-cover")).order(egui::Order::Foreground).fixed_pos(egui::pos2(0.0, 0.0)).show(ui.ctx(), |ui| {
|
||||
ui.allocate_exact_size(vec2(400.0, 400.0), egui::Sense::click());
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_right_press_on_a_layer_covering_a_chip_leaves_the_stroke() {
|
||||
COVER.with(|c| c.set(true));
|
||||
let mut panel = Panel::new(app(), covered);
|
||||
let before = paints(&panel).1;
|
||||
let at = panel.rect("Strip Green").center();
|
||||
assert!(at.x < 400.0 && at.y < 400.0, "the cover lies over the chip: {at:?}");
|
||||
let button = |pressed| egui::Event::PointerButton { pos: at, button: egui::PointerButton::Secondary, pressed, modifiers: Modifiers::NONE };
|
||||
panel.frame_with(vec![egui::Event::PointerMoved(at)]);
|
||||
panel.frame_with(vec![button(true)]);
|
||||
// The cover goes away while the button is down, so the release lands on the chip itself.
|
||||
COVER.with(|c| c.set(false));
|
||||
panel.frame();
|
||||
panel.frame();
|
||||
panel.frame_with(vec![button(false)]);
|
||||
panel.frame();
|
||||
assert_eq!(paints(&panel).1, before, "a press on the covering layer does not count as a press on the chip");
|
||||
|
||||
panel.secondary_click("Strip Green");
|
||||
assert_eq!(swatch_of(&paints(&panel).1).as_deref(), Some("Strip Green"), "with the cover gone a right-click reaches the chip");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_none_chip_comes_first_and_takes_the_paint_off() {
|
||||
let mut panel = Panel::new(app(), show);
|
||||
|
||||
Reference in New Issue
Block a user