PhotoCraft: An open-source, photo editor written by scratch in pure Rust
Number keys set opacity, Shift+[ / Shift+] step hardness, Shift+Cmd+A opens Camera Raw (part of #352) (#473)
BFlat93bd44899baf
diff --git a/crates/engine/src/lens_cmds.rs b/crates/engine/src/lens_cmds.rsindex f9c2cdadf..533642537 100644--- a/crates/engine/src/lens_cmds.rs+++ b/crates/engine/src/lens_cmds.rs@@ -401,7 +401,7 @@ pub fn specs() -> Vec<CommandSpec> {id: RAW,label: "Camera Raw Filter…",menu: &["Filter"],- shortcut: None,+ shortcut: Some("Cmd+Shift+A"),params: r##"{"temperature":-100..100=0,"tint":-100..100=0,"exposure":-5..5=0,"contrast":-100..100=0,"highlights":-100..100=0,"shadows":-100..100=0,"whites":-100..100=0,"blacks":-100..100=0,"texture":-100..100=0,"clarity":-100..100=0,"dehaze":-100..100=0,"vibrance":-100..100=0,"saturation":-100..100=0,"curveHighlights":-100..100=0,"curveLights":-100..100=0,"curveDarks":-100..100=0,"curveShadows":-100..100=0,"curveSplits":[25,50,75],"pointCurve":[[in,out]],"pointCurveRed":[[in,out]],"pointCurveGreen":[[in,out]],"pointCurveBlue":[[in,out]],"hslHue":[8],"hslSat":[8],"hslLum":[8],"gradeShadows":{"hue":deg,"sat":0..100,"lum":-100..100},"gradeMidtones":{},"gradeHighlights":{},"gradeGlobal":{},"gradeBlending":0..100=50,"gradeBalance":-100..100=0,"sharpenAmount":0..150=0,"sharpenRadius":0.5..3=1,"sharpenDetail":0..100=25,"sharpenMasking":0..100=0,"noiseLuminance":0..100=0,"noiseLuminanceDetail":0..100=50,"noiseColor":0..100=0,"noiseColorDetail":0..100=50,"grainAmount":0..100=0,"grainSize":0..100=25,"grainRoughness":0..100=50,"vignetteAmount":-100..100=0,"vignetteMidpoint":0..100=50,"vignetteRoundness":-100..100=0,"vignetteFeather":0..100=50,"vignetteHighlights":0..100=0,"vignetteStyle":"highlightPriority|colorPriority|paintOverlay","seed":u32=0}"##,enabled: raw_enabled,run: camera_raw_cmd,diff --git a/crates/ui-egui/src/lib.rs b/crates/ui-egui/src/lib.rsindex ed44289ef..ac58525af 100644--- a/crates/ui-egui/src/lib.rs+++ b/crates/ui-egui/src/lib.rs@@ -76,6 +76,7 @@ pub mod move_mods;pub mod move_ui;pub mod new_doc_ui;pub mod notices;+mod opacity_keys;pub mod outline;pub mod paint_mouse;pub mod palette;@@ -251,6 +252,8 @@ pub struct PhotocraftApp {/// This press began with ⌥ (Alt) held on a painting tool, so it samples colours instead of/// painting until it is released (`canvas::alt_eyedropper`, #417).pub(crate) alt_sampling: bool,+ /// The first digit of a two-digit opacity typed on the number keys (`opacity_keys`, #352).+ pub(crate) opacity_keys: opacity_keys::Pending,control_rx: Option<Receiver<ControlRequest>>,pending_screenshots: Vec<(u64, Option<String>, Sender<ControlResponse>)>,/// Screenshots not yet requested from the viewport: (token, earliest time in ms, frames seen).@@ -377,6 +380,7 @@ impl PhotocraftApp {brush_resize: None,brush_resize_armed: false,alt_sampling: false,+ opacity_keys: None,control_rx: None,pending_screenshots: Vec::new(),queued_screenshots: Vec::new(),diff --git a/crates/ui-egui/src/menu_catalog.rs b/crates/ui-egui/src/menu_catalog.rsindex 9f48793c9..e1da3b74a 100644--- a/crates/ui-egui/src/menu_catalog.rs+++ b/crates/ui-egui/src/menu_catalog.rs@@ -535,7 +535,7 @@ pub static CATALOG: &[(&[&str], &str, Option<&str>, &str)] = &[(&["Filter"], "Convert for Smart Filters", None, "filter.convertForSmartFilters"),(&["Filter"], "---", None, "---"),(&["Filter"], "Filter Gallery…", None, "filter.filterGallery"),- (&["Filter"], "Camera Raw Filter…", None, "filter.cameraRaw"),+ (&["Filter"], "Camera Raw Filter…", Some("Cmd+Shift+A"), "filter.cameraRaw"),(&["Filter"], "Adaptive Wide Angle…", Some("Cmd+Alt+Shift+A"), "filter.adaptiveWideAngle"),(&["Filter"], "Lens Correction…", Some("Cmd+Shift+R"), "filter.lensCorrection"),(&["Filter"], "Liquify…", Some("Cmd+Shift+X"), "filter.liquify"),diff --git a/crates/ui-egui/src/opacity_keys.rs b/crates/ui-egui/src/opacity_keys.rsnew file mode 100644index 000000000..1a52a781d--- /dev/null+++ b/crates/ui-egui/src/opacity_keys.rs@@ -0,0 +1,236 @@+//! Number keys set opacity (#352): 1 = 10% … 9 = 90%, 0 = 100%; two digits+//! typed quickly set an exact value (4 then 5 = 45%, 0 then 0 = 0%). With the Brush, Pencil or+//! Eraser they set the brush's Opacity (⇧: Flow); with the Gradient or Paint Bucket their+//! Opacity; with tools that have no opacity of their own (Move, the selection tools, Type…) the+//! active layer's Opacity (⇧: Fill). Tools whose number is a strength or exposure are left alone.++use egui::{Event, Key};+use serde_json::json;++use crate::PhotocraftApp;+use crate::state::Tool;++/// A second digit within this long of the first makes a two-digit value.+const TWO_DIGITS_MS: f64 = 800.0;++/// The first digit of a possible two-digit value: (digit, ⇧ held, when).+pub type Pending = Option<(u8, bool, f64)>;++/// What a tool's number keys change.+#[derive(Clone, Copy, Debug, PartialEq)]+enum Target {+ /// The shared brush settings' opacity (⇧: flow, when the tool has one).+ Brush {+ flow: bool,+ },+ /// The Gradient / Paint Bucket opacity option.+ ToolOpacity,+ /// The active layer's opacity (⇧: fill).+ Layer,+ None,+}++fn target(tool: Tool) -> Target {+ match tool {+ Tool::Brush | Tool::Eraser => Target::Brush { flow: true },+ Tool::Pencil => Target::Brush { flow: false },+ Tool::Gradient | Tool::PaintBucket => Target::ToolOpacity,+ // Their options-bar number is a strength, exposure or tolerance, not this opacity.+ t if t.is_brushlike() || t == Tool::MagicEraser => Target::None,+ _ => Target::Layer,+ }+}++fn digit(k: Key) -> Option<u8> {+ Some(match k {+ Key::Num0 => 0,+ Key::Num1 => 1,+ Key::Num2 => 2,+ Key::Num3 => 3,+ Key::Num4 => 4,+ Key::Num5 => 5,+ Key::Num6 => 6,+ Key::Num7 => 7,+ Key::Num8 => 8,+ Key::Num9 => 9,+ _ => return None,+ })+}++/// Consumes this frame's unmodified (or ⇧) digit presses and applies them.+pub fn handle(app: &mut PhotocraftApp, ctx: &egui::Context) {+ let presses: Vec<(u8, bool)> = ctx.input_mut(|i| {+ let mut out = Vec::new();+ i.events.retain(|e| match e {+ // The physical key, so ⇧+digit is still a digit whatever symbol the layout puts there.+ Event::Key { key, physical_key, pressed: true, modifiers, .. } if !modifiers.command && !modifiers.ctrl && !modifiers.alt => {+ match physical_key.and_then(digit).or_else(|| digit(*key)) {+ Some(d) => {+ out.push((d, modifiers.shift));+ false+ }+ None => true,+ }+ }+ _ => true,+ });+ out+ });+ let now = crate::gpu_canvas::now_ms();+ for (d, shift) in presses {+ press(app, d, shift, now);+ }+}++/// One digit press at `now` (ms).+pub fn press(app: &mut PhotocraftApp, d: u8, shift: bool, now: f64) {+ let percent = match app.opacity_keys.take() {+ Some((first, s, at)) if s == shift && now - at < TWO_DIGITS_MS && now >= at => f32::from(first * 10 + d),+ _ => {+ app.opacity_keys = Some((d, shift, now));+ if d == 0 { 100.0 } else { f32::from(d) * 10.0 }+ }+ };+ apply(app, percent, shift);+}++fn apply(app: &mut PhotocraftApp, percent: f32, shift: bool) {+ let v = percent / 100.0;+ match (target(app.ui.tool), shift) {+ (Target::Brush { .. }, false) => {+ let _ = app.run("tools.setBrush", json!({ "brush": { "opacity": v } }));+ }+ // The options bar's Flow field starts at 1%.+ (Target::Brush { flow: true }, true) => {+ let _ = app.run("tools.setBrush", json!({ "brush": { "flow": v.max(0.01) } }));+ }+ (Target::ToolOpacity, false) => app.ui.tool_options.fill_opacity = percent,+ (Target::Layer, _) => {+ // The locked Background has no opacity or fill.+ let Some(st) = app.session.active() else { return };+ let Some(layer) = st.active_layer.and_then(|id| st.doc.layer(id)) else { return };+ if crate::doc_props_ui::is_background(&st.doc, layer) {+ return;+ }+ let key = if shift { "fill" } else { "opacity" };+ let _ = app.run("layer.setProps", json!({ "layer": layer.id.0, key: v }));+ }+ _ => {}+ }+}++#[cfg(test)]+mod tests {+ use super::*;++ fn app(tool: Tool) -> PhotocraftApp {+ let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), Default::default());+ app.run("file.new", json!({"width": 32, "height": 32})).unwrap();+ app.run("layer.new.layer", json!({})).unwrap();+ app.ui.tool = tool;+ app+ }++ fn layer(app: &PhotocraftApp) -> (f32, f32) {+ let st = app.session.active().unwrap();+ let l = st.doc.layer(st.active_layer.unwrap()).unwrap();+ (l.opacity, l.fill_opacity)+ }++ #[test]+ fn digits_set_the_brush_opacity_and_shift_digits_its_flow() {+ let mut a = app(Tool::Brush);+ press(&mut a, 5, false, 0.0);+ assert_eq!(a.session.tools.brush.opacity, 0.5);+ // Two quick digits: an exact value; a slow second digit starts over.+ press(&mut a, 4, false, 1000.0);+ press(&mut a, 5, false, 1300.0);+ assert!((a.session.tools.brush.opacity - 0.45).abs() < 1e-6);+ press(&mut a, 3, false, 5000.0);+ assert!((a.session.tools.brush.opacity - 0.3).abs() < 1e-6);+ press(&mut a, 0, false, 9000.0);+ assert_eq!(a.session.tools.brush.opacity, 1.0, "0 = 100%");+ press(&mut a, 0, false, 9100.0);+ assert_eq!(a.session.tools.brush.opacity, 0.0, "00 = 0%");+ press(&mut a, 2, true, 20000.0);+ assert!((a.session.tools.brush.flow - 0.2).abs() < 1e-6);+ press(&mut a, 0, true, 30000.0);+ assert_eq!(a.session.tools.brush.flow, 1.0);+ press(&mut a, 0, true, 30100.0);+ assert_eq!(a.session.tools.brush.flow, 0.01, "flow keeps its 1% floor");+ assert_eq!(layer(&a), (1.0, 1.0), "the layer is untouched");+ // The Pencil has no flow.+ let mut p = app(Tool::Pencil);+ press(&mut p, 7, true, 0.0);+ assert_eq!(p.session.tools.brush.flow, 1.0);+ }++ #[test]+ fn other_tools_set_the_layer_or_their_own_opacity() {+ let mut a = app(Tool::Move);+ let rev = a.session.active().unwrap().revision;+ press(&mut a, 6, false, 0.0);+ press(&mut a, 3, true, 5000.0);+ assert!((layer(&a).0 - 0.6).abs() < 1e-6 && (layer(&a).1 - 0.3).abs() < 1e-6);+ assert!(a.session.active().unwrap().revision > rev, "a layer change is an edit");+ a.ui.tool = Tool::PaintBucket;+ press(&mut a, 2, false, 9000.0);+ assert_eq!(a.ui.tool_options.fill_opacity, 20.0);+ // Dodge's number is its exposure: nothing changes here.+ a.ui.tool = Tool::Dodge;+ let before = (a.session.tools.brush.clone(), layer(&a), a.ui.tool_options.fill_opacity);+ press(&mut a, 9, false, 20000.0);+ assert_eq!((a.session.tools.brush.clone(), layer(&a), a.ui.tool_options.fill_opacity), before);+ // The locked Background keeps its opacity; no document is a no-op.+ a.ui.tool = Tool::Move;+ let bg = a.session.active().unwrap().doc.layers[0].id;+ a.run("layer.select", json!({"layer": bg.0})).unwrap();+ press(&mut a, 5, false, 30000.0);+ assert_eq!(a.session.active().unwrap().doc.layers[0].opacity, 1.0);+ let mut none = PhotocraftApp::new(photocraft_engine::Session::new(), Default::default());+ press(&mut none, 5, false, 0.0);+ }++ #[test]+ fn number_key_events_reach_the_handler_and_modified_ones_do_not() {+ let mut a = app(Tool::Brush);+ let ctx = egui::Context::default();+ let key = |k: Key, m: egui::Modifiers| Event::Key { key: k, physical_key: Some(k), pressed: true, repeat: false, modifiers: m };+ let input = egui::RawInput { events: vec![key(Key::Num7, egui::Modifiers::NONE), key(Key::Num3, egui::Modifiers::COMMAND)], ..Default::default() };+ let mut out = ctx.run_ui(input, |ui| {+ handle(&mut a, ui.ctx());+ assert_eq!(ui.input(|i| i.events.len()), 1, "⌘3 is left for its shortcut");+ });+ out.textures_delta.clear();+ assert!((a.session.tools.brush.opacity - 0.7).abs() < 1e-6);+ }++ /// Through the whole app: number keys and ⇧[ ⇧] from the canvas (no widget focused).+ #[test]+ fn keys_work_in_the_app() {+ use egui_kittest::Harness;+ let mut h = Harness::builder().with_size(egui::vec2(1200.0, 800.0)).with_max_steps(64).build_eframe(|cc| {+ PhotocraftApp::setup_context(&cc.egui_ctx, Default::default());+ let mut a = app(Tool::Brush);+ a.run("tools.setBrush", json!({"brush": {"hardness": 0.5, "opacity": 1.0, "flow": 1.0}})).unwrap();+ a+ });+ h.run_steps(4);+ let key = |h: &mut Harness<'_, PhotocraftApp>, k: Key, m: egui::Modifiers| {+ h.event(Event::Key { key: k, physical_key: Some(k), pressed: true, repeat: false, modifiers: m });+ h.event(Event::Key { key: k, physical_key: Some(k), pressed: false, repeat: false, modifiers: m });+ h.run_steps(2);+ };+ key(&mut h, Key::Num5, egui::Modifiers::NONE);+ key(&mut h, Key::CloseBracket, egui::Modifiers::SHIFT);+ let b = &h.state().session.tools.brush;+ assert_eq!((b.opacity, b.hardness), (0.5, 0.75));+ key(&mut h, Key::OpenBracket, egui::Modifiers::SHIFT);+ key(&mut h, Key::OpenBracket, egui::Modifiers::SHIFT);+ key(&mut h, Key::OpenBracket, egui::Modifiers::SHIFT);+ assert_eq!(h.state().session.tools.brush.hardness, 0.0, "stops at 0%");+ let size = h.state().session.tools.brush.size;+ key(&mut h, Key::CloseBracket, egui::Modifiers::NONE);+ assert!(h.state().session.tools.brush.size > size, "] still resizes");+ }+}diff --git a/crates/ui-egui/src/shortcuts.rs b/crates/ui-egui/src/shortcuts.rsindex 40c20f27a..14cdd4635 100644--- a/crates/ui-egui/src/shortcuts.rs+++ b/crates/ui-egui/src/shortcuts.rs@@ -304,6 +304,23 @@ pub fn handle(app: &mut PhotocraftApp, ctx: &egui::Context) {return;}}+ // ⇧[ and ⇧] step a painting tool's hardness by 25% (#352). First: egui's `consume_key`+ // ignores ⇧, so the size keys below would take them.+ if app.ui.tool.is_brushlike() {+ let hardness = app.session.tools.brush.hardness;+ let step = |sc: &str| parse(sc).is_some_and(|sc| consume(ctx, &sc));+ let quarter = (hardness * 4.0).round();+ let next = if step("Shift+[") {+ (quarter - 1.0).max(0.0) / 4.0+ } else if step("Shift+]") {+ (quarter + 1.0).min(4.0) / 4.0+ } else {+ hardness+ };+ if next != hardness {+ let _ = app.run("tools.setBrush", serde_json::json!({ "brush": { "hardness": next } }));+ }+ }// [ and ] resize the brush through `tools.setBrush` (journaled, drivable).let size = app.session.tools.brush.size;let next = if pressed(Key::OpenBracket) {@@ -316,6 +333,8 @@ pub fn handle(app: &mut PhotocraftApp, ctx: &egui::Context) {if next != size {let _ = app.run("tools.setBrush", serde_json::json!({ "brush": { "size": next } }));}+ // 1–0 set opacity, ⇧1–0 flow or fill (`opacity_keys`).+ crate::opacity_keys::handle(app, ctx);}#[cfg(test)]