mirror of
https://github.com/storytold/photocraft.git
synced 2026-12-02 06:20:48 +01:00
The picker covers every screen and stays on top, and a report on #4291 describes one that ignored Esc, right-click and left-click, so the computer could only be freed from another device. In the picker's frame: - Esc only worked once Windows gave the picker keyboard focus, which nothing asked for. The picker now takes focus on its first shown frame. - A right-click did nothing; it now cancels. - A left click was dropped when no snapshot pixel mapped to the pointer, and a press that moved before its release never counted as a click. Any left release over the picker now ends it: it picks the pixel under the pointer, or cancels when there's none. The frame moved into screen_picker::frame so it can be tested. Test: every_way_out_of_the_picker_ends_it. Co-authored-by: Claude Opus 5.5 <[email protected]>
This commit is contained in:
1 parent
f769c3735d
commit
caabe4b836
1 file changed
+125
-44
@@ -293,60 +293,80 @@ pub fn show(ctx: &Context) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
if ui.ctx().input(|i| i.viewport().close_requested() || i.key_pressed(egui::Key::Escape)) {
|
||||
lock(&shared).answer = Some(Ok(None));
|
||||
ui.ctx().request_repaint_of(egui::ViewportId::ROOT);
|
||||
return;
|
||||
}
|
||||
egui::CentralPanel::default().frame(egui::Frame::NONE).show(ui, |ui| {
|
||||
let rect = ui.max_rect();
|
||||
ui.painter().image(texture.id(), rect, Rect::from_min_max(Pos2::ZERO, egui::pos2(1.0, 1.0)), Color32::WHITE);
|
||||
let response = ui.interact(rect, ui.id().with("screen-sample"), Sense::click());
|
||||
ui.ctx().set_cursor_icon(egui::CursorIcon::Crosshair);
|
||||
let Some(pos) = ui.ctx().pointer_hover_pos() else {
|
||||
return;
|
||||
};
|
||||
let Some((x, y)) = image.at(rect, pos) else {
|
||||
return;
|
||||
};
|
||||
let zoom = {
|
||||
let delta = ui.input(|i| {
|
||||
i.raw
|
||||
.events
|
||||
.iter()
|
||||
.filter_map(|e| match e {
|
||||
egui::Event::MouseWheel { delta, .. } => Some(delta.y),
|
||||
_ => None,
|
||||
})
|
||||
.sum::<f32>()
|
||||
});
|
||||
let mut s = lock(&shared);
|
||||
if delta > 0.0 {
|
||||
s.zoom = (s.zoom + 2).min(24);
|
||||
}
|
||||
if delta < 0.0 {
|
||||
s.zoom = s.zoom.saturating_sub(2).max(4);
|
||||
}
|
||||
s.zoom
|
||||
};
|
||||
let picked = image.sample(x, y, conversion.as_deref());
|
||||
loupe(ui, &image, rect, pos, (x, y), zoom, picked.as_ref().ok().copied());
|
||||
if response.clicked() {
|
||||
lock(&shared).answer = Some(picked.map(Some));
|
||||
ui.ctx().request_repaint_of(egui::ViewportId::ROOT);
|
||||
}
|
||||
});
|
||||
frame(ui, &shared, &image, &texture, conversion.as_deref());
|
||||
// Showing a new native viewport before this frame is painted can briefly cover
|
||||
// the screen with the window's default white clear color.
|
||||
ui.ctx().send_viewport_cmd(egui::ViewportCommand::Visible(true));
|
||||
// Once per picker window: `show` runs every frame, so a local flag would resend it.
|
||||
// Once per picker window (`show` runs every frame, so a local flag would resend it):
|
||||
// raise it, and take the keyboard so Esc reaches the picker: a window shown later
|
||||
// isn't always given focus by the OS, and the picker covers the screen (#4291).
|
||||
let raised = Id::new(("screen-picker-raised", ui.ctx().viewport_id()));
|
||||
if !ui.ctx().data_mut(|d| std::mem::replace(d.get_temp_mut_or_default::<bool>(raised), true)) {
|
||||
ui.ctx().send_viewport_cmd(egui::ViewportCommand::WindowLevel(egui::WindowLevel::AlwaysOnTop));
|
||||
ui.ctx().send_viewport_cmd(egui::ViewportCommand::Focus);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
/// What the picker resolves to: the colour from a pick, or `Ok(None)` to cancel.
|
||||
type Answer = Result<Option<[f32; 3]>, String>;
|
||||
|
||||
/// One frame of a picker window over its snapshot `image`. The picker covers the whole screen
|
||||
/// and stays on top, so every way out must work (#4291): Esc, closing the window, a right-click,
|
||||
/// and a left click (or a press and release that moved) anywhere, which picks the pixel under
|
||||
/// it, or cancels when there's none to sample. Nothing it gets leaves the user stuck in it.
|
||||
fn frame(ui: &mut egui::Ui, shared: &Shared, image: &ScreenImage, texture: &egui::TextureHandle, conversion: Option<&photocraft_cms::Transform>) {
|
||||
let ctx = ui.ctx().clone();
|
||||
let answer = |a: Answer| {
|
||||
lock(shared).answer = Some(a);
|
||||
ctx.request_repaint_of(egui::ViewportId::ROOT);
|
||||
};
|
||||
if ctx.input(|i| i.viewport().close_requested() || i.key_pressed(egui::Key::Escape) || i.pointer.secondary_released()) {
|
||||
answer(Ok(None));
|
||||
return;
|
||||
}
|
||||
egui::CentralPanel::default().frame(egui::Frame::NONE).show(ui, |ui| {
|
||||
let rect = ui.max_rect();
|
||||
ui.painter().image(texture.id(), rect, Rect::from_min_max(Pos2::ZERO, egui::pos2(1.0, 1.0)), Color32::WHITE);
|
||||
let response = ui.interact(rect, ui.id().with("screen-sample"), Sense::click_and_drag());
|
||||
ui.ctx().set_cursor_icon(egui::CursorIcon::Crosshair);
|
||||
let released = response.clicked() || response.drag_stopped();
|
||||
let pixel = ui.ctx().pointer_latest_pos().and_then(|pos| Some((pos, image.at(rect, pos)?)));
|
||||
let Some((pos, (x, y))) = pixel else {
|
||||
// No pixel under the pointer to sample: a click still ends the picker.
|
||||
if released {
|
||||
answer(Ok(None));
|
||||
}
|
||||
return;
|
||||
};
|
||||
let zoom = {
|
||||
let delta = ui.input(|i| {
|
||||
i.raw
|
||||
.events
|
||||
.iter()
|
||||
.filter_map(|e| match e {
|
||||
egui::Event::MouseWheel { delta, .. } => Some(delta.y),
|
||||
_ => None,
|
||||
})
|
||||
.sum::<f32>()
|
||||
});
|
||||
let mut s = lock(shared);
|
||||
if delta > 0.0 {
|
||||
s.zoom = (s.zoom + 2).min(24);
|
||||
}
|
||||
if delta < 0.0 {
|
||||
s.zoom = s.zoom.saturating_sub(2).max(4);
|
||||
}
|
||||
s.zoom
|
||||
};
|
||||
let picked = image.sample(x, y, conversion);
|
||||
loupe(ui, image, rect, pos, (x, y), zoom, picked.as_ref().ok().copied());
|
||||
if released {
|
||||
answer(picked.map(Some));
|
||||
}
|
||||
});
|
||||
}
|
||||
fn loupe(ui: &egui::Ui, image: &ScreenImage, screen: Rect, pos: Pos2, pixel: (usize, usize), zoom: usize, rgb: Option<[f32; 3]>) {
|
||||
let (x, y) = pixel;
|
||||
let t = crate::theme::Tokens::get(ui.ctx());
|
||||
@@ -437,6 +457,67 @@ mod tests {
|
||||
i.profile = Some(photocraft_cms::Builtin::LabD50.profile().to_bytes().as_ref().clone());
|
||||
assert!(i.conversion().is_err());
|
||||
}
|
||||
/// The picker window's frame over [`image`] (red | azure) filling a 200 × 100 window, with its
|
||||
/// own runtime: what the frame answered after `act`.
|
||||
fn picker_answer(act: impl FnOnce(&mut egui_kittest::Harness<'_>)) -> Option<Answer> {
|
||||
let shared: Shared = Arc::default();
|
||||
let inner = shared.clone();
|
||||
let texture: std::cell::OnceCell<egui::TextureHandle> = std::cell::OnceCell::new();
|
||||
let image = image();
|
||||
let mut h = egui_kittest::Harness::builder().with_size(vec2(200.0, 100.0)).build_ui(move |ui| {
|
||||
let tex = texture.get_or_init(|| {
|
||||
ui.ctx().load_texture("test-snapshot", egui::ColorImage::new([2, 1], vec![Color32::RED, Color32::BLUE]), TextureOptions::NEAREST)
|
||||
});
|
||||
frame(ui, &inner, &image, tex, None);
|
||||
});
|
||||
h.run();
|
||||
act(&mut h);
|
||||
h.run();
|
||||
lock(&shared).answer.take()
|
||||
}
|
||||
/// #4291: the picker covers the screen and stays on top, so every input ends it: Esc, a
|
||||
/// right-click, a click (the pixel under it), a press dragged before its release (the pixel
|
||||
/// where it's released).
|
||||
#[test]
|
||||
fn every_way_out_of_the_picker_ends_it() {
|
||||
let left = egui::pos2(40.0, 50.0);
|
||||
let right = egui::pos2(160.0, 50.0);
|
||||
assert_eq!(picker_answer(|h| h.hover_at(left)), None, "hovering alone keeps it open");
|
||||
assert_eq!(
|
||||
picker_answer(|h| {
|
||||
h.hover_at(left);
|
||||
h.run();
|
||||
h.drag_at(left);
|
||||
h.run();
|
||||
h.drop_at(left);
|
||||
}),
|
||||
Some(Ok(Some([1.0, 0.0, 0.0]))),
|
||||
"a click picks the pixel under it"
|
||||
);
|
||||
assert_eq!(
|
||||
picker_answer(|h| {
|
||||
h.drag_at(left);
|
||||
h.run();
|
||||
h.hover_at(right);
|
||||
h.run();
|
||||
h.drop_at(right);
|
||||
}),
|
||||
Some(Ok(Some([0.0, 128.0 / 255.0, 1.0]))),
|
||||
"a press dragged across picks where it's released, instead of being ignored"
|
||||
);
|
||||
assert_eq!(
|
||||
picker_answer(|h| {
|
||||
h.hover_at(left);
|
||||
h.run();
|
||||
h.event(egui::Event::PointerButton { pos: left, button: egui::PointerButton::Secondary, pressed: true, modifiers: Default::default() });
|
||||
h.run();
|
||||
h.event(egui::Event::PointerButton { pos: left, button: egui::PointerButton::Secondary, pressed: false, modifiers: Default::default() });
|
||||
}),
|
||||
Some(Ok(None)),
|
||||
"a right-click cancels"
|
||||
);
|
||||
assert_eq!(picker_answer(|h| h.key_press(egui::Key::Escape)), Some(Ok(None)), "Esc cancels");
|
||||
}
|
||||
fn app() -> PhotocraftApp {
|
||||
let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default());
|
||||
app.run("file.new", serde_json::json!({"width":8,"height":8})).unwrap();
|
||||
|
||||
Reference in new issue
Block a user