Skip to content

Turn one illustration into an animated 2D mesh avatar with a coding agent and a local editor

Switch desktop avatars from the macOS menu or by dropping files

shinshin86cb32a239b705
diff --git a/native/Cargo.lock b/native/Cargo.lock
index 9c1de06..b7f041b 100644
--- a/native/Cargo.lock
+++ b/native/Cargo.lock
@@ -151,6 +151,9 @@ name = "bitflags"
version = "2.13.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3ded4057c258ba199e2d26386d3af3780957ecaee6c4ef4041c6b4b8b97c0b06"
+dependencies = [
+ "serde_core",
+]
[[package]]
name = "block"
@@ -164,7 +167,16 @@ version = "0.5.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2c132eebf10f5cad5289222520a4a058514204aed6d791f1cf4fe8088b82d15f"
dependencies = [
- "objc2",
+ "objc2 0.5.3",
+]
+
+[[package]]
+name = "block2"
+version = "0.6.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "cdeb9d870516001442e364c5220d3574d2da8dc765554b4a617230d33fa58ef5"
+dependencies = [
+ "objc2 0.6.5",
]
[[package]]
@@ -349,6 +361,15 @@ dependencies = [
"cfg-if",
]
+[[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-utils"
version = "0.8.23"
@@ -378,12 +399,45 @@ dependencies = [
"syn 3.0.9",
]
+[[package]]
+name = "dirs"
+version = "6.0.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "c3e8aa94d75141228480295a7d0e7feb620b1a5ad9f12bc40be62411e38cce4e"
+dependencies = [
+ "dirs-sys",
+]
+
+[[package]]
+name = "dirs-sys"
+version = "0.5.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "e01a3366d27ee9890022452ee61b2b63a67e6f13f58900b651ff5665f0bb1fab"
+dependencies = [
+ "libc",
+ "option-ext",
+ "redox_users",
+ "windows-sys 0.61.2",
+]
+
[[package]]
name = "dispatch"
version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bd0c93bb4b0c6d9b77f4435b0ae98c24d17f1c45b2ff844c6151a07256ca923b"
+[[package]]
+name = "dispatch2"
+version = "0.3.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "1e0e367e4e7da84520dedcac1901e4da967309406d1e51017ae1abfb97adbd38"
+dependencies = [
+ "bitflags 2.13.2",
+ "block2 0.6.2",
+ "libc",
+ "objc2 0.6.5",
+]
+
[[package]]
name = "dlib"
version = "0.5.3"
@@ -778,6 +832,17 @@ dependencies = [
"wasm-bindgen",
]
+[[package]]
+name = "keyboard-types"
+version = "0.7.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "b750dcadc39a09dbadd74e118f6dd6598df77fa01df0cfcdc52c28dece74528a"
+dependencies = [
+ "bitflags 2.13.2",
+ "serde",
+ "unicode-segmentation",
+]
+
[[package]]
name = "khronos-egl"
version = "6.0.0"
@@ -891,7 +956,7 @@ name = "mesh-avatar-core"
version = "0.1.0"
dependencies = [
"anyhow",
- "png",
+ "png 0.17.16",
"serde",
"serde_json",
"zip",
@@ -902,9 +967,13 @@ name = "mesh-avatar-desktop"
version = "0.1.0"
dependencies = [
"anyhow",
+ "dirs",
"mesh-avatar-core",
"mesh-avatar-wgpu",
+ "muda",
"pollster",
+ "rfd",
+ "serde_json",
"wgpu",
"winit",
]
@@ -916,7 +985,7 @@ dependencies = [
"anyhow",
"bytemuck",
"mesh-avatar-core",
- "png",
+ "png 0.17.16",
"pollster",
"serde",
"serde_json",
@@ -958,6 +1027,25 @@ dependencies = [
"simd-adler32",
]
+[[package]]
+name = "muda"
+version = "0.19.3"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "1dd04e60bc0b07438a6771710ee1698f98f6ebbc7f89b61264af1563b8aeb878"
+dependencies = [
+ "crossbeam-channel",
+ "dpi",
+ "keyboard-types",
+ "objc2 0.6.5",
+ "objc2-app-kit 0.3.2",
+ "objc2-core-foundation",
+ "objc2-foundation 0.3.2",
+ "once_cell",
+ "png 0.18.1",
+ "thiserror 2.0.21",
+ "windows-sys 0.61.2",
+]
+
[[package]]
name = "naga"
version = "27.0.3"
@@ -1071,6 +1159,15 @@ dependencies = [
"objc2-encode",
]
+[[package]]
+name = "objc2"
+version = "0.6.5"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "08849bbd4767dfae9457696856ae1c84fe4e0281bbe4a7abff2d0e06fb7981f8"
+dependencies = [
+ "objc2-encode",
+]
+
[[package]]
name = "objc2-app-kit"
version = "0.2.2"
@@ -1078,15 +1175,28 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e4e89ad9e3d7d297152b17d39ed92cd50ca8063a89a9fa569046d41568891eff"
dependencies = [
"bitflags 2.13.2",
- "block2",
+ "block2 0.5.1",
"libc",
- "objc2",
+ "objc2 0.5.3",
"objc2-core-data",
"objc2-core-image",
- "objc2-foundation",
+ "objc2-foundation 0.2.2",
"objc2-quartz-core",
]
+[[package]]
+name = "objc2-app-kit"
+version = "0.3.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "d49e936b501e5c5bf01fda3a9452ff86dc3ea98ad5f283e1455153142d97518c"
+dependencies = [
+ "bitflags 2.13.2",
+ "block2 0.6.2",
+ "objc2 0.6.5",
+ "objc2-core-foundation",
+ "objc2-foundation 0.3.2",
+]
+
[[package]]
name = "objc2-cloud-kit"
version = "0.2.2"
@@ -1094,10 +1204,10 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "74dd3b56391c7a0596a295029734d3c1c5e7e510a4cb30245f8221ccea96b009"
dependencies = [
"bitflags 2.13.2",
- "block2",
- "objc2",
+ "block2 0.5.1",
+ "objc2 0.5.3",
"objc2-core-location",
- "objc2-foundation",
+ "objc2-foundation 0.2.2",
]
[[package]]
@@ -1106,9 +1216,9 @@ version = "0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a5ff520e9c33812fd374d8deecef01d4a840e7b41862d849513de77e44aa4889"
dependencies = [
- "block2",
- "objc2",
- "objc2-foundation",
+ "block2 0.5.1",
+ "objc2 0.5.3",
+ "objc2-foundation 0.2.2",
]
[[package]]
@@ -1118,9 +1228,20 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "617fbf49e071c178c0b24c080767db52958f716d9eabdf0890523aeae54773ef"
dependencies = [
"bitflags 2.13.2",
- "block2",
- "objc2",
- "objc2-foundation",
+ "block2 0.5.1",
+ "objc2 0.5.3",
+ "objc2-foundation 0.2.2",
+]
+
+[[package]]
+name = "objc2-core-foundation"
+version = "0.3.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "2a180dd8642fa45cdb7dd721cd4c11b1cadd4929ce112ebd8b9f5803cc79d536"
+dependencies = [
+ "bitflags 2.13.2",
+ "dispatch2",
+ "objc2 0.6.5",
]
[[package]]
@@ -1129,9 +1250,9 @@ version = "0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "55260963a527c99f1819c4f8e3b47fe04f9650694ef348ffd2227e8196d34c80"
dependencies = [
- "block2",
- "objc2",
- "objc2-foundation",
+ "block2 0.5.1",
+ "objc2 0.5.3",
+ "objc2-foundation 0.2.2",
"objc2-metal",
]
@@ -1141,10 +1262,10 @@ version = "0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "000cfee34e683244f284252ee206a27953279d370e309649dc3ee317b37e5781"
dependencies = [
- "block2",
- "objc2",
+ "block2 0.5.1",
+ "objc2 0.5.3",
"objc2-contacts",
- "objc2-foundation",
+ "objc2-foundation 0.2.2",
]
[[package]]
@@ -1160,10 +1281,21 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0ee638a5da3799329310ad4cfa62fbf045d5f56e3ef5ba4149e7452dcf89d5a8"
dependencies = [
"bitflags 2.13.2",
- "block2",
+ "block2 0.5.1",
"dispatch",
"libc",
- "objc2",
+ "objc2 0.5.3",
+]
+
+[[package]]
+name = "objc2-foundation"
+version = "0.3.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "e3e0adef53c21f888deb4fa59fc59f7eb17404926ee8a6f59f5df0fd7f9f3272"
+dependencies = [
+ "bitflags 2.13.2",
+ "objc2 0.6.5",
+ "objc2-core-foundation",
]
[[package]]
@@ -1172,10 +1304,10 @@ version = "0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a1a1ae721c5e35be65f01a03b6d2ac13a54cb4fa70d8a5da293d7b0020261398"
dependencies = [
- "block2",
- "objc2",
- "objc2-app-kit",
- "objc2-foundation",
+ "block2 0.5.1",
+ "objc2 0.5.3",
+ "objc2-app-kit 0.2.2",
+ "objc2-foundation 0.2.2",
]
[[package]]
@@ -1185,9 +1317,9 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dd0cba1276f6023976a406a14ffa85e1fdd19df6b0f737b063b95f6c8c7aadd6"
dependencies = [
"bitflags 2.13.2",
- "block2",
- "objc2",
- "objc2-foundation",
+ "block2 0.5.1",
+ "objc2 0.5.3",
+ "objc2-foundation 0.2.2",
]
[[package]]
@@ -1197,9 +1329,9 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e42bee7bff906b14b167da2bac5efe6b6a07e6f7c0a21a7308d40c960242dc7a"
dependencies = [
"bitflags 2.13.2",
- "block2",
- "objc2",
- "objc2-foundation",
+ "block2 0.5.1",
+ "objc2 0.5.3",
+ "objc2-foundation 0.2.2",
"objc2-metal",
]
@@ -1209,8 +1341,8 @@ version = "0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0a684efe3dec1b305badae1a28f6555f6ddd3bb2c2267896782858d5a78404dc"
dependencies = [
- "objc2",
- "objc2-foundation",
+ "objc2 0.5.3",
+ "objc2-foundation 0.2.2",
]
[[package]]
@@ -1220,13 +1352,13 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b8bb46798b20cd6b91cbd113524c490f1686f4c4e8f49502431415f3512e2b6f"
dependencies = [
"bitflags 2.13.2",
- "block2",
- "objc2",
+ "block2 0.5.1",
+ "objc2 0.5.3",
"objc2-cloud-kit",
"objc2-core-data",
"objc2-core-image",
"objc2-core-location",
- "objc2-foundation",
+ "objc2-foundation 0.2.2",
"objc2-link-presentation",
"objc2-quartz-core",
"objc2-symbols",
@@ -1240,9 +1372,9 @@ version = "0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "44fa5f9748dbfe1ca6c0b79ad20725a11eca7c2218bceb4b005cb1be26273bfe"
dependencies = [
- "block2",
- "objc2",
- "objc2-foundation",
+ "block2 0.5.1",
+ "objc2 0.5.3",
+ "objc2-foundation 0.2.2",
]
[[package]]
@@ -1252,10 +1384,10 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "76cfcbf642358e8689af64cee815d139339f3ed8ad05103ed5eaf73db8d84cb3"
dependencies = [
"bitflags 2.13.2",
- "block2",
- "objc2",
+ "block2 0.5.1",
+ "objc2 0.5.3",
"objc2-core-location",
- "objc2-foundation",
+ "objc2-foundation 0.2.2",
]
[[package]]
@@ -1264,6 +1396,12 @@ version = "1.21.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9f7c3e4beb33f85d45ae3e3a1792185706c8e16d043238c593331cc7cd313b50"
+[[package]]
+name = "option-ext"
+version = "0.2.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "04744f49eae99ab78e0d5c0b603ab218f515ea8cfe5a456d7629ad883a3b6e7d"
+
[[package]]
name = "orbclient"
version = "0.3.55"
@@ -1378,6 +1516,19 @@ dependencies = [
"miniz_oxide 0.8.9",
]
+[[package]]
+name = "png"
+version = "0.18.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "60769b8b31b2a9f263dae2776c37b1b28ae246943cf719eb6946a1db05128a61"
+dependencies = [
+ "bitflags 2.13.2",
+ "crc32fast",
+ "fdeflate",
+ "flate2",
+ "miniz_oxide 0.8.9",
+]
+
[[package]]
name = "polling"
version = "3.11.0"
@@ -1512,12 +1663,45 @@ dependencies = [
"bitflags 2.13.2",
]
+[[package]]
+name = "redox_users"
+version = "0.5.3"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "60dc65c0ff1a7ae1294b0c67b9f14baf70b644404010370171787bfac1038fc0"
+dependencies = [
+ "libredox",
+ "thiserror 2.0.21",
+]
+
[[package]]
name = "renderdoc-sys"
version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "19b30a45b0cd0bcca8037f3d0dc3421eaf95327a17cad11964fb8179b4fc4832"
+[[package]]
+name = "rfd"
+version = "0.17.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "20dafead71c16a34e1ff357ddefc8afc11e7d51d6d2b9fbd07eaa48e3e540220"
+dependencies = [
+ "block2 0.6.2",
+ "dispatch2",
+ "js-sys",
+ "libc",
+ "log",
+ "objc2 0.6.5",
+ "objc2-app-kit 0.3.2",
+ "objc2-core-foundation",
+ "objc2-foundation 0.3.2",
+ "percent-encoding",
+ "raw-window-handle",
+ "wasm-bindgen",
+ "wasm-bindgen-futures",
+ "web-sys",
+ "windows-sys 0.61.2",
+]
+
[[package]]
name = "rustc-hash"
version = "1.1.0"
@@ -2468,7 +2652,7 @@ dependencies = [
"android-activity",
"atomic-waker",
"bitflags 2.13.2",
- "block2",
+ "block2 0.5.1",
"bytemuck",
"calloop",
"cfg_aliases",
@@ -2481,9 +2665,9 @@ dependencies = [
"libc",
"memmap2",
"ndk",
- "objc2",
- "objc2-app-kit",
- "objc2-foundation",
+ "objc2 0.5.3",
+ "objc2-app-kit 0.2.2",
+ "objc2-foundation 0.2.2",
"objc2-ui-kit",
"orbclient",
"percent-encoding",
diff --git a/native/README.md b/native/README.md
index 59c6d9d..d3fc66d 100644
--- a/native/README.md
+++ b/native/README.md
@@ -42,8 +42,25 @@ Scroll and keyboard scaling share a 40%–400% range, further limited by the
monitor's size and the GPU's texture limit. Input bursts are combined before
resizing; rendering waits until the surface and window have matching sizes.
+On macOS, **File → Open Avatar…** opens a native dialog for either a `.mavatar`
+archive or a project folder containing `rig.json` and `built/`. You can also drop
+either onto the avatar window. Loading runs in the background; an invalid avatar
+leaves the current one displayed and shows an error. A successful switch rebuilds
+its GPU resources and viewport, keeps the current zoom within the new size limits,
+preserves the window's top-left position and click-through state, and prints the
+new motion list in the terminal.
+
+**File → Open Recent** keeps the last eight successfully opened paths. They are
+stored in `mesh-avatar-desktop/recent.json` under the OS user configuration
+directory (`~/Library/Application Support/` on macOS). Missing paths are removed
+at startup and when the app regains focus. **File → Reload** reopens the current
+avatar after edits. Loading a new avatar resets expressions and animation state.
+
| Control | Action |
|---|---|
+| Command + `O` | Open an avatar archive or built project folder on macOS |
+| Command + `R` | Reload the current avatar |
+| Drop a `.mavatar` or project folder | Switch avatars in the same window |
| Left-drag | Move the avatar window |
| Scroll | Scale the window |
| Command + `+` / `-` | Scale the window |
@@ -102,6 +119,9 @@ Metal backend on macOS, and `--dpr=2` checks equal physical sizes at HiDPI.
- Windows and Linux desktop behavior has not been verified. Window transparency,
always-on-top behavior and cursor click-through depend on the OS compositor.
The app exits if a transparent surface is unavailable.
+- The native menu bar and Open Avatar dialog are macOS-only. The executable
+ embeds the name “Mesh Avatar” without an app bundle; the name shown elsewhere
+ by macOS can depend on how it was launched.
- Desktop input is limited to the controls above. Camera tracking, microphone
input, audio playback and lighting are not included.
- The desktop is a Cargo-built executable, without an installer, signed app
diff --git a/native/apps/mesh-avatar-desktop/Cargo.toml b/native/apps/mesh-avatar-desktop/Cargo.toml
index 4634055..de7cc72 100644
--- a/native/apps/mesh-avatar-desktop/Cargo.toml
+++ b/native/apps/mesh-avatar-desktop/Cargo.toml
@@ -12,3 +12,9 @@ winit.workspace = true
wgpu.workspace = true
pollster.workspace = true
anyhow.workspace = true
+serde_json.workspace = true
+dirs = "6"
+
+[target.'cfg(target_os = "macos")'.dependencies]
+muda = { version = "0.19.3", default-features = false }
+rfd = { version = "0.17", default-features = false }
diff --git a/native/apps/mesh-avatar-desktop/src/app-info.plist b/native/apps/mesh-avatar-desktop/src/app-info.plist
new file mode 100644
index 0000000..06c5a71
--- /dev/null
+++ b/native/apps/mesh-avatar-desktop/src/app-info.plist
@@ -0,0 +1,9 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
+<plist version="1.0">
+<dict>
+ <key>CFBundleName</key><string>Mesh Avatar</string>
+ <key>CFBundleDisplayName</key><string>Mesh Avatar</string>
+ <key>NSHighResolutionCapable</key><true/>
+</dict>
+</plist>
diff --git a/native/apps/mesh-avatar-desktop/src/geometry.rs b/native/apps/mesh-avatar-desktop/src/geometry.rs
new file mode 100644
index 0000000..a4edfc4
--- /dev/null
+++ b/native/apps/mesh-avatar-desktop/src/geometry.rs
@@ -0,0 +1,102 @@
+//! Prepare a replacement viewport without moving or mutating the live window.
+use crate::scaling::{ScaleBounds, Scaling};
+use anyhow::Result;
+use mesh_avatar_wgpu::ViewOptions;
+use winit::dpi::{LogicalSize, PhysicalPosition};
+
+pub fn base_size(view: &ViewOptions) -> LogicalSize<f64> {
+ LogicalSize::new(
+ 400.0,
+ 400.0 * f64::from(view.image_size[1]) * (1.0 + view.pad_top)
+ / (f64::from(view.image_size[0]) * (1.0 + 2.0 * view.pad_side)),
+ )
+}
+
+pub fn fit_base(view: &ViewOptions, screen: Option<LogicalSize<f64>>) -> Result<LogicalSize<f64>> {
+ view.transform([1, 1])?;
+ let mut base = base_size(view);
+ Scaling::new(base)?;
+ if let Some(screen) = screen {
+ let fit = (screen.width * 0.85 / base.width)
+ .min(screen.height * 0.85 / base.height)
+ .min(1.0);
+ base.width *= fit;
+ base.height *= fit;
+ }
+ Scaling::new(base)?;
+ Ok(base)
+}
+
+pub struct SwitchLayout {
+ pub scaling: Scaling,
+ pub position: PhysicalPosition<i32>,
+}
+pub fn switch_layout(
+ view: &ViewOptions,
+ current: &Scaling,
+ bounds: ScaleBounds,
+ position: PhysicalPosition<i32>,
+) -> Result<SwitchLayout> {
+ let base = fit_base(view, bounds.screen)?;
+ Ok(SwitchLayout {
+ scaling: current.rebase(base, bounds)?,
+ position,
+ })
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+ use mesh_avatar_wgpu::Fit;
+ use winit::dpi::PhysicalSize;
+ fn bounds() -> ScaleBounds {
+ ScaleBounds {
+ dpi: 2.0,
+ screen: None,
+ texture_limit: 8192,
+ }
+ }
+ #[test]
+ fn switching_aspect_preserves_zoom_top_left_and_padding() {
+ let mut current = Scaling::new(LogicalSize::new(400.0, 372.0)).unwrap();
+ current.keyboard(true, bounds());
+ current.take_request();
+ let view = ViewOptions {
+ image_size: [1000, 500],
+ pad_top: -0.1,
+ pad_side: 0.1,
+ fit: Fit::Contain,
+ };
+ let position = PhysicalPosition::new(-1200, 140);
+ let mut layout = switch_layout(&view, &current, bounds(), position).unwrap();
+ assert_eq!(layout.position, position);
+ assert_eq!(
+ layout.scaling.take_request(),
+ Some(PhysicalSize::new(880, 330))
+ );
+ assert!(current.take_request().is_none());
+ }
+ #[test]
+ fn extreme_aspect_is_fitted_and_bad_rig_leaves_current_layout_intact() {
+ let current = Scaling::new(LogicalSize::new(400.0, 372.0)).unwrap();
+ let mut view = ViewOptions {
+ image_size: [100, 10000],
+ pad_top: 0.0,
+ pad_side: 0.0,
+ fit: Fit::Contain,
+ };
+ let limits = ScaleBounds {
+ dpi: 2.0,
+ screen: Some(LogicalSize::new(1200.0, 800.0)),
+ texture_limit: 8192,
+ };
+ let mut layout =
+ switch_layout(&view, &current, limits, PhysicalPosition::new(0, 0)).unwrap();
+ assert_eq!(
+ layout.scaling.take_request(),
+ Some(PhysicalSize::new(14, 1360))
+ );
+ view.pad_top = -1.0;
+ assert!(switch_layout(&view, &current, bounds(), PhysicalPosition::new(0, 0)).is_err());
+ }
+}
diff --git a/native/apps/mesh-avatar-desktop/src/loading.rs b/native/apps/mesh-avatar-desktop/src/loading.rs
new file mode 100644
index 0000000..337a754
--- /dev/null
+++ b/native/apps/mesh-avatar-desktop/src/loading.rs
@@ -0,0 +1,284 @@
+//! Avatar input validation, background I/O, and the successful-open history.
+use anyhow::{ensure, Context, Result};
+use mesh_avatar_core::format::AvatarPackage;
+use std::{
+ fs,
+ path::{Path, PathBuf},
+ sync::mpsc,
+ thread,
+};
+
+const RECENT_LIMIT: usize = 8;
+
+pub fn avatar_path(path: &Path) -> Result<PathBuf> {
+ let path = path.canonicalize().context("avatar path is unavailable")?;
+ if path.is_dir() {
+ ensure!(
+ path.join("rig.json").is_file() && path.join("built").is_dir(),
+ "a project folder must contain rig.json and built/"
+ );
+ } else {
+ ensure!(
+ path.is_file()
+ && path
+ .extension()
+ .is_some_and(|s| s.eq_ignore_ascii_case("mavatar")),
+ "choose a .mavatar archive or a built project folder"
+ );
+ }
+ Ok(path)
+}
+
+pub struct Loaded {
+ pub generation: u64,
+ pub path: PathBuf,
+ pub result: Result<AvatarPackage, String>,
+}
+
+/// One worker serializes I/O and skips superseded queued requests.
+pub struct Loader {
+ requests: mpsc::Sender<(u64, PathBuf)>,
+ generation: u64,
+}
+impl Loader {
+ pub fn new(notify: impl Fn(Loaded) + Send + 'static) -> Result<Self> {
+ let (requests, receiver) = mpsc::channel::<(u64, PathBuf)>();
+ thread::Builder::new()
+ .name("avatar loader".into())
+ .spawn(move || {
+ while let Ok(mut request) = receiver.recv() {
+ for newer in receiver.try_iter() {
+ request = newer;
+ }
+ let (generation, requested) = request;
+ let loaded = std::panic::catch_unwind(|| {
+ avatar_path(&requested).and_then(|path| {
+ let package = AvatarPackage::open(&path)?;
+ Ok((path, package))
+ })
+ })
+ .unwrap_or_else(|_| Err(anyhow::anyhow!("avatar decoder failed unexpectedly")));
+ let (path, result) = match loaded {
+ Ok((path, package)) => (path, Ok(package)),
+ Err(error) => (requested, Err(format!("{error:#}"))),
+ };
+ notify(Loaded {
+ generation,
+ path,
+ result,
+ });
+ }
+ })?;
+ Ok(Self {
+ requests,
+ generation: 0,
+ })
+ }
+ pub fn request(&mut self, path: PathBuf) -> Result<()> {
+ let generation = self.generation.wrapping_add(1);
+ self.requests
+ .send((generation, path))
+ .context("avatar loader stopped")?;
+ self.generation = generation;
+ Ok(())
+ }
+ pub fn is_current(&self, loaded: &Loaded) -> bool {
+ self.generation == loaded.generation
+ }
+}
+
+pub struct Recent {
+ file: Option<PathBuf>,
+ entries: Vec<PathBuf>,
+}
+impl Recent {
+ pub fn config_file() -> Option<PathBuf> {
+ dirs::config_dir().map(|dir| dir.join("mesh-avatar-desktop/recent.json"))
+ }
+ pub fn empty(file: Option<PathBuf>) -> Self {
+ Self {
+ file,
+ entries: Vec::new(),
+ }
+ }
+ pub fn load(file: Option<PathBuf>) -> Result<Self> {
+ let mut recent = Self::empty(file);
+ if let Some(file) = &recent.file {
+ match fs::read(file) {
+ Ok(bytes) => {
+ ensure!(bytes.len() <= 65536, "recent avatar list is too large");
+ recent.entries =
+ serde_json::from_slice(&bytes).context("invalid recent avatar list")?;
+ }
+ Err(error) if error.kind() == std::io::ErrorKind::NotFound => return Ok(recent),
+ Err(error) => return Err(error.into()),
+ }
+ }
+ if recent.prune() {
+ recent.save()?;
+ }
+ Ok(recent)
+ }
+ pub fn entries(&self) -> &[PathBuf] {
+ &self.entries
+ }
+ pub fn prune(&mut self) -> bool {
+ let mut entries = Vec::new();
+ for path in &self.entries {
+ if let Ok(path) = avatar_path(path) {
+ if !entries.contains(&path) {
+ entries.push(path);
+ }
+ }
+ if entries.len() == RECENT_LIMIT {
+ break;
+ }
+ }
+ let changed = entries != self.entries;
+ self.entries = entries;
+ changed
+ }
+ pub fn record(&mut self, path: &Path) -> Result<()> {
+ let path = avatar_path(path)?;
+ self.entries.retain(|entry| entry != &path);
+ self.entries.insert(0, path);
+ self.prune();
+ self.save()
+ }
+ pub fn save(&self) -> Result<()> {
+ if let Some(file) = &self.file {
+ if let Some(parent) = file.parent() {
+ fs::create_dir_all(parent)?;
+ }
+ let temporary = file.with_extension("json.tmp");
+ fs::write(&temporary, serde_json::to_vec_pretty(&self.entries)?)?;
+ fs::rename(&temporary, file)?;
+ }
+ Ok(())
+ }
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+ use std::{
+ sync::atomic::{AtomicU64, Ordering},
+ time::Duration,
+ };
+ static NEXT: AtomicU64 = AtomicU64::new(0);
+ struct Fixture(PathBuf);
+ impl Fixture {
+ fn new() -> Self {
+ let path = std::env::temp_dir().join(format!(
+ "mesh-avatar-open-{}-{}",
+ std::process::id(),
+ NEXT.fetch_add(1, Ordering::Relaxed)
+ ));
+ fs::create_dir_all(&path).unwrap();
+ Self(path)
+ }
+ fn archive(&self, name: &str) -> PathBuf {
+ let path = self.0.join(format!("{name}.mavatar"));
+ fs::write(&path, b"not an archive").unwrap();
+ path
+ }
+ }
+ impl Drop for Fixture {
+ fn drop(&mut self) {
+ let _ = fs::remove_dir_all(&self.0);
+ }
+ }
+
+ #[test]
+ fn inputs_require_archive_extension_or_built_project() {
+ let fixture = Fixture::new();
+ let archive = fixture.archive("avatar");
+ assert_eq!(
+ avatar_path(&archive).unwrap(),
+ archive.canonicalize().unwrap()
+ );
+ assert!(avatar_path(&fixture.0).is_err());
+ fs::write(fixture.0.join("rig.json"), b"{}").unwrap();
+ fs::create_dir(fixture.0.join("built")).unwrap();
+ assert!(avatar_path(&fixture.0).is_ok());
+ let text = fixture.0.join("avatar.txt");
+ fs::write(&text, b"invalid").unwrap();
+ assert!(avatar_path(&text).is_err());
+ assert!(avatar_path(&fixture.0.join("missing.mavatar")).is_err());
+ }
+
+ #[test]
+ fn recent_roundtrip_orders_deduplicates_caps_and_prunes_missing_entries() {
+ let fixture = Fixture::new();
+ let file = fixture.0.join("config/recent.json");
+ let mut recent = Recent::load(Some(file.clone())).unwrap();
+ assert!(!file.exists());
+ let paths: Vec<_> = (0..12).map(|i| fixture.archive(&i.to_string())).collect();
+ for path in &paths {
+ recent.record(path).unwrap();
+ }
+ recent.record(&paths[8]).unwrap();
+ assert_eq!(recent.entries.len(), 8);
+ assert_eq!(recent.entries[0], paths[8].canonicalize().unwrap());
+ fs::remove_file(&paths[8]).unwrap();
+ let restored = Recent::load(Some(file.clone())).unwrap();
+ assert_eq!(restored.entries.len(), 7);
+ assert_eq!(restored.entries[0], paths[11].canonicalize().unwrap());
+ let on_disk: Vec<PathBuf> = serde_json::from_slice(&fs::read(file).unwrap()).unwrap();
+ assert_eq!(on_disk, restored.entries);
+ }
+
+ #[test]
+ fn malformed_history_and_failed_persistence_are_recoverable() {
+ let fixture = Fixture::new();
+ let file = fixture.0.join("recent.json");
+ fs::write(&file, b"invalid JSON").unwrap();
+ assert!(Recent::load(Some(file)).is_err());
+ let path = fixture.archive("new");
+ let blocked = fixture.0.join("blocked");
+ fs::create_dir(&blocked).unwrap();
+ let mut recent = Recent::empty(Some(blocked));
+ assert!(recent.record(&path).is_err());
+ assert_eq!(recent.entries.len(), 1);
+ }
+
+ #[test]
+ fn worker_returns_load_errors_and_ignores_stale_results() {
+ let fixture = Fixture::new();
+ let (sender, receiver) = mpsc::channel();
+ let mut worker = Loader::new(move |loaded| {
+ let _ = sender.send(loaded);
+ })
+ .unwrap();
+ worker.request(fixture.archive("first")).unwrap();
+ let first = receiver.recv_timeout(Duration::from_secs(5)).unwrap();
+ assert!(worker.is_current(&first));
+ assert!(first.result.is_err());
+ worker.request(fixture.archive("second")).unwrap();
+ assert!(!worker.is_current(&first));
+ let second = receiver.recv_timeout(Duration::from_secs(5)).unwrap();
+ assert!(worker.is_current(&second));
+ assert!(second.result.is_err());
+ }
+
+ #[test]
+ fn worker_loads_a_real_built_project() {
+ let sample = Path::new(env!("CARGO_MANIFEST_DIR")).join("../../../samples/miko-qipao");
+ if !sample.join("built/base.png").is_file() {
+ eprintln!("Skipping background load integration: sample images are not installed");
+ return;
+ }
+ let (sender, receiver) = mpsc::channel();
+ let mut worker = Loader::new(move |loaded| {
+ let _ = sender.send(loaded);
+ })
+ .unwrap();
+ worker.request(sample.clone()).unwrap();
+ let loaded = receiver.recv_timeout(Duration::from_secs(15)).unwrap();
+ assert!(worker.is_current(&loaded));
+ assert_eq!(loaded.path, sample.canonicalize().unwrap());
+ let package = loaded.result.unwrap();
+ assert!(!package.manifest.name.is_empty());
+ assert!(package.textures.contains_key("base.png"));
+ }
+}
diff --git a/native/apps/mesh-avatar-desktop/src/main.rs b/native/apps/mesh-avatar-desktop/src/main.rs
index 97cc685..8ce3d38 100644
--- a/native/apps/mesh-avatar-desktop/src/main.rs
+++ b/native/apps/mesh-avatar-desktop/src/main.rs
@@ -1,8 +1,14 @@
+mod geometry;
+mod loading;
+mod menu;
mod presentation;
mod scaling;
use anyhow::{bail, ensure, Context, Result};
+use geometry::{fit_base, switch_layout};
+use loading::{Loaded, Loader, Recent};
use mesh_avatar_core::{
engine::{expression::OVERLAY_EXPRESSIONS, motion::SpeakOptions},
+ format::AvatarPackage,
Avatar, Frame,
};
use mesh_avatar_wgpu::{Fit, Renderer, ViewOptions};
@@ -20,9 +26,9 @@ use std::{
};
use winit::{
application::ApplicationHandler,
- dpi::{LogicalSize, PhysicalPosition, PhysicalSize},
+ dpi::{PhysicalPosition, PhysicalSize},
event::{ElementState, MouseButton, WindowEvent},
- event_loop::{ActiveEventLoop, ControlFlow, EventLoop},
+ event_loop::{ActiveEventLoop, ControlFlow, EventLoop, EventLoopProxy},
keyboard::{KeyCode, ModifiersState, PhysicalKey},
window::{Window, WindowId, WindowLevel},
};
@@ -43,12 +49,20 @@ fn default_sample() -> Result<PathBuf> {
}
bail!("Default sample not found. Run from a source checkout or pass a .mavatar file or built project folder.")
}
-fn base_size(view: &ViewOptions) -> LogicalSize<f64> {
- LogicalSize::new(
- 400.0,
- 400.0 * f64::from(view.image_size[1]) * (1.0 + view.pad_top)
- / (f64::from(view.image_size[0]) * (1.0 + 2.0 * view.pad_side)),
- )
+fn avatar_view(avatar: &Avatar) -> ViewOptions {
+ let rig = &avatar.package().rig;
+ ViewOptions {
+ image_size: [rig.image.width, rig.image.height],
+ pad_top: rig.view.pad_top,
+ pad_side: rig.view.pad_side,
+ fit: Fit::Contain,
+ }
+}
+fn print_motions(avatar: &Avatar) {
+ eprintln!("Avatar: {}", avatar.package().manifest.name);
+ for (i, motion) in avatar.motions().iter().take(9).enumerate() {
+ eprintln!("{}: {}", i + 1, motion.label);
+ }
}
/// Keep a painted source-image cutoff outside the window, including extreme poses.
@@ -132,6 +146,7 @@ struct Scene {
top_edge: TopEdgeGuard,
last_frame: Instant,
scaling: Scaling,
+ pending_position: Option<PhysicalPosition<i32>>,
pending_surface_size: Option<PhysicalSize<u32>>,
redraw_requested: bool,
gpu_failed: Arc<AtomicBool>,
@@ -142,23 +157,11 @@ struct Scene {
}
impl Scene {
async fn new(event_loop: &ActiveEventLoop, mut avatar: Avatar) -> Result<Self> {
- let rig = &avatar.package().rig;
- let view = ViewOptions {
- image_size: [rig.image.width, rig.image.height],
- pad_top: rig.view.pad_top,
- pad_side: rig.view.pad_side,
- fit: Fit::Contain,
- };
- let mut base_size = base_size(&view);
- Scaling::new(base_size)?;
- if let Some(monitor) = event_loop.primary_monitor() {
- let screen = monitor.size().to_logical::<f64>(monitor.scale_factor());
- let fit = (screen.width * 0.85 / base_size.width)
- .min(screen.height * 0.85 / base_size.height)
- .min(1.0);
- base_size.width *= fit;
- base_size.height *= fit;
- }
+ let view = avatar_view(&avatar);
+ let screen = event_loop
+ .primary_monitor()
+ .map(|m| m.size().to_logical(m.scale_factor()));
+ let base_size = fit_base(&view, screen)?;
let attributes = Window::default_attributes()
.with_title("Mesh Avatar")
.with_inner_size(base_size)
@@ -267,9 +270,7 @@ impl Scene {
window.scale_factor()
);
eprintln!("Drag to move · scroll or Cmd +/- to scale · E expression · K kana · C click-through · Esc quit");
- for (i, motion) in avatar.motions().iter().take(9).enumerate() {
- eprintln!("{}: {}", i + 1, motion.label);
- }
+ print_motions(&avatar);
window.set_visible(true);
window.focus_window();
window.request_redraw();
@@ -287,6 +288,7 @@ impl Scene {
top_edge,
last_frame: Instant::now(),
scaling: Scaling::new(base_size)?,
+ pending_position: None,
pending_surface_size: None,
redraw_requested: true,
gpu_failed,
@@ -325,6 +327,54 @@ impl Scene {
texture_limit: self.device.limits().max_texture_dimension_2d,
}
}
+ /// Build all replacement state first so a bad avatar cannot remove the live one.
+ fn replace_avatar(&mut self, package: AvatarPackage) -> Result<()> {
+ let mut avatar = Avatar::from_package(package)?;
+ let view = avatar_view(&avatar);
+ let layout = switch_layout(
+ &view,
+ &self.scaling,
+ self.scale_bounds(),
+ self.window.outer_position()?,
+ )?;
+ avatar.set_auto_idle(true);
+ avatar.set_auto_motion(true);
+ let initial = avatar.tick(0.0);
+ let top_edge = TopEdgeGuard::new(&avatar, &initial);
+ self.device.push_error_scope(wgpu::ErrorFilter::Validation);
+ self.device.push_error_scope(wgpu::ErrorFilter::OutOfMemory);
+ let renderer = catch_unwind(AssertUnwindSafe(|| {
+ Renderer::new(
+ &self.device,
+ &self.queue,
+ self.view_format,
+ 4,
+ avatar.textures(),
+ )
+ }))
+ .unwrap_or_else(|_| {
+ Err(anyhow::anyhow!(
+ "graphics backend could not prepare the new avatar"
+ ))
+ });
+ let memory_error = pollster::block_on(self.device.pop_error_scope());
+ let validation_error = pollster::block_on(self.device.pop_error_scope());
+ if let Some(error) = memory_error.or(validation_error) {
+ bail!("could not upload avatar: {error}");
+ }
+ let renderer = renderer?;
+ self.avatar = avatar;
+ self.renderer = renderer;
+ self.view = view;
+ self.top_edge = top_edge;
+ self.scaling = layout.scaling;
+ self.pending_position = Some(layout.position);
+ self.expression_index = 0;
+ self.last_frame = Instant::now();
+ self.redraw_requested = true;
+ print_motions(&self.avatar);
+ Ok(())
+ }
/// Run native resize and GPU work after input/drawRect callbacks return.
fn service(&mut self) -> Result<()> {
ensure!(
@@ -340,6 +390,9 @@ impl Scene {
}
}
}
+ if let Some(position) = self.pending_position.take() {
+ self.window.set_outer_position(position);
+ }
if let Some(size) = self.pending_surface_size.take() {
// Several notifications can be queued during a trackpad input burst.
if size == self.window.inner_size() {
@@ -471,12 +524,116 @@ impl Scene {
}
}
+enum UserEvent {
+ Menu(String),
+ Picked(Option<PathBuf>),
+ AlertClosed,
+ Loaded(Box<Loaded>),
+}
struct App {
avatar: Option<Avatar>,
scene: Option<Scene>,
error: Option<anyhow::Error>,
+ current_path: PathBuf,
+ recent: Recent,
+ loader: Loader,
+ pending_load: Option<Loaded>,
+ proxy: EventLoopProxy<UserEvent>,
+ menu: Option<menu::AppMenu>,
+ commands: Vec<menu::Command>,
+ dialog_open: bool,
+}
+impl App {
+ fn refresh_recent(&mut self) {
+ if self.recent.prune() {
+ if let Err(error) = self.recent.save() {
+ eprintln!("Could not save recent avatars: {error:#}");
+ }
+ }
+ if let Some(menu) = &mut self.menu {
+ if let Err(error) = menu.set_recent(self.recent.entries()) {
+ eprintln!("Could not update recent menu: {error:#}");
+ }
+ }
+ }
+ fn request_load(&mut self, path: PathBuf) {
+ if let Err(error) = self.loader.request(path) {
+ self.load_error(&format!("{error:#}"));
+ }
+ }
+ fn load_error(&mut self, message: &str) {
+ eprintln!("Could not open avatar; keeping current avatar: {message}");
+ if !self.dialog_open {
+ if let Some(scene) = &self.scene {
+ match menu::alert(self.proxy.clone(), &scene.window, message) {
+ Ok(()) => self.dialog_open = true,
+ Err(error) => eprintln!("Could not show avatar error dialog: {error:#}"),
+ }
+ }
+ }
+ }
+ fn service(&mut self, event_loop: &ActiveEventLoop) -> Result<()> {
+ for command in std::mem::take(&mut self.commands) {
+ match command {
+ menu::Command::Quit => {
+ event_loop.exit();
+ return Ok(());
+ }
+ menu::Command::Open if !self.dialog_open => {
+ if let Some(scene) = &self.scene {
+ let directory = if self.current_path.is_dir() {
+ Some(self.current_path.as_path())
+ } else {
+ self.current_path.parent()
+ };
+ match menu::open(self.proxy.clone(), &scene.window, directory) {
+ Ok(()) => self.dialog_open = true,
+ Err(error) => self.load_error(&format!("{error:#}")),
+ }
+ }
+ }
+ menu::Command::Recent(path) => {
+ self.refresh_recent();
+ self.request_load(path);
+ }
+ menu::Command::Reload => self.request_load(self.current_path.clone()),
+ _ => {}
+ }
+ }
+ if let Some(loaded) = self
+ .pending_load
+ .take()
+ .filter(|loaded| self.loader.is_current(loaded))
+ {
+ match loaded
+ .result
+ .map_err(anyhow::Error::msg)
+ .and_then(|package| {
+ catch_unwind(AssertUnwindSafe(|| {
+ self.scene
+ .as_mut()
+ .context("avatar window is unavailable")?
+ .replace_avatar(package)
+ }))
+ .unwrap_or_else(|_| Err(anyhow::anyhow!("could not prepare the new avatar")))
+ }) {
+ Ok(()) => {
+ self.current_path = loaded.path;
+ if let Err(error) = self.recent.record(&self.current_path) {
+ eprintln!("Could not save recent avatars: {error:#}");
+ }
+ self.refresh_recent();
+ }
+ Err(error) => self.load_error(&format!("{error:#}")),
+ }
+ }
+ if let Some(scene) = &mut self.scene {
+ scene.service()?;
+ }
+ Ok(())
+ }
}
-impl ApplicationHandler for App {
+impl ApplicationHandler<UserEvent> for App {
fn resumed(&mut self, event_loop: &ActiveEventLoop) {
if let Some(scene) = &mut self.scene {
scene.last_frame = Instant::now();
@@ -486,7 +643,16 @@ impl ApplicationHandler for App {
}
if let Some(avatar) = self.avatar.take() {
match pollster::block_on(Scene::new(event_loop, avatar)) {
- Ok(scene) => self.scene = Some(scene),
+ Ok(scene) => {
+ self.scene = Some(scene);
+ if let Err(error) = self.recent.record(&self.current_path) {
+ eprintln!("Could not save recent avatars: {error:#}");
+ }
+ match menu::AppMenu::install(self.proxy.clone(), self.recent.entries()) {
+ Ok(menu) => self.menu = Some(menu),
+ Err(error) => eprintln!("Could not install avatar menu: {error:#}"),
+ }
+ }
Err(error) => {
self.error = Some(error);
event_loop.exit();
@@ -502,6 +668,8 @@ impl ApplicationHandler for App {
return;
}
match event {
+ WindowEvent::DroppedFile(path) => self.commands.push(menu::Command::Recent(path)),
+ WindowEvent::Focused(true) => self.refresh_recent(),
WindowEvent::CloseRequested => event_loop.exit(),
WindowEvent::Resized(size) => scene.pending_surface_size = Some(size),
WindowEvent::ScaleFactorChanged { .. } => scene.scaling.refresh(scene.scale_bounds()),
@@ -513,7 +681,18 @@ impl ApplicationHandler for App {
WindowEvent::ModifiersChanged(value) => scene.modifiers = value.state(),
WindowEvent::KeyboardInput { event, .. } if event.state == ElementState::Pressed => {
if let PhysicalKey::Code(key) = event.physical_key {
- scene.key(event_loop, key, event.repeat);
+ let command = if cfg!(target_os = "macos") {
+ scene.modifiers.super_key()
+ } else {
+ scene.modifiers.control_key()
+ };
+ if command && !event.repeat && key == KeyCode::KeyO {
+ self.commands.push(menu::Command::Open);
+ } else if command && !event.repeat && key == KeyCode::KeyR {
+ self.commands.push(menu::Command::Reload);
+ } else {
+ scene.key(event_loop, key, event.repeat);
+ }
}
}
WindowEvent::MouseInput {
@@ -533,21 +712,40 @@ impl ApplicationHandler for App {
}
}
fn about_to_wait(&mut self, event_loop: &ActiveEventLoop) {
- if let Some(scene) = &mut self.scene {
- // Contain unexpected backend panics before they cross an AppKit C ABI boundary.
- let result =
- catch_unwind(AssertUnwindSafe(|| scene.service())).unwrap_or_else(|payload| {
- let message = payload
- .downcast_ref::<String>()
- .map(String::as_str)
- .or_else(|| payload.downcast_ref::<&str>().copied())
- .unwrap_or("unknown graphics backend panic");
- Err(anyhow::anyhow!("desktop update failed: {message}"))
- });
- if let Err(error) = result {
- eprintln!("Desktop update stopped: {error:#}");
- self.error = Some(error);
- event_loop.exit();
+ // Contain unexpected backend panics before they cross an AppKit C ABI boundary.
+ let result =
+ catch_unwind(AssertUnwindSafe(|| self.service(event_loop))).unwrap_or_else(|payload| {
+ let message = payload
+ .downcast_ref::<String>()
+ .map(String::as_str)
+ .or_else(|| payload.downcast_ref::<&str>().copied())
+ .unwrap_or("unknown graphics backend panic");
+ Err(anyhow::anyhow!("desktop update failed: {message}"))
+ });
+ if let Err(error) = result {
+ eprintln!("Desktop update stopped: {error:#}");
+ self.error = Some(error);
+ event_loop.exit();
+ }
+ }
+ fn user_event(&mut self, _: &ActiveEventLoop, event: UserEvent) {
+ match event {
+ UserEvent::Menu(id) => {
+ if let Some(command) = self.menu.as_ref().and_then(|menu| menu.command(&id)) {
+ self.commands.push(command);
+ }
+ }
+ UserEvent::Picked(path) => {
+ self.dialog_open = false;
+ if let Some(path) = path {
+ self.request_load(path);
+ }
+ }
+ UserEvent::AlertClosed => self.dialog_open = false,
+ UserEvent::Loaded(loaded) => {
+ if self.loader.is_current(&loaded) {
+ self.pending_load = Some(*loaded);
+ }
}
}
}
@@ -563,9 +761,10 @@ fn run() -> Result<()> {
None => default_sample()?,
};
ensure!(args.next().is_none(), "expected at most one avatar path");
- let avatar = Avatar::open(path)
+ let path = loading::avatar_path(&path)?;
+ let avatar = Avatar::open(&path)
.context("Could not load avatar; a built project needs rig.json and built/ PNG assets")?;
- let mut builder = EventLoop::builder();
+ let mut builder = EventLoop::<UserEvent>::with_user_event();
#[cfg(target_os = "macos")]
{
use winit::platform::macos::{ActivationPolicy, EventLoopBuilderExtMacOS};
@@ -573,10 +772,28 @@ fn run() -> Result<()> {
}
let event_loop = builder.build()?;
event_loop.set_control_flow(ControlFlow::Wait);
+ let proxy = event_loop.create_proxy();
+ let notify = proxy.clone();
+ let loader = Loader::new(move |loaded| {
+ let _ = notify.send_event(UserEvent::Loaded(Box::new(loaded)));
+ })?;
+ let file = Recent::config_file();
+ let recent = Recent::load(file.clone()).unwrap_or_else(|error| {
+ eprintln!("Could not read recent avatars: {error:#}");
+ Recent::empty(file)
+ });
let mut app = App {
avatar: Some(avatar),
scene: None,
error: None,
+ current_path: path,
+ recent,
+ loader,
+ pending_load: None,
+ proxy,
+ menu: None,
+ commands: Vec::new(),
+ dialog_open: false,
};
event_loop.run_app(&mut app)?;
if let Some(error) = app.error {
@@ -602,7 +819,7 @@ mod tests {
pad_side: 0.0,
fit: Fit::Contain,
};
- let size = base_size(&view);
+ let size = geometry::base_size(&view);
assert!((size.height - 372.0).abs() < 1e-8);
for dpr in [1.0, 2.0] {
let physical: PhysicalSize<u32> = size.to_physical(dpr);
diff --git a/native/apps/mesh-avatar-desktop/src/menu.rs b/native/apps/mesh-avatar-desktop/src/menu.rs
new file mode 100644
index 0000000..950e1ab
--- /dev/null
+++ b/native/apps/mesh-avatar-desktop/src/menu.rs
@@ -0,0 +1,187 @@
+//! Native macOS menus and dialogs. All AppKit entry points stay on the main thread.
+use crate::UserEvent;
+use anyhow::Result;
+use std::path::{Path, PathBuf};
+use winit::{event_loop::EventLoopProxy, window::Window};
+
+pub enum Command {
+ Open,
+ Recent(PathBuf),
+ Reload,
+ Quit,
+}
+
+#[cfg(target_os = "macos")]
+mod platform {
+ use super::*;
+ use muda::{
+ accelerator::{Accelerator, Code, Modifiers},
+ Menu, MenuEvent, MenuItem, PredefinedMenuItem, Submenu,
+ };
+
+ // Supply the unbundled executable's application name without creating an .app.
+ const INFO: &[u8] = include_bytes!("app-info.plist");
+ #[used]
+ #[link_section = "__TEXT,__info_plist"]
+ static APP_INFO: [u8; INFO.len()] = *include_bytes!("app-info.plist");
+
+ pub struct AppMenu {
+ _root: Menu,
+ recent: Submenu,
+ entries: Vec<(MenuItem, PathBuf)>,
+ }
+ impl AppMenu {
+ pub fn install(proxy: EventLoopProxy<UserEvent>, paths: &[PathBuf]) -> Result<Self> {
+ let root = Menu::new();
+ let app = Submenu::new("Mesh Avatar", true);
+ let quit = MenuItem::with_id(
+ "quit",
+ "Quit Mesh Avatar",
+ true,
+ Some(Accelerator::new(Some(Modifiers::SUPER), Code::KeyQ)),
+ );
+ app.append_items(&[
+ &PredefinedMenuItem::hide(Some("Hide Mesh Avatar")),
+ &PredefinedMenuItem::hide_others(None),
+ &PredefinedMenuItem::show_all(None),
+ &PredefinedMenuItem::separator(),
+ &quit,
+ ])?;
+ let file = Submenu::new("File", true);
+ let open = MenuItem::with_id(
+ "open",
+ "Open Avatar…",
+ true,
+ Some(Accelerator::new(Some(Modifiers::SUPER), Code::KeyO)),
+ );
+ let recent = Submenu::new("Open Recent", true);
+ let reload = MenuItem::with_id(
+ "reload",
+ "Reload",
+ true,
+ Some(Accelerator::new(Some(Modifiers::SUPER), Code::KeyR)),
+ );
+ file.append_items(&[&open, &recent, &PredefinedMenuItem::separator(), &reload])?;
+ let edit = Submenu::with_items(
+ "Edit",
+ true,
+ &[
+ &PredefinedMenuItem::undo(None),
+ &PredefinedMenuItem::redo(None),
+ &PredefinedMenuItem::separator(),
+ &PredefinedMenuItem::cut(None),
+ &PredefinedMenuItem::copy(None),
+ &PredefinedMenuItem::paste(None),
+ &PredefinedMenuItem::select_all(None),
+ ],
+ )?;
+ root.append_items(&[&app, &file, &edit])?;
+ let mut menu = Self {
+ _root: root,
+ recent,
+ entries: Vec::new(),
+ };
+ menu.set_recent(paths)?;
+ MenuEvent::set_event_handler(Some(move |event: MenuEvent| {
+ let _ = proxy.send_event(UserEvent::Menu(event.id.0));
+ }));
+ menu._root.init_for_nsapp();
+ Ok(menu)
+ }
+ pub fn set_recent(&mut self, paths: &[PathBuf]) -> Result<()> {
+ if !paths.is_empty() && paths.iter().eq(self.entries.iter().map(|(_, path)| path)) {
+ return Ok(());
+ }
+ while self.recent.remove_at(0).is_some() {}
+ self.entries.clear();
+ for path in paths {
+ // Escape muda's mnemonic marker in real filenames.
+ let label = path.to_string_lossy().replace('&', "&&");
+ let item = MenuItem::new(label, true, None);
+ self.recent.append(&item)?;
+ self.entries.push((item, path.clone()));
+ }
+ if paths.is_empty() {
+ self.recent
+ .append(&MenuItem::new("No Recent Avatars", false, None))?;
+ }
+ Ok(())
+ }
+ pub fn command(&self, id: &str) -> Option<Command> {
+ match id {
+ "open" => Some(Command::Open),
+ "reload" => Some(Command::Reload),
+ "quit" => Some(Command::Quit),
+ _ => self
+ .entries
+ .iter()
+ .find(|(item, _)| item.id().as_ref() == id)
+ .map(|(_, path)| Command::Recent(path.clone())),
+ }
+ }
+ }
+
+ pub fn open(
+ proxy: EventLoopProxy<UserEvent>,
+ window: &Window,
+ directory: Option<&Path>,
+ ) -> Result<()> {
+ let mut dialog = rfd::AsyncFileDialog::new()
+ .set_title("Open Avatar")
+ .add_filter("Mesh Avatar", &["mavatar"])
+ .set_can_create_directories(false)
+ .set_parent(window);
+ if let Some(directory) = directory {
+ dialog = dialog.set_directory(directory);
+ }
+ // Construct on the main thread; the sheet runs asynchronously on AppKit.
+ let selected = dialog.pick_file_or_folder();
+ std::thread::Builder::new()
+ .name("avatar file dialog".into())
+ .spawn(move || {
+ let path = pollster::block_on(selected).map(|file| file.path().to_path_buf());
+ let _ = proxy.send_event(UserEvent::Picked(path));
+ })?;
+ Ok(())
+ }
+ pub fn alert(proxy: EventLoopProxy<UserEvent>, window: &Window, message: &str) -> Result<()> {
+ let alert = rfd::AsyncMessageDialog::new()
+ .set_parent(window)
+ .set_title("Could Not Open Avatar")
+ .set_description(message)
+ .set_level(rfd::MessageLevel::Error)
+ .show();
+ std::thread::Builder::new()
+ .name("avatar error dialog".into())
+ .spawn(move || {
+ pollster::block_on(alert);
+ let _ = proxy.send_event(UserEvent::AlertClosed);
+ })?;
+ Ok(())
+ }
+}
+
+#[cfg(not(target_os = "macos"))]
+mod platform {
+ use super::*;
+ pub struct AppMenu;
+ impl AppMenu {
+ pub fn install(_: EventLoopProxy<UserEvent>, _: &[PathBuf]) -> Result<Self> {
+ Ok(Self)
+ }
+ pub fn set_recent(&mut self, _: &[PathBuf]) -> Result<()> {
+ Ok(())
+ }
+ pub fn command(&self, _: &str) -> Option<Command> {
+ None
+ }
+ }
+ pub fn open(_: EventLoopProxy<UserEvent>, _: &Window, _: Option<&Path>) -> Result<()> {
+ anyhow::bail!("the native Open Avatar dialog is currently available only on macOS")
+ }
+ pub fn alert(proxy: EventLoopProxy<UserEvent>, _: &Window, _: &str) -> Result<()> {
+ let _ = proxy.send_event(UserEvent::AlertClosed);
+ Ok(())
+ }
+}
+pub use platform::*;
diff --git a/native/apps/mesh-avatar-desktop/src/scaling.rs b/native/apps/mesh-avatar-desktop/src/scaling.rs
index ac084ba..349fcdb 100644
--- a/native/apps/mesh-avatar-desktop/src/scaling.rs
+++ b/native/apps/mesh-avatar-desktop/src/scaling.rs
@@ -11,6 +11,7 @@ pub struct ScaleBounds {
pub texture_limit: u32,
}
+#[derive(Clone)]
pub struct Scaling {
base: LogicalSize<f64>,
zoom: f64,
@@ -56,6 +57,15 @@ impl Scaling {
self.change(0.0, bounds);
}
+ /// Prepare a different avatar's aspect without changing the live scaling state.
+ pub fn rebase(&self, base: LogicalSize<f64>, bounds: ScaleBounds) -> Result<Self> {
+ let mut replacement = Self::new(base)?;
+ replacement.zoom = self.zoom;
+ replacement.refresh(bounds);
+ ensure!(replacement.pending.is_some(), "invalid scale bounds");
+ Ok(replacement)
+ }
+
fn change(&mut self, amount: f64, bounds: ScaleBounds) {
if !amount.is_finite()
|| !bounds.dpi.is_finite()