Merge main into issue-635-ctrl-scroll-pan (main's automation test fixed in #1074)

This commit is contained in:
Claude committed 2026-10-08 18:21:01 +00:00
commit 6f5bc2e6c7
32 files changed
+2158 -69

No files matched your search

+3 -3
View File
@@ -39,7 +39,7 @@ jobs:
version:
name: version
runs-on: ubuntu-latest
environment: release
# No `environment: release`: this job reads no secrets, so a branch dry run can run it.
outputs:
version: ${{ steps.v.outputs.version }}
date: ${{ steps.v.outputs.date }}
@@ -186,7 +186,7 @@ jobs:
# The oldest GitHub-hosted image, so the binaries only need glibc >= 2.35 (Ubuntu 22.04+,
# Debian 12+, Fedora 36+, RHEL 10). See docs/releasing.md.
runs-on: ${{ matrix.runner }}
environment: release
# No `environment: release`: this job reads no secrets, so a branch dry run can run it.
strategy:
fail-fast: false
matrix:
@@ -339,7 +339,7 @@ jobs:
name: Web (wasm)
needs: version
runs-on: ubuntu-latest
environment: release
# No `environment: release`: this job reads no secrets, so a branch dry run can run it.
env:
PHOTOCRAFT_VERSION: ${{ needs.version.outputs.version }}
PHOTOCRAFT_BUILD_DATE: ${{ needs.version.outputs.date }}
Generated
+67 -29
View File
@@ -1025,6 +1025,15 @@ dependencies = [
"itertools 0.10.5",
]
[[package]]
name = "crossbeam-channel"
version = "0.5.17"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "98b0cc327b5bc766e7fda9c9260cc0fa81b43a8e240440422dff70788e3f9ef1"
dependencies = [
"crossbeam-utils",
]
[[package]]
name = "crossbeam-deque"
version = "0.8.8"
@@ -2451,6 +2460,16 @@ dependencies = [
"wasm-bindgen",
]
[[package]]
name = "keyboard-types"
version = "0.8.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0fbe853b403ae61a04233030ae8a79d94975281ed9770a1f9e246732b534b28d"
dependencies = [
"bitflags 2.13.2",
"serde",
]
[[package]]
name = "khronos-egl"
version = "6.0.0"
@@ -2707,6 +2726,24 @@ dependencies = [
"pxfm",
]
[[package]]
name = "muda"
version = "0.21.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9a7b000cca0783af26837fe2ebd9ddf1c42279463c085b97622ee1d2f444b89e"
dependencies = [
"crossbeam-channel",
"dpi",
"keyboard-types",
"objc2 0.6.4",
"objc2-app-kit 0.3.2",
"objc2-core-foundation",
"objc2-foundation 0.3.2",
"png 0.18.1",
"thiserror 2.0.21",
"windows-sys 0.61.2",
]
[[package]]
name = "naga"
version = "30.0.1"
@@ -3263,7 +3300,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7d8fae84b431384b68627d0f9b3b1245fcf9f46f6c0e3dc902e9dce64edd1967"
dependencies = [
"libc",
"windows-sys 0.52.0",
"windows-sys 0.61.2",
]
[[package]]
@@ -3421,13 +3458,14 @@ dependencies = [
[[package]]
name = "photocraft"
version = "0.3.0"
version = "0.5.0"
dependencies = [
"arboard",
"eframe",
"egui",
"fmv-macos-events",
"log",
"muda",
"objc2 0.6.4",
"objc2-app-kit 0.3.2",
"objc2-foundation 0.3.2",
@@ -3454,7 +3492,7 @@ dependencies = [
[[package]]
name = "photocraft-algo"
version = "0.3.0"
version = "0.5.0"
dependencies = [
"photocraft-color",
"photocraft-geom",
@@ -3466,7 +3504,7 @@ dependencies = [
[[package]]
name = "photocraft-automation"
version = "0.3.0"
version = "0.5.0"
dependencies = [
"base64",
"cap-std",
@@ -3488,7 +3526,7 @@ dependencies = [
[[package]]
name = "photocraft-cli"
version = "0.3.0"
version = "0.5.0"
dependencies = [
"photocraft-automation",
"photocraft-codecs",
@@ -3501,7 +3539,7 @@ dependencies = [
[[package]]
name = "photocraft-cms"
version = "0.3.0"
version = "0.5.0"
dependencies = [
"moxcms",
"rayon",
@@ -3509,7 +3547,7 @@ dependencies = [
[[package]]
name = "photocraft-codecs"
version = "0.3.0"
version = "0.5.0"
dependencies = [
"exr",
"flate2",
@@ -3529,7 +3567,7 @@ dependencies = [
[[package]]
name = "photocraft-color"
version = "0.3.0"
version = "0.5.0"
dependencies = [
"photocraft-cms",
"serde",
@@ -3537,7 +3575,7 @@ dependencies = [
[[package]]
name = "photocraft-compose"
version = "0.3.0"
version = "0.5.0"
dependencies = [
"criterion",
"photocraft-color",
@@ -3552,7 +3590,7 @@ dependencies = [
[[package]]
name = "photocraft-doc"
version = "0.3.0"
version = "0.5.0"
dependencies = [
"photocraft-color",
"photocraft-geom",
@@ -3563,7 +3601,7 @@ dependencies = [
[[package]]
name = "photocraft-engine"
version = "0.3.0"
version = "0.5.0"
dependencies = [
"blake3",
"flate2",
@@ -3593,7 +3631,7 @@ dependencies = [
[[package]]
name = "photocraft-format"
version = "0.3.0"
version = "0.5.0"
dependencies = [
"blake3",
"flate2",
@@ -3611,14 +3649,14 @@ dependencies = [
[[package]]
name = "photocraft-geom"
version = "0.3.0"
version = "0.5.0"
dependencies = [
"serde",
]
[[package]]
name = "photocraft-gpu"
version = "0.3.0"
version = "0.5.0"
dependencies = [
"half",
"log",
@@ -3635,14 +3673,14 @@ dependencies = [
[[package]]
name = "photocraft-heif"
version = "0.3.0"
version = "0.5.0"
dependencies = [
"heic-rs",
]
[[package]]
name = "photocraft-io"
version = "0.3.0"
version = "0.5.0"
dependencies = [
"blake3",
"photocraft-algo",
@@ -3666,14 +3704,14 @@ dependencies = [
[[package]]
name = "photocraft-ops"
version = "0.3.0"
version = "0.5.0"
dependencies = [
"photocraft-doc",
]
[[package]]
name = "photocraft-paint"
version = "0.3.0"
version = "0.5.0"
dependencies = [
"photocraft-color",
"photocraft-geom",
@@ -3684,7 +3722,7 @@ dependencies = [
[[package]]
name = "photocraft-plugins"
version = "0.3.0"
version = "0.5.0"
dependencies = [
"photocraft-color",
"photocraft-geom",
@@ -3699,7 +3737,7 @@ dependencies = [
[[package]]
name = "photocraft-psd"
version = "0.3.0"
version = "0.5.0"
dependencies = [
"flate2",
"photocraft-psd",
@@ -3710,7 +3748,7 @@ dependencies = [
[[package]]
name = "photocraft-raster"
version = "0.3.0"
version = "0.5.0"
dependencies = [
"photocraft-color",
"photocraft-geom",
@@ -3720,7 +3758,7 @@ dependencies = [
[[package]]
name = "photocraft-raw"
version = "0.3.0"
version = "0.5.0"
dependencies = [
"photocraft-raw",
"png 0.18.1",
@@ -3729,7 +3767,7 @@ dependencies = [
[[package]]
name = "photocraft-tablet"
version = "0.3.0"
version = "0.5.0"
dependencies = [
"block2 0.6.2",
"objc2 0.6.4",
@@ -3741,7 +3779,7 @@ dependencies = [
[[package]]
name = "photocraft-testkit"
version = "0.3.0"
version = "0.5.0"
dependencies = [
"memory-stats",
"photocraft-color",
@@ -3752,7 +3790,7 @@ dependencies = [
[[package]]
name = "photocraft-text"
version = "0.3.0"
version = "0.5.0"
dependencies = [
"flate2",
"parley",
@@ -3766,7 +3804,7 @@ dependencies = [
[[package]]
name = "photocraft-ui-egui"
version = "0.3.0"
version = "0.5.0"
dependencies = [
"base64",
"eframe",
@@ -3800,7 +3838,7 @@ dependencies = [
[[package]]
name = "photocraft-vector"
version = "0.3.0"
version = "0.5.0"
dependencies = [
"photocraft-color",
"photocraft-doc",
@@ -3811,7 +3849,7 @@ dependencies = [
[[package]]
name = "photocraft-web"
version = "0.3.0"
version = "0.5.0"
dependencies = [
"eframe",
"egui",
@@ -6377,7 +6415,7 @@ checksum = "ec7a2a501ed189703dba8b08142f057e887dfc4b2cc4db2d343ac6376ba3e0b9"
[[package]]
name = "xtask"
version = "0.3.0"
version = "0.5.0"
dependencies = [
"serde",
"serde_json",
+1 -1
View File
@@ -4,7 +4,7 @@ members = ["crates/*", "apps/*", "xtask"]
default-members = ["crates/*", "apps/photocraft", "apps/photocraft-cli", "xtask"]
[workspace.package]
version = "0.3.0"
version = "0.5.0"
edition = "2024"
license = "MIT OR Apache-2.0"
rust-version = "1.95"
+46
View File
@@ -52,6 +52,7 @@
<a href="#built-for-agents">Agents</a> ·
<a href="#under-the-hood">Under the hood</a> ·
<a href="#get-started">Get started</a> ·
<a href="#downloads">Downloads</a> ·
<a href="#the-crafting-apps">Crafting Apps</a> ·
<a href="https://discord.gg/artcraft">Discord</a>
</p>
@@ -312,6 +313,51 @@ PhotoCraft is tested against real files: our own Photoshop-authored oracle PSDs
sha256-verified. Fetch them with `cargo xtask corpus --all` and run the tests with
`cargo xtask test-corpus` (details in [docs/development.md](docs/development.md#test-corpora)).
## Downloads
**New to PhotoCraft?** Download it from the [PhotoCraft page on getartcraft.com](https://getartcraft.com/apps/photocraft). That's the easiest way to install it.
**Want a specific build or format?** On GitHub, the [latest release](https://github.com/storytold/photocraft/releases/latest) has every build listed below, and [all releases](https://github.com/storytold/photocraft/releases) has earlier versions and their notes. `<ver>` in the file names is the version number, and `SHA256SUMS.txt` lists a checksum for every file.
### Windows
| Build | Installer | Portable |
|---|---|---|
| x64 (64-bit Intel/AMD) | `photocraft-<ver>-windows-x64.msi` | `photocraft-<ver>-windows-x64-portable.zip` |
| arm64 (Snapdragon and other ARM PCs) | `photocraft-<ver>-windows-arm64.msi` | `photocraft-<ver>-windows-arm64-portable.zip` |
| x86 (32-bit) | `photocraft-<ver>-windows-x86.msi` | `photocraft-<ver>-windows-x86-portable.zip` |
Installers and executables are code-signed.
### macOS
| Build | File | Notes |
|---|---|---|
| App, universal (Apple silicon + Intel) | `photocraft-<ver>-macos-universal.dmg` | Signed and notarized |
| Command-line tool, universal | `photocraft-cli-<ver>-macos-universal.zip` | Signed and notarized |
### Linux
| Format | x86_64 | aarch64 (ARM64) | Notes |
|---|---|---|---|
| AppImage | `photocraft-<ver>-linux-x86_64.AppImage` | `photocraft-<ver>-linux-aarch64.AppImage` | Runs anywhere; updates itself with [AppImageUpdate](https://github.com/AppImageCommunity/AppImageUpdate) (`.zsync` files) |
| Flatpak | `photocraft-<ver>-linux-x86_64.flatpak` | `photocraft-<ver>-linux-aarch64.flatpak` | Sandboxed; `flatpak install --user <file>` |
| Debian/Ubuntu | `photocraft-<ver>-linux-x86_64.deb` | `photocraft-<ver>-linux-aarch64.deb` | |
| Fedora/RHEL/openSUSE | `photocraft-<ver>-linux-x86_64.rpm` | `photocraft-<ver>-linux-aarch64.rpm` | |
| Tarball | `photocraft-<ver>-linux-x86_64.tar.gz` | `photocraft-<ver>-linux-aarch64.tar.gz` | Unpack anywhere |
### FreeBSD
| Build | File |
|---|---|
| x86_64 | `photocraft-<ver>-freebsd-x86_64.tar.gz` |
### Web (WebAssembly)
| Build | File | Notes |
|---|---|---|
| Static site | `photocraft-web-<ver>.zip` | Runs in a modern browser; host it on any static server |
## The Crafting Apps
PhotoCraft is one of the **Crafting Apps**: free, open-source creative tools from the
+12
View File
@@ -52,6 +52,9 @@ fmv-macos-events = "=0.1.0"
objc2 = "=0.6.4"
objc2-foundation = { version = "=0.3.2", default-features = false, features = ["std", "NSArray", "NSEnumerator", "NSGeometry"] }
objc2-app-kit = { version = "=0.3.2", default-features = false, features = ["std", "NSApplication", "NSApplicationScripting", "NSEvent", "NSResponder", "NSView", "NSWindow"] }
# The macOS menu bar (mac_menu.rs): Tauri's menu crate, AppKit menus behind a safe API (no GTK
# with default features off). Pinned exactly: review the diff before bumping.
muda = { version = "=0.21.1", default-features = false }
# Drop position on X11 (`cursor.rs`): pure-Rust X client, already in Cargo.lock; pinned exactly.
[target.'cfg(target_os = "linux")'.dependencies]
@@ -62,6 +65,15 @@ x11rb = { version = "=0.13.2", default-features = false }
[target.'cfg(target_os = "windows")'.dependencies]
winsafe = { version = "=0.0.29", default-features = false, features = ["user"] }
# tests/mac_menu_appkit.rs: reads AppKit's menu bar back and sends it real key events.
[target.'cfg(target_os = "macos")'.dev-dependencies]
objc2-foundation = { version = "=0.3.2", default-features = false, features = ["std", "NSString"] }
objc2-app-kit = { version = "=0.3.2", default-features = false, features = ["std", "NSApplication", "NSEvent", "NSGraphicsContext", "NSMenu", "NSMenuItem", "NSResponder"] }
[[test]]
name = "mac_menu_appkit"
harness = false
[build-dependencies]
# Icon + VERSIONINFO for photocraft.exe; build.rs only uses it when targeting Windows.
winresource = "0.1"
+359
View File
@@ -0,0 +1,359 @@
//! The macOS menu bar, through muda (Tauri's menu crate: AppKit menus behind a safe API, no GTK on
//! macOS). The menus themselves, their Mac layout and how their keys reach PhotoCraft are in
//! `photocraft_ui_egui::native_menu`; this file only builds and updates the `NSMenu`s.
//!
//! - Item ids are command ids; a command in two menus (Keyboard Shortcuts is in Edit and Window)
//! gets `id#2` for its second copy, as muda ids must be unique.
//! - A structure change (a new Open Recent file, a language switch) rebuilds the menu; anything
//! else (labels, enabled, checked) is updated in place.
//! - A chosen item is a click or a key equivalent: AppKit's current event says which. Key
//! equivalents go back to egui as the key press that was made (the event's modifiers, not the
//! item's: AppKit may match ⌘R to a ⇧⌘R item), so PhotoCraft's key rules still apply.
//! - Hide is a custom item calling `NSApplication hide:`, as the predefined one always takes ⌘H.
use std::collections::HashMap;
use std::sync::mpsc::{Receiver, channel};
use muda::accelerator::{Accelerator, Code, Modifiers};
use muda::{CheckMenuItem, Menu, MenuEvent, MenuItem, PredefinedMenuItem, Submenu};
use objc2::MainThreadMarker;
use objc2_app_kit::{NSApplication, NSEventModifierFlags, NSEventType};
use photocraft_ui_egui::native_menu::{self, Backend, Chord, Event, MenuBar, MenuRole, Node, Standard};
enum Handle {
Plain(MenuItem),
Check(CheckMenuItem),
}
/// One native item.
struct Entry {
handle: Handle,
/// The command id (several entries can share one).
command: String,
chord: Option<Chord>,
label: String,
enabled: bool,
}
/// How an item was chosen, read from AppKit's current event while it calls the handler.
struct Chosen {
id: String,
/// A key press (not a mouse click), and the modifiers held: ⌘, ⌃, ⌥, ⇧.
key: bool,
mods: [bool; 4],
}
pub struct MacMenu {
menu: Option<Menu>,
/// By muda id.
entries: HashMap<String, Entry>,
rx: Receiver<Chosen>,
structure: u64,
}
impl MacMenu {
pub fn new(ctx: &egui::Context) -> MacMenu {
let (tx, rx) = channel();
let repaint = ctx.clone();
MenuEvent::set_event_handler(Some(move |e: MenuEvent| {
// AppKit calls this on the main thread, from the event that chose the item: a key
// equivalent is a key-down, a click a mouse-up (or ↩ inside the open menu).
let event = MainThreadMarker::new().and_then(|mtm| NSApplication::sharedApplication(mtm).currentEvent());
let key = event.as_ref().is_some_and(|ev| ev.r#type() == NSEventType::KeyDown);
let flags = event.as_ref().map(|ev| ev.modifierFlags()).unwrap_or(NSEventModifierFlags::empty());
let mods = [NSEventModifierFlags::Command, NSEventModifierFlags::Control, NSEventModifierFlags::Option, NSEventModifierFlags::Shift]
.map(|m| flags.contains(m));
let _ = tx.send(Chosen { id: e.id.0, key, mods });
// Wake egui, so the item runs now rather than on the next mouse move.
repaint.request_repaint();
}));
MacMenu { menu: None, entries: HashMap::new(), rx, structure: 0 }
}
fn build(&mut self, bar: &MenuBar) {
if let Some(old) = self.menu.take() {
old.remove_for_nsapp();
}
self.entries.clear();
let menu = Menu::new();
let mut special = Vec::new();
for m in &bar.menus {
let sub = Submenu::new(escape(&m.title), true);
self.append(&sub, &m.children);
let _ = menu.append(&sub);
if matches!(m.role, MenuRole::Window | MenuRole::Help) {
special.push((m.role, sub));
}
}
menu.init_for_nsapp();
// Only once the menu is NSApp's: muda looks the submenus up in the installed main menu.
for (role, sub) in special {
match role {
MenuRole::Window => sub.set_as_windows_menu_for_nsapp(),
_ => sub.set_as_help_menu_for_nsapp(),
}
}
self.menu = Some(menu);
self.structure = native_menu::structure_key(bar);
}
fn append(&mut self, sub: &Submenu, nodes: &[Node]) {
for n in nodes {
match n {
Node::Separator => {
let _ = sub.append(&PredefinedMenuItem::separator());
}
Node::Submenu { label, children, .. } => {
let s = Submenu::new(escape(label), !children.is_empty());
self.append(&s, children);
let _ = sub.append(&s);
}
Node::Standard(item) => {
let p = match item {
Standard::Services => PredefinedMenuItem::services(None),
Standard::HideOthers => PredefinedMenuItem::hide_others(None),
Standard::ShowAll => PredefinedMenuItem::show_all(None),
Standard::Minimize => PredefinedMenuItem::minimize(None),
Standard::Zoom => PredefinedMenuItem::maximize(Some(photocraft_ui_egui::i18n::t("Zoom"))),
Standard::BringAllToFront => PredefinedMenuItem::bring_all_to_front(None),
Standard::CloseWindow => PredefinedMenuItem::close_window(None),
};
let _ = sub.append(&p);
}
Node::Item(it) => {
let id = self.unique_id(&it.id);
let chord = it.shortcut.as_deref().and_then(Chord::parse);
let accel = chord.as_ref().filter(|c| c.native_ok()).and_then(accelerator);
let handle = match it.checked {
Some(c) => {
let h = CheckMenuItem::with_id(id.clone(), escape(&it.label), it.enabled, c, accel);
let _ = sub.append(&h);
Handle::Check(h)
}
None => {
let h = MenuItem::with_id(id.clone(), escape(&it.label), it.enabled, accel);
let _ = sub.append(&h);
Handle::Plain(h)
}
};
self.entries.insert(id, Entry { handle, command: it.id.clone(), chord, label: it.label.clone(), enabled: it.enabled });
}
}
}
}
/// muda ids must be unique: the second `edit.keyboardShortcuts` becomes `…#2`.
fn unique_id(&self, id: &str) -> String {
let mut unique = id.to_string();
let mut n = 2;
while self.entries.contains_key(&unique) {
unique = format!("{id}#{n}");
n += 1;
}
unique
}
}
impl Backend for MacMenu {
fn sync(&mut self, bar: &MenuBar) {
if self.menu.is_none() || native_menu::structure_key(bar) != self.structure {
self.build(bar);
return;
}
let items: HashMap<&str, &native_menu::Item> = bar.items().into_iter().map(|it| (it.id.as_str(), it)).collect();
for e in self.entries.values_mut() {
let Some(it) = items.get(e.command.as_str()) else { continue };
if e.label != it.label {
let text = escape(&it.label);
match &e.handle {
Handle::Plain(h) => h.set_text(text),
Handle::Check(h) => h.set_text(text),
}
e.label = it.label.clone();
}
if e.enabled != it.enabled {
match &e.handle {
Handle::Plain(h) => h.set_enabled(it.enabled),
Handle::Check(h) => h.set_enabled(it.enabled),
}
e.enabled = it.enabled;
}
// Compare with AppKit's own state: muda toggles a check item when it's clicked.
if let (Handle::Check(h), Some(c)) = (&e.handle, it.checked)
&& h.is_checked() != c
{
h.set_checked(c);
}
}
}
fn drain(&mut self) -> Vec<Event> {
let mut out = Vec::new();
while let Ok(chosen) = self.rx.try_recv() {
// Ids that aren't ours are AppKit's own items (Services, Zoom, …), already handled.
let Some(e) = self.entries.get(&chosen.id) else { continue };
if e.command == native_menu::HIDE {
if let Some(mtm) = MainThreadMarker::new() {
NSApplication::sharedApplication(mtm).hide(None);
}
continue;
}
// A key-down chose it: its key equivalent, unless it was ↩ or Space inside the open
// menu (no ⌘ or ⌃, and the item's shortcut needs one). The key is the item's (AppKit
// matched on it); the modifiers are the ones held.
let [cmd, ctrl, alt, shift] = chosen.mods;
let key =
e.chord.as_ref().filter(|c| chosen.key && (cmd || ctrl || !(c.cmd || c.ctrl))).map(|c| Chord { cmd, ctrl, alt, shift, key: c.key.clone() });
out.push(match key {
Some(chord) if e.command != native_menu::MINIMIZE => Event::Key(chord),
_ => Event::Click(e.command.clone()),
});
}
out
}
}
/// Install the menu bar now (in eframe's creator closure, so winit's default menu doesn't stay).
pub fn install(ctx: &egui::Context, app: &photocraft_ui_egui::PhotocraftApp) -> Option<native_menu::NativeMenu> {
let language = &app.session.prefs().interface.language;
let lang = photocraft_ui_egui::i18n::Lang::from_pref(language);
let bar = native_menu::photocraft_layout(&photocraft_ui_egui::menus::menu_items(app), lang, language).bar;
// Never crash for a menu: if AppKit refuses, keep the in-window menus.
let built = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
let mut menu = MacMenu::new(ctx);
menu.sync(&bar);
menu
}));
match built {
Ok(menu) => Some(native_menu::NativeMenu::new(Box::new(menu))),
Err(_) => {
log::warn!("couldn't install the macOS menu bar; using the in-window menus");
None
}
}
}
/// `&` marks a mnemonic in muda labels.
fn escape(s: &str) -> String {
s.replace('&', "&&")
}
/// A key equivalent for AppKit. `Cmd` is ⌘ (`META`) and `Ctrl` is ⌃: never folded together.
fn accelerator(c: &Chord) -> Option<Accelerator> {
let mut mods = Modifiers::empty();
for (on, m) in [(c.cmd, Modifiers::META), (c.ctrl, Modifiers::CONTROL), (c.alt, Modifiers::ALT), (c.shift, Modifiers::SHIFT)] {
if on {
mods |= m;
}
}
const LETTERS: [Code; 26] = [
Code::KeyA,
Code::KeyB,
Code::KeyC,
Code::KeyD,
Code::KeyE,
Code::KeyF,
Code::KeyG,
Code::KeyH,
Code::KeyI,
Code::KeyJ,
Code::KeyK,
Code::KeyL,
Code::KeyM,
Code::KeyN,
Code::KeyO,
Code::KeyP,
Code::KeyQ,
Code::KeyR,
Code::KeyS,
Code::KeyT,
Code::KeyU,
Code::KeyV,
Code::KeyW,
Code::KeyX,
Code::KeyY,
Code::KeyZ,
];
const DIGITS: [Code; 10] =
[Code::Digit0, Code::Digit1, Code::Digit2, Code::Digit3, Code::Digit4, Code::Digit5, Code::Digit6, Code::Digit7, Code::Digit8, Code::Digit9];
const F: [Code; 24] = [
Code::F1,
Code::F2,
Code::F3,
Code::F4,
Code::F5,
Code::F6,
Code::F7,
Code::F8,
Code::F9,
Code::F10,
Code::F11,
Code::F12,
Code::F13,
Code::F14,
Code::F15,
Code::F16,
Code::F17,
Code::F18,
Code::F19,
Code::F20,
Code::F21,
Code::F22,
Code::F23,
Code::F24,
];
let k = c.key.as_str();
let mut chars = k.chars();
let code = match (chars.next(), chars.next()) {
(Some(ch), None) if ch.is_ascii_alphabetic() => *LETTERS.get((ch.to_ascii_uppercase() as usize).checked_sub('A' as usize)?)?,
(Some(ch), None) if ch.is_ascii_digit() => *DIGITS.get((ch as usize).checked_sub('0' as usize)?)?,
_ if c.is_function_key() => *F.get(k.get(1..)?.parse::<usize>().ok()?.checked_sub(1)?)?,
_ => match k {
"[" => Code::BracketLeft,
"]" => Code::BracketRight,
"\\" => Code::Backslash,
"/" => Code::Slash,
"=" => Code::Equal,
"-" => Code::Minus,
"'" => Code::Quote,
";" => Code::Semicolon,
"," => Code::Comma,
"." => Code::Period,
"`" => Code::Backquote,
"Delete" | "Backspace" => Code::Backspace,
"Enter" if !mods.is_empty() => Code::Enter,
"Tab" if !mods.is_empty() => Code::Tab,
"Space" if !mods.is_empty() => Code::Space,
_ => return None,
},
};
Some(Accelerator::new(mods, code))
}
#[cfg(test)]
mod tests {
use super::*;
/// Every shortcut the Mac menu shows becomes a key equivalent (or stays egui's, for bare keys).
#[test]
fn every_menu_shortcut_maps_to_a_key_equivalent() {
let app = photocraft_ui_egui::PhotocraftApp::new(photocraft_engine::Session::new(), photocraft_ui_egui::Services::default());
let bar = native_menu::photocraft_layout(&photocraft_ui_egui::menus::menu_items(&app), photocraft_ui_egui::i18n::Lang::EN, "auto").bar;
let mut native = 0;
for it in bar.items() {
let Some(c) = it.shortcut.as_deref().and_then(Chord::parse) else { continue };
if c.native_ok() {
assert!(accelerator(&c).is_some(), "{} ({}) has no key equivalent", c.portable(), it.id);
native += 1;
}
}
assert!(native > 70, "{native} key equivalents");
}
#[test]
fn control_and_command_stay_distinct() {
let a = accelerator(&Chord::parse("Cmd+M").unwrap()).unwrap();
let b = accelerator(&Chord::parse("Ctrl+Cmd+M").unwrap()).unwrap();
assert_ne!(a, b);
}
}
+14 -1
View File
@@ -2,12 +2,15 @@
//!
//! Usage: `photocraft [--control <port>] [--control-token <64-hex> |
//! --control-token-file <path>] [--automation-read-root <dir>]
//! [--automation-write-root <dir>] [--safe-gpu] [files…]`
//! [--automation-write-root <dir>] [--safe-gpu] [--in-window-menus] [files…]`
//!
//! `--safe-gpu` starts with the CPU renderer (no GPU canvas; a software adapter for the window
//! where the platform has one) for this launch, e.g. after a graphics driver crash. A start that
//! crashes inside the driver also falls back by itself next time (see `gpu_startup`).
//!
//! `--in-window-menus` (or `PHOTOCRAFT_IN_WINDOW_MENUS=1`) keeps the menus inside the window on
//! macOS instead of the macOS menu bar (`mac_menu`).
//!
//! `--control <port>` (or `PHOTOCRAFT_CONTROL_PORT`) starts a localhost JSON-lines control server.
//! The first line must authenticate; subsequent request lines get reply lines.
//! `{"id":1,"ok":true,"result":…}`. See `photocraft_ui_egui::control` for the methods.
@@ -26,6 +29,8 @@ mod crash_guard;
mod cursor;
mod gpu_startup;
#[cfg(target_os = "macos")]
mod mac_menu;
#[cfg(target_os = "macos")]
mod mac_window;
// Pure logic is tested on every platform; only Linux runs the check.
#[cfg(any(target_os = "linux", test))]
@@ -131,6 +136,7 @@ fn main() -> eframe::Result {
let mut automation_write_root = std::env::var_os("PHOTOCRAFT_AUTOMATION_WRITE_ROOT").map(std::path::PathBuf::from);
let mut files = Vec::new();
let mut safe_gpu = false;
let mut in_window_menus = std::env::var_os("PHOTOCRAFT_IN_WINDOW_MENUS").is_some_and(|v| !v.is_empty() && v != "0");
let mut args = std::env::args().skip(1);
while let Some(a) = args.next() {
match a.as_str() {
@@ -146,6 +152,7 @@ fn main() -> eframe::Result {
"--automation-read-root" => automation_read_root = args.next().map(std::path::PathBuf::from),
"--automation-write-root" => automation_write_root = args.next().map(std::path::PathBuf::from),
"--safe-gpu" => safe_gpu = true,
"--in-window-menus" => in_window_menus = true,
"--version" => {
println!("photocraft {}", photocraft_engine::build_info::long_version());
return Ok(());
@@ -360,7 +367,13 @@ fn main() -> eframe::Result {
#[cfg(target_os = "macos")]
{
app.services.os_events = Some(apple_events.connect(&cc.egui_ctx));
// The macOS menu bar, installed now so winit's default menu doesn't stay up.
if !in_window_menus {
app.services.native_menu = mac_menu::install(&cc.egui_ctx, &app);
}
}
#[cfg(not(target_os = "macos"))]
let _ = in_window_menus;
// Where file drags and drops are (winit 0.30 doesn't say).
app.services.cursor_pos = cursor::service(cc);
// Tablet pressure/tilt/eraser (winit drops them): the macOS monitor installed above
+171
View File
@@ -0,0 +1,171 @@
//! The macOS menu bar end to end, on the main thread (`harness = false`): install it through the
//! real muda backend (`src/mac_menu.rs`), read AppKit's menu bar back, open a document and see the
//! items update in place, then choose items the two ways AppKit can: a key equivalent (a real ⌘R
//! key-down through `-[NSMenu performKeyEquivalent:]`) must reach egui as the key press it was, and
//! a click (`-[NSMenu performActionForItemAtIndex:]`) must run as a click, also when the click is
//! ↩ inside the open menu.
// The app's own module, built into this test (cargo builds harness = false tests with cfg(test), so its
// unit tests compile here too without a harness to run them).
#[cfg(target_os = "macos")]
#[path = "../src/mac_menu.rs"]
#[allow(unused_imports, dead_code)]
mod mac_menu;
#[cfg(target_os = "macos")]
fn main() {
use objc2::MainThreadMarker;
use objc2::rc::Retained;
use objc2_app_kit::{NSApplication, NSEvent, NSEventMask, NSEventModifierFlags, NSEventSubtype, NSEventType, NSMenu};
use objc2_foundation::{NSPoint, NSString};
use photocraft_ui_egui::PhotocraftApp;
use photocraft_ui_egui::native_menu;
use serde_json::json;
let mtm = MainThreadMarker::new().expect("harness = false runs on the main thread");
let nsapp = NSApplication::sharedApplication(mtm);
nsapp.finishLaunching();
let titles = |menu: &NSMenu| -> Vec<String> {
(0..menu.numberOfItems()).filter_map(|i| menu.itemAtIndex(i)).filter(|it| !it.isSeparatorItem()).map(|it| it.title().to_string()).collect()
};
let submenu = |menu: &NSMenu, title: &str| -> Retained<NSMenu> {
(0..menu.numberOfItems())
.filter_map(|i| menu.itemAtIndex(i))
.find(|it| it.title().to_string() == title)
.and_then(|it| it.submenu())
.unwrap_or_else(|| panic!("no {title} submenu in {:?}", titles(menu)))
};
let index = |menu: &NSMenu, title: &str| -> isize {
(0..menu.numberOfItems())
.find(|&i| menu.itemAtIndex(i).is_some_and(|it| it.title().to_string() == title))
.unwrap_or_else(|| panic!("no {title} in {:?}", titles(menu)))
};
let main_menu = || nsapp.mainMenu().expect("a main menu");
// Make `event` AppKit's current event, as the run loop does before dispatching it.
let dequeue = |event: &NSEvent| {
nsapp.postEvent_atStart(event, true);
let mode = NSString::from_str("kCFRunLoopDefaultMode");
let got = nsapp.nextEventMatchingMask_untilDate_inMode_dequeue(NSEventMask::Any, None, &mode, true).expect("the posted event");
assert_eq!(got.r#type(), event.r#type());
got
};
let key_down = |chars: &str, code: u16, flags: NSEventModifierFlags| {
let s = NSString::from_str(chars);
NSEvent::keyEventWithType_location_modifierFlags_timestamp_windowNumber_context_characters_charactersIgnoringModifiers_isARepeat_keyCode(
NSEventType::KeyDown,
NSPoint::new(0.0, 0.0),
flags,
0.0,
0,
None,
&s,
&s,
false,
code,
)
.expect("a key event")
};
let other = || {
NSEvent::otherEventWithType_location_modifierFlags_timestamp_windowNumber_context_subtype_data1_data2(
NSEventType::ApplicationDefined,
NSPoint::new(0.0, 0.0),
NSEventModifierFlags::empty(),
0.0,
0,
None,
NSEventSubtype::ApplicationActivated.0,
0,
0,
)
.expect("an application-defined event")
};
let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), photocraft_ui_egui::Services::default());
let ctx = egui::Context::default();
let frame = |app: &mut PhotocraftApp, raw: egui::RawInput| {
let mut out = ctx.run_ui(raw, |ui| {
native_menu::run(app, ui.ctx());
photocraft_ui_egui::shortcuts::handle(app, ui.ctx());
native_menu::sync(app, ui.ctx());
});
out.textures_delta.clear();
};
app.services.native_menu = mac_menu::install(&ctx, &app);
assert!(app.services.native_menu.is_some(), "the menu bar installed");
// The Mac layout, as AppKit has it.
let bar = main_menu();
let tops: Vec<String> = (0..bar.numberOfItems()).filter_map(|i| bar.itemAtIndex(i)?.submenu()).map(|m| m.title().to_string()).collect();
assert_eq!(tops, ["PhotoCraft", "File", "Edit", "Image", "Layer", "Type", "Select", "Filter", "View", "Window", "Help"]);
let app_menu = submenu(&bar, "PhotoCraft");
let names = titles(&app_menu);
for want in ["About PhotoCraft", "Settings", "Language", "Appearance", "Hide PhotoCraft", "Quit PhotoCraft"] {
assert!(names.iter().any(|n| n == want), "{want} missing from the app menu: {names:?}");
}
let quit = app_menu.itemAtIndex(index(&app_menu, "Quit PhotoCraft")).expect("Quit");
assert_eq!(quit.keyEquivalent().to_string(), "q");
assert_eq!(quit.keyEquivalentModifierMask(), NSEventModifierFlags::Command);
assert!(!titles(&submenu(&bar, "File")).iter().any(|n| n == "Exit"), "Exit moved to the app menu as Quit");
assert_eq!(nsapp.windowsMenu().map(|m| m.title().to_string()).as_deref(), Some("Window"), "AppKit knows the Window menu");
assert_eq!(nsapp.helpMenu().map(|m| m.title().to_string()).as_deref(), Some("Help"), "AppKit knows the Help menu");
// Opening a document enables Layer › New › Layer… in place: same NSMenu, no rebuild.
let new_layer = |bar: &NSMenu| {
let new = submenu(&submenu(bar, "Layer"), "New");
new.itemAtIndex(index(&new, "Layer…")).expect("Layer…").isEnabled()
};
assert!(!new_layer(&bar), "no document: New Layer is disabled");
app.run("file.new", json!({"width": 64, "height": 48})).expect("a document");
frame(&mut app, egui::RawInput::default());
assert!(Retained::as_ptr(&bar) == Retained::as_ptr(&main_menu()), "opening a document updated the menu, not rebuilt it");
assert!(new_layer(&main_menu()), "with a document New Layer is enabled");
// A key equivalent: ⌘R reaches egui as ⌘R and toggles View › Rulers once.
let rulers = app.ui.extras.rulers;
let press = dequeue(&key_down("r", 15, NSEventModifierFlags::Command));
assert!(main_menu().performKeyEquivalent(&press), "⌘R is View › Rulers' key equivalent");
let mut raw = egui::RawInput::default();
if let Some(m) = app.services.native_menu.as_mut() {
m.raw_input(&mut raw);
}
let keys: Vec<_> = raw
.events
.iter()
.filter_map(|e| if let egui::Event::Key { key, pressed, modifiers, .. } = e { Some((*key, *pressed, modifiers.command)) } else { None })
.collect();
assert_eq!(keys, [(egui::Key::R, true, true), (egui::Key::R, false, true)], "the key press, then its release");
frame(&mut app, raw);
let ran: Vec<String> = photocraft_ui_egui::shortcut_dispatch::take_log(&ctx).into_iter().map(|(id, _)| id).collect();
assert_eq!(ran, ["view.rulers"], "⌘R ran Rulers, whichever ⌘R item AppKit matched");
assert_eq!(app.ui.extras.rulers, !rulers, "⌘R toggled Rulers once");
// A click: View › Rulers chosen with the mouse runs the command.
let view = submenu(&main_menu(), "View");
let at = index(&view, "Rulers");
dequeue(&other());
view.performActionForItemAtIndex(at);
let mut raw = egui::RawInput::default();
if let Some(m) = app.services.native_menu.as_mut() {
m.raw_input(&mut raw);
}
assert!(raw.events.is_empty(), "a click is no key press: {:?}", raw.events);
frame(&mut app, raw);
assert_eq!(app.ui.extras.rulers, rulers, "the click toggled Rulers back");
// ↩ inside the open menu is a key-down without ⌘: still a click.
dequeue(&key_down("\r", 36, NSEventModifierFlags::empty()));
view.performActionForItemAtIndex(at);
let mut raw = egui::RawInput::default();
if let Some(m) = app.services.native_menu.as_mut() {
m.raw_input(&mut raw);
}
assert!(raw.events.is_empty(), "↩ in the menu is a click: {:?}", raw.events);
frame(&mut app, raw);
assert_eq!(app.ui.extras.rulers, !rulers, "↩ on Rulers toggled it");
println!("mac_menu_appkit: ok");
}
#[cfg(not(target_os = "macos"))]
fn main() {}
+3 -1
View File
@@ -33,7 +33,9 @@ Automation is a privilege boundary because requests can cause filesystem access,
are rejected before file effects.
- Engine commands that still use ambient paths fail closed at the automation boundary. Synthetic
UI input reaches the same command policy, and automation-triggered file hooks do not run
user-configured script-event paths.
user-configured script-event paths. The policy also covers every command a command runs on its
own behalf (an action's steps, `file.automate.conditionalModeChange` running `image.mode.*`), so
an allowed command can't reach a denied one.
- `prefs.set` applies the engine's dotted-path normalization before checking for filesystem-bearing
preference sections, including paths with leading or repeated dots. It refuses whole-preferences
updates and those sections; individual safe preference keys remain available.
+12
View File
@@ -460,6 +460,14 @@ mod tests {
assert!(authorize_engine_command("prefs.set", &serde_json::json!({"path": "colorSettings.workingRgb", "value": "outside.icc"})).is_err());
assert!(authorize_engine_step("file.open", &serde_json::json!({})).is_err());
assert!(authorize_engine_step("actions.play", &serde_json::json!({})).is_ok());
// An allowed command can't reach a denied one by running it on its own behalf.
let mut session = photocraft_engine::Session::new();
session.execute("file.new", serde_json::json!({"width": 4, "height": 4})).unwrap();
session.authorize = Some(authorize_desktop_engine_step);
let params = serde_json::json!({"to": "grayscale"});
assert!(authorize_desktop_engine_command("file.automate.conditionalModeChange", &params).is_ok());
assert!(session.execute("file.automate.conditionalModeChange", params).is_err());
assert_eq!(session.active().unwrap().doc.mode, photocraft_engine::doc::ColorMode::Rgb);
assert!(authorize_desktop_engine_step("file.saveACopy", &serde_json::json!({})).is_err());
}
@@ -576,6 +584,10 @@ mod tests {
| "path.info"
| "path.set"
| "path.transform"
| "path.moveAnchors"
| "path.moveHandle"
| "path.bendSegment"
| "path.convertPoint"
| "path.clippingPath.set"
| "path.rename"
| "select.toWorkPath"
+47 -5
View File
@@ -68,13 +68,26 @@ fn lift(src: &Surface, sel: Option<&Surface>, canvas: Rect) -> Clip {
if area.is_empty() {
return Clip { surface: out, bounds: Rect::EMPTY };
}
let conv = src.convert(with_alpha);
let mut px = conv.read_region(area);
// Only the selected area is read: converting the whole layer (a 24 MP Background gaining
// alpha) cost about a second per Layer via Copy (#668).
let n = with_alpha.channels();
let mut px = if fmt.alpha {
src.read_region(area)
} else {
let opaque = src.read_region(area);
let k = fmt.channels();
let mut v = Vec::with_capacity(opaque.len() / k.max(1) * n);
for p in opaque.chunks_exact(k.max(1)) {
v.extend_from_slice(p);
v.push(1.0);
}
v
};
if let Some(m) = sel {
let w = area.width() as usize;
for (i, p) in px.chunks_exact_mut(n).enumerate() {
p[n - 1] *= m.sample_channel(area.x0 + (i % w) as i32, area.y0 + (i / w) as i32, 0);
let mask = m.read_region(area);
let mk = m.format().channels().max(1);
for (p, a) in px.chunks_exact_mut(n).zip(mask.chunks_exact(mk)) {
p[n - 1] *= a[0];
}
}
out.write_region(area, &px);
@@ -695,6 +708,35 @@ mod tests {
assert_eq!(px(&s, 0, 8, 8)[3], 0.0);
}
#[test]
fn layer_via_copy_of_a_background_keeps_colour_and_partial_selection() {
// #668: lift reads only the selected area now; the result must be what converting the
// whole layer gave: the colour, with the selection's coverage as alpha.
for depth in [8, 16, 32] {
let mut s = Session::new();
s.execute("file.new", json!({"width": 40, "height": 30, "depth": depth, "background": "#336699"})).unwrap();
s.edit("partial selection", |doc, _| {
let mut m = photocraft_raster::Surface::new(PixelFormat::GRAY8);
m.fill_rect(Rect::new(5, 5, 15, 15), &[1.0]);
m.fill_rect(Rect::new(15, 5, 20, 15), &[0.5]);
doc.selection = Some(m);
Ok(())
})
.unwrap();
let id = s.execute("layer.new.layerViaCopy", json!({})).unwrap()["layer"].as_u64().unwrap();
let d = s.active().unwrap();
let surf = d.doc.layer(LayerId(id)).unwrap().surface().unwrap();
assert!(surf.format().alpha, "@{depth}");
let full = surf.read_region(Rect::new(6, 6, 7, 7));
let half = surf.read_region(Rect::new(16, 6, 17, 7));
for (got, want) in full.iter().zip([0.2, 0.4, 0.6, 1.0]) {
assert!((got - want).abs() < 0.01, "@{depth}: {full:?}");
}
assert!((half[3] - 0.5).abs() < 0.01 && (half[0] - 0.2).abs() < 0.01, "@{depth}: {half:?}");
assert_eq!(surf.content_bounds(), Rect::new(5, 5, 20, 15), "@{depth}");
}
}
#[test]
fn cut_clears_and_layer_via_copy_cut() {
let mut s = session();
+15
View File
@@ -239,6 +239,21 @@ fn fit_image_and_conditional_mode_change() {
assert!(s.execute("file.automate.conditionalModeChange", json!({"to": "duotone"})).is_err());
}
#[test]
fn conditional_mode_change_cannot_run_a_mode_change_the_gate_denies() {
fn deny_mode(id: &str, _: &Value) -> Result<()> {
if id.starts_with("image.mode.") { Err(EngineError::Other(format!("automation command `{id}` is disabled"))) } else { Ok(()) }
}
let mut s = session(4, 4, 8);
s.authorize = Some(deny_mode);
let e = s.execute("file.automate.conditionalModeChange", json!({"to": "grayscale"})).unwrap_err();
assert!(e.to_string().contains("image.mode.grayscale"), "{e}");
assert_eq!(doc(&s).mode, ColorMode::Rgb, "the refused nested step leaves the document alone");
// Commands that compose only allowed steps still run.
s.execute("file.automate.fitImage", json!({"width": 2, "height": 2})).unwrap();
assert_eq!(doc(&s).size.width, 2);
}
#[test]
fn flatten_all_layer_effects_and_masks() {
for depth in [8, 16, 32] {
+6
View File
@@ -594,6 +594,12 @@ impl Session {
if !(params.is_object() || params.is_null()) {
return Err(EngineError::BadParams { cmd: id.to_string(), msg: "params must be a JSON object".into() });
}
// Untrusted sessions gate every command, including the ones a command runs on its own
// behalf (`file.automate.conditionalModeChange` runs `image.mode.*`), before any side
// effect.
if let Some(gate) = self.authorize {
gate(id, &params)?;
}
// A floating selection drops before any other command (Undo puts it back instead).
if let Some(v) = crate::float_cmds::before_command(self, id)? {
return Ok(Started::Done(v));
+3 -2
View File
@@ -303,8 +303,9 @@ pub struct Session {
pub preset_store: Option<preset_store::PresetStore>,
/// Window › Actions. The list persists with the preset store when one is attached.
pub actions: actions_cmds::ActionState,
/// Per-step gate for `actions.play`. Untrusted sessions (MCP, the control channel) install
/// the same check a top-level command sees. `None` runs every step, which is what a local
/// Gate for every command the session runs, including the ones a command runs on its own
/// behalf and `actions.play` steps. Untrusted sessions (MCP, the control channel) install
/// the same check a top-level request sees. `None` runs everything, which is what a local
/// UI and `photocraft-cli run` do.
pub authorize: Option<fn(&str, &serde_json::Value) -> Result<()>>,
/// Background jobs (see [`jobs`]).
+8 -2
View File
@@ -17,7 +17,13 @@ photocraft-doc = { workspace = true }
photocraft-psd = { workspace = true }
# No `system` feature: fontique's system backend links fontconfig on Linux. We scan font
# directories ourselves (native) and always register the bundled fonts (native + web).
# Include ICU dictionary data for Japanese word boundaries and complex-script line breaking.
parley = { version = "0.11", default-features = false, features = ["std", "complex-scripts"] }
parley = { version = "0.11", default-features = false, features = ["std"] }
skrifa = "0.44"
flate2 = { workspace = true }
# ICU dictionary data for Japanese word boundaries and complex-script (Thai, Lao, Khmer, Burmese)
# line breaking. Native only: the CJK dictionary alone is 3.8 MB of wasm, and the web build must
# stay under Cloudflare's 25 MiB per-file cap (packaging/web/package.sh). The web build falls back
# to the generic Unicode rules for those scripts.
[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
parley = { version = "0.11", default-features = false, features = ["std", "complex-scripts"] }
+2 -1
View File
@@ -82,8 +82,9 @@ pub fn checked(app: &PhotocraftApp, id: &str) -> Option<bool> {
"image.analysis.countTool" => Some(app.ui.tool == Tool::Count),
_ => {
// View › Proof Setup simulations: checked while that proof is shown.
// A check item with no document too: an item's kind never changes (native menus).
let kind = id.strip_prefix("view.proofSetup.").and_then(photocraft_engine::proof_sim::ProofKind::from_id)?;
let d = app.session.active()?;
let Some(d) = app.session.active() else { return Some(false) };
let pv = app.session.color.proof(d.doc.id);
Some(pv.enabled && pv.setup.kind == kind)
}
+36 -2
View File
@@ -213,6 +213,23 @@ pub(crate) fn stroke_command(tool: Tool) -> &'static str {
if tool == Tool::Pencil { "paint.pencil" } else { "paint.stroke" }
}
/// Windows' crosshair cursor inverts the pixels under it, so over mid-grey (the pasteboard, many
/// photos) it vanishes (#737). There the canvas draws a black-and-white crosshair itself, like
/// Photoshop's, and hides the system one.
fn visible_crosshair(icon: egui::CursorIcon, painter: &egui::Painter, p: Pos2, draw: bool) -> egui::CursorIcon {
if !draw || icon != egui::CursorIcon::Crosshair {
return icon;
}
let p = pos2(p.x.round() + 0.5, p.y.round() + 0.5);
let (gap, len) = (2.0, 8.0);
for (w, c) in [(3.0, Color32::from_black_alpha(160)), (1.0, Color32::from_white_alpha(235))] {
for d in [vec2(1.0, 0.0), vec2(-1.0, 0.0), vec2(0.0, 1.0), vec2(0.0, -1.0)] {
painter.line_segment([p + d * gap, p + d * len], Stroke::new(w, c));
}
}
egui::CursorIcon::None
}
/// The Pencil's cursor at `doc` (document pixels): the whole-pixel square its dab fills
/// (`paint::grid_square`), in screen points with its edges on physical pixels (`ppp` = pixels
/// per point), so it lines up with the pixel grid at any zoom.
@@ -1735,6 +1752,7 @@ pub fn canvas_view(app: &mut PhotocraftApp, ui: &mut egui::Ui, idx: usize, rect:
// drawn; otherwise Space is the Hand and ⌘Space / ⌘⌥Space the Zoom tool while held.
let reposition = crate::hold_keys::reposition_held(app, &ctx);
crate::crop_ui::set_space(app, reposition);
crate::crop_ui::ensure_frame(app);
let mut drawing = crate::crop_ui::active(app);
if let Some(d) = app.drag.as_mut().filter(|d| crate::hold_keys::repositions(d.tool)) {
d.reposition = reposition;
@@ -2059,6 +2077,7 @@ pub fn canvas_view(app: &mut PhotocraftApp, ui: &mut egui::Ui, idx: usize, rect:
Tool::MagneticLasso => crate::magnetic_lasso_ui::cursor(app, &painter, p, view.zoom),
_ => egui::CursorIcon::Crosshair,
};
let icon = visible_crosshair(icon, &painter, p, cfg!(target_os = "windows"));
ui.ctx().set_cursor_icon(icon);
// Selection tools: + / − / × badge for the effective mode (#170). A gesture keeps the
// mode it started with (⇧ then constrains the marquee instead of adding).
@@ -2293,7 +2312,7 @@ fn draw_drag_preview(app: &mut PhotocraftApp, painter: &egui::Painter, xf: &View
trail.feed(&d.points, app.session.tools.brush.size);
trail.draw(painter, doc_rect, xf.flip, col);
}
t if crate::vector_ui::is_shape_tool(t) => crate::vector_ui::draw_shape_preview(app, painter, xf, t, d.start, last, d.modifiers),
t if crate::vector_ui::is_shape_tool(t) => crate::vector_ui::draw_shape_preview(app, painter, xf, t, d.start, last, d.live),
Tool::RectMarquee | Tool::EllipseMarquee | Tool::ObjectSelection => {
// Marching ants, visible on any pixels (#172).
let (a, b) = marquee.unwrap_or((d.start, last));
@@ -2714,7 +2733,7 @@ pub(crate) fn finish_gesture(app: &mut PhotocraftApp, d: Drag) {
}
match d.tool {
Tool::ObjectSelection => crate::retouch_ui::finish_object_selection(app, d.start, [end[0], end[1]], d.modifiers),
t if crate::vector_ui::is_shape_tool(t) => crate::vector_ui::finish_shape(app, t, d.start, [end[0], end[1]], d.modifiers),
t if crate::vector_ui::is_shape_tool(t) => crate::vector_ui::finish_shape(app, t, d.start, [end[0], end[1]], d.live),
Tool::PathSelection => crate::vector_ui::path_selection_finish(app, d.start, [end[0], end[1]]),
Tool::Type | Tool::VerticalType => crate::type_tool::pointer_up(app, d.start, [end[0], end[1]]),
Tool::Brush | Tool::Pencil | Tool::Eraser => {
@@ -2854,6 +2873,10 @@ pub fn commit_polygon(app: &mut PhotocraftApp) {
/// Apply the crop tool's rectangle.
pub fn commit_crop(app: &mut PhotocraftApp) {
let Some(r) = app.ui.crop_rect.take() else { return };
// The untouched default frame crops nothing (Photoshop's ↵ on it does nothing).
if std::mem::take(&mut app.crop.default_frame) {
return;
}
let (x, y) = (r[0].round(), r[1].round());
let (w, h) = ((r[2] - r[0]).round().max(1.0), (r[3] - r[1]).round().max(1.0));
let delete = app.ui.tool_options.crop_delete;
@@ -2888,6 +2911,17 @@ fn hex(c: [f32; 4]) -> String {
#[cfg(test)]
mod tests {
#[test]
fn windows_draws_its_own_crosshair() {
// #737: Windows' inverting crosshair vanishes over mid-grey; the canvas draws one instead.
let ctx = egui::Context::default();
let painter = egui::Painter::new(ctx, egui::LayerId::background(), egui::Rect::EVERYTHING);
let p = egui::pos2(10.0, 10.0);
assert_eq!(super::visible_crosshair(egui::CursorIcon::Crosshair, &painter, p, true), egui::CursorIcon::None);
assert_eq!(super::visible_crosshair(egui::CursorIcon::Crosshair, &painter, p, false), egui::CursorIcon::Crosshair);
assert_eq!(super::visible_crosshair(egui::CursorIcon::Move, &painter, p, true), egui::CursorIcon::Move);
}
use super::*;
/// #569: displays 1 (sRGB) and 4 (Display P3) side by side, and a document filled with an
+69 -1
View File
@@ -27,6 +27,11 @@ pub struct CropState {
pub drag: Option<CropDrag>,
/// Space is held: drawing a new frame moves it instead of sizing it.
pub space: bool,
/// `UiState::crop_rect` is the untouched default frame ([`ensure_frame`]): a drag inside it
/// draws a new frame (as in Photoshop) instead of moving it, and committing it does nothing.
pub default_frame: bool,
/// The document (index and size) the default frame was made for.
frame_for: Option<(usize, u32, u32)>,
}
/// A crop gesture in progress. Rects are `[x0, y0, x1, y1]` in document coordinates.
@@ -149,6 +154,37 @@ pub fn set_space(app: &mut PhotocraftApp, down: bool) {
app.crop.space = down;
}
/// As in Photoshop, the Crop tool always shows a frame: on picking the tool, and after a crop is
/// cancelled or committed, it frames the selection's bounds, or the whole canvas.
pub fn ensure_frame(app: &mut PhotocraftApp) {
if app.ui.tool != Tool::Crop {
// Leaving the tool drops an untouched default frame (a drawn one stays pending).
if app.crop.default_frame {
app.ui.crop_rect = None;
app.crop.default_frame = false;
}
return;
}
let key = app.session.active_index().zip(app.session.active()).map(|(i, st)| (i, st.doc.size.width, st.doc.size.height));
// Another document (or a resized one) gets its own default frame.
if app.crop.default_frame && app.crop.frame_for != key && app.crop.drag.is_none() {
app.ui.crop_rect = None;
}
if app.ui.crop_rect.is_some() || app.crop.drag.is_some() {
return;
}
let Some(st) = app.session.active() else { return };
let canvas = st.doc.bounds();
// Computed once per new frame, not per frame drawn.
let r = st.doc.selection.as_ref().map(|s| s.content_bounds().intersect(&canvas)).filter(|b| !b.is_empty()).unwrap_or(canvas);
if r.is_empty() {
return;
}
app.ui.crop_rect = Some([f64::from(r.x0), f64::from(r.y0), f64::from(r.x1), f64::from(r.y1)]);
app.crop.default_frame = true;
app.crop.frame_for = key;
}
/// A crop gesture is in progress: Space repositions the frame rather than panning.
pub fn active(app: &PhotocraftApp) -> bool {
app.ui.tool == Tool::Crop && app.crop.drag.is_some()
@@ -175,7 +211,7 @@ pub fn pointer(app: &mut PhotocraftApp, ev: ToolEvent, mods: Modifiers) -> bool
let frame = app.ui.crop_rect.filter(|r| r.iter().all(|v| v.is_finite()));
app.crop.drag = Some(match frame.map(|r| (r, hit(r, p, tolerance(app)))) {
Some((rect, Hit::Handle(hx, hy))) => CropDrag::Resize { hx, hy, start: p, rect },
Some((rect, Hit::Inside)) => CropDrag::Move { start: p, rect },
Some((rect, Hit::Inside)) if !app.crop.default_frame => CropDrag::Move { start: p, rect },
_ => CropDrag::Draw { anchor: p, cur: p, last: p, prev: app.ui.crop_rect },
});
}
@@ -184,6 +220,9 @@ pub fn pointer(app: &mut PhotocraftApp, ev: ToolEvent, mods: Modifiers) -> bool
update(app, p, mods);
let Some(drag) = app.crop.drag.take() else { return true };
let ok = app.ui.crop_rect.is_some_and(size_ok);
if ok {
app.crop.default_frame = false;
}
match drag {
CropDrag::Draw { prev, .. } if !ok => app.ui.crop_rect = prev,
CropDrag::Move { rect, .. } | CropDrag::Resize { rect, .. } if !ok => app.ui.crop_rect = Some(rect),
@@ -282,6 +321,35 @@ mod tests {
tool_event(app, ToolEvent::Up { x: last[0], y: last[1] }, mods);
}
#[test]
fn picking_the_tool_frames_the_canvas_or_the_selection() {
// #668: the Crop tool started with no frame.
let mut app = app(SampleType::U8);
ensure_frame(&mut app);
assert_eq!(app.ui.crop_rect, Some([0.0, 0.0, 200.0, 100.0]));
// Committing the untouched frame crops nothing.
let steps = app.session.active().unwrap().history.past_len();
crate::canvas::commit_crop(&mut app);
assert_eq!(app.session.active().unwrap().history.past_len(), steps);
assert_eq!(app.session.active().unwrap().doc.size, Size::new(200, 100));
// A drag inside the untouched frame draws a new one instead of moving it.
ensure_frame(&mut app);
drag(&mut app, &[[50.0, 20.0], [80.0, 40.0], [120.0, 70.0]], NONE);
assert_eq!(app.ui.crop_rect, Some([50.0, 20.0, 120.0, 70.0]));
// Now it's a real frame: a drag inside moves it.
drag(&mut app, &[[60.0, 30.0], [70.0, 30.0]], NONE);
assert_eq!(app.ui.crop_rect, Some([60.0, 20.0, 130.0, 70.0]));
// Cancelling frames the selection's bounds when there is one.
app.run("select.rect", json!({"x": 10, "y": 10, "width": 30, "height": 20})).unwrap();
app.ui.crop_rect = None;
ensure_frame(&mut app);
assert_eq!(app.ui.crop_rect, Some([10.0, 10.0, 40.0, 30.0]));
// Leaving the tool drops the untouched frame.
app.ui.tool = Tool::Brush;
ensure_frame(&mut app);
assert_eq!(app.ui.crop_rect, None);
}
#[test]
fn hit_test_finds_handles_inside_and_outside() {
let r = [10.0, 10.0, 110.0, 60.0];
+1
View File
@@ -1714,6 +1714,7 @@
Constraints ({n}) Omezení ({n})
Align {what} Zarovnat: {what}
@plural {n} Layer|{n} Layers {n} vrstva|{n} vrstvy|{n} vrstev
@plural {n} layer|{n} layers {n} vrstva|{n} vrstvy|{n} vrstev
@plural Group · {n} layer|Group · {n} layers Skupina · {n} vrstva|Skupina · {n} vrstvy|Skupina · {n} vrstev
# Preferences (labels are generated from the setting names)
Can't render this file because it contains an unexpected character in line 6 and column 47.
+42
View File
@@ -222,3 +222,45 @@ fn dragging_down_the_eyes_sweeps_visibility_without_reordering() {
assert!(visible(&h, rows[1].layer));
}
}
/// #736: a layer row dropped on the footer's New Layer button is duplicated, on New Group it is
/// grouped, on Delete it is deleted; a row outside the selection goes alone.
#[test]
fn dropping_a_row_on_the_footer_buttons_duplicates_groups_and_deletes() {
let mut s = photocraft_engine::Session::new();
s.execute("file.new", json!({"width": 64, "height": 48})).unwrap();
for i in 0..3 {
s.execute("layer.new.layer", json!({"name": format!("L{i}")})).unwrap();
}
let mut h = harness(s, 1.0, "promedium", 290.0);
let names = |h: &Harness<'_, PhotocraftApp>| h.state().session.active().unwrap().doc.layers.iter().map(|l| l.name.clone()).collect::<Vec<_>>();
let drag_to = |h: &mut Harness<'static, PhotocraftApp>, row: usize, label: &str| {
let from = recorded(&h.ctx)[row].row.center();
let to = h.get_by_label(label).rect().center();
h.event(egui::Event::PointerMoved(from));
h.run_steps(1);
h.event(egui::Event::PointerButton { pos: from, button: PointerButton::Primary, pressed: true, modifiers: Modifiers::NONE });
h.run_steps(1);
for k in 1..=8 {
h.event(egui::Event::PointerMoved(from + (to - from) * (k as f32 / 8.0)));
h.run_steps(1);
}
h.event(egui::Event::PointerButton { pos: to, button: PointerButton::Primary, pressed: false, modifiers: Modifiers::NONE });
h.run_steps(3);
};
// Rows top to bottom: L2 (selected), L1, L0, Background. L1 isn't selected: it goes alone.
drag_to(&mut h, 1, "Create a new layer");
assert_eq!(names(&h), ["Background", "L0", "L1", "L1 copy", "L2"]);
let steps = h.state().session.active().unwrap().history.past_len();
// Rows: L2, L1 copy (now selected), L1, L0, Background.
drag_to(&mut h, 3, "Create a new group");
let doc = &h.state().session.active().unwrap().doc;
let group = doc.layers.iter().find(|l| l.is_group()).expect("a group");
let LayerContent::Group(g) = &group.content else { panic!("not a group") };
assert_eq!(g.children.iter().map(|l| l.name.as_str()).collect::<Vec<_>>(), ["L0"]);
assert_eq!(h.state().session.active().unwrap().history.past_len(), steps + 1, "one undo step");
let n = h.state().session.active().unwrap().doc.layers.len();
drag_to(&mut h, 0, "Delete layer");
assert!(!names(&h).iter().any(|n| n == "L2"));
assert_eq!(h.state().session.active().unwrap().doc.layers.len(), n - 1);
}
+9
View File
@@ -85,6 +85,7 @@ pub mod menus;
pub mod monitor_status;
pub mod move_mods;
pub mod move_ui;
pub mod native_menu;
pub mod new_doc_ui;
pub mod notices;
mod opacity_keys;
@@ -289,6 +290,8 @@ pub struct Services {
/// Reads the displays and their ICC profiles in the background (desktop macOS; see
/// `monitor_status`). Without one, the canvas uses the profile chosen in Color Settings, or sRGB.
pub read_displays: Option<monitor_status::ReadDisplaysFn>,
/// The macOS menu bar, when the desktop app installed one; the in-window menus are hidden then.
pub native_menu: Option<native_menu::NativeMenu>,
}
pub struct PhotocraftApp {
@@ -980,6 +983,7 @@ impl eframe::App for PhotocraftApp {
self.checker = None;
}
self.drain_control(ctx);
native_menu::run(self, ctx);
if self.ui.text_edit.is_some() && !self.ui.tool.is_type() {
type_tool::commit(self);
}
@@ -1029,6 +1033,10 @@ impl eframe::App for PhotocraftApp {
}
fn raw_input_hook(&mut self, ctx: &egui::Context, raw_input: &mut egui::RawInput) {
// Native menu key equivalents become the key presses they were (see `native_menu`).
if let Some(menu) = self.services.native_menu.as_mut() {
menu.raw_input(raw_input);
}
shortcuts::clipboard_keys(ctx, ctx.text_edit_focused() || self.ui.text_edit.is_some(), raw_input);
// Windows sends a touchpad pinch as Ctrl + wheel; make it a pinch again (wheel_nav.rs).
if cfg!(target_os = "windows") {
@@ -1096,6 +1104,7 @@ impl eframe::App for PhotocraftApp {
// A device lost while drawing this frame: switch to the CPU canvas before the next one.
gpu_status::check(self, &ctx);
self.automation_input = false;
native_menu::sync(self, &ctx);
self.perf.frame(gpu_canvas::now_ms() - t0);
// Synthetic input is injected one press/release step per frame: keep frames coming until
// the queue is empty, then release control replies waiting on it.
+24 -4
View File
@@ -286,6 +286,14 @@ pub(crate) fn invoke_unguarded(app: &mut PhotocraftApp, ctx: &egui::Context, id:
app.set_theme(ctx, k);
Ok(Value::Null)
}
// The macOS app menu's Language (`native_menu::language_node`).
i if i.starts_with(crate::native_menu::LANGUAGE_PREFIX) => {
let code = &i[crate::native_menu::LANGUAGE_PREFIX.len()..];
if code != "auto" && !crate::i18n::Lang::all().any(|l| l.code() == code) {
return Err(format!("unknown UI language `{code}`"));
}
app.run("prefs.set", json!({"values": {"interface.language": code}}))
}
"edit.search" => {
app.ui.palette_open = !app.ui.palette_open;
Ok(Value::Null)
@@ -523,9 +531,15 @@ pub fn is_enabled(app: &PhotocraftApp, id: &str) -> bool {
}
}
/// Image › Mode items that carry a checkmark.
const MODE_CHECKS: [&str; 11] = ["rgb", "grayscale", "cmyk", "lab", "multichannel", "indexedColor", "bitmap", "duotone", "bits8", "bits16", "bits32"];
/// Is a UI-level panel toggle currently on (for checkmarks)?
fn checked(app: &PhotocraftApp, id: &str) -> Option<bool> {
use photocraft_doc::{ColorMode, SampleType};
if let Some(name) = id.strip_prefix("window.theme.").filter(|n| *n != "toggle") {
return Some(crate::theme::ThemeKind::from_name(name) == Some(app.ui.theme));
}
if let Some(c) = crate::view_cmds::checked(app, id) {
return Some(c);
}
@@ -544,11 +558,13 @@ fn checked(app: &PhotocraftApp, id: &str) -> Option<bool> {
if let Some(alias) = panel_alias(id) {
return checked(app, alias);
}
// Check items stay check items with no document open (`Some(false)`, not `None`): a native
// menu can't change an item's kind in place, so a change would rebuild the whole menu.
if id == "select.isolateLayers" {
return Some(!app.session.active()?.isolated_layers.is_empty());
return Some(app.session.active().is_some_and(|d| !d.isolated_layers.is_empty()));
}
if id == "view.proofColors" || id == "view.gamutWarning" {
let d = app.session.active()?;
let Some(d) = app.session.active() else { return Some(false) };
let pv = app.session.color.proof(d.doc.id);
return Some(if id == "view.proofColors" { pv.enabled } else { pv.gamut_warning });
}
@@ -557,7 +573,9 @@ fn checked(app: &PhotocraftApp, id: &str) -> Option<bool> {
return Some(app.ui.workspace == if want.is_empty() { "Essentials" } else { want });
}
if let Some(m) = id.strip_prefix("image.mode.") {
let d = &app.session.active()?.doc;
let Some(d) = app.session.active().map(|s| &s.doc) else {
return MODE_CHECKS.contains(&m).then_some(false);
};
return match m {
"rgb" => Some(d.mode == ColorMode::Rgb),
"grayscale" => Some(d.mode == ColorMode::Grayscale),
@@ -639,7 +657,9 @@ pub fn menu_items(app: &PhotocraftApp) -> Vec<MenuItem> {
id: id.to_string(),
label: label.to_string(),
path: path.iter().map(|s| s.to_string()).collect(),
shortcut: sc.map(Into::into),
// A live item shows the shortcut that runs it ([`crate::shortcut_dispatch::bindings`]
// prefers the command's own over the catalogue's): Undo ⌘Z, Copy ⌘C, Hide Layers ⌘,.
shortcut: if known(id) { crate::shortcuts::default_shortcut(id) } else { sc.map(Into::into) },
enabled: known(id) && is_enabled(app, id),
checked: checked(app, id),
color: None,
+982
View File
@@ -0,0 +1,982 @@
//! The macOS menu bar: the same menus as the in-window bar ([`crate::menus::menu_items`]), laid
//! out the way a Mac app's are and handed to a platform [`Backend`] (the desktop app's muda one).
//!
//! Pure and platform-free, so it is tested everywhere and builds for the web; only the backend
//! touches AppKit. What the layout does ([`mac_layout`]):
//! - an **app menu** named PhotoCraft: About (from Help), Settings (Edit › Preferences), Language,
//! Appearance (Window › Theme), Services, Hide, Hide Others, Show All and Quit (File › Exit,
//! still the app's own `file.exit` so the unsaved-changes prompt runs). The other Craft apps
//! use the same app menu;
//! - **Window** gets Minimize, Zoom and Bring All to Front, and **Help** the system search field;
//! - **clashes** with the system's keys (⌘H, ⌘M, ⌘W, ⌘,) are resolved in PhotoCraft's favour, and
//! reported; Hide and Minimize move to ⌃⌘H and ⌃⌘M, as Photoshop does.
//!
//! **Keys stay with egui.** AppKit runs a menu's key equivalents before the window sees the key,
//! which would skip PhotoCraft's key rules (text fields keep ⌘C/⌘V/⌘Z, dialogs and Camera Raw are
//! modal, Liquify has its own ⌘Z). So the backend reports a key equivalent as [`Event::Key`], and
//! [`NativeMenu::raw_input`] feeds it to egui as the key press it was: the shortcut runs through
//! [`crate::shortcuts::handle`] exactly as without a native menu. A click is [`Event::Click`] and
//! runs the command like an in-window menu click.
use std::collections::HashMap;
use std::hash::{DefaultHasher, Hash, Hasher};
use egui::{Key, Modifiers};
use crate::PhotocraftApp;
use crate::i18n::{Lang, tr, tr_id};
use crate::menus::MenuItem;
pub const APP_NAME: &str = "PhotoCraft";
#[derive(Clone, Debug, Default, PartialEq)]
pub struct MenuBar {
pub menus: Vec<Menu>,
}
#[derive(Clone, Debug, PartialEq)]
pub struct Menu {
pub title: String,
pub role: MenuRole,
pub children: Vec<Node>,
}
/// What a top-level menu is for. macOS treats the app, Window and Help menus specially.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub enum MenuRole {
#[default]
Normal,
App,
Window,
Help,
}
#[derive(Clone, Debug, PartialEq)]
pub enum Node {
Item(Item),
Separator,
Submenu {
label: String,
children: Vec<Node>,
role: Option<ItemRole>,
},
/// An item AppKit provides and runs itself (Services, Zoom, …).
Standard(Standard),
}
#[derive(Clone, Debug, PartialEq)]
pub struct Item {
/// The command id (`file.open`), or one of [`HIDE`] / [`MINIMIZE`].
pub id: String,
pub label: String,
/// `Cmd+Shift+Z` form; `Ctrl` is the real Control key.
pub shortcut: Option<String>,
pub enabled: bool,
/// `Some` for check items.
pub checked: Option<bool>,
pub role: Option<ItemRole>,
}
/// Items macOS expects in fixed places.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum ItemRole {
About,
Settings,
Quit,
Hide,
Minimize,
/// Window › Theme and Next Theme, which become the app menu's Appearance submenu.
Appearance,
/// The app menu's Language submenu (its labels are final, never translated by id).
Language,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum Standard {
Services,
HideOthers,
ShowAll,
Minimize,
Zoom,
BringAllToFront,
CloseWindow,
}
/// Hide PhotoCraft: a custom item (the predefined one always takes ⌘H, which is View › Extras).
pub const HIDE: &str = "app.hide";
/// Minimize: a custom item (the predefined one always takes ⌘M, which is Curves).
pub const MINIMIZE: &str = "window.minimize";
impl Item {
fn new(id: &str, label: &str) -> Item {
Item { id: id.into(), label: label.into(), shortcut: None, enabled: true, checked: None, role: None }
}
}
impl MenuBar {
/// Every item, depth first.
pub fn items(&self) -> Vec<&Item> {
fn walk<'a>(nodes: &'a [Node], out: &mut Vec<&'a Item>) {
for n in nodes {
match n {
Node::Item(it) => out.push(it),
Node::Submenu { children, .. } => walk(children, out),
Node::Separator | Node::Standard(_) => {}
}
}
}
let mut out = Vec::new();
for m in &self.menus {
walk(&m.children, &mut out);
}
out
}
pub fn find(&self, id: &str) -> Option<&Item> {
self.items().into_iter().find(|it| it.id == id)
}
pub fn menu(&self, role: MenuRole) -> Option<&Menu> {
self.menus.iter().find(|m| m.role == role)
}
fn tag_item(&mut self, id: &str, role: ItemRole) {
fn walk(nodes: &mut [Node], id: &str, role: ItemRole) {
for n in nodes {
match n {
Node::Item(it) if it.id == id => it.role = Some(role),
Node::Submenu { children, .. } => walk(children, id, role),
_ => {}
}
}
}
for m in &mut self.menus {
walk(&mut m.children, id, role);
}
}
fn tag_submenu(&mut self, top: &str, name: &str, role: ItemRole) {
let Some(menu) = self.menus.iter_mut().find(|m| m.title == top) else { return };
for n in &mut menu.children {
if let Node::Submenu { label, role: r, .. } = n
&& label == name
{
*r = Some(role);
}
}
}
fn tag_menu(&mut self, title: &str, role: MenuRole) {
if let Some(m) = self.menus.iter_mut().find(|m| m.title == title) {
m.role = role;
}
}
}
/// The menus as a tree, from the flat rows [`crate::menus::menu_items`] returns, in the order the
/// in-window bar draws them: at each depth leaves and separators in order, and a submenu where its
/// first child is.
pub fn from_items(tops: &[&str], items: &[MenuItem]) -> MenuBar {
fn level(rows: &[&MenuItem], depth: usize) -> Vec<Node> {
let mut out = Vec::new();
let mut shown: Vec<&str> = Vec::new();
for it in rows {
if it.path.len() == depth {
if it.label == "---" {
out.push(Node::Separator);
} else {
out.push(Node::Item(Item {
id: it.id.clone(),
label: it.label.clone(),
shortcut: it.shortcut.clone(),
enabled: it.enabled,
checked: it.checked,
role: None,
}));
}
} else if let Some(name) = it.path.get(depth) {
let name = name.as_str();
if shown.contains(&name) {
continue;
}
shown.push(name);
let child: Vec<&MenuItem> = rows.iter().copied().filter(|c| c.path.get(depth).map(String::as_str) == Some(name)).collect();
out.push(Node::Submenu { label: name.to_string(), children: level(&child, depth + 1), role: None });
}
}
tidy(out)
}
let menus = tops
.iter()
.map(|top| {
let rows: Vec<&MenuItem> = items.iter().filter(|i| i.path.first().map(String::as_str) == Some(*top)).collect();
Menu { title: top.to_string(), role: MenuRole::Normal, children: level(&rows, 1) }
})
.collect();
MenuBar { menus }
}
/// Drop separators at the start or end of a level and collapse runs of them, at every depth.
fn tidy(nodes: Vec<Node>) -> Vec<Node> {
let mut out: Vec<Node> = Vec::with_capacity(nodes.len());
for n in nodes {
let n = match n {
Node::Submenu { label, children, role } => Node::Submenu { label, children: tidy(children), role },
other => other,
};
if matches!(n, Node::Separator) && matches!(out.last(), None | Some(Node::Separator)) {
continue;
}
out.push(n);
}
while matches!(out.last(), Some(Node::Separator)) {
out.pop();
}
out
}
/// A hash of everything a native menu can only change by rebuilding: menus, roles, item ids,
/// kinds (plain or check), shortcuts and submenu labels. Labels, enabled and checked are updated
/// in place.
pub fn structure_key(bar: &MenuBar) -> u64 {
fn walk(nodes: &[Node], h: &mut DefaultHasher) {
for n in nodes {
match n {
Node::Item(it) => ('i', &it.id, it.checked.is_some(), &it.shortcut, it.role).hash(h),
Node::Separator => '-'.hash(h),
Node::Submenu { label, children, role } => {
('[', label, role).hash(h);
walk(children, h);
']'.hash(h);
}
Node::Standard(s) => ('s', s).hash(h),
}
}
}
let mut h = DefaultHasher::new();
for m in &bar.menus {
(&m.title, m.role).hash(&mut h);
walk(&m.children, &mut h);
}
h.finish()
}
// ----------------------------------------------------------------------------- shortcuts
/// A shortcut as PhotoCraft writes it (`Cmd+Shift+Z`). `Cmd` is ⌘ and `Ctrl` the Control key,
/// never folded together: ⌃⌘M and ⌘M are different shortcuts.
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub struct Chord {
pub cmd: bool,
pub ctrl: bool,
pub alt: bool,
pub shift: bool,
/// The key as written: `Z`, `1`, `'`, `F5`, `Delete`, `Enter`.
pub key: String,
}
impl Chord {
pub fn parse(s: &str) -> Option<Chord> {
let mut c = Chord { cmd: false, ctrl: false, alt: false, shift: false, key: String::new() };
for part in s.split('+') {
match part {
"Cmd" => c.cmd = true,
"Ctrl" => c.ctrl = true,
"Alt" => c.alt = true,
"Shift" => c.shift = true,
// `Cmd++` splits into an empty last part.
"" => c.key = "+".into(),
k if c.key.is_empty() => c.key = k.into(),
_ => return None,
}
}
(!c.key.is_empty()).then_some(c)
}
/// The written form, modifiers in a fixed order.
pub fn portable(&self) -> String {
let mut parts = Vec::new();
for (on, name) in [(self.ctrl, "Ctrl"), (self.cmd, "Cmd"), (self.alt, "Alt"), (self.shift, "Shift")] {
if on {
parts.push(name);
}
}
parts.push(&self.key);
parts.join("+")
}
pub fn is_function_key(&self) -> bool {
self.key.strip_prefix('F').and_then(|n| n.parse::<u8>().ok()).is_some_and(|n| (1..=24).contains(&n))
}
/// May the native menu show this as a key equivalent? Only with ⌘ or ⌃, or a function key.
/// Bare letters and ⌥/⇧-only keys are tool keys: as key equivalents AppKit would take a `b`
/// typed into a text field.
pub fn native_ok(&self) -> bool {
self.cmd || self.ctrl || self.is_function_key()
}
/// The key press egui would have received for this chord.
pub fn key_event(&self, pressed: bool) -> Option<egui::Event> {
let key = match self.key.as_str() {
"+" => Key::Plus,
"'" => Key::Quote,
";" => Key::Semicolon,
"=" => Key::Equals,
"-" => Key::Minus,
"[" => Key::OpenBracket,
"]" => Key::CloseBracket,
"\\" => Key::Backslash,
"/" => Key::Slash,
"," => Key::Comma,
"." => Key::Period,
k => Key::from_name(k)?,
};
let modifiers = Modifiers { alt: self.alt, ctrl: self.ctrl, shift: self.shift, mac_cmd: self.cmd, command: self.cmd };
Some(egui::Event::Key { key, physical_key: Some(key), pressed, repeat: false, modifiers })
}
}
// ----------------------------------------------------------------------------- macOS layout
/// A system key equivalent PhotoCraft already uses for something else.
#[derive(Clone, Debug, PartialEq)]
pub struct Clash {
pub shortcut: &'static str,
/// What macOS uses it for.
pub system: &'static str,
/// The PhotoCraft command bound to it.
pub command: String,
/// What the layout did about it.
pub resolution: &'static str,
}
pub struct Layout {
pub bar: MenuBar,
pub clashes: Vec<Clash>,
}
/// Turn PhotoCraft's menus (tagged with roles) into a Mac menu bar. See the module docs.
pub fn mac_layout(bar: &MenuBar, lang: Lang) -> Layout {
let mut bar = bar.clone();
let mut clashes = Vec::new();
let bound: HashMap<String, String> = bar.items().iter().filter_map(|it| Some((Chord::parse(it.shortcut.as_deref()?)?.portable(), it.id.clone()))).collect();
let taken = |sc: &str, own: Option<&str>| bound.get(sc).filter(|cmd| Some(cmd.as_str()) != own).cloned();
let named = |s: &str| crate::i18n::fmt(tr(lang, s), &[("app", APP_NAME)]);
let about = take(&mut bar, |n| matches!(n, Node::Item(it) if it.role == Some(ItemRole::About)));
let settings = take(&mut bar, |n| match n {
Node::Item(it) => it.role == Some(ItemRole::Settings),
Node::Submenu { role, .. } => *role == Some(ItemRole::Settings),
_ => false,
});
let quit = take(&mut bar, |n| matches!(n, Node::Item(it) if it.role == Some(ItemRole::Quit)));
let appearance = appearance_node(&mut bar, lang);
let mut menu = Vec::new();
if let Some(Node::Item(mut it)) = about {
it.label = named("About {app}");
menu.extend([Node::Item(it), Node::Separator]);
}
if let Some(s) = settings {
let own = node_ids(&s);
let comma = bound.get("Cmd+,").filter(|cmd| !own.contains(*cmd)).cloned();
menu.push(settings_node(s, comma, lang, &mut clashes));
}
menu.extend(appearance);
if !menu.is_empty() && menu.last() != Some(&Node::Separator) {
menu.push(Node::Separator);
}
menu.extend([Node::Standard(Standard::Services), Node::Separator]);
let mut hide = Item::new(HIDE, &named("Hide {app}"));
hide.role = Some(ItemRole::Hide);
match taken("Cmd+H", None) {
Some(command) => {
let free = taken("Ctrl+Cmd+H", None).is_none();
if free {
hide.shortcut = Some("Ctrl+Cmd+H".into());
}
let resolution = if free { "Hide moves to ⌃⌘H, as in Photoshop" } else { "Hide has no shortcut" };
clashes.push(Clash { shortcut: "Cmd+H", system: "Hide", command, resolution });
}
None => hide.shortcut = Some("Cmd+H".into()),
}
menu.push(Node::Item(hide));
if let Some(command) = taken("Cmd+Alt+H", None) {
clashes.push(Clash { shortcut: "Cmd+Alt+H", system: "Hide Others", command, resolution: "AppKit's Hide Others keeps ⌥⌘H" });
}
menu.extend([Node::Standard(Standard::HideOthers), Node::Standard(Standard::ShowAll), Node::Separator]);
if let Some(Node::Item(mut quit)) = quit {
quit.label = named("Quit {app}");
match taken("Cmd+Q", Some(&quit.id)) {
Some(command) => {
clashes.push(Clash { shortcut: "Cmd+Q", system: "Quit", command, resolution: "Quit has no shortcut" });
quit.shortcut = None;
}
None => quit.shortcut = Some("Cmd+Q".into()),
}
menu.push(Node::Item(quit));
}
bar.menus.insert(0, Menu { title: APP_NAME.into(), role: MenuRole::App, children: menu });
match taken("Cmd+W", None) {
Some(command) => clashes.push(Clash { shortcut: "Cmd+W", system: "Close Window", command, resolution: "no system Close Window item" }),
None => {
if let Some(file) = bar.menus.iter_mut().find(|m| m.title == "File") {
file.children.extend([Node::Separator, Node::Standard(Standard::CloseWindow)]);
}
}
}
if bar.menu(MenuRole::Window).is_none() {
let at = bar.menus.iter().position(|m| m.role == MenuRole::Help).unwrap_or(bar.menus.len());
bar.menus.insert(at, Menu { title: "Window".into(), role: MenuRole::Window, children: Vec::new() });
}
let minimize = match taken("Cmd+M", None) {
Some(command) => {
let mut it = Item::new(MINIMIZE, tr(lang, "Minimize"));
it.role = Some(ItemRole::Minimize);
let free = taken("Ctrl+Cmd+M", None).is_none();
if free {
it.shortcut = Some("Ctrl+Cmd+M".into());
}
let resolution = if free { "Minimize moves to ⌃⌘M, as in Photoshop" } else { "Minimize has no shortcut" };
clashes.push(Clash { shortcut: "Cmd+M", system: "Minimize", command, resolution });
Node::Item(it)
}
None => Node::Standard(Standard::Minimize),
};
if let Some(window) = bar.menus.iter_mut().find(|m| m.role == MenuRole::Window) {
let mut children = vec![minimize, Node::Standard(Standard::Zoom), Node::Separator];
children.append(&mut window.children);
children.extend([Node::Separator, Node::Standard(Standard::BringAllToFront)]);
window.children = children;
}
for m in &mut bar.menus {
m.children = tidy(std::mem::take(&mut m.children));
}
// A menu emptied by the moves goes away (the Help menu stays: it has the search field).
bar.menus.retain(|m| !m.children.is_empty() || m.role == MenuRole::Help);
Layout { bar, clashes }
}
/// Settings in the app menu: Edit › Preferences, a submenu of settings pages, becomes "Settings"
/// with ⌘, on its first page unless PhotoCraft binds ⌘, elsewhere.
fn settings_node(node: Node, comma_taken: Option<String>, lang: Lang, clashes: &mut Vec<Clash>) -> Node {
if let Some(command) = &comma_taken {
clashes.push(Clash { shortcut: "Cmd+,", system: "Settings", command: command.clone(), resolution: "Settings has no shortcut" });
}
let give = |it: &mut Item| {
if comma_taken.is_none() && it.shortcut.is_none() {
it.shortcut = Some("Cmd+,".into());
}
};
match node {
Node::Item(mut it) => {
it.label = tr(lang, "Settings…").into();
give(&mut it);
Node::Item(it)
}
Node::Submenu { mut children, role, .. } => {
if let Some(Node::Item(first)) = children.iter_mut().find(|n| matches!(n, Node::Item(i) if i.enabled)) {
give(first);
}
Node::Submenu { label: tr(lang, "Settings").into(), children, role }
}
other => other,
}
}
/// Appearance in the app menu: the themes (Window › Theme, tagged [`ItemRole::Appearance`]), then
/// Next Theme, both leaving the Window menu. None when there are no themes.
fn appearance_node(bar: &mut MenuBar, lang: Lang) -> Option<Node> {
let Some(Node::Submenu { mut children, .. }) = take(bar, |n| matches!(n, Node::Submenu { role, .. } if *role == Some(ItemRole::Appearance))) else {
return None;
};
if let Some(Node::Item(mut next)) = take(bar, |n| matches!(n, Node::Item(it) if it.role == Some(ItemRole::Appearance))) {
// Placed now: translated by id like the themes above it.
next.role = None;
children.extend([Node::Separator, Node::Item(next)]);
}
Some(Node::Submenu { label: tr(lang, "Appearance").into(), children, role: Some(ItemRole::Appearance) })
}
/// Command ids of the app menu's Language items: `app.language.<code>`, where `auto` follows the
/// system (the `interface.language` preference values).
pub const LANGUAGE_PREFIX: &str = "app.language.";
/// Language in the app menu, after Settings: Auto, then every UI language in its own name, with
/// the current preference checked.
fn language_node(pref: &str, lang: Lang) -> Node {
let item = |code: &str, label: &str| Item {
id: format!("{LANGUAGE_PREFIX}{code}"),
label: label.into(),
shortcut: None,
enabled: true,
checked: Some(pref == code),
role: Some(ItemRole::Language),
};
let mut children = vec![Node::Item(item("auto", tr(lang, "Auto"))), Node::Separator];
children.extend(Lang::all().map(|l| Node::Item(item(l.code(), l.name()))));
Node::Submenu { label: tr(lang, "Language").into(), children, role: Some(ItemRole::Language) }
}
fn node_ids(node: &Node) -> Vec<String> {
match node {
Node::Item(it) => vec![it.id.clone()],
Node::Submenu { children, .. } => children.iter().flat_map(node_ids).collect(),
_ => Vec::new(),
}
}
/// Remove and return the first node matching `pred`, at any depth.
fn take(bar: &mut MenuBar, pred: impl Fn(&Node) -> bool + Copy) -> Option<Node> {
fn walk(nodes: &mut Vec<Node>, pred: impl Fn(&Node) -> bool + Copy) -> Option<Node> {
if let Some(i) = nodes.iter().position(pred) {
return Some(nodes.remove(i));
}
nodes.iter_mut().find_map(|n| match n {
Node::Submenu { children, .. } => walk(children, pred),
_ => None,
})
}
bar.menus.iter_mut().find_map(|m| walk(&mut m.children, pred))
}
/// Translate menu titles, submenus and items the layout didn't name itself.
fn translate(bar: &mut MenuBar, lang: Lang) {
fn walk(nodes: &mut [Node], lang: Lang) {
for n in nodes {
match n {
Node::Item(it) if it.role.is_none() => it.label = tr_id(lang, &it.id, &it.label).to_string(),
Node::Submenu { label, children, role } => {
if role.is_none() {
*label = tr(lang, label).to_string();
}
walk(children, lang);
}
_ => {}
}
}
}
for m in &mut bar.menus {
if m.role != MenuRole::App {
m.title = tr(lang, &m.title).to_string();
}
walk(&mut m.children, lang);
}
}
/// PhotoCraft's menus as a Mac menu bar, in the UI language; `language` is the
/// `interface.language` preference (`auto` or a code), checked in the app menu's Language.
pub fn photocraft_layout(items: &[MenuItem], lang: Lang, language: &str) -> Layout {
let mut bar = from_items(&crate::menus::TOP_MENUS, items);
bar.tag_item("help.about", ItemRole::About);
bar.tag_item("file.exit", ItemRole::Quit);
bar.tag_submenu("Edit", "Preferences", ItemRole::Settings);
bar.tag_submenu("Window", "Theme", ItemRole::Appearance);
bar.tag_item("window.theme.toggle", ItemRole::Appearance);
bar.tag_menu("Window", MenuRole::Window);
bar.tag_menu("Help", MenuRole::Help);
let mut layout = mac_layout(&bar, lang);
// Language goes right after Settings (before Appearance), as in the other Craft apps.
if let Some(app_menu) = layout.bar.menus.iter_mut().find(|m| m.role == MenuRole::App) {
let settings = |n: &Node| match n {
Node::Item(it) => it.role == Some(ItemRole::Settings),
Node::Submenu { role, .. } => *role == Some(ItemRole::Settings),
_ => false,
};
let at = app_menu.children.iter().position(settings).map_or(0, |i| i + 1);
app_menu.children.insert(at, language_node(language, lang));
}
translate(&mut layout.bar, lang);
layout
}
// ----------------------------------------------------------------------------- the app's side
/// What the native menu reports.
#[derive(Clone, Debug, PartialEq)]
pub enum Event {
/// An item was chosen with the mouse (or the keyboard inside an open menu).
Click(String),
/// A key equivalent was pressed: the shortcut, to be handled as the key press it was.
Key(Chord),
}
/// A platform's native menu bar (the desktop app implements it for macOS with muda).
pub trait Backend {
/// Bring the menu in step with `bar`: update labels, enabled and checked in place, or
/// rebuild when its structure changed.
fn sync(&mut self, bar: &MenuBar);
/// Events since the last call. Platform items (Hide, Services, Zoom, …) are handled by the
/// backend and not reported.
fn drain(&mut self) -> Vec<Event>;
}
/// The native menu bar, while PhotoCraft uses one (the in-window menus are hidden then).
pub struct NativeMenu {
backend: Box<dyn Backend>,
/// Hash of the state the menus were last built from.
state: Option<u64>,
clicks: Vec<String>,
/// AppKit toggled a check item itself: re-sync.
dirty: bool,
logged: bool,
}
impl NativeMenu {
pub fn new(backend: Box<dyn Backend>) -> NativeMenu {
NativeMenu { backend, state: None, clicks: Vec::new(), dirty: true, logged: false }
}
/// Before egui sees this frame's input: key equivalents become the key presses they were,
/// and clicks wait for [`NativeMenu::run`].
pub fn raw_input(&mut self, raw: &mut egui::RawInput) {
for e in self.backend.drain() {
match e {
Event::Key(chord) => {
raw.events.extend(chord.key_event(true));
raw.events.extend(chord.key_event(false));
}
Event::Click(id) => self.clicks.push(id),
}
self.dirty = true;
}
}
}
/// Run the native menu clicks that arrived since the last frame, like in-window menu clicks.
pub fn run(app: &mut PhotocraftApp, ctx: &egui::Context) {
let Some(menu) = app.services.native_menu.as_mut() else { return };
let clicks = std::mem::take(&mut menu.clicks);
for id in clicks {
if id == MINIMIZE {
ctx.send_viewport_cmd(egui::ViewportCommand::Minimized(true));
continue;
}
if let Err(e) = crate::menus::invoke(app, ctx, &id, serde_json::json!({})) {
app.ui.status = e;
app.ui.status_error = true;
}
}
}
/// Bring the native menu up to date when anything it shows may have changed: a command ran, the
/// document or selection changed, or the user clicked or pressed a key. Building the rows walks
/// every command's enabled state, so it doesn't run on frames that only animate or scroll.
pub fn sync(app: &mut PhotocraftApp, ctx: &egui::Context) {
let Some(menu) = app.services.native_menu.as_ref() else { return };
let input = ctx.input(|i| i.events.iter().any(|e| matches!(e, egui::Event::Key { pressed: true, .. } | egui::Event::PointerButton { pressed: false, .. })));
let state = state_hash(app, input.then_some(app.frame));
if menu.state == Some(state) && !menu.dirty {
return;
}
let lang = crate::i18n::current();
let layout = photocraft_layout(&crate::menus::menu_items(app), lang, &app.session.prefs().interface.language);
let Some(menu) = app.services.native_menu.as_mut() else { return };
if !menu.logged {
for c in &layout.clashes {
log::info!("menu: {} is {} on macOS and {} in PhotoCraft; {}", c.shortcut, c.system, c.command, c.resolution);
}
menu.logged = true;
}
menu.backend.sync(&layout.bar);
menu.state = Some(state);
menu.dirty = false;
}
/// What the menus' rows depend on, cheaply: commands run, the documents and their revisions,
/// selection, recent files, panels and view state, the language. `input` forces a change on
/// frames with a click or key press, which covers state the hash doesn't list.
fn state_hash(app: &PhotocraftApp, input: Option<u64>) -> u64 {
let mut h = DefaultHasher::new();
input.hash(&mut h);
let s = &app.session;
s.journal.len().hash(&mut h);
s.active_index().hash(&mut h);
s.clipboard.is_some().hash(&mut h);
if let Some(d) = s.active() {
(d.doc.id.0, d.revision, d.active_layer.map(|l| l.0), d.selected_layers.len(), d.isolated_layers.len()).hash(&mut h);
}
let ui = &app.ui;
(&ui.recent_files, &ui.workspace, ui.palette_open, ui.transform.is_some(), ui.text_edit.is_some(), ui.dialogs.len()).hash(&mut h);
serde_json::to_string(&(&ui.panels, &ui.extras, &ui.view, ui.theme, ui.tool)).unwrap_or_default().hash(&mut h);
s.prefs().interface.language.hash(&mut h);
h.finish()
}
#[cfg(test)]
mod tests {
use super::*;
use serde_json::json;
fn app(doc: bool) -> PhotocraftApp {
let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default());
if doc {
app.run("file.new", json!({"width": 64, "height": 48})).unwrap();
app.sync_views();
}
app
}
fn layout(doc: bool) -> Layout {
photocraft_layout(&crate::menus::menu_items(&app(doc)), Lang::EN, "auto")
}
fn ids(nodes: &[Node]) -> Vec<String> {
nodes
.iter()
.map(|n| match n {
Node::Item(it) => it.id.clone(),
Node::Separator => "---".into(),
Node::Submenu { label, .. } => format!("[{label}]"),
Node::Standard(s) => format!("<{s:?}>"),
})
.collect()
}
#[test]
fn photocraft_gets_a_mac_app_menu() {
let l = layout(true);
let app = &l.bar.menus[0];
assert_eq!((app.title.as_str(), app.role), ("PhotoCraft", MenuRole::App));
assert_eq!(
ids(&app.children),
[
"help.about",
"---",
"[Settings]",
"[Language]",
"[Appearance]",
"---",
"<Services>",
"---",
HIDE,
"<HideOthers>",
"<ShowAll>",
"---",
"file.exit"
]
);
let quit = l.bar.find("file.exit").unwrap();
assert_eq!((quit.label.as_str(), quit.shortcut.as_deref()), ("Quit PhotoCraft", Some("Cmd+Q")));
assert_eq!(l.bar.find("help.about").unwrap().label, "About PhotoCraft");
}
#[test]
fn moved_items_leave_their_old_menus() {
let l = layout(true);
let by = |t: &str| l.bar.menus.iter().find(|m| m.title == t).unwrap();
assert!(!ids(&by("File").children).contains(&"file.exit".to_string()));
assert!(!ids(&by("Help").children).contains(&"help.about".to_string()));
assert!(!ids(&by("Edit").children).contains(&"[Preferences]".to_string()));
let titles: Vec<&str> = l.bar.menus.iter().map(|m| m.title.as_str()).collect();
assert_eq!(titles, ["PhotoCraft", "File", "Edit", "Image", "Layer", "Type", "Select", "Filter", "View", "Window", "Help"]);
}
/// PhotoCraft keeps Photoshop's ⌘H, ⌘M, ⌘W and ⌘, (Hide Layers): the system items give way.
#[test]
fn clashes_are_resolved_in_photocrafts_favour() {
let l = layout(true);
let clash = |sc: &str| l.clashes.iter().find(|c| c.shortcut == sc).map(|c| c.command.as_str());
assert_eq!(clash("Cmd+H"), Some("view.extras"));
assert_eq!(clash("Cmd+M"), Some("image.adjustments.curves"));
assert_eq!(clash("Cmd+W"), Some("file.close"));
assert_eq!(clash("Cmd+,"), Some("layer.hideLayers"));
assert_eq!(l.bar.find(HIDE).unwrap().shortcut.as_deref(), Some("Ctrl+Cmd+H"), "as in Photoshop");
assert_eq!(l.bar.find(MINIMIZE).unwrap().shortcut.as_deref(), Some("Ctrl+Cmd+M"));
let window = l.bar.menu(MenuRole::Window).unwrap();
assert_eq!(ids(&window.children)[..3], [MINIMIZE.to_string(), "<Zoom>".into(), "---".into()]);
assert_eq!(ids(&window.children).last().unwrap(), "<BringAllToFront>");
let file = l.bar.menus.iter().find(|m| m.title == "File").unwrap();
assert!(!file.children.contains(&Node::Standard(Standard::CloseWindow)), "⌘W stays File › Close");
}
/// Themes and Next Theme move from Window to the app menu's Appearance, checked by the theme.
#[test]
fn themes_move_to_the_app_menus_appearance() {
let mut a = app(false);
a.ui.theme = crate::theme::ThemeKind::Classic;
let l = photocraft_layout(&crate::menus::menu_items(&a), Lang::EN, "auto");
let app_menu = l.bar.menu(MenuRole::App).unwrap();
let Some(Node::Submenu { children, .. }) = app_menu.children.iter().find(|n| matches!(n, Node::Submenu { label, .. } if label == "Appearance")) else {
panic!("an Appearance submenu");
};
let themes = ids(children);
assert_eq!(themes.last().map(String::as_str), Some("window.theme.toggle"));
assert!(themes.contains(&"window.theme.pro".to_string()) && themes.contains(&"window.theme.classic".to_string()));
assert_eq!(l.bar.find("window.theme.classic").unwrap().checked, Some(true));
assert_eq!(l.bar.find("window.theme.pro").unwrap().checked, Some(false));
assert_eq!(l.bar.find("window.theme.toggle").unwrap().label, "Next Theme");
let window = ids(&l.bar.menu(MenuRole::Window).unwrap().children);
assert!(!window.iter().any(|id| id.starts_with("window.theme.") || id == "[Theme]"), "{window:?}");
}
/// Language follows Settings in the app menu: Auto and every UI language in its own name,
/// the preference checked; choosing one sets `interface.language`.
#[test]
fn the_app_menu_switches_the_ui_language() {
let l = photocraft_layout(&crate::menus::menu_items(&app(false)), Lang::EN, "ja");
let app_menu = l.bar.menu(MenuRole::App).unwrap();
let Some(Node::Submenu { children, .. }) = app_menu.children.iter().find(|n| matches!(n, Node::Submenu { label, .. } if label == "Language")) else {
panic!("a Language submenu");
};
assert_eq!(children.iter().filter(|n| matches!(n, Node::Item(_))).count(), Lang::all().count() + 1);
assert_eq!(l.bar.find("app.language.ja").map(|it| (it.label.as_str(), it.checked)), Some(("日本語", Some(true))));
assert_eq!(l.bar.find("app.language.auto").and_then(|it| it.checked), Some(false));
// Native names stay native in another UI language.
let de = photocraft_layout(&crate::menus::menu_items(&app(false)), Lang::from_pref("de"), "de");
assert_eq!(de.bar.find("app.language.ja").map(|it| it.label.as_str()), Some("日本語"));
let mut a = app(false);
let ctx = egui::Context::default();
crate::menus::invoke(&mut a, &ctx, "app.language.fr", json!({})).unwrap();
assert_eq!(a.session.prefs().interface.language, "fr");
crate::menus::invoke(&mut a, &ctx, "app.language.auto", json!({})).unwrap();
assert_eq!(a.session.prefs().interface.language, "auto");
assert!(crate::menus::invoke(&mut a, &ctx, "app.language.xx-nope", json!({})).is_err(), "an unknown code is an error");
}
/// The menus show the shortcut that runs each command (Undo ⌘Z, Copy ⌘C were blank).
#[test]
fn menu_rows_show_the_shortcut_that_runs_them() {
let l = layout(true);
let sc = |id: &str| l.bar.find(id).and_then(|it| it.shortcut.clone());
assert_eq!(sc("edit.undo").as_deref(), Some("Cmd+Z"));
assert_eq!(sc("edit.copy").as_deref(), Some("Cmd+C"));
assert_eq!(sc("edit.paste").as_deref(), Some("Cmd+V"));
assert_eq!(sc("layer.hideLayers").as_deref(), Some("Cmd+,"));
let app = app(true);
let bindings = crate::shortcut_dispatch::bindings(&app);
for it in crate::menus::menu_items(&app).iter().filter(|it| it.enabled) {
let Some(shown) = it.shortcut.as_deref().and_then(crate::shortcuts::parse) else { continue };
let runs = bindings.iter().find(|(_, b)| *b == shown).map(|(id, _)| id.as_str());
assert!(runs.is_some(), "{} shows {:?}, which runs nothing", it.id, it.shortcut);
}
}
/// A native menu can't turn a plain item into a check item in place: opening the first
/// document must not change any item's kind, or the whole menu is rebuilt.
#[test]
fn opening_a_document_changes_state_not_structure() {
let (a, b) = (crate::menus::menu_items(&app(false)), crate::menus::menu_items(&app(true)));
let kind = |items: &[MenuItem]| items.iter().map(|i| (i.id.clone(), i.checked.is_some())).collect::<HashMap<_, _>>();
let (ka, kb) = (kind(&a), kind(&b));
let flips: Vec<&String> = ka.iter().filter(|(id, k)| kb.get(*id).is_some_and(|o| o != *k)).map(|(id, _)| id).collect();
assert!(flips.is_empty(), "items that change kind: {flips:?}");
assert_eq!(structure_key(&layout(false).bar), structure_key(&layout(true).bar));
}
#[test]
fn native_key_equivalents_are_command_and_function_keys_only() {
assert!(Chord::parse("Cmd+B").unwrap().native_ok());
assert!(Chord::parse("F5").unwrap().native_ok());
assert!(!Chord::parse("B").unwrap().native_ok());
assert!(!Chord::parse("Alt+F").unwrap().native_ok());
assert!(!Chord::parse("Shift+X").unwrap().native_ok());
assert_eq!(Chord::parse("Ctrl+Cmd+M").unwrap().portable(), "Ctrl+Cmd+M");
assert_eq!(Chord::parse("Cmd++").unwrap().key, "+");
}
/// Every menu shortcut is written so it parses and turns back into the key press egui gets.
#[test]
fn every_shortcut_becomes_a_key_press() {
for it in layout(true).bar.items() {
let Some(sc) = &it.shortcut else { continue };
let c = Chord::parse(sc).unwrap_or_else(|| panic!("{sc} on {} doesn't parse", it.id));
assert_eq!(&c.portable(), sc, "{} writes its modifiers in another order", it.id);
let Some(egui::Event::Key { key, modifiers, .. }) = c.key_event(true) else { panic!("{sc} on {} has no egui key", it.id) };
let parsed = crate::shortcuts::parse(sc).unwrap();
assert!(crate::shortcuts::key_matches(&parsed, key, modifiers), "{sc} on {} doesn't match its own key press", it.id);
}
}
fn frame(ctx: &egui::Context, raw: egui::RawInput, f: impl FnMut(&mut egui::Ui)) {
let mut out = ctx.run_ui(raw, f);
out.textures_delta.clear();
}
struct Fake {
synced: std::rc::Rc<std::cell::RefCell<Vec<MenuBar>>>,
events: Vec<Event>,
}
impl Backend for Fake {
fn sync(&mut self, bar: &MenuBar) {
self.synced.borrow_mut().push(bar.clone());
}
fn drain(&mut self) -> Vec<Event> {
std::mem::take(&mut self.events)
}
}
/// A key equivalent reaches PhotoCraft's own key handling as the key press it was, so the
/// shortcut runs once, through the same rules as without a native menu.
#[test]
fn key_equivalents_run_through_the_normal_shortcut_path() {
let synced = std::rc::Rc::new(std::cell::RefCell::new(Vec::new()));
let mut app = app(true);
let ctx = egui::Context::default();
let rulers = Chord::parse("Cmd+R").unwrap();
app.services.native_menu = Some(NativeMenu::new(Box::new(Fake { synced: synced.clone(), events: vec![Event::Key(rulers)] })));
let before = app.ui.extras.rulers;
let mut raw = egui::RawInput::default();
if let Some(m) = app.services.native_menu.as_mut() {
m.raw_input(&mut raw);
}
assert_eq!(raw.events.len(), 2, "a press and a release");
frame(&ctx, raw, |ui| {
run(&mut app, ui.ctx());
crate::shortcuts::handle(&mut app, ui.ctx());
sync(&mut app, ui.ctx());
});
assert_eq!(app.ui.extras.rulers, !before, "⌘R toggled View › Rulers once");
assert_eq!(synced.borrow().len(), 1, "the menu was built");
// Nothing changed: no rebuild of the rows on the next frame.
frame(&ctx, egui::RawInput::default(), |ui| sync(&mut app, ui.ctx()));
assert_eq!(synced.borrow().len(), 1);
}
#[test]
fn clicks_run_the_command_and_resync() {
let synced = std::rc::Rc::new(std::cell::RefCell::new(Vec::new()));
let mut app = app(true);
let ctx = egui::Context::default();
app.services.native_menu = Some(NativeMenu::new(Box::new(Fake { synced: synced.clone(), events: vec![Event::Click("window.panel.layers".into())] })));
let before = app.ui.panels.layers;
let mut raw = egui::RawInput::default();
if let Some(m) = app.services.native_menu.as_mut() {
m.raw_input(&mut raw);
}
frame(&ctx, raw, |ui| {
run(&mut app, ui.ctx());
sync(&mut app, ui.ctx());
});
assert_eq!(app.ui.panels.layers, !before);
let bar = synced.borrow().last().cloned().unwrap();
assert_eq!(bar.find("window.panel.layers").unwrap().checked, Some(!before));
}
/// With the Mac menu bar the title bar draws no menu titles: each title shows once fewer.
#[test]
fn the_title_bar_hides_its_menus_with_the_mac_menu_bar() {
use egui_kittest::{Harness, kittest::Queryable};
let counts = |native: bool| -> Vec<usize> {
let mut h = Harness::builder().with_size(egui::vec2(1440.0, 900.0)).with_max_steps(64).build_eframe(move |cc| {
PhotocraftApp::setup_context(&cc.egui_ctx, Default::default());
let mut app = app(true);
if native {
app.services.native_menu = Some(NativeMenu::new(Box::new(Fake { synced: Default::default(), events: Vec::new() })));
}
app
});
h.run_steps(4);
crate::menus::TOP_MENUS.iter().map(|t| h.query_all_by_label(t).count()).collect()
};
let (in_window, native) = (counts(false), counts(true));
for (i, title) in crate::menus::TOP_MENUS.iter().enumerate() {
assert_eq!(native[i] + 1, in_window[i], "{title}: {} in-window, {} with the Mac menu bar", in_window[i], native[i]);
}
}
}
+45 -4
View File
@@ -333,7 +333,8 @@ pub fn title_bar(app: &mut PhotocraftApp, ui: &mut egui::Ui) {
let (mark, _) = ui.allocate_exact_size(vec2(side, side), Sense::hover());
crate::brand::paint_mark(ui, mark);
ui.add_space(6.0);
menus_right = crate::menus::menu_bar(app, ui);
// With the macOS menu bar the menus are at the top of the screen instead.
menus_right = if app.services.native_menu.is_some() { ui.cursor().left() } else { crate::menus::menu_bar(app, ui) };
// The menu bar takes the whole row, so the right-hand group gets its own rect:
// from the menus to the bar's end, or to the caption buttons.
let right_edge = if custom { full.right() - crate::titlebar::WIDTH - 4.0 } else { full.right() };
@@ -1526,6 +1527,12 @@ fn layers(app: &mut PhotocraftApp, ui: &mut egui::Ui) {
actions.push(done);
}
});
// A layer being dragged can also be dropped on the footer's Delete, New Layer and Group
// buttons (#736); read the drag before it ends.
let footer_drag = ctx.data(|d| d.get_temp::<u64>(egui::Id::new("layer-drag"))).map(|id| {
let in_selection = selection.iter().any(|s| s.0 == id);
(id, in_selection)
});
// End any layer drag after every row has had a chance to accept the drop.
if ctx.input(|i| i.pointer.any_released()) {
ctx.data_mut(|d| d.remove::<u64>(egui::Id::new("layer-drag")));
@@ -1536,15 +1543,21 @@ fn layers(app: &mut PhotocraftApp, ui: &mut egui::Ui) {
ui.horizontal(|ui| {
ui.spacing_mut().item_spacing.x = 2.0;
ui.with_layout(egui::Layout::right_to_left(egui::Align::Center), |ui| {
if icons::button(ui, "trash", 26.0, false, tl!("Delete layer")).clicked() {
let trash = icons::button(ui, "trash", 26.0, false, tl!("Delete layer"));
if trash.clicked() {
actions.push(("layer.delete".into(), json!({})));
}
if icons::button(ui, "square-plus", 26.0, false, &crate::shortcuts::tip_label(app, "Create a new layer", "layer.new.layer")).clicked() {
actions.extend(footer_drop(ui, &trash, footer_drag, "layer.delete"));
let new_layer = icons::button(ui, "square-plus", 26.0, false, &crate::shortcuts::tip_label(app, "Create a new layer", "layer.new.layer"));
if new_layer.clicked() {
actions.push(("layer.new.layer".into(), json!({})));
}
if icons::button(ui, "folder", 26.0, false, tl!("Create a new group")).clicked() {
actions.extend(footer_drop(ui, &new_layer, footer_drag, "layer.duplicate"));
let group = icons::button(ui, "folder", 26.0, false, tl!("Create a new group"));
if group.clicked() {
actions.push(("layer.new.group".into(), json!({})));
}
actions.extend(footer_drop(ui, &group, footer_drag, "layer.groupLayers"));
let adj = icons::button(ui, "contrast", 26.0, false, tl!("Create new fill or adjustment layer"));
egui::Popup::menu(&adj).show(|ui| {
ui.set_min_width(190.0);
@@ -1622,6 +1635,34 @@ fn layers(app: &mut PhotocraftApp, ui: &mut egui::Ui) {
}
}
/// A layer row dragged onto a Layers panel footer button, as in Photoshop: onto Delete deletes it,
/// onto New Layer duplicates it, onto New Group groups it. A row that is part of the selection
/// carries the whole selection; any other row goes alone. Highlights the button while over it and
/// returns the command on release.
fn footer_drop(ui: &egui::Ui, button: &egui::Response, drag: Option<(u64, bool)>, command: &str) -> Option<(String, Value)> {
// Named for screen readers (and tests) after the command a drop runs.
button.widget_info(|| egui::WidgetInfo::labeled(egui::WidgetType::Button, true, footer_label(command)));
let (layer, in_selection) = drag?;
let p = ui.ctx().input(|i| i.pointer.interact_pos())?;
if !button.rect.contains(p) {
return None;
}
let t = Tokens::get(ui.ctx());
ui.painter().rect_stroke(button.rect, t.radius_sm, Stroke::new(2.0, t.accent), StrokeKind::Inside);
if !ui.ctx().input(|i| i.pointer.any_released()) {
return None;
}
Some((command.into(), if in_selection { json!({}) } else { json!({"layer": layer}) }))
}
fn footer_label(command: &str) -> &'static str {
match command {
"layer.delete" => tl!("Delete layer"),
"layer.duplicate" => tl!("Create a new layer"),
_ => tl!("Create a new group"),
}
}
/// Photoshop's italic "fx" footer button (no icon-font equivalent).
fn fx_button(ui: &mut egui::Ui, size: f32, tip: &str) -> egui::Response {
let t = Tokens::get(ui.ctx());
+45 -1
View File
@@ -788,6 +788,25 @@ fn choice_label(v: &str) -> String {
}
}
/// An editable `#rrggbb` field beside a colour swatch (#668): typing a valid value sets the
/// colour; while it's being typed a partial value is kept, and it shows the colour otherwise.
fn hex_field(ui: &mut egui::Ui, key: &str, rgb: &mut [u8; 3]) {
let id = egui::Id::new(("pref-hex", key));
let shown = format!("#{:02x}{:02x}{:02x}", rgb[0], rgb[1], rgb[2]);
let mut text = ui.data(|d| d.get_temp::<String>(id)).unwrap_or_else(|| shown.clone());
let r = ui.add(egui::TextEdit::singleline(&mut text).font(crate::theme::mono(11.5)).desired_width(72.0).char_limit(7));
if r.changed()
&& let Some(c) = prefs::parse_hex(&text)
{
*rgb = c;
}
if r.has_focus() {
ui.data_mut(|d| d.insert_temp(id, text));
} else {
ui.data_mut(|d| d.remove::<String>(id));
}
}
fn color_of(s: &str) -> Color32 {
prefs::parse_hex(s).map_or(Color32::GRAY, |c| Color32::from_rgb(c[0], c[1], c[2]))
}
@@ -971,7 +990,7 @@ fn section_fields(ui: &mut egui::Ui, section: &str, obj: &mut Map<String, Value>
let mut rgb = c;
ui.horizontal(|ui| {
ui.color_edit_button_srgb(&mut rgb);
ui.label(RichText::new(format!("#{:02x}{:02x}{:02x}", rgb[0], rgb[1], rgb[2])).font(crate::theme::mono(11.5)).color(t.text_dim));
hex_field(ui, &path, &mut rgb);
});
obj.insert(k, json!(format!("#{:02x}{:02x}{:02x}", rgb[0], rgb[1], rgb[2])));
let _ = color_of(s);
@@ -1386,6 +1405,31 @@ mod shortcut_capture_tests;
#[cfg(test)]
mod tests {
#[test]
fn colour_preferences_take_a_typed_hex_value() {
// #668: the canvas colour (and every colour preference) showed its hex but couldn't take one.
use egui::accesskit::Role;
use egui_kittest::kittest::Queryable;
let mut h = egui_kittest::Harness::new_ui_state(|ui, rgb: &mut [u8; 3]| super::hex_field(ui, "interface.canvasCustomColor", rgb), [0x28, 0x28, 0x28]);
h.run();
assert_eq!(h.get_by_role(Role::TextInput).value().as_deref(), Some("#282828"));
h.get_by_role(Role::TextInput).click();
h.run_steps(1);
h.key_press_modifiers(egui::Modifiers::COMMAND, egui::Key::A);
h.run_steps(1);
for ch in "#80".chars() {
h.event(egui::Event::Text(ch.to_string()));
h.run_steps(1);
}
// A partial value stays as typed and leaves the colour alone.
assert_eq!(h.get_by_role(Role::TextInput).value().as_deref(), Some("#80"));
assert_eq!(*h.state(), [0x28, 0x28, 0x28]);
for ch in "8080".chars() {
h.event(egui::Event::Text(ch.to_string()));
h.run_steps(1);
}
assert_eq!(*h.state(), [0x80, 0x80, 0x80]);
}
#[test]
fn rendering_mode_display_respects_explicit_mode_and_legacy_disable() {
+15
View File
@@ -1128,4 +1128,19 @@ mod tests {
app.session.presets.clone.active_mut().anchor = Some([20.0, 20.0]);
assert_eq!(clone_sample_point(&app, Some([30.0, 30.0])), Some([20.0, 20.0]));
}
#[test]
fn clone_marker_shows_only_while_painting() {
// #668: ⌥-click used to leave a "+" on the canvas until the next stroke.
let (mut app, _) = app();
app.ui.tool = Tool::CloneStamp;
crate::retouch_ui::set_source(&mut app, 10.0, 10.0);
app.hover_doc = Some([40.0, 40.0]);
assert_eq!(crate::retouch_ui::source_marker_point(&app), None, "hovering after ⌥-click shows nothing");
app.drag = Some(crate::canvas::Drag::new(Tool::CloneStamp, [40.0, 40.0], vec![[40.0, 40.0, 1.0]], egui::Modifiers::NONE, false));
assert!(crate::retouch_ui::source_marker_point(&app).is_some(), "painting shows where it samples");
app.drag = None;
app.ui.tool = Tool::Brush;
assert_eq!(crate::retouch_ui::source_marker_point(&app), None);
}
}
+14 -11
View File
@@ -169,20 +169,23 @@ pub fn set_source(app: &mut PhotocraftApp, x: f64, y: f64) {
app.ui.status_error = false;
}
/// Crosshair where the clone source is sampled from for the current pointer position.
pub fn draw_source_marker(app: &PhotocraftApp, painter: &egui::Painter, xf: &ViewXform) {
/// Where the clone source is sampled from, shown only while a Clone Stamp or Healing Brush stroke
/// is painted, as in Photoshop: setting the source (⌥-click) leaves nothing on the canvas (#668).
pub fn source_marker_point(app: &PhotocraftApp) -> Option<[f64; 2]> {
if !matches!(app.ui.tool, Tool::CloneStamp | Tool::Healing) {
return;
return None;
}
let at =
app.drag.as_ref().filter(|d| matches!(d.tool, Tool::CloneStamp | Tool::Healing)).and_then(|d| d.points.last().map(|p| [p[0], p[1]])).or(app.hover_doc);
let src = match (crate::preset_panels::clone_sample_point(app, at), app.ui.clone_offset, at, app.ui.clone_source) {
let at = app.drag.as_ref().filter(|d| matches!(d.tool, Tool::CloneStamp | Tool::Healing)).and_then(|d| d.points.last().map(|p| [p[0], p[1]]))?;
match (crate::preset_panels::clone_sample_point(app, Some(at)), app.ui.clone_offset, app.ui.clone_source) {
(Some(p), ..) => Some(p),
(None, Some(off), Some(h), _) => Some([h[0] + off[0], h[1] + off[1]]),
(None, None, _, Some(s)) => Some(s),
_ => None,
};
let Some(s) = src else { return };
(None, Some(off), _) => Some([at[0] + off[0], at[1] + off[1]]),
(None, None, s) => s,
}
}
/// Crosshair at [`source_marker_point`].
pub fn draw_source_marker(app: &PhotocraftApp, painter: &egui::Painter, xf: &ViewXform) {
let Some(s) = source_marker_point(app) else { return };
let c = xf.to_screen(s[0] as f32, s[1] as f32);
for (w, col) in [(3.0, Color32::from_black_alpha(160)), (1.0, Color32::WHITE)] {
painter.line_segment([c - vec2(7.0, 0.0), c + vec2(7.0, 0.0)], Stroke::new(w, col));
+5
View File
@@ -518,6 +518,11 @@ pub fn handle_keys(app: &mut PhotocraftApp, ctx: &egui::Context) -> bool {
e.caret = n;
}
}
// Photoshop: while editing type, ⌘/Ctrl+T shows or hides the Character panel
// (Free Transform of the layer being typed into is not what it means here).
Key::T if m.command && !m.shift && !m.alt => {
let _ = crate::menus::invoke(app, ctx, "window.panel.character", json!({}));
}
// Other command shortcuts (⌘Z, ⌘S, …) pass through to the menus.
_ if m.command => handled[k] = false,
_ => {}
+18
View File
@@ -434,3 +434,21 @@ fn vertical_type_tool_creates_point_and_paragraph_text_with_one_undo() {
assert!(app.session.active().unwrap().doc.layer(id).is_none());
}
}
/// #668: ⌘/Ctrl+T while typing shows or hides the Character panel, as in Photoshop, instead of
/// starting Free Transform on the layer being typed into.
#[test]
fn command_t_while_typing_toggles_the_character_panel() {
let mut app = new_app();
let id = LayerId(app.run("type.create", json!({"text": "HOHO", "size": 60, "x": 300, "y": 420})).unwrap()["layer"].as_u64().unwrap());
let mut h = harness(1.0, app);
let p = glyph(&mut h, id, 1, 0.5);
click(&mut h, p);
assert!(h.state().ui.text_edit.is_some(), "editing");
let before = crate::view_cmds::checked(h.state(), "window.panel.character");
h.event(egui::Event::Key { key: egui::Key::T, physical_key: None, pressed: true, repeat: false, modifiers: Modifiers::COMMAND });
h.run_steps(2);
assert!(h.state().ui.transform.is_none(), "no Free Transform while typing");
assert!(h.state().ui.text_edit.is_some(), "still editing");
assert_ne!(crate::view_cmds::checked(h.state(), "window.panel.character"), before, "the Character panel toggled");
}
+26 -1
View File
@@ -110,7 +110,7 @@ const METHODS: &[(&str, &str)] = &[("fit", "Fit"), ("fill", "Fill"), ("conform",
pub fn body(app: &mut PhotocraftApp, ui: &mut egui::Ui, fields: &mut Map<String, Value>) {
let t = Tokens::get(ui.ctx());
let layers = layer_options(app);
let mut state = fields.get("__variables").cloned().unwrap_or_else(|| json!({"defs": [], "dataSets": []}));
let mut state = normalize(fields.get("__variables").unwrap_or(&Value::Null));
let mut page = fields.get("__page").and_then(Value::as_str).unwrap_or("define").to_string();
ui.set_min_width(420.0);
@@ -137,6 +137,22 @@ pub fn body(app: &mut PhotocraftApp, ui: &mut egui::Ui, fields: &mut Map<String,
fields.insert("__page".into(), json!(page));
}
/// The dialog state with only object entries in `defs`, `dataSets` and each set's `values`.
/// `ui.dialog.set` can store any JSON in `__variables`, and the pages write fields into entries
/// (`d["name"] = …`), which panics on a number or a string.
fn normalize(state: &Value) -> Value {
let objects =
|v: Option<&Value>| -> Vec<Value> { v.and_then(Value::as_array).map(|a| a.iter().filter(|e| e.is_object()).cloned().collect()).unwrap_or_default() };
let sets: Vec<Value> = objects(state.get("dataSets"))
.into_iter()
.map(|mut set| {
set["values"] = Value::Array(objects(set.get("values")));
set
})
.collect();
json!({"defs": objects(state.get("defs")), "dataSets": sets})
}
fn define_page(ui: &mut egui::Ui, _t: &Tokens, layers: &[(u64, String, bool)], state: &mut Value) {
let Some(defs) = state.get_mut("defs").and_then(Value::as_array_mut) else { return };
ui.label(tl!("Variables bind a layer's visibility, text or pixels to a named data slot."));
@@ -339,6 +355,15 @@ mod tests {
assert_eq!(data_set_index(&fields, 0), 0);
}
#[test]
fn normalize_keeps_only_object_entries() {
let s = normalize(&json!({"defs": [1, {"name": "a"}], "dataSets": ["x", {"name": "A", "values": [true, {"variable": "a"}]}, {"values": 3}]}));
assert_eq!(s, json!({"defs": [{"name": "a"}], "dataSets": [{"name": "A", "values": [{"variable": "a"}]}, {"values": []}]}));
for bad in [Value::Null, json!(5), json!("s"), json!([1]), json!({"defs": "x", "dataSets": {}})] {
assert_eq!(normalize(&bad), json!({"defs": [], "dataSets": []}), "{bad}");
}
}
#[test]
fn owns_detects_the_field() {
let mut f = Map::new();
+24
View File
@@ -938,6 +938,30 @@ mod tests {
assert_eq!(app.session.active().unwrap().doc.layers.len(), 4);
}
#[test]
fn shift_pressed_during_a_shape_drag_constrains_it() {
// #668: ⇧ only counted when held at the press; Photoshop constrains when it is pressed
// mid-drag (and stops when it is released).
use crate::canvas::{ToolEvent, tool_event};
for (tool, sides) in [(Tool::Polygon, 6), (Tool::Rectangle, 4), (Tool::EllipseShape, 0)] {
let mut app = app();
app.ui.tool = tool;
app.ui.tool_options.polygon_sides = 6;
let none = egui::Modifiers::NONE;
tool_event(&mut app, ToolEvent::Down { x: 10.0, y: 10.0, pressure: 1.0 }, none);
tool_event(&mut app, ToolEvent::Move { x: 40.0, y: 30.0, pressure: 1.0 }, none);
tool_event(&mut app, ToolEvent::Move { x: 70.0, y: 40.0, pressure: 1.0 }, egui::Modifiers::SHIFT);
tool_event(&mut app, ToolEvent::Up { x: 70.0, y: 40.0 }, egui::Modifiers::SHIFT);
let st = app.session.active().unwrap();
let l = st.doc.layer(st.active_layer.unwrap()).unwrap();
assert!(matches!(l.content, LayerContent::Shape(_)), "{tool:?} ({sides} sides): a shape layer");
let b = l.surface().unwrap().content_bounds();
// The drag is 60 × 30; constrained, the shape's box is 60 × 60 (a hexagon is narrower
// than its box, so the height tells).
assert!(b.height().abs_diff(60) <= 1 && b.width() <= 61, "{tool:?}: ⇧ mid-drag keeps proportions: {b:?}");
}
}
#[test]
fn shape_preview_is_the_committed_path() {
let mut app = app();
@@ -55,3 +55,37 @@ fn out_of_range_data_set_index_is_clamped_before_delete() {
h.get_by_label("Delete").click();
h.run_steps(2);
}
/// `ui.dialog.set` can store any JSON in `__variables`; the editors write into its entries.
fn app_with_malformed_variables(page: &str) -> PhotocraftApp {
let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), photocraft_ui_egui::Services::default());
app.run("file.new", json!({"width": 64, "height": 64})).unwrap();
app.run("layer.new.layer", json!({})).unwrap();
let mut fields = Map::new();
fields.insert(
"__variables".into(),
json!({"defs": [42, {"name": "v1", "layer": 0, "type": "textReplacement"}], "dataSets": ["bad", {"name": "A", "values": [7, "x"]}, {"name": "B", "values": "nope"}]}),
);
fields.insert("__page".into(), json!(page));
app.ui.open_dialog(DialogKind::Command, fields);
app
}
fn type_into_first_text_field(h: &mut Harness<'static, PhotocraftApp>) {
h.query_all_by_role(egui::accesskit::Role::TextInput).next().expect("a text field").click();
h.run_steps(2);
h.query_all_by_role(egui::accesskit::Role::TextInput).next().expect("a text field").type_text("z");
h.run_steps(2);
}
#[test]
fn malformed_variable_entries_are_dropped_before_an_edit() {
for page in ["define", "dataSets"] {
let mut h = harness(app_with_malformed_variables(page));
type_into_first_text_field(&mut h);
let state = &h.state().ui.dialogs.last().unwrap().fields["__variables"];
assert_eq!(state["defs"].as_array().unwrap().len(), 1, "{page}: {state}");
assert_eq!(state["dataSets"].as_array().unwrap().len(), 2, "{page}: {state}");
assert!(state["dataSets"].as_array().unwrap().iter().all(|s| s["values"].as_array().unwrap().iter().all(|v| v.is_object())), "{page}: {state}");
}
}