mirror of
https://github.com/storytold/photocraft.git
synced 2026-12-02 06:20:48 +01:00
fix(ui): pasting an image copied in another app never ran (#102)
edit.paste's `enabled` gate checks only the session clipboard, but the OS clipboard import ran inside run()/invoke -- after the gate. Ctrl+V was consumed then dropped, and Edit > Paste stayed grey, so images copied in other apps could never be pasted. The app now mirrors the OS clipboard at 4 Hz (a read every frame is too costly), the shortcut handler imports before checking enablement on paste presses, and a clip_external flag keeps external images centring instead of pasting at (0,0). Generated with [Devin](https://devin.ai) Co-authored-by: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
This commit is contained in:
1 parent
7c53a3c1ad
commit
2c7b86bc02
3 files changed
+44
-1
No files matched your search
@@ -248,6 +248,11 @@ pub struct PhotocraftApp {
|
||||
pub(crate) wide_angle: Option<wide_angle_ui::WideAngleDialog>,
|
||||
/// Signature of the image we last put on the OS clipboard (to tell ours from other apps').
|
||||
os_clip_sig: Option<u64>,
|
||||
/// The session clipboard currently holds an image imported from the OS clipboard: it has no
|
||||
/// original position, so Paste centres it (see `menus` "edit.paste").
|
||||
pub(crate) clip_external: bool,
|
||||
/// Last `ctx` time (s) the OS clipboard was polled; a read every frame is too costly.
|
||||
last_clip_poll: f64,
|
||||
/// Pointer position over the canvas (document px), for the Info panel and status bar.
|
||||
pub(crate) hover_doc: Option<[f64; 2]>,
|
||||
/// Info panel sample cache: ((x, y, revision), composite RGBA).
|
||||
@@ -318,6 +323,8 @@ impl PhotocraftApp {
|
||||
hover_doc: None,
|
||||
info_sample: None,
|
||||
os_clip_sig: None,
|
||||
clip_external: false,
|
||||
last_clip_poll: f64::NEG_INFINITY,
|
||||
transform_preview: None,
|
||||
style_preview: None,
|
||||
distort: Default::default(),
|
||||
@@ -373,6 +380,7 @@ impl PhotocraftApp {
|
||||
}
|
||||
let r = self.session.execute(id, params).map_err(|e| e.to_string());
|
||||
if r.is_ok() && matches!(id, "edit.copy" | "edit.cut" | "edit.copyMerged") {
|
||||
self.clip_external = false;
|
||||
self.export_os_clipboard();
|
||||
}
|
||||
self.perf.last_command = id.to_string();
|
||||
@@ -650,6 +658,12 @@ impl eframe::App for PhotocraftApp {
|
||||
self.issue_screenshots(ctx);
|
||||
prefs_ui::tick(self, ctx);
|
||||
discard_ui::guard_window_close(self, ctx);
|
||||
// Mirror the OS clipboard a few times a second so Edit > Paste greys correctly and Ctrl+V
|
||||
// sees images copied in other apps (a clipboard read every frame is too costly).
|
||||
if now - self.last_clip_poll >= 0.25 {
|
||||
self.last_clip_poll = now;
|
||||
self.import_os_clipboard();
|
||||
}
|
||||
shortcuts::handle(self, ctx);
|
||||
let arrived: Vec<(String, Vec<u8>)> =
|
||||
self.services.inbox.as_ref().map(|q| std::mem::take(&mut *q.lock().unwrap_or_else(|e| e.into_inner()))).unwrap_or_default();
|
||||
@@ -995,6 +1009,7 @@ impl PhotocraftApp {
|
||||
surface.prune();
|
||||
self.session.clipboard = Some(photocraft_engine::edit_cmds::Clip { surface, bounds: r });
|
||||
self.os_clip_sig = Some(sig);
|
||||
self.clip_external = true;
|
||||
true
|
||||
}
|
||||
}
|
||||
@@ -1036,4 +1051,26 @@ mod clipboard_tests {
|
||||
let surf = st.doc.layer(st.active_layer.unwrap()).unwrap().surface().unwrap();
|
||||
assert_eq!(surf.content_bounds().width(), 3);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn external_clipboard_enables_and_centres_paste() {
|
||||
// The periodic clipboard poll in `logic` (or the ⌘V handler) imports the OS image into
|
||||
// the session clipboard before the enabled check and the menu's positioning run.
|
||||
type OsClip = Arc<Mutex<Option<(u32, u32, Vec<u8>)>>>;
|
||||
let os: OsClip = Arc::default();
|
||||
let b = os.clone();
|
||||
let services = Services { clipboard_get_image: Some(Box::new(move || b.lock().unwrap().clone())), ..Default::default() };
|
||||
let mut app = PhotocraftApp::new(Session::new(), services);
|
||||
app.session.execute("file.new", serde_json::json!({"width": 64, "height": 64})).unwrap();
|
||||
app.sync_views();
|
||||
// An image copied in another app: Paste greys until the import brings it in.
|
||||
*os.lock().unwrap() = Some((4, 4, [255u8, 0, 0, 255].repeat(16)));
|
||||
assert!(!crate::menus::is_enabled(&app, "edit.paste"));
|
||||
assert!(app.import_os_clipboard());
|
||||
assert!(crate::menus::is_enabled(&app, "edit.paste"));
|
||||
// Invoking Paste after the import still treats the image as external and centres it
|
||||
// instead of pasting at (0,0) like an in-app copy.
|
||||
let r = crate::menus::invoke(&mut app, &egui::Context::default(), "edit.paste", serde_json::json!({})).unwrap();
|
||||
assert_ne!(r["offset"], serde_json::json!([0, 0]), "external image is centred: {r}");
|
||||
}
|
||||
}
|
||||
@@ -313,7 +313,8 @@ pub(crate) fn invoke_unguarded(app: &mut PhotocraftApp, ctx: &egui::Context, id:
|
||||
"edit.paste" if params.as_object().is_none_or(|o| o.is_empty()) => {
|
||||
// Photoshop: paste in place when the copied area is visible, else centred in the view;
|
||||
// images from other apps are always centred.
|
||||
let external = app.import_os_clipboard();
|
||||
app.import_os_clipboard();
|
||||
let external = app.clip_external;
|
||||
let visible = !external
|
||||
&& app.session.active_index().zip(app.session.clipboard.as_ref()).is_some_and(|(i, clip)| {
|
||||
let v = &app.ui.views[i];
|
||||
|
||||
@@ -216,6 +216,11 @@ pub fn handle(app: &mut PhotocraftApp, ctx: &egui::Context) {
|
||||
continue;
|
||||
}
|
||||
if consume(ctx, &sc) {
|
||||
// ⌘V can arrive with an external image still on the OS clipboard (the periodic import
|
||||
// hasn't run yet): pull it in before deciding whether Paste is enabled.
|
||||
if matches!(id.as_str(), "edit.paste" | "edit.pasteSpecial.pasteInPlace") {
|
||||
app.import_os_clipboard();
|
||||
}
|
||||
if crate::menus::is_enabled(app, &id) {
|
||||
let r = if crate::adjust_dialog::has_dialog(&id) {
|
||||
crate::adjust_dialog::open(app, &id);
|
||||
|
||||
Reference in new issue
Block a user