mirror of
https://github.com/storytold/photocraft.git
synced 2026-12-02 06:20:48 +01:00
Merge pull request #17018 from davidmann4/fix/color-picker-no-screen-eyedropper
Color Picker: no screen eyedropper button, as in the reference application (#17017)
This commit is contained in:
6 files changed
+118
-75
No files matched your search
@@ -1,10 +1,10 @@
|
||||
//! Render synthetic color-picker evidence without capturing the user's desktop.
|
||||
//! cargo run -p photocraft-ui-egui --example color_picker_demo -- /tmp/color-picker
|
||||
use craft::color_ui::ui::{CaptureSource, StillCapture};
|
||||
use craft::color_ui::ui::{CaptureSource, ScreenPickGesture, StillCapture};
|
||||
use craft::platform::screen::{Capture, ScreenImage};
|
||||
use craft::theme::Srgba;
|
||||
use egui_kittest::{Harness, kittest::Queryable};
|
||||
use photocraft_ui_egui::{PhotocraftApp, Services, color_picker_ui, screen_picker};
|
||||
use photocraft_ui_egui::{PhotocraftApp, Services, screen_picker};
|
||||
use serde_json::json;
|
||||
use std::sync::Arc;
|
||||
|
||||
@@ -37,12 +37,9 @@ fn main() {
|
||||
app
|
||||
});
|
||||
text.run_steps(8);
|
||||
text.get_by_label("Set the text color").click();
|
||||
text.get_all_by_role_and_label(egui::accesskit::Role::ColorWell, "Text color").next().expect("the text swatch").click();
|
||||
text.run_steps(6);
|
||||
text.render().unwrap().save(format!("{output}/text-color-picker.png")).unwrap();
|
||||
text.get_by_label("Sample color from the screen").click();
|
||||
text.run_steps(6);
|
||||
text.render().unwrap().save(format!("{output}/text-color-picked.png")).unwrap();
|
||||
|
||||
// PLAN: the loupe renders through the app's own frame, which calls `screen_picker::tick` and
|
||||
// `show` once per pass (frame.rs, hub-w1); calling them again here would run the pick twice a pass.
|
||||
@@ -57,9 +54,9 @@ fn main() {
|
||||
PhotocraftApp::with_context(cc.egui_ctx.clone(), session, services)
|
||||
});
|
||||
loupe.run_steps(8);
|
||||
color_picker_ui::open(loupe.state_mut(), "foreground");
|
||||
loupe.run_steps(6);
|
||||
loupe.get_by_label("Sample color from the screen").click();
|
||||
// The Color Picker dialog has no screen button (#17017); the compact popovers start the same pick.
|
||||
let ctx = loupe.ctx.clone();
|
||||
screen_picker::hold(loupe.state_mut(), &ctx, "color-picker-demo", ScreenPickGesture::Clicked);
|
||||
loupe.run_steps(3);
|
||||
loupe.input_mut().events.push(egui::Event::PointerMoved(egui::pos2(360.0, 220.0)));
|
||||
loupe.run_steps(2);
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
//!
|
||||
//! Every opener builds the dialog, opens it on the dialog host and records what it is for in
|
||||
//! `app.color_targets` ([`PickTarget`]). [`take`] runs once per pass from `dialogs::TAKERS`: it
|
||||
//! serves the dialog's screen pick, Add to Swatches and canvas eyedropper, previews the picked
|
||||
//! serves the dialog's Add to Swatches and canvas eyedropper, previews the picked
|
||||
//! colour in its parent ([`live_preview`]) and commits an OK to the target ([`commit`]). A hosted
|
||||
//! dialog's colour row opens a child with [`open_for_request`] and pulls the answer with
|
||||
//! [`answer_for`]; a window the app holds itself opens one with [`open_for_window`] and pulls the
|
||||
@@ -169,9 +169,10 @@ fn converter(app: &PhotocraftApp) -> CmykConverter {
|
||||
CmykConverter::new(app.session.color().working(photocraft_doc::ColorMode::Cmyk)).ok().unwrap_or_else(|| CmykConverter::coated().clone())
|
||||
}
|
||||
|
||||
/// Opens `dialog` for `target` with the app's picker features, under `parent` when it has one.
|
||||
/// Opens `dialog` for `target` with the app's picker features, under `parent` when it has one. No screen eyedropper
|
||||
/// button: the reference application's picker has none and samples by a click on the canvas (#17017).
|
||||
fn launch(app: &mut PhotocraftApp, dialog: ColorPickerDialog, parent: Option<DialogId>, target: PickTarget) -> DialogId {
|
||||
let mut dialog = dialog.cmyk(&converter(app)).canvas_samples(true).eyedropper(true).add_to_swatches(true).instance(target.identity());
|
||||
let mut dialog = dialog.cmyk(&converter(app)).canvas_samples(true).eyedropper(false).add_to_swatches(true).instance(target.identity());
|
||||
if let Some(idx) = app.session.active_index() {
|
||||
dialog = dialog.canvas(crate::canvas::document_display::CanvasHost::primary(app, idx).canvas_id());
|
||||
}
|
||||
@@ -335,31 +336,18 @@ pub(crate) fn find(app: &PhotocraftApp, matches: impl Fn(&PickTarget) -> bool) -
|
||||
.last()
|
||||
}
|
||||
|
||||
/// Once per pass: serves each open picker's screen pick, Add to Swatches and canvas eyedropper request,
|
||||
/// Once per pass: serves each open picker's Add to Swatches and canvas eyedropper request,
|
||||
/// previews the picked colours in their parents, and commits the pickers that finished. A second pass in
|
||||
/// one frame finds nothing new: every request is taken from the dialog once.
|
||||
pub(crate) fn take(app: &mut PhotocraftApp, ctx: &egui::Context) {
|
||||
pub(crate) fn take(app: &mut PhotocraftApp, _ctx: &egui::Context) {
|
||||
if app.color_targets.is_empty() {
|
||||
return;
|
||||
}
|
||||
let ids: Vec<DialogId> = app.color_targets.keys().copied().collect();
|
||||
// PLAN: assumed `screen_picker::busy(app: &PhotocraftApp) -> bool` and `hold(app, ctx, id, gesture)` (dialogs-w39).
|
||||
let busy = crate::screen_picker::busy(app);
|
||||
for &id in &ids {
|
||||
let Some(d) = app.framework.dialogs.get_mut_by_id::<ColorPickerDialog>(id) else { continue };
|
||||
d.screen_busy(busy);
|
||||
let gesture = d.take_screen_pick();
|
||||
let swatch = d.take_add_to_swatches();
|
||||
let dropper = d.requests().take_eyedropper();
|
||||
if let Some(gesture) = gesture {
|
||||
crate::screen_picker::hold(app, ctx, id, gesture);
|
||||
}
|
||||
// PLAN: assumed `screen_picker::take(app, id) -> Option<Srgba>` (dialogs-w39).
|
||||
if let Some(color) = crate::screen_picker::take(app, id)
|
||||
&& let Some(d) = app.framework.dialogs.get_mut_by_id::<ColorPickerDialog>(id)
|
||||
{
|
||||
d.set_color(PickedColor::rgb(color));
|
||||
}
|
||||
if let Some(picked) = swatch {
|
||||
crate::swatches_ui::add_from_picker(app, picked);
|
||||
}
|
||||
@@ -488,46 +476,8 @@ pub(crate) mod live_preview;
|
||||
mod tests;
|
||||
|
||||
#[cfg(test)]
|
||||
#[path = "color_picker_swatch_name_tests.rs"]
|
||||
mod swatch_name_tests;
|
||||
mod screen_button_tests;
|
||||
|
||||
#[cfg(test)]
|
||||
mod screen_integration_tests {
|
||||
use super::*;
|
||||
use craft::color_ui::ui::StillCapture;
|
||||
use egui::vec2;
|
||||
use egui_kittest::{Harness, kittest::Queryable};
|
||||
|
||||
#[test]
|
||||
fn text_swatch_opens_full_dialog_and_screen_pick_waits_for_ok() {
|
||||
let mut session = photocraft_engine::Session::new();
|
||||
session.execute("file.new", json!({"width":160,"height":120})).unwrap();
|
||||
let id = session.execute("type.create", json!({"text":"Hello world","color":"#000000","size":24,"x":10,"y":60})).unwrap()["layer"].as_u64().unwrap();
|
||||
let mut h = Harness::builder().with_size(vec2(1440.0, 900.0)).with_max_steps(64).build_eframe(move |cc| {
|
||||
PhotocraftApp::setup_context(&cc.egui_ctx, Default::default());
|
||||
let mut app = PhotocraftApp::with_context(cc.egui_ctx.clone(), session, Default::default());
|
||||
app.ui.tool = crate::state::Tool::Type;
|
||||
app.services.screen_capture = Some(std::sync::Arc::new(|| Box::new(StillCapture::color(Srgba::rgb(1.0, 136.0 / 255.0, 0.0)))));
|
||||
app
|
||||
});
|
||||
h.run_steps(8);
|
||||
let before = h.state().session.active().unwrap().history.entries().len();
|
||||
let fg = h.state().session.tools.foreground;
|
||||
h.get_all_by_role_and_label(egui::accesskit::Role::ColorWell, "Text color").next().expect("the text swatch").click();
|
||||
h.run_steps(6);
|
||||
let picker = top(h.state()).expect("the text swatch opens the Color Picker");
|
||||
assert!(matches!(target(h.state(), picker), Some(PickTarget::Command { command, .. }) if command == "type.setStyle"));
|
||||
h.get_by_label("Sample color from the screen").click();
|
||||
h.run_steps(6);
|
||||
assert_eq!(rgb_hex(testing::current(h.state(), picker).unwrap().color), "#ff8800");
|
||||
assert_eq!(h.state().session.active().unwrap().history.entries().len(), before, "sampling only changes the picker's colour");
|
||||
assert_eq!(h.state().session.tools.foreground, fg);
|
||||
h.get_by_label("OK").click();
|
||||
h.run_steps(4);
|
||||
let doc = &h.state().session.active().unwrap().doc;
|
||||
let layer = doc.layer(photocraft_doc::LayerId(id)).unwrap();
|
||||
let photocraft_doc::LayerContent::Text(text) = &layer.content else { panic!("not text") };
|
||||
assert_eq!(text.color.to_rgba8(), [255, 136, 0, 255]);
|
||||
assert_eq!(h.state().session.active().unwrap().history.entries().len(), before + 1);
|
||||
}
|
||||
}
|
||||
#[path = "color_picker_swatch_name_tests.rs"]
|
||||
mod swatch_name_tests;
|
||||
@@ -0,0 +1,92 @@
|
||||
//! The Color Picker dialog has no screen eyedropper button, as in the reference application (#17017): whichever chip
|
||||
//! opens it, it samples by a click on the canvas only.
|
||||
|
||||
use craft::color_ui::ui::StillCapture;
|
||||
use egui::vec2;
|
||||
use egui_kittest::{Harness, kittest::Queryable};
|
||||
|
||||
use super::*;
|
||||
|
||||
const SCREEN_BUTTON: &str = "Sample color from the screen";
|
||||
|
||||
/// The whole app with a screen capture service, so the platform could sample the screen and the button would show.
|
||||
fn app_harness(setup: impl FnOnce(&mut photocraft_engine::Session) + 'static) -> Harness<'static, Box<PhotocraftApp>> {
|
||||
let mut session = photocraft_engine::Session::new();
|
||||
session.execute("file.new", json!({"width":160,"height":120})).unwrap();
|
||||
setup(&mut session);
|
||||
let mut h = Harness::builder().with_size(vec2(1440.0, 900.0)).with_max_steps(64).build_eframe(move |cc| {
|
||||
PhotocraftApp::setup_context(&cc.egui_ctx, Default::default());
|
||||
let mut app = PhotocraftApp::with_context(cc.egui_ctx.clone(), session, Default::default());
|
||||
app.services.screen_capture = Some(std::sync::Arc::new(|| Box::new(StillCapture::color(Srgba::rgb(1.0, 136.0 / 255.0, 0.0)))));
|
||||
app
|
||||
});
|
||||
h.run_steps(8);
|
||||
h
|
||||
}
|
||||
|
||||
/// The picker is drawn in its full layout (Only Web Colors under the field) and has no screen eyedropper button.
|
||||
fn assert_no_screen_button(h: &Harness<'static, Box<PhotocraftApp>>, opener: &str) {
|
||||
assert!(h.query_by_label("Only Web Colors").is_some(), "{opener}: the full Color Picker is shown");
|
||||
assert!(h.query_by_label(SCREEN_BUTTON).is_none(), "{opener}: the Color Picker shows a screen eyedropper button");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn foreground_and_background_pickers_have_no_screen_button() {
|
||||
for target in ["foreground", "background"] {
|
||||
let mut h = app_harness(|_| {});
|
||||
open(h.state_mut(), target);
|
||||
h.run_steps(6);
|
||||
assert_no_screen_button(&h, target);
|
||||
}
|
||||
}
|
||||
|
||||
/// The Type options bar's swatch opens the full picker for `type.setStyle`; OK still applies the picked colour.
|
||||
#[test]
|
||||
fn text_swatch_picker_has_no_screen_button_and_ok_applies() {
|
||||
let layer = std::rc::Rc::new(std::cell::Cell::new(0));
|
||||
let created = layer.clone();
|
||||
let mut h = app_harness(move |session| {
|
||||
let reply = session.execute("type.create", json!({"text":"Hello world","color":"#000000","size":24,"x":10,"y":60})).unwrap();
|
||||
created.set(reply["layer"].as_u64().unwrap());
|
||||
});
|
||||
h.state_mut().ui.tool = crate::state::Tool::Type;
|
||||
h.run_steps(4);
|
||||
h.get_all_by_role_and_label(egui::accesskit::Role::ColorWell, "Text color").next().expect("the text swatch").click();
|
||||
h.run_steps(6);
|
||||
let picker = top(h.state()).expect("the text swatch opens the Color Picker");
|
||||
assert!(matches!(target(h.state(), picker), Some(PickTarget::Command { command, .. }) if command == "type.setStyle"));
|
||||
assert_no_screen_button(&h, "text swatch");
|
||||
testing::set_color(h.state_mut(), picker, "#ff8800");
|
||||
h.run_steps(2);
|
||||
h.get_by_label("OK").click();
|
||||
h.run_steps(4);
|
||||
let doc = &h.state().session.active().unwrap().doc;
|
||||
let photocraft_doc::LayerContent::Text(text) = &doc.layer(photocraft_doc::LayerId(layer.get())).unwrap().content else { panic!("not text") };
|
||||
assert_eq!(text.color.to_rgba8(), [255, 136, 0, 255]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn layer_style_picker_has_no_screen_button() {
|
||||
let mut h = app_harness(|_| {});
|
||||
let parent = crate::layer_style::open(h.state_mut(), Some("colorOverlay")).expect("the Layer Style dialog opens");
|
||||
h.run_steps(4);
|
||||
crate::dialogs::layer_style::color_picker::open(h.state_mut(), parent.value(), "fx1", "color", None).expect("the picker opens");
|
||||
h.run_steps(6);
|
||||
assert_no_screen_button(&h, "Layer Style");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn text_style_picker_has_no_screen_button() {
|
||||
let style = std::rc::Rc::new(std::cell::Cell::new(0));
|
||||
let created = style.clone();
|
||||
let mut h = app_harness(move |session| {
|
||||
let layer = session.execute("type.create", json!({"x":10,"y":55,"text":"Shared color","size":30})).unwrap()["layer"].as_u64().unwrap();
|
||||
let id = session.execute("type.characterStyle.new", json!({"fromSelection":false,"attrs":{"color":"#ff0000"}})).unwrap()["id"].as_u64().unwrap();
|
||||
session.execute("type.characterStyle.apply", json!({"id":id,"layer":layer})).unwrap();
|
||||
created.set(u32::try_from(id).unwrap());
|
||||
});
|
||||
let kind = crate::type_panels_ui::color_picker::Kind::Character;
|
||||
crate::type_panels_ui::color_picker::open(h.state_mut(), kind, style.get()).expect("the text style picker opens");
|
||||
h.run_steps(6);
|
||||
assert_no_screen_button(&h, "Character Style Options");
|
||||
}
|
||||
@@ -48,8 +48,9 @@ pub(super) fn open_request(app: &mut PhotocraftApp, parent: DialogId, request: C
|
||||
}
|
||||
let opening = color_at(app, parent, &request.key)?;
|
||||
let key = request.key.clone();
|
||||
// An instance keeps the picker's result apart from the other pickers' (`take_by_id` only).
|
||||
let picker = ColorPickerDialog::from_request(request).instance(parent);
|
||||
// An instance keeps the picker's result apart from the other pickers' (`take_by_id` only). No screen eyedropper
|
||||
// button, as in the reference application (#17017).
|
||||
let picker = ColorPickerDialog::from_request(request).eyedropper(false).instance(parent);
|
||||
let id = app.framework.dialogs.open_child(picker, parent);
|
||||
app.layer_style_child = Some((parent, Child::Picker { id, key, opening }));
|
||||
Ok(id)
|
||||
|
||||
@@ -88,8 +88,11 @@ pub(crate) fn open(app: &mut PhotocraftApp, kind: Kind, id: u32) -> Result<Dialo
|
||||
let color = Srgba::from_rgb_array(rgb);
|
||||
panel_docking::show_panel(app, kind.panel(), Some(true))?;
|
||||
app.ui.type_panels.options = Some((kind.paragraph(), id));
|
||||
// The instance keeps two style pickers apart: the host replaces a dialog only by one of the same instance.
|
||||
let picker = ColorPickerDialog::new(format!("textStyle.{}.{id}", kind.name()), tl!("Color Picker (Text Color)"), color).instance((kind.name(), id));
|
||||
// The instance keeps two style pickers apart: the host replaces a dialog only by one of the same instance. No
|
||||
// screen eyedropper button, as in the reference application (#17017).
|
||||
let picker = ColorPickerDialog::new(format!("textStyle.{}.{id}", kind.name()), tl!("Color Picker (Text Color)"), color)
|
||||
.eyedropper(false)
|
||||
.instance((kind.name(), id));
|
||||
let dialog = app.framework.dialogs.open(picker);
|
||||
app.ui.type_panels.color_target = Some(Target { document, revision, kind, id, color, dialog });
|
||||
Ok(dialog)
|
||||
|
||||
@@ -12,11 +12,11 @@ The existing canvas eyedropper continues to sample the document while the Color
|
||||
|
||||
## Pick a color outside PhotoCraft
|
||||
|
||||
**Pick screen color** is available in full Color Pickers and compact color-selection popups, including fills, shapes, gradients, layer effects, guides and preferences.
|
||||
**Pick screen color** is available in compact color-selection popups, including fills, shapes, gradients, layer effects, guides and preferences. The full Color Picker dialog has no screen button, as in the reference application: while it is open, a click on the canvas samples the image into its new colour.
|
||||
|
||||
On X11, Windows and macOS, clicking it takes one snapshot of the connected screens and opens a sampling viewport on each monitor. Move the crosshair over a pixel, use the wheel to change the 11 × 11 pixel loupe from 4× to 24×, then click to select. **Esc** cancels. The center pixel has a black/white outline and its sampled hex value is displayed below the loupe.
|
||||
|
||||
A full dialog receives the new color for inspection; **OK** is still required to apply it. Compact popups retain their normal immediate-edit behavior. Existing alpha and additive blending are preserved; float RGB widgets retain their linear RGB storage.
|
||||
Compact popups retain their normal immediate-edit behavior. Existing alpha and additive blending are preserved; float RGB widgets retain their linear RGB storage.
|
||||
|
||||
Screen snapshots are held in memory, are never written or transmitted by the picker, and are released after selection, cancellation, or a change of document, layer or dialog. The combined capture budget is 64 × 1024 × 1024 pixels (about 67 MP) across at most 16 displays, subject to the GPU's texture-size limit. These limits affect screen sampling only, not document dimensions.
|
||||
|
||||
@@ -28,7 +28,7 @@ Screen snapshots are held in memory, are never written or transmitted by the pic
|
||||
- **macOS:** safe CoreGraphics bindings capture the composed screen and use the snapshot's own ICC profile when available. Screen Recording permission may be required. The existing macOS 11 deployment target is preserved. The window-list capture API is deprecated by Apple, so current macOS releases require explicit manual validation; migration to modern native sampling must also preserve older-app compatibility.
|
||||
- **Web:** browsers exposing the standard EyeDropper API use the browser's native sampling/magnification UI. Browser support and zoom controls vary. Unsupported browsers retain document sampling and color fields; external screen sampling is unavailable.
|
||||
|
||||
The implementation follows publicly documented Photoshop behavior for the full Type Color Picker and outside-image sampling. PhotoCraft uses an explicit screen-pick action and frozen snapshots rather than Photoshop's press-and-drag gesture.
|
||||
The implementation follows publicly documented Photoshop behavior for the full Type Color Picker and outside-image sampling. PhotoCraft's popups use an explicit screen-pick action and frozen snapshots rather than the reference application's press-and-drag gesture; dragging from the canvas to outside the document is tracked in #11755.
|
||||
|
||||
## Verification
|
||||
|
||||
|
||||
Reference in new issue
Block a user