Skip to content

PhotoCraft: An open-source, photo editor written by scratch in pure Rust

New Document: a Clipboard preset with the clipboard image's size (#891)

jason4shortcf758c5b04ea
diff --git a/crates/ui-egui/src/canvas.rs b/crates/ui-egui/src/canvas.rs
index b6b738bba..a54558ef4 100644
--- a/crates/ui-egui/src/canvas.rs
+++ b/crates/ui-egui/src/canvas.rs
@@ -1403,7 +1403,8 @@ fn start_screen(app: &mut PhotocraftApp, ui: &mut egui::Ui) {
ui.add_space(((card.width() - 2.0 * 190.0 - 12.0) / 2.0).max(0.0));
ui.spacing_mut().item_spacing.x = 12.0;
if crate::widgets::primary_button(ui, &new_label, 190.0).clicked() {
- app.ui.open_dialog(crate::state::DialogKind::NewDocument, crate::state::UiState::new_document_fields());
+ let fields = app.new_document_fields();
+ app.ui.open_dialog(crate::state::DialogKind::NewDocument, fields);
}
if crate::widgets::secondary_button(ui, &open_label, 190.0).clicked() {
app.open_dialog_file();
diff --git a/crates/ui-egui/src/lib.rs b/crates/ui-egui/src/lib.rs
index d002f8e59..3723121b9 100644
--- a/crates/ui-egui/src/lib.rs
+++ b/crates/ui-egui/src/lib.rs
@@ -1430,6 +1430,18 @@ impl PhotocraftApp {
}
}
+ /// File › New's fields: the defaults, plus the Clipboard preset (the clipboard image's size,
+ /// selected) when the clipboard holds an image. Opening the dialog is an explicit request, so
+ /// the OS clipboard is read here, as for a paste.
+ pub(crate) fn new_document_fields(&mut self) -> serde_json::Map<String, serde_json::Value> {
+ let mut f = crate::state::UiState::new_document_fields();
+ self.import_os_clipboard();
+ if let Some(c) = self.session.clipboard.as_ref().filter(|c| !c.bounds.is_empty()) {
+ crate::new_doc_ui::set_clipboard(&mut f, c.bounds.width(), c.bounds.height());
+ }
+ f
+ }
+
/// If the OS clipboard holds an image that isn't the one we put there, make it the session
/// clipboard (so ⌘V pastes screenshots and images copied in other apps, like Photoshop).
/// Returns true when a new external image was imported.
diff --git a/crates/ui-egui/src/menus.rs b/crates/ui-egui/src/menus.rs
index 0835c45b0..e73520c23 100644
--- a/crates/ui-egui/src/menus.rs
+++ b/crates/ui-egui/src/menus.rs
@@ -3,7 +3,7 @@
use serde_json::{Value, json};
use crate::PhotocraftApp;
-use crate::state::{DialogKind, UiState};
+use crate::state::DialogKind;
/// Top-level menus in Photoshop order.
pub const TOP_MENUS: [&str; 10] = ["File", "Edit", "Image", "Layer", "Type", "Select", "Filter", "View", "Window", "Help"];
@@ -215,7 +215,8 @@ pub(crate) fn invoke_unguarded(app: &mut PhotocraftApp, ctx: &egui::Context, id:
Ok(Value::Null)
}
"file.new" if params.as_object().is_none_or(|o| o.is_empty()) => {
- let d = app.ui.open_dialog(DialogKind::NewDocument, UiState::new_document_fields());
+ let fields = app.new_document_fields();
+ let d = app.ui.open_dialog(DialogKind::NewDocument, fields);
Ok(json!({"dialog": d}))
}
"file.open" => {
diff --git a/crates/ui-egui/src/new_doc_ui.rs b/crates/ui-egui/src/new_doc_ui.rs
index 035583153..701723367 100644
--- a/crates/ui-egui/src/new_doc_ui.rs
+++ b/crates/ui-egui/src/new_doc_ui.rs
@@ -101,6 +101,26 @@ pub fn px_value(px: f32) -> Value {
json!(if px.is_finite() { px.round().clamp(1.0, 300_000.0) as u32 } else { 1 })
}
+/// Name of the preset that takes the clipboard image's size.
+pub const CLIPBOARD: &str = "Clipboard";
+
+/// Offer the Clipboard preset (`w` × `h` px, at 72 ppi) first under the Recent presets, and
+/// select it.
+pub fn set_clipboard(f: &mut Map<String, Value>, w: u32, h: u32) {
+ if w == 0 || h == 0 {
+ return;
+ }
+ f.insert("__clipboard".into(), json!([w, h]));
+ apply_preset(f, &(CLIPBOARD, w, h, 72.0));
+}
+
+/// The clipboard image's size, when the dialog offers the Clipboard preset.
+fn clipboard_preset(f: &Map<String, Value>) -> Option<Preset> {
+ let size = f.get("__clipboard")?.as_array()?;
+ let dim = |i: usize| size.get(i)?.as_u64().and_then(|v| u32::try_from(v).ok()).filter(|v| *v > 0);
+ Some((CLIPBOARD, dim(0)?, dim(1)?, 72.0))
+}
+
/// Apply a preset to the dialog fields.
pub fn apply_preset(f: &mut Map<String, Value>, p: &Preset) {
f.insert("width".into(), json!(p.1));
@@ -173,6 +193,8 @@ pub fn body(ui: &mut egui::Ui, f: &mut Map<String, Value>) {
widgets::hairline(ui);
ui.add_space(8.0);
let presets = CATEGORIES.iter().find(|c| c.0 == cat).map_or(CATEGORIES[0].1, |c| c.1);
+ // The clipboard image's size comes first among the Recent presets.
+ let presets: Vec<Preset> = clipboard_preset(f).filter(|_| cat == CATEGORIES[0].0).into_iter().chain(presets.iter().copied()).collect();
let chosen = get_s(f, "__preset", "");
ui.horizontal_top(|ui| {
// Left: preset grid.
@@ -360,6 +382,25 @@ mod tests {
}
}
+ #[test]
+ fn clipboard_preset_comes_first_and_is_selected() {
+ // Nothing on the clipboard: the dialog opens as before.
+ let mut app = crate::PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default());
+ let f = app.new_document_fields();
+ assert_eq!(f, crate::state::UiState::new_document_fields());
+ assert!(clipboard_preset(&f).is_none());
+ // Pixels copied in the app: the Clipboard preset takes their size, at 72 ppi, selected.
+ app.run("file.new", json!({"width": 200, "height": 100})).unwrap();
+ app.run("select.rect", json!({"x": 10, "y": 20, "width": 123, "height": 45})).unwrap();
+ app.run("edit.copy", json!({})).unwrap();
+ let f = app.new_document_fields();
+ assert_eq!(clipboard_preset(&f), Some((CLIPBOARD, 123, 45, 72.0)));
+ assert_eq!((f["width"].as_u64(), f["height"].as_u64(), f["__preset"].as_str()), (Some(123), Some(45), Some(CLIPBOARD)));
+ let p = command_params(&f);
+ assert!(p.get("__clipboard").is_none(), "file.new never sees the dialog's keys");
+ assert_eq!((p["width"].as_u64(), p["height"].as_u64(), p["resolution"].as_f64()), (Some(123), Some(45), Some(72.0)));
+ }
+
#[test]
fn preset_sets_size_resolution_and_create_params_drop_ui_keys() {
let mut f = crate::state::UiState::new_document_fields();
@@ -472,6 +513,24 @@ mod tests {
assert_eq!(created(&h), (512, 512, 72.0));
}
+ #[test]
+ fn the_clipboard_card_is_first_and_creates_the_clipboard_size() {
+ let mut h = harness();
+ let mut f = fields(&h);
+ super::super::set_clipboard(&mut f, 640, 360);
+ set_fields(&mut h, f);
+ // Recent lists the Clipboard card first: three presets instead of two.
+ assert!(h.query_by_label_contains("BLANK DOCUMENT PRESETS (3)").is_some());
+ let heading = h.get_by_label_contains("BLANK DOCUMENT PRESETS").rect();
+ // Pick the second card, then the first (Clipboard) again.
+ click_at(&mut h, heading.left_bottom() + egui::vec2(80.0 + 172.0, 60.0));
+ assert_eq!(fields(&h).get("__preset").and_then(|v| v.as_str()), Some("Default Photoshop Size"));
+ click_at(&mut h, heading.left_bottom() + egui::vec2(80.0, 60.0));
+ assert_eq!(fields(&h).get("__preset").and_then(|v| v.as_str()), Some(super::super::CLIPBOARD));
+ enter(&mut h);
+ assert_eq!(created(&h), (640, 360, 72.0));
+ }
+
#[test]
fn clicking_a_preset_card_after_typing_sets_its_size() {
let mut h = harness();