UI-8, UI-3: native macOS menu bar; timeline columns, search, reveal-add, UU, CTI snapping
Native menu bar (apps/effectcraft, muda, macOS only): the engine's After Effects menu tree mirrored into NSMenu through a pure, serializable spec (ui-egui native_menu: submenus, separators, live labels, check marks, enabled state, Cmd/Ctrl/function-key shortcuts as accelerators, Open Recent). Hide / Hide Others / Show All are macOS' predefined items (app.hideOthers/app.showAll are real commands now, via NSApplication); Services added; About, Settings and Quit dispatch our commands. State is pushed only when the session revision, selection, history, settings or journal change; structural changes rebuild. Activations dispatch the same command ids as the in-window bar; Edit Cut/Copy/Paste/Select All/Undo/Redo act on a focused text field. Settings > Appearance > Use In-Window Menu Bar on macOS switches back. Timeline: column visibility from the column header's context menu (A/V Features, Keys, Label, #, Comment, Switches, Modes, Parent & Link, In, Out, Duration, Stretch; Source/Layer Name toggle), Comment editing (layer.setComment), draggable In/Out/Duration cells, Stretch opens Time Stretch; property search filters layers and reveals matching properties with their groups; Shift+P/S/R/T/A/E/M/F/L/U add to the revealed properties; UU reveals all modified properties; Shift-drag in the ruler snaps the CTI to keys, in/out points, markers and the work area. Commands: timeline.column, timeline.sourceName, timeline.search, timeline.revealAdd.*. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
758c3cebd4
commit
8404fead60
@@ -18,6 +18,7 @@ eframe = { workspace = true, features = ["default_fonts", "wgpu", "accesskit", "
|
||||
serde_json = { workspace = true }
|
||||
rfd = { workspace = true }
|
||||
cpal = { workspace = true }
|
||||
log = { workspace = true }
|
||||
|
||||
[build-dependencies]
|
||||
# Icon + VERSIONINFO for effectcraft.exe; build.rs only uses it when targeting Windows.
|
||||
@@ -28,3 +29,8 @@ winresource = "0.1"
|
||||
winit = { version = "0.30", default-features = false }
|
||||
# Keep the control channel responsive when the window is covered (App Nap / occlusion throttling).
|
||||
objc2-foundation = { version = "0.3", default-features = false, features = ["std", "NSProcessInfo", "NSString", "NSObject"] }
|
||||
# The native menu bar (NSMenu). MIT OR Apache-2.0; macOS only, never in the web build.
|
||||
muda = { version = "0.21", default-features = false }
|
||||
objc2 = "0.6"
|
||||
# NSApplication hide / hide others / show all, and the pasteboard for Edit ▸ Paste in text fields.
|
||||
objc2-app-kit = { version = "0.3", default-features = false, features = ["std", "NSApplication", "NSResponder", "NSPasteboard"] }
|
||||
|
||||
@@ -7,6 +7,8 @@
|
||||
|
||||
mod audio_out;
|
||||
mod control_server;
|
||||
#[cfg(target_os = "macos")]
|
||||
mod native_menu;
|
||||
|
||||
use effectcraft_ui_egui::EffectcraftApp;
|
||||
use serde_json::json;
|
||||
@@ -100,16 +102,52 @@ fn main() -> eframe::Result {
|
||||
app.hooks.pick_save_file = Some(Box::new(|name: &str, ext: &str| {
|
||||
rfd::FileDialog::new().add_filter(ext, &[ext]).set_file_name(name).save_file().map(|p| p.to_string_lossy().to_string())
|
||||
}));
|
||||
#[cfg(target_os = "macos")]
|
||||
{
|
||||
app.hooks.app_action = Some(Box::new(native_menu::app_action));
|
||||
app.hooks.clipboard_text = Some(Box::new(native_menu::clipboard_text));
|
||||
}
|
||||
if let Some(port) = control_port {
|
||||
disable_app_nap();
|
||||
let rx = control_server::start(port, cc.egui_ctx.clone());
|
||||
app = app.with_control(rx);
|
||||
}
|
||||
Ok(Box::new(app))
|
||||
Ok(Box::new(Desktop {
|
||||
#[cfg(target_os = "macos")]
|
||||
menu: native_menu::NativeBar::new(&cc.egui_ctx),
|
||||
app,
|
||||
}))
|
||||
}),
|
||||
)
|
||||
}
|
||||
|
||||
/// The desktop app: the egui app plus, on macOS, the system menu bar.
|
||||
struct Desktop {
|
||||
app: EffectcraftApp,
|
||||
#[cfg(target_os = "macos")]
|
||||
menu: native_menu::NativeBar,
|
||||
}
|
||||
|
||||
impl eframe::App for Desktop {
|
||||
fn logic(&mut self, ctx: &egui::Context, frame: &mut eframe::Frame) {
|
||||
#[cfg(target_os = "macos")]
|
||||
self.menu.update(&mut self.app, ctx);
|
||||
self.app.logic(ctx, frame);
|
||||
}
|
||||
|
||||
fn ui(&mut self, ui: &mut egui::Ui, frame: &mut eframe::Frame) {
|
||||
self.app.ui(ui, frame);
|
||||
}
|
||||
|
||||
fn on_exit(&mut self) {
|
||||
self.app.on_exit();
|
||||
}
|
||||
|
||||
fn raw_input_hook(&mut self, ctx: &egui::Context, raw_input: &mut egui::RawInput) {
|
||||
self.app.raw_input_hook(ctx, raw_input);
|
||||
}
|
||||
}
|
||||
|
||||
/// The platform config directory for EffectCraft (`EFFECTCRAFT_CONFIG_DIR` overrides):
|
||||
/// `~/Library/Application Support/EffectCraft` (macOS), `%APPDATA%\EffectCraft` (Windows),
|
||||
/// `$XDG_CONFIG_HOME/effectcraft` or `~/.config/effectcraft` (Linux and others).
|
||||
|
||||
@@ -0,0 +1,210 @@
|
||||
//! macOS: the menu bar in the system menu bar (`NSMenu` through `muda`), built from
|
||||
//! [`effectcraft_ui_egui::native_menu`]'s spec of the engine's After Effects menu tree.
|
||||
//!
|
||||
//! Each frame [`NativeBar::update`] runs menu activations (the same command ids as the in-window
|
||||
//! bar), and when [`native_menu::state_key`] changed it rebuilds the spec and pushes labels,
|
||||
//! enabled and check state into the existing items; a structural change (Open Recent, a new
|
||||
//! shortcut preset) rebuilds the menu. Settings ▸ Appearance ▸ Use In-Window Menu Bar on macOS
|
||||
//! swaps back to the in-window bar.
|
||||
|
||||
use std::collections::HashMap;
|
||||
use std::sync::mpsc::{Receiver, channel};
|
||||
|
||||
use effectcraft_ui_egui::EffectcraftApp;
|
||||
use effectcraft_ui_egui::native_menu::{self, NativeMenu, NativeNode, Role};
|
||||
use muda::{CheckMenuItem, Menu, MenuEvent, PredefinedMenuItem, Submenu};
|
||||
|
||||
/// The `muda` accelerator for a spec accelerator string.
|
||||
pub fn parse_accelerator(s: &str) -> Option<muda::accelerator::Accelerator> {
|
||||
s.parse().ok()
|
||||
}
|
||||
|
||||
pub struct NativeBar {
|
||||
menu: Option<Menu>,
|
||||
items: HashMap<String, CheckMenuItem>,
|
||||
spec: NativeMenu,
|
||||
structure: u64,
|
||||
state: Option<u64>,
|
||||
events: Receiver<MenuEvent>,
|
||||
/// The in-window-bar setting last applied (None: not yet).
|
||||
in_window: Option<bool>,
|
||||
}
|
||||
|
||||
impl NativeBar {
|
||||
pub fn new(ctx: &egui::Context) -> Self {
|
||||
let (tx, rx) = channel();
|
||||
let ctx = ctx.clone();
|
||||
MenuEvent::set_event_handler(Some(move |e: MenuEvent| {
|
||||
let _ = tx.send(e);
|
||||
ctx.request_repaint();
|
||||
}));
|
||||
NativeBar { menu: None, items: HashMap::new(), spec: NativeMenu::default(), structure: 0, state: None, events: rx, in_window: None }
|
||||
}
|
||||
|
||||
/// Run activations and keep the native menu in step with the app (call once per frame).
|
||||
pub fn update(&mut self, app: &mut EffectcraftApp, ctx: &egui::Context) {
|
||||
let in_window = app.session.prefs.appearance.in_window_menu_bar_mac;
|
||||
if self.in_window != Some(in_window) {
|
||||
self.in_window = Some(in_window);
|
||||
app.ui.show_menu_bar = in_window;
|
||||
if in_window {
|
||||
if let Some(m) = self.menu.take() {
|
||||
m.remove_for_nsapp();
|
||||
}
|
||||
self.items.clear();
|
||||
} else {
|
||||
self.state = None;
|
||||
self.structure = 0;
|
||||
}
|
||||
}
|
||||
while let Ok(e) = self.events.try_recv() {
|
||||
if let Err(err) = native_menu::activate(app, ctx, &self.spec, &e.id.0) {
|
||||
log::warn!("menu: {err}");
|
||||
}
|
||||
// Check items toggle themselves when clicked: resync with the app.
|
||||
self.state = None;
|
||||
}
|
||||
if in_window {
|
||||
return;
|
||||
}
|
||||
let key = native_menu::state_key(app);
|
||||
if self.state == Some(key) && self.menu.is_some() {
|
||||
return;
|
||||
}
|
||||
self.state = Some(key);
|
||||
let spec = native_menu::build(app);
|
||||
let structure = spec.structure();
|
||||
if self.menu.is_none() || structure != self.structure {
|
||||
self.install(&spec);
|
||||
self.structure = structure;
|
||||
} else {
|
||||
self.refresh(&spec);
|
||||
}
|
||||
self.spec = spec;
|
||||
}
|
||||
|
||||
fn install(&mut self, spec: &NativeMenu) {
|
||||
if let Some(m) = self.menu.take() {
|
||||
m.remove_for_nsapp();
|
||||
}
|
||||
self.items.clear();
|
||||
let menu = Menu::new();
|
||||
for node in &spec.menus {
|
||||
if let NativeNode::Submenu { label, children } = node {
|
||||
let sub = Submenu::new(label, true);
|
||||
self.fill(&sub, children);
|
||||
match label.as_str() {
|
||||
"Window" => sub.set_as_windows_menu_for_nsapp(),
|
||||
"Help" => sub.set_as_help_menu_for_nsapp(),
|
||||
_ => {}
|
||||
}
|
||||
if let Err(e) = menu.append(&sub) {
|
||||
log::warn!("menu {label}: {e}");
|
||||
}
|
||||
}
|
||||
}
|
||||
menu.init_for_nsapp();
|
||||
self.menu = Some(menu);
|
||||
}
|
||||
|
||||
fn fill(&mut self, parent: &Submenu, nodes: &[NativeNode]) {
|
||||
for n in nodes {
|
||||
let r = match n {
|
||||
NativeNode::Separator => parent.append(&PredefinedMenuItem::separator()),
|
||||
NativeNode::Predefined { role, label, .. } => {
|
||||
let item = match role {
|
||||
Role::Services => PredefinedMenuItem::services(Some(label)),
|
||||
Role::Hide => PredefinedMenuItem::hide(Some(label)),
|
||||
Role::HideOthers => PredefinedMenuItem::hide_others(Some(label)),
|
||||
Role::ShowAll => PredefinedMenuItem::show_all(Some(label)),
|
||||
};
|
||||
parent.append(&item)
|
||||
}
|
||||
NativeNode::Submenu { label, children } => {
|
||||
let sub = Submenu::new(label, true);
|
||||
self.fill(&sub, children);
|
||||
parent.append(&sub)
|
||||
}
|
||||
NativeNode::Item(i) => {
|
||||
let accel = i.accelerator.as_deref().and_then(parse_accelerator);
|
||||
let item = CheckMenuItem::with_id(i.id.clone(), &i.label, i.enabled, i.checked == Some(true), accel);
|
||||
let r = parent.append(&item);
|
||||
self.items.insert(i.id.clone(), item);
|
||||
r
|
||||
}
|
||||
};
|
||||
if let Err(e) = r {
|
||||
log::warn!("menu item: {e}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Push label / enabled / check changes into the existing items.
|
||||
fn refresh(&self, spec: &NativeMenu) {
|
||||
let old: HashMap<&str, &native_menu::NativeItem> = self.spec.items().into_iter().map(|i| (i.id.as_str(), i)).collect();
|
||||
for i in spec.items() {
|
||||
let Some(native) = self.items.get(&i.id) else { continue };
|
||||
let prev = old.get(i.id.as_str());
|
||||
if prev.is_none_or(|p| p.label != i.label) {
|
||||
native.set_text(&i.label);
|
||||
}
|
||||
if prev.is_none_or(|p| p.enabled != i.enabled) {
|
||||
native.set_enabled(i.enabled);
|
||||
}
|
||||
let checked = i.checked == Some(true);
|
||||
if native.is_checked() != checked {
|
||||
native.set_checked(checked);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// `app.hide` / `app.hideOthers` / `app.showAll` through NSApplication (main thread only).
|
||||
pub fn app_action(id: &str) -> bool {
|
||||
use objc2::MainThreadMarker;
|
||||
use objc2_app_kit::NSApplication;
|
||||
let Some(mtm) = MainThreadMarker::new() else { return false };
|
||||
let app = NSApplication::sharedApplication(mtm);
|
||||
match id {
|
||||
"app.hide" => app.hide(None),
|
||||
"app.hideOthers" => app.hideOtherApplications(None),
|
||||
"app.showAll" => app.unhideAllApplications(None),
|
||||
_ => return false,
|
||||
}
|
||||
true
|
||||
}
|
||||
|
||||
/// The general pasteboard's text.
|
||||
pub fn clipboard_text() -> Option<String> {
|
||||
use objc2_app_kit::NSPasteboard;
|
||||
// NSPasteboardTypeString's value (the UTI), without touching the extern static.
|
||||
let ty = objc2_foundation::NSString::from_str("public.utf8-plain-text");
|
||||
NSPasteboard::generalPasteboard().stringForType(&ty).map(|s| s.to_string())
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use serde_json::json;
|
||||
|
||||
#[test]
|
||||
fn every_accelerator_parses_with_muda() {
|
||||
let mut s = effectcraft_engine::Session::default();
|
||||
s.execute("file.openDemoProject", json!({})).unwrap();
|
||||
let app = EffectcraftApp::new(s);
|
||||
let spec = native_menu::build(&app);
|
||||
let mut n = 0;
|
||||
for i in spec.items() {
|
||||
if let Some(a) = &i.accelerator {
|
||||
assert!(parse_accelerator(a).is_some(), "{} `{a}` does not parse", i.label);
|
||||
n += 1;
|
||||
}
|
||||
}
|
||||
assert!(n > 50, "only {n} accelerators");
|
||||
// Spot checks against the AE shortcuts.
|
||||
let accel = |cmd: &str| spec.items().into_iter().find(|i| i.command == cmd).and_then(|i| i.accelerator.clone());
|
||||
assert_eq!(accel("file.saveAs").as_deref(), Some("Cmd+Shift+S"));
|
||||
assert_eq!(accel("comp.new").as_deref(), Some("Cmd+N"));
|
||||
assert_eq!(parse_accelerator("Cmd+Shift+S"), "CmdOrCtrl+Shift+S".parse().ok());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user