Merge pull request #12145 from coqui-software/fix/color-sampler-cursors

fix(ui): show sampler target and scissors cursors over color samplers (#11744)
This commit is contained in:
David Mann authored and GitHub committed 2026-11-11 13:20:51 +01:00
commit ee4b55166f
5 files changed
+230 -7

No files matched your search

+14 -4
View File
@@ -67,7 +67,8 @@ pub mod zoom_window_fit;
#[cfg(test)]
pub(crate) use cursors::eyedropper_ring_colors;
pub(crate) use cursors::{
alt_picks, alt_samples, composite_color, paint_bucket_cursor, pen_cursor, pencil_cursor_rect, pencil_tip_is_round, pipette_cursor, precise_other_cursor,
alt_picks, alt_samples, color_sampler_cursor, composite_color, paint_bucket_cursor, pen_cursor, pencil_cursor_rect, pencil_tip_is_round, pipette_cursor,
precise_other_cursor, sampler_hover_cursor,
};
use cursors::{brush_tip_centre, eyedropper_ring, preview_locator_cursor, visible_crosshair};
pub(crate) use display::GPU_OUTPUT;
@@ -1954,12 +1955,18 @@ fn draw_default_tool_cursor(app: &mut PhotocraftApp, ui: &mut egui::Ui, input: &
let over = crate::analysis_ui::count_marker_at(app, xf.to_doc(p)).is_some();
crate::tool_cursor::count(ui.ctx(), p, crate::tool_cursor::CountCursor::pick(over, alt))
}
// The Color Sampler: a pipette, or the crosshair for precise Other Cursors.
// The Color Sampler (#11744):
// - Over a sampler: arrow with the sampler target, or with Alt the arrow with scissors.
// - Off a sampler: eyedropper with the sampler target at its top-left, or the crosshair
// for precise Other Cursors.
Tool::ColorSampler => {
if precise_other_cursor(app) {
let [x, y] = xf.to_doc(p);
if crate::analysis_ui::sampler_at(app, [x, y]).is_some() {
sampler_hover_cursor(ui.ctx(), p, alt || app.ui.shell.sticky_alt)
} else if precise_other_cursor(app) {
egui::CursorIcon::Crosshair
} else {
pipette_cursor(ui.ctx(), p)
color_sampler_cursor(ui.ctx(), p)
}
}
// The Paint Bucket: a bucket, or the crosshair for precise Other Cursors (#3968).
@@ -2629,6 +2636,9 @@ mod guide_ants_tests;
#[cfg(test)]
mod live_frame_tests;
#[cfg(test)]
mod sampler_cursor_tests;
#[cfg(test)]
mod rotation_preview_isolation_tests {
use super::*;
+31
View File
@@ -144,6 +144,37 @@ pub fn pen_cursor(ctx: &egui::Context, tool: Tool, p: Pos2) -> egui::CursorIcon
egui::CursorIcon::None
}
/// The Color Sampler's cursor over an existing sampler point: the arrow with the sampler target,
/// or with Alt the arrow with scissors (#11744).
pub(crate) fn sampler_hover_cursor(ctx: &egui::Context, p: Pos2, alt: bool) -> egui::CursorIcon {
if alt {
crate::icons::cursor_badge(ctx, "scissors", p);
} else {
crate::icons::cursor_badge(ctx, "sampler", p);
}
egui::CursorIcon::Default
}
/// The Color Sampler's cursor: an eyedropper with a small sampler target at its top-left,
/// hotspot at the pipette tip (#11744).
pub(crate) fn color_sampler_cursor(ctx: &egui::Context, p: Pos2) -> egui::CursorIcon {
pipette_cursor(ctx, p);
let size = 11.0;
// The pipette tip is at (2, 22) in its 24-unit box of size 20.0. The top-left region is
// around (5, 5), which is offset by (3, -17) * (20/24) = (2.5, -14.17) from the tip.
let target_center = p + vec2(2.5, -14.0);
let rect = Rect::from_center_size(target_center, egui::Vec2::splat(size));
let area = egui::Area::new(egui::Id::unique("pc-sampler-badge")).order(egui::Order::Tooltip).fixed_pos(rect.min).constrain(false).interactable(false);
area.show(ctx, |ui| {
let outline = crate::icons::image("sampler", size, Color32::from_black_alpha(200));
for (dx, dy) in [(-1.0, 0.0), (1.0, 0.0), (0.0, -1.0), (0.0, 1.0), (-1.0, -1.0), (1.0, 1.0), (-1.0, 1.0), (1.0, -1.0)] {
outline.paint_at(ui, rect.translate(vec2(dx, dy)));
}
crate::icons::image("sampler", size, Color32::WHITE).paint_at(ui, rect);
});
egui::CursorIcon::None
}
/// The sampling cursor: a pipette whose tip is the sampled pixel. Draws it at `p` and returns the
/// OS cursor to set (hidden).
pub(crate) fn pipette_cursor(ctx: &egui::Context, p: Pos2) -> egui::CursorIcon {
@@ -0,0 +1,182 @@
use egui::{Modifiers, PointerButton, Pos2, vec2};
use egui_kittest::Harness;
use serde_json::json;
use super::ViewXform;
use crate::PhotocraftApp;
use crate::state::Tool;
fn harness() -> Harness<'static, PhotocraftApp> {
let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default());
app.run("file.new", json!({"width": 400, "height": 300})).unwrap();
app.sync_views();
app.ui.extras.rulers = false;
app.ui.tool = Tool::ColorSampler;
let mut h = Harness::builder().with_size(vec2(1000.0, 700.0)).build_ui_state(
|ui, app: &mut PhotocraftApp| {
let ctx = ui.ctx().clone();
if !ctx.fonts(|f| f.families().contains(&egui::FontFamily::Name("medium".into()))) {
return;
}
crate::shortcuts::handle(app, &ctx);
egui::CentralPanel::default().show(ui, |ui| crate::canvas::document_area(app, ui));
},
app,
);
PhotocraftApp::setup_context(&h.ctx, crate::theme::ThemeKind::ALL[0]);
h.run_steps(4);
let v = &mut h.state_mut().ui.views[0];
v.zoom = 1.0;
v.center = [200.0, 150.0];
v.fit_pending = false;
h.run_steps(2);
h
}
fn screen(h: &Harness<'static, PhotocraftApp>, x: f32, y: f32) -> Pos2 {
let app = h.state();
let v = &app.ui.views[0];
ViewXform {
rect: crate::rulers::content_rect(app, app.last_canvas_rect),
zoom: v.zoom,
center: v.center,
flip: app.ui.view.flip_horizontal,
rotation: v.rotation,
aspect: app.ui.view.display_aspect(),
}
.to_screen(x, y)
}
fn cursor_area_visible(h: &Harness<'static, PhotocraftApp>, id: &str) -> bool {
let layer = egui::LayerId::new(egui::Order::Tooltip, egui::Id::unique(id));
h.ctx.memory(|memory| memory.areas().visible_layer_ids().contains(&layer))
}
fn cursor_badge_visible(h: &Harness<'static, PhotocraftApp>, name: &str) -> bool {
let layer = egui::LayerId::new(egui::Order::Tooltip, egui::Id::unique(("pc-cursor-badge", name)));
h.ctx.memory(|memory| memory.areas().visible_layer_ids().contains(&layer))
}
fn hover_at(h: &mut Harness<'static, PhotocraftApp>, doc_x: f32, doc_y: f32, m: Modifiers) -> egui::CursorIcon {
h.event(egui::Event::ModifiersChanged(m));
let p = screen(h, doc_x, doc_y);
h.event(egui::Event::PointerMoved(p));
h.run_steps(2);
h.output().platform_output.cursor_icon
}
#[test]
fn off_sampler_no_key_or_shift_or_alt_shows_eyedropper_with_sampler_target() {
let mut h = harness();
// 1. Off a sampler, no key: eyedropper with small sampler target at top-left.
let icon = hover_at(&mut h, 80.0, 80.0, Modifiers::NONE);
assert_eq!(icon, egui::CursorIcon::None, "pipette cursor replaces OS pointer");
assert!(cursor_area_visible(&h, "pc-icon-cursor"), "pipette icon is drawn");
assert!(cursor_area_visible(&h, "pc-sampler-badge"), "sampler target at top-left is drawn");
assert!(!cursor_badge_visible(&h, "sampler") && !cursor_badge_visible(&h, "scissors"), "arrow badge is not drawn off sampler");
// 2. Off a sampler, Shift held: still eyedropper with sampler target.
let icon_shift = hover_at(&mut h, 80.0, 80.0, Modifiers::SHIFT);
assert_eq!(icon_shift, egui::CursorIcon::None);
assert!(cursor_area_visible(&h, "pc-icon-cursor"));
assert!(cursor_area_visible(&h, "pc-sampler-badge"));
// 3. Off a sampler, Alt held: still eyedropper with sampler target.
let icon_alt = hover_at(&mut h, 80.0, 80.0, Modifiers::ALT);
assert_eq!(icon_alt, egui::CursorIcon::None);
assert!(cursor_area_visible(&h, "pc-icon-cursor"));
assert!(cursor_area_visible(&h, "pc-sampler-badge"));
}
#[test]
fn off_sampler_precise_other_cursors_shows_crosshair() {
let mut h = harness();
h.state_mut().run("prefs.set", json!({"values": {"cursors.other": "precise"}})).unwrap();
let icon = hover_at(&mut h, 80.0, 80.0, Modifiers::NONE);
assert_eq!(icon, egui::CursorIcon::Crosshair, "Precise Other Cursors shows crosshair off sampler");
assert!(!cursor_area_visible(&h, "pc-icon-cursor"), "no pipette icon with precise cursor");
assert!(!cursor_area_visible(&h, "pc-sampler-badge"), "no sampler badge with precise cursor");
}
#[test]
fn over_sampler_shows_arrow_with_sampler_target_and_alt_shows_scissors() {
let mut h = harness();
// Place a sampler at (150.0, 150.0).
h.state_mut().run("view.colorSamplers.add", json!({"x": 150.0, "y": 150.0})).unwrap();
// 1. Over sampler, no key: arrow with sampler target.
let icon = hover_at(&mut h, 150.0, 150.0, Modifiers::NONE);
assert_eq!(icon, egui::CursorIcon::Default, "over sampler uses standard arrow pointer");
assert!(cursor_badge_visible(&h, "sampler"), "sampler target badge is drawn");
assert!(!cursor_badge_visible(&h, "scissors"), "scissors badge is not drawn without Alt");
assert!(!cursor_area_visible(&h, "pc-icon-cursor"), "no pipette icon over sampler");
// Even with Precise Other Cursors, over a sampler is the arrow cursor.
h.state_mut().run("prefs.set", json!({"values": {"cursors.other": "precise"}})).unwrap();
let icon_precise = hover_at(&mut h, 150.0, 150.0, Modifiers::NONE);
assert_eq!(icon_precise, egui::CursorIcon::Default, "Precise cursor does not suppress sampler grab pointer");
assert!(cursor_badge_visible(&h, "sampler"));
assert!(!cursor_badge_visible(&h, "scissors"));
h.state_mut().run("prefs.set", json!({"values": {"cursors.other": "standard"}})).unwrap();
// 2. Alt held over sampler: arrow with scissors.
let icon_alt = hover_at(&mut h, 150.0, 150.0, Modifiers::ALT);
assert_eq!(icon_alt, egui::CursorIcon::Default, "Alt over sampler keeps arrow pointer");
assert!(cursor_badge_visible(&h, "scissors"), "scissors badge is drawn with Alt");
assert!(!cursor_badge_visible(&h, "sampler"), "sampler badge is not drawn when Alt shows scissors");
// 3. Releasing Alt without moving pointer reverts to arrow with sampler target.
h.event(egui::Event::ModifiersChanged(Modifiers::NONE));
h.run_steps(2);
assert_eq!(h.output().platform_output.cursor_icon, egui::CursorIcon::Default);
assert!(cursor_badge_visible(&h, "sampler"), "sampler target badge restored when Alt is released");
assert!(!cursor_badge_visible(&h, "scissors"), "scissors badge removed when Alt is released");
// 4. Sticky Alt (e.g. tablet button) also shows scissors.
h.state_mut().ui.shell.sticky_alt = true;
h.run_steps(2);
assert_eq!(h.output().platform_output.cursor_icon, egui::CursorIcon::Default);
assert!(cursor_badge_visible(&h, "scissors"), "sticky Alt shows scissors");
assert!(!cursor_badge_visible(&h, "sampler"));
h.state_mut().ui.shell.sticky_alt = false;
h.run_steps(2);
assert!(cursor_badge_visible(&h, "sampler"));
assert!(!cursor_badge_visible(&h, "scissors"));
}
#[test]
fn cursor_follows_sampler_hit_test_tolerance() {
let mut h = harness();
h.state_mut().run("view.colorSamplers.add", json!({"x": 150.0, "y": 150.0})).unwrap();
// At zoom = 1.0, tolerance is 7.0 px.
// Inside hit square (dx = 6.0 <= 7.0): over sampler -> CursorIcon::Default.
let icon_in = hover_at(&mut h, 156.0, 150.0, Modifiers::NONE);
assert_eq!(icon_in, egui::CursorIcon::Default, "dx = 6 px is within tolerance");
assert!(cursor_badge_visible(&h, "sampler"));
assert!(!cursor_badge_visible(&h, "scissors"));
// Outside hit square (dx = 8.0 > 7.0): off sampler -> CursorIcon::None (eyedropper).
let icon_out = hover_at(&mut h, 158.0, 150.0, Modifiers::NONE);
assert_eq!(icon_out, egui::CursorIcon::None, "dx = 8 px is outside tolerance");
assert!(cursor_area_visible(&h, "pc-sampler-badge"));
// A press at dx = 6 grabs the existing sampler.
let p_in = screen(&h, 156.0, 150.0);
h.event(egui::Event::PointerMoved(p_in));
h.run_steps(1);
h.event(egui::Event::PointerButton { pos: p_in, button: PointerButton::Primary, pressed: true, modifiers: Modifiers::NONE });
h.run_steps(1);
let q = screen(&h, 170.0, 150.0);
h.event(egui::Event::PointerMoved(q));
h.run_steps(1);
h.event(egui::Event::PointerButton { pos: q, button: PointerButton::Primary, pressed: false, modifiers: Modifiers::NONE });
h.run_steps(2);
let samplers = h.state().session.active().unwrap().color_samplers.clone();
assert_eq!(samplers.len(), 1, "dragging within tolerance moved the existing sampler");
assert!((samplers[0][0] - 170.0).abs() < 1.0, "moved to 170");
}
+2 -1
View File
@@ -95,7 +95,8 @@ pub fn cursor_copy_badge(ctx: &egui::Context, p: egui::Pos2) {
fn badge(ctx: &egui::Context, name: &str, p: egui::Pos2, doubled: bool) {
const SIZE: f32 = 12.0;
let rect = Rect::from_center_size(p + egui::vec2(20.0, 19.0), Vec2::splat(SIZE));
let area = egui::Area::new(egui::Id::unique("pc-cursor-badge")).order(egui::Order::Tooltip).fixed_pos(rect.min).constrain(false).interactable(false);
let area =
egui::Area::new(egui::Id::unique(("pc-cursor-badge", name))).order(egui::Order::Tooltip).fixed_pos(rect.min).constrain(false).interactable(false);
area.show(ctx, |ui| {
let outline = image(name, SIZE, Color32::from_black_alpha(220));
let halo = [(-1.0, 0.0), (1.0, 0.0), (0.0, -1.0), (0.0, 1.0), (-1.0, -1.0), (1.0, 1.0), (-1.0, 1.0), (1.0, -1.0)];
+1 -2
View File
@@ -283,7 +283,6 @@ pub fn pointer(app: &mut PhotocraftApp, ev: ToolEvent, mods: egui::Modifiers) ->
return true;
};
let tol = tolerance(app);
let samplers: Vec<[f64; 2]> = app.session.active().map(|d| d.color_samplers.clone()).unwrap_or_default();
match (tool, ev) {
(Tool::Ruler, ToolEvent::Down { x, y, .. }) => {
let p = [x, y];
@@ -384,7 +383,7 @@ pub fn pointer(app: &mut PhotocraftApp, ev: ToolEvent, mods: egui::Modifiers) ->
// there (a click on it moves it to the clicked pixel); Escape cancels either drag.
(Tool::ColorSampler, ToolEvent::Down { x, y, .. }) => {
let p = [x, y];
match nearest_sampler(&samplers, p, tol) {
match sampler_at(app, p) {
// ⌥-click a point removes it.
Some(i) if mods.alt => {
let _ = app.run("view.colorSamplers.delete", json!({"index": i}));