Merge main (layer styles, engine menu tree) into time/audio branch

Resolve conflicts; register playback.audio (Composition > Preview > Audio)
as a real frontend command replacing its stub; layer_at uses content_buf.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Brandon Thomas
2026-10-02 00:19:34 -04:00
co-authored by Claude Opus 5.5
58 changed files with 7925 additions and 371 deletions
+524 -231
View File
@@ -1,13 +1,16 @@
//! Menus, keyboard shortcuts and UI-level commands. The menu bar is generated from the engine
//! registry plus the UI command table (commands that only affect the frontend: tools, playback,
//! viewer zoom, panels, reveal shortcuts). `invoke` is the single entry point used by menus,
//! shortcuts and the control channel.
//! Menus, keyboard shortcuts and UI-level commands. The menu bar is the engine's After Effects
//! menu tree (`effectcraft_engine::menus`): every entry is an engine command, and frontend-only
//! commands (viewer zoom, panels, dialogs…) come back as `Event::Frontend` and are performed by
//! [`frontend`]. [`UI_COMMANDS`] holds the remaining UI-only shortcuts (tools, timeline
//! navigation, reveal keys). `invoke` is the single entry point used by menus, shortcuts and the
//! control channel.
use serde_json::{Value, json};
use crate::EffectcraftApp;
use crate::dock::PanelKind;
use crate::state::{Resolution, Tool};
use effectcraft_engine::menus::{MenuEntry, MenuNode};
pub struct UiCommand {
pub id: &'static str,
@@ -22,32 +25,23 @@ macro_rules! uic {
};
}
/// UI-only commands (not in the AE menus; shortcuts and the command palette).
pub const UI_COMMANDS: &[UiCommand] = &[
uic!("playback.toggle", "Play/Stop", ["Composition", "Preview"], Some("Space")),
uic!("playback.ramPreview", "Play Current Preview", ["Composition", "Preview"], Some("Num0")),
uic!("playback.ramPreview", "Play Current Preview", [], Some("Num0")),
uic!("playback.stop", "Stop", [], None),
uic!("view.zoomIn", "Zoom In", ["View"], Some(".")),
uic!("view.zoomOut", "Zoom Out", ["View"], Some(",")),
uic!("view.fit", "Fit", ["View"], Some("Shift+/")),
uic!("view.actualSize", "100%", ["View"], Some("/")),
uic!("view.res.full", "Full", ["View", "Resolution"], Some("Cmd+J")),
uic!("view.res.half", "Half", ["View", "Resolution"], Some("Cmd+Shift+J")),
uic!("view.res.third", "Third", ["View", "Resolution"], None),
uic!("view.res.quarter", "Quarter", ["View", "Resolution"], Some("Cmd+Alt+Shift+J")),
uic!("view.res.auto", "Auto", ["View", "Resolution"], None),
uic!("view.rulers", "Show Rulers", ["View"], Some("Cmd+R")),
uic!("view.grid", "Show Grid", ["View"], Some("Cmd+'")),
uic!("view.safeMargins", "Title/Action Safe", ["View"], None),
uic!("view.transparencyGrid", "Transparency Grid", ["View"], None),
uic!("view.layerControls", "Show Layer Controls", ["View"], Some("Cmd+Shift+H")),
uic!("view.fastPreviews", "Fast Previews", ["View"], None),
uic!("view.theme.dark", "Dark", ["View", "Appearance"], None),
uic!("view.theme.darker", "Darker", ["View", "Appearance"], None),
uic!("view.theme.light", "Light", ["View", "Appearance"], None),
uic!("view.fit", "Fit", [], Some("Shift+/")),
uic!("view.actualSize", "100%", [], Some("/")),
uic!("view.res.auto", "Resolution: Auto", [], None),
uic!("view.safeMargins", "Title/Action Safe", [], None),
uic!("view.transparencyGrid", "Transparency Grid", [], None),
uic!("view.fastPreviews", "Fast Previews", [], None),
uic!("view.theme.dark", "Theme: Dark", [], None),
uic!("view.theme.darker", "Theme: Darker", [], None),
uic!("view.theme.light", "Theme: Light", [], None),
uic!("timeline.zoomIn", "Zoom In Time", [], Some("=")),
uic!("timeline.zoomOut", "Zoom Out Time", [], Some("-")),
uic!("timeline.zoomFit", "Zoom to Fit Comp", [], Some(";")),
uic!("timeline.graphEditor", "Graph Editor", ["Window"], Some("Shift+F3")),
uic!("timeline.graphEditor", "Graph Editor", [], Some("Shift+F3")),
uic!("timeline.switchesModes", "Toggle Switches / Modes", [], Some("F4")),
uic!("timeline.workAreaBegin", "Set Work Area Begin", [], Some("B")),
uic!("timeline.workAreaEnd", "Set Work Area End", [], Some("N")),
@@ -60,25 +54,12 @@ pub const UI_COMMANDS: &[UiCommand] = &[
uic!("timeline.reveal.rotation", "Reveal Rotation", [], Some("R")),
uic!("timeline.reveal.opacity", "Reveal Opacity", [], Some("T")),
uic!("timeline.reveal.anchor", "Reveal Anchor Point", [], Some("A")),
uic!("timeline.reveal.animated", "Reveal Animated Properties", ["Animation"], Some("U")),
uic!("timeline.reveal.effects", "Reveal Effects", [], Some("E")),
uic!("timeline.reveal.masks", "Reveal Masks", [], Some("M")),
uic!("timeline.reveal.feather", "Reveal Mask Feather", [], Some("F")),
uic!("timeline.reveal.levels", "Reveal Audio Levels (press twice: Waveform)", [], Some("L")),
uic!("timeline.reveal.waveform", "Reveal Audio Waveform", [], None),
uic!("playback.muteAudio", "Mute Audio", ["Composition", "Preview"], None),
uic!("timeline.collapseAll", "Collapse All", [], Some("Cmd+`")),
uic!("window.workspace.default", "Default", ["Window", "Workspace"], Some("Shift+F10")),
uic!("window.workspace.standard", "Standard", ["Window", "Workspace"], Some("Shift+F11")),
uic!("window.workspace.smallscreen", "Small Screen", ["Window", "Workspace"], Some("Shift+F12")),
uic!("window.workspace.animation", "Animation", ["Window", "Workspace"], None),
uic!("window.workspace.effects", "Effects", ["Window", "Workspace"], None),
uic!("window.workspace.motiontracking", "Motion Tracking", ["Window", "Workspace"], None),
uic!("window.workspace.paint", "Paint", ["Window", "Workspace"], None),
uic!("window.workspace.text", "Text", ["Window", "Workspace"], None),
uic!("window.workspace.minimal", "Minimal", ["Window", "Workspace"], None),
uic!("window.workspace.allpanels", "All Panels", ["Window", "Workspace"], None),
uic!("window.workspace.reset", "Reset to Saved Layout", ["Window", "Workspace"], None),
uic!("tool.selection", "Selection Tool", [], Some("V")),
uic!("tool.hand", "Hand Tool", [], Some("H")),
uic!("tool.zoom", "Zoom Tool", [], Some("Z")),
@@ -93,12 +74,10 @@ pub const UI_COMMANDS: &[UiCommand] = &[
uic!("tool.brush", "Brush Tools", [], Some("Cmd+B")),
uic!("tool.rotoBrush", "Roto Brush Tool", [], Some("Alt+W")),
uic!("tool.puppet", "Puppet Tools", [], Some("Cmd+P")),
uic!("app.newComp", "New Composition…", [], None),
uic!("app.compSettings", "Composition Settings…", [], None),
uic!("app.solidSettings", "New Solid…", [], None),
uic!("app.commandPalette", "Command Palette…", ["Window"], Some("Cmd+Shift+P")),
uic!("app.about", "About EffectCraft", ["Help"], None),
uic!("app.home", "Home", ["Window"], None),
uic!("app.newComp", "New Composition...", [], None),
uic!("app.compSettings", "Composition Settings...", [], None),
uic!("app.solidSettings", "New Solid...", [], None),
uic!("app.home", "Home", [], None),
];
pub fn panel_command_id(p: PanelKind) -> String {
@@ -127,6 +106,10 @@ fn reveal(app: &mut EffectcraftApp, kind: &str, now: f64) {
app.ui.timeline.open_layers = sel.iter().map(|l| l.0).collect();
}
fn no_params(p: &Value) -> bool {
p.as_object().is_none_or(|m| m.is_empty())
}
/// Execute a UI or engine command by id.
pub fn invoke(app: &mut EffectcraftApp, ctx: &egui::Context, id: &str, params: Value) -> Result<Value, String> {
let now = ctx.input(|i| i.time);
@@ -134,20 +117,15 @@ pub fn invoke(app: &mut EffectcraftApp, ctx: &egui::Context, id: &str, params: V
if crate::panels::dialogs_3d::route(app, id, &params)? {
return Ok(Value::Null);
}
// Legacy per-panel / per-workspace ids (`window.panel.Project`, `window.workspace.default`).
if let Some(rest) = id.strip_prefix("window.panel.") {
let p = PanelKind::from_name(rest).ok_or_else(|| format!("unknown panel `{rest}`"))?;
app.show_panel(p);
return Ok(Value::Null);
return frontend(app, ctx, "window.panel", json!({"panel": rest}));
}
if let Some(ws) = id.strip_prefix("window.workspace.") {
if ws == "reset" {
let name = app.ui.workspace.clone();
app.set_workspace(&name);
return Ok(Value::Null);
return frontend(app, ctx, "window.resetWorkspace", json!({}));
}
let name = crate::dock::WORKSPACES.iter().find(|w| w.to_ascii_lowercase().replace(' ', "") == ws).ok_or_else(|| format!("unknown workspace `{ws}`"))?;
app.set_workspace(name);
return Ok(json!({"workspace": name}));
return frontend(app, ctx, "window.workspace", json!({"name": ws}));
}
if let Some(t) = id.strip_prefix("tool.") {
// Slot shortcuts cycle through the slot's tools.
@@ -178,11 +156,14 @@ pub fn invoke(app: &mut EffectcraftApp, ctx: &egui::Context, id: &str, params: V
return Ok(json!({"tool": tool}));
}
if let Some(k) = id.strip_prefix("timeline.reveal.") {
if k == "animated" {
return run_engine(app, ctx, "anim.reveal", json!({"kind": "keyframes"}));
}
reveal(app, k, now);
return Ok(Value::Null);
}
if let Some(r) = id.strip_prefix("view.res.") {
app.ui.viewer.res = Resolution::ALL.into_iter().find(|x| x.label().eq_ignore_ascii_case(r)).ok_or("unknown resolution")?;
if id == "view.res.auto" {
app.ui.viewer.res = Resolution::Auto;
return Ok(Value::Null);
}
if let Some(th) = id.strip_prefix("view.theme.") {
@@ -192,7 +173,7 @@ pub fn invoke(app: &mut EffectcraftApp, ctx: &egui::Context, id: &str, params: V
}
let timing = |app: &mut EffectcraftApp, op: &str| app.session.execute("layer.timing", json!({"op": op})).map_err(|e| e.to_string());
match id {
"playback.toggle" | "playback.ramPreview" => {
"playback.ramPreview" => {
app.toggle_play(now);
return Ok(json!({"playing": app.playback.playing}));
}
@@ -200,24 +181,6 @@ pub fn invoke(app: &mut EffectcraftApp, ctx: &egui::Context, id: &str, params: V
app.stop();
return Ok(Value::Null);
}
"playback.muteAudio" => {
app.ui.preview_audio = !app.ui.preview_audio;
if !app.ui.preview_audio {
app.audio = None;
}
return Ok(json!({"muted": !app.ui.preview_audio}));
}
"view.zoomIn" | "view.zoomOut" => {
let cur = app.ui.viewer.zoom.unwrap_or_else(|| crate::panels::viewer::last_fit(ctx));
const STEPS: [f32; 16] = [0.015, 0.03, 0.0625, 0.125, 0.25, 0.333, 0.5, 0.66, 1.0, 1.5, 2.0, 3.0, 4.0, 8.0, 16.0, 32.0];
let next = if id == "view.zoomIn" {
STEPS.iter().copied().find(|s| *s > cur + 1e-3).unwrap_or(32.0)
} else {
STEPS.iter().rev().copied().find(|s| *s < cur - 1e-3).unwrap_or(0.015)
};
app.ui.viewer.zoom = Some(next);
return Ok(json!({"zoom": next}));
}
"view.fit" => {
app.ui.viewer.zoom = None;
app.ui.viewer.pan = [0.0, 0.0];
@@ -228,11 +191,8 @@ pub fn invoke(app: &mut EffectcraftApp, ctx: &egui::Context, id: &str, params: V
app.ui.viewer.pan = [0.0, 0.0];
return Ok(Value::Null);
}
"view.rulers" => app.ui.viewer.rulers = !app.ui.viewer.rulers,
"view.grid" => app.ui.viewer.grid = !app.ui.viewer.grid,
"view.safeMargins" => app.ui.viewer.safe_margins = !app.ui.viewer.safe_margins,
"view.transparencyGrid" => app.ui.viewer.transparency_grid = !app.ui.viewer.transparency_grid,
"view.layerControls" => app.ui.viewer.show_layer_controls = !app.ui.viewer.show_layer_controls,
"view.fastPreviews" => app.ui.viewer.fast_preview = !app.ui.viewer.fast_preview,
"timeline.zoomIn" | "timeline.zoomOut" => {
let k = if id == "timeline.zoomIn" { 1.5 } else { 1.0 / 1.5 };
@@ -253,31 +213,38 @@ pub fn invoke(app: &mut EffectcraftApp, ctx: &egui::Context, id: &str, params: V
"timeline.moveOutToTime" => return timing(app, "moveOutToTime"),
"timeline.trimInToTime" => return timing(app, "trimInToTime"),
"timeline.trimOutToTime" => return timing(app, "trimOutToTime"),
"app.about" => app.dialog = Some(crate::Dialog::About),
"app.home" => app.ui.start_screen = !app.ui.start_screen,
"app.commandPalette" => {
app.dialog_state.palette_query.clear();
app.dialog = Some(crate::Dialog::CommandPalette);
}
"app.newComp" | "comp.new" if params.as_object().is_none_or(|m| m.is_empty()) => {
crate::panels::dialogs::open_new_comp(app);
}
"app.compSettings" | "comp.settings" if params.as_object().is_none_or(|m| m.is_empty()) => {
crate::panels::dialogs::open_comp_settings(app)?;
}
"keys.velocity" if params.as_object().is_none_or(|m| m.is_empty()) => crate::panels::key_dialogs::open_velocity(app)?,
"keys.interpolation" if params.as_object().is_none_or(|m| m.is_empty()) => crate::panels::key_dialogs::open_interpolation(app)?,
"layer.timeStretch" if params.as_object().is_none_or(|m| m.is_empty()) => crate::panels::key_dialogs::open_time_stretch(app)?,
"app.solidSettings" | "layer.newSolid" if params.as_object().is_none_or(|m| m.is_empty()) => {
crate::panels::dialogs::open_new_solid(app)?;
}
"app.newComp" | "comp.new" if no_params(&params) => crate::panels::dialogs::open_new_comp(app),
"app.compSettings" | "comp.settings" if no_params(&params) => crate::panels::dialogs::open_comp_settings(app)?,
"app.solidSettings" | "layer.newSolid" if no_params(&params) => crate::panels::dialogs::open_new_solid(app)?,
"keys.velocity" if no_params(&params) => crate::panels::key_dialogs::open_velocity(app)?,
"keys.interpolation" if no_params(&params) => crate::panels::key_dialogs::open_interpolation(app)?,
"layer.timeStretch" if no_params(&params) => crate::panels::key_dialogs::open_time_stretch(app)?,
_ => {
if (id == "file.import" && params.get("paths").is_none() && params.get("path").is_none())
|| (id == "file.saveAs" && params.get("path").is_none())
|| (id == "file.open" && params.get("path").is_none())
|| (id == "file.save" && params.get("path").is_none() && app.session.path.is_none())
{
return file_dialog(app, id);
if id == "renderQueue.render" && params.get("wait").is_none() {
// The UI renders in the background; the panel shows progress.
let mut p = params.clone();
if let Some(m) = p.as_object_mut() {
m.insert("wait".into(), json!(false));
} else {
p = json!({"wait": false});
}
app.show_panel(PanelKind::RenderQueue);
return app.session.execute(id, p).map_err(|e| e.to_string());
}
if id == "renderQueue.add" {
let r = app.session.execute(id, params).map_err(|e| e.to_string());
match &r {
Ok(_) => app.show_panel(PanelKind::RenderQueue),
Err(e) => app.ui.status = e.clone(),
}
return r;
}
if let Some(r) = file_dialog(app, id, &params) {
return r;
}
if crate::panels::dialogs::open_form(app, id, &params) {
return Ok(json!({"dialog": id}));
}
if id == "renderQueue.render" && params.get("wait").is_none() {
// The UI renders in the background; the panel shows progress.
@@ -302,44 +269,270 @@ pub fn invoke(app: &mut EffectcraftApp, ctx: &egui::Context, id: &str, params: V
crate::panels::timeline::begin_rename(app, ctx);
return Ok(Value::Null);
}
let r = app.session.execute(id, params).map_err(|e| e.to_string());
if let Err(e) = &r {
app.ui.status = e.clone();
}
return r;
return run_engine(app, ctx, id, params);
}
}
Ok(Value::Null)
}
fn file_dialog(app: &mut EffectcraftApp, id: &str) -> Result<Value, String> {
match id {
"file.import" => {
let f = app.hooks.pick_files.as_ref().ok_or("no file dialog available (pass `paths`)")?;
/// Run an engine command and perform the frontend events it emitted right away.
fn run_engine(app: &mut EffectcraftApp, ctx: &egui::Context, id: &str, params: Value) -> Result<Value, String> {
let r = app.session.execute(id, params).map_err(|e| e.to_string());
if let Err(e) = &r {
app.ui.status = e.clone();
}
let events = app.session.drain_events();
for ev in events {
match ev {
effectcraft_engine::Event::Frontend { command, params } => {
if let Err(e) = frontend(app, ctx, &command, params) {
app.ui.status = e;
}
}
other => app.session.events.push(other),
}
}
r
}
fn toggle(slot: &mut bool, p: &Value) -> Value {
*slot = p.get("value").and_then(Value::as_bool).unwrap_or(!*slot);
json!(*slot)
}
/// Perform a frontend command (from `Event::Frontend`).
pub fn frontend(app: &mut EffectcraftApp, ctx: &egui::Context, id: &str, p: Value) -> Result<Value, String> {
let now = ctx.input(|i| i.time);
let v = &mut app.ui.viewer;
Ok(match id {
"app.about" => {
app.dialog = Some(crate::Dialog::About);
Value::Null
}
"app.settings" => {
app.dialog_state.settings_page = p.get("page").and_then(Value::as_str).unwrap_or("general").to_string();
app.dialog = Some(crate::Dialog::Settings);
Value::Null
}
"app.hide" => {
ctx.send_viewport_cmd(egui::ViewportCommand::Minimized(true));
Value::Null
}
"app.quit" => {
ctx.send_viewport_cmd(egui::ViewportCommand::Close);
Value::Null
}
"app.commandPalette" => {
app.dialog_state.palette_query = p.get("query").and_then(Value::as_str).unwrap_or_default().to_string();
app.dialog_state.palette_sel = 0;
app.dialog = Some(crate::Dialog::CommandPalette);
Value::Null
}
"app.keyboardShortcuts" => {
app.dialog = Some(crate::Dialog::Shortcuts);
Value::Null
}
"app.templates" => {
let kind = p.get("kind").and_then(Value::as_str).unwrap_or("renderSettings");
let title = if kind == "outputModule" { "Output Module Templates" } else { "Render Settings Templates" };
crate::panels::dialogs::info(app, title, "Render and output templates are managed from the Render Queue panel (Window ▸ Render Queue).");
Value::Null
}
"app.find" => {
if let Some(q) = p.get("query").and_then(Value::as_str) {
app.ui.project_search = q.to_string();
}
app.show_panel(PanelKind::Project);
Value::Null
}
"playback.toggle" => {
app.toggle_play(now);
json!({"playing": app.playback.playing})
}
"playback.cacheWhenIdle" => toggle(&mut app.ui.cache_when_idle, &p),
"playback.audio" => {
let r = toggle(&mut app.ui.preview_audio, &p);
if !app.ui.preview_audio {
app.audio = None;
}
r
}
"view.zoomIn" | "view.zoomOut" => {
let cur = v.zoom.unwrap_or_else(|| crate::panels::viewer::last_fit(ctx));
const STEPS: [f32; 16] = [0.015, 0.03, 0.0625, 0.125, 0.25, 0.333, 0.5, 0.66, 1.0, 1.5, 2.0, 3.0, 4.0, 8.0, 16.0, 32.0];
let next = if id == "view.zoomIn" {
STEPS.iter().copied().find(|s| *s > cur + 1e-3).unwrap_or(32.0)
} else {
STEPS.iter().rev().copied().find(|s| *s < cur - 1e-3).unwrap_or(0.015)
};
v.zoom = Some(next);
json!({"zoom": next})
}
"view.res.full" | "view.res.half" | "view.res.third" | "view.res.quarter" => {
let r = id.trim_start_matches("view.res.");
v.res = Resolution::ALL.into_iter().find(|x| x.label().eq_ignore_ascii_case(r)).ok_or("unknown resolution")?;
Value::Null
}
"view.rulers" => toggle(&mut v.rulers, &p),
"view.guides" => toggle(&mut v.guides, &p),
"view.snapToGuides" => toggle(&mut v.snap_guides, &p),
"view.lockGuides" => toggle(&mut v.lock_guides, &p),
"view.grid" => toggle(&mut v.grid, &p),
"view.snapToGrid" => toggle(&mut v.snap_grid, &p),
"view.layerControls" => toggle(&mut v.show_layer_controls, &p),
"view.options" => {
app.dialog = Some(crate::Dialog::ViewOptions);
Value::Null
}
"view.panelBackground" => {
if p.get("pick").and_then(Value::as_bool) == Some(true) {
let [r, g, b] = v.custom_pasteboard;
crate::panels::dialogs::form(
app,
"Panel Background Color",
"view.panelBackground",
json!({}),
vec![crate::panels::dialogs::Field::text("color", "Color (#rrggbb)", &format!("#{r:02x}{g:02x}{b:02x}"))],
);
return Ok(Value::Null);
}
let c = p.get("color").and_then(Value::as_str).unwrap_or("mediumGray");
v.pasteboard = match c {
"black" => Some([0, 0, 0]),
"darkGray" => Some([0x2a, 0x2a, 0x2a]),
"mediumGray" => None,
"lightGray" => Some([0xb4, 0xb4, 0xb4]),
"white" => Some([0xff, 0xff, 0xff]),
"custom" => Some(v.custom_pasteboard),
hex => {
let c = effectcraft_engine::color::Rgba::from_hex(hex).ok_or_else(|| format!("unknown colour `{hex}`"))?;
let rgb = [(c.r * 255.0).round() as u8, (c.g * 255.0).round() as u8, (c.b * 255.0).round() as u8];
v.custom_pasteboard = rgb;
Some(rgb)
}
};
Value::Null
}
"view.fullScreen" => {
let full = ctx.input(|i| i.viewport().fullscreen.unwrap_or(false));
ctx.send_viewport_cmd(egui::ViewportCommand::Fullscreen(!full));
json!(!full)
}
"window.panel" => {
let name = p.get("panel").and_then(Value::as_str).unwrap_or_default();
if name.eq_ignore_ascii_case("tools") {
app.ui.status = "The Tools panel is the toolbar in the header".into();
return Ok(Value::Null);
}
let panel = PanelKind::from_name(name).ok_or_else(|| format!("unknown panel `{name}`"))?;
app.show_panel(panel);
Value::Null
}
"window.workspace" => {
let want = p.get("name").and_then(Value::as_str).unwrap_or("Default").to_ascii_lowercase().replace(' ', "");
let name = crate::dock::WORKSPACES
.iter()
.find(|w| w.to_ascii_lowercase().replace(' ', "") == want)
.ok_or_else(|| format!("unknown workspace `{want}`"))?;
app.set_workspace(name);
json!({"workspace": name})
}
"window.resetWorkspace" => {
let name = app.ui.workspace.clone();
app.set_workspace(&name);
Value::Null
}
"comp.flowchart" | "comp.miniFlowchart" => {
app.show_panel(PanelKind::Flowchart);
Value::Null
}
"layer.openLayer" => {
app.show_panel(PanelKind::Layer);
Value::Null
}
"effect.manage" | "anim.browsePresets" => {
app.show_panel(PanelKind::EffectsPresets);
Value::Null
}
"timeline.revealProps" => {
let props = p.get("props").and_then(Value::as_array).cloned().unwrap_or_default();
let tl = &mut app.ui.timeline;
tl.reveal_props = props.iter().filter_map(|x| x.get("prop").and_then(Value::as_u64)).collect();
tl.open_layers = props.iter().filter_map(|x| x.get("layer").and_then(Value::as_u64)).collect();
tl.reveal = if tl.reveal_props.is_empty() { vec![] } else { vec!["props".into()] };
json!({"revealed": tl.reveal_props.len()})
}
_ => return Err(format!("`{id}` is not a frontend command")),
})
}
/// Commands that need a file or folder path: ask the host's file dialog when `params` lacks one.
fn file_dialog(app: &mut EffectcraftApp, id: &str, params: &Value) -> Option<Result<Value, String>> {
enum Ask {
Import,
OpenProject,
Save(&'static str),
Open(&'static [&'static str]),
}
let (key, ask) = match id {
"file.import" | "file.importMultiple" => ("paths", Ask::Import),
"file.open" => ("path", Ask::OpenProject),
"file.save" if app.session.path.is_none() => ("path", Ask::Save("Untitled Project.ecproj")),
"file.saveAs" | "file.saveCopy" => ("path", Ask::Save("Untitled Project.ecproj")),
"comp.saveFrameAs" => ("path", Ask::Save("Frame.png")),
"anim.savePreset" => ("path", Ask::Save("Preset.ecpreset")),
"anim.applyPreset" => ("path", Ask::Open(&["ecpreset", "json"])),
"view.exportGuides" => ("path", Ask::Save("Guides.json")),
"view.importGuides" => ("path", Ask::Open(&["json"])),
"file.runScript" => ("path", Ask::Open(&["jsonl", "json", "txt"])),
"file.replaceFootage" => ("path", Ask::Import),
"file.collectFiles" => ("folder", Ask::Save("Collected Files")),
_ => return None,
};
if params.get(key).is_some()
|| params.get("path").is_some()
|| params.get("paths").is_some()
|| params.get("steps").is_some()
|| params.get("preset").is_some()
{
return None;
}
let mut p = params.as_object().cloned().unwrap_or_default();
let picked: Option<Value> = match ask {
Ask::Import => {
let Some(f) = app.hooks.pick_files.as_ref() else { return Some(Err("no file dialog available (pass `paths`)".into())) };
let paths = f(&[
"mp4", "mov", "m4v", "mkv", "webm", "png", "jpg", "jpeg", "gif", "webp", "tif", "tiff", "bmp", "exr", "wav", "aif", "aiff", "mp3", "flac",
"ogg", "opus", "svg",
]);
if paths.is_empty() {
return Ok(Value::Null);
}
app.session.execute("file.import", json!({"paths": paths})).map_err(|e| e.to_string())
}
"file.open" => {
let f = app.hooks.pick_open_project.as_ref().ok_or("no file dialog available (pass `path`)")?;
match f() {
Some(p) => app.session.execute("file.open", json!({"path": p})).map_err(|e| e.to_string()),
None => Ok(Value::Null),
match (paths.is_empty(), key) {
(true, _) => None,
(false, "paths") => Some(json!(paths)),
(false, _) => paths.first().map(|s| json!(s)),
}
}
_ => {
let f = app.hooks.pick_save.as_ref().ok_or("no file dialog available (pass `path`)")?;
match f("Untitled Project.ecproj") {
Some(p) => app.session.execute("file.saveAs", json!({"path": p})).map_err(|e| e.to_string()),
None => Ok(Value::Null),
}
Ask::OpenProject => {
let Some(f) = app.hooks.pick_open_project.as_ref() else { return Some(Err("no file dialog available (pass `path`)".into())) };
f().map(|s| json!(s))
}
Ask::Open(exts) => {
let Some(f) = app.hooks.pick_files.as_ref() else { return Some(Err("no file dialog available (pass `path`)".into())) };
f(exts).first().map(|s| json!(s))
}
Ask::Save(default) => {
let Some(f) = app.hooks.pick_save.as_ref() else { return Some(Err("no file dialog available (pass `path`)".into())) };
f(default).map(|s| json!(s))
}
};
let Some(v) = picked else { return Some(Ok(Value::Null)) };
p.insert(key.to_string(), v);
// Save (untitled) becomes Save As.
let id = if id == "file.save" { "file.saveAs" } else { id };
let r = app.session.execute(id, Value::Object(p)).map_err(|e| e.to_string());
if let Err(e) = &r {
app.ui.status = e.clone();
}
Some(r)
}
/// A menu entry for display / `ui.menu.list`.
@@ -350,70 +543,90 @@ pub struct MenuItem {
pub path: Vec<String>,
pub shortcut: Option<String>,
pub enabled: bool,
#[serde(skip_serializing_if = "Option::is_none")]
pub checked: Option<bool>,
/// Parameters bound to the entry (e.g. the effect for Effect menu items).
#[serde(skip_serializing_if = "Value::is_null")]
pub params: Value,
}
pub const MENUS: [&str; 9] = ["File", "Edit", "Composition", "Layer", "Effect", "Animation", "View", "Window", "Help"];
/// Top-level menus in order.
pub fn menus() -> Vec<&'static str> {
effectcraft_engine::menus::top_level()
}
fn entry_label(app: &EffectcraftApp, e: &MenuEntry) -> String {
match e.command.as_str() {
"edit.undo" => app.session.history.undo.last().map(|u| format!("Undo {}", u.0)).unwrap_or_else(|| "Can't Undo".into()),
"edit.redo" => app.session.history.redo.last().map(|u| format!("Redo {}", u.0)).unwrap_or_else(|| "Can't Redo".into()),
_ => e.label.clone(),
}
}
/// Check-mark state of frontend toggles (engine state is answered by the engine).
fn entry_checked(app: &EffectcraftApp, e: &MenuEntry) -> Option<bool> {
let v = &app.ui.viewer;
let pstr = |k: &str| e.params.get(k).and_then(Value::as_str);
match e.command.as_str() {
"view.rulers" => Some(v.rulers),
"view.guides" => Some(v.guides),
"view.snapToGuides" => Some(v.snap_guides),
"view.lockGuides" => Some(v.lock_guides),
"view.grid" => Some(v.grid),
"view.snapToGrid" => Some(v.snap_grid),
"view.layerControls" => Some(v.show_layer_controls),
"playback.cacheWhenIdle" => Some(app.ui.cache_when_idle),
"playback.audio" => Some(app.ui.preview_audio),
"view.res.full" | "view.res.half" | "view.res.third" | "view.res.quarter" => {
Some(v.res.label().eq_ignore_ascii_case(e.command.trim_start_matches("view.res.")))
}
"window.workspace" => Some(pstr("name").is_some_and(|n| n == app.ui.workspace)),
"view.panelBackground" if e.params.get("pick").is_none() => {
let cur = match v.pasteboard {
None => "mediumGray",
Some([0, 0, 0]) => "black",
Some([0x2a, 0x2a, 0x2a]) => "darkGray",
Some([0xb4, 0xb4, 0xb4]) => "lightGray",
Some([0xff, 0xff, 0xff]) => "white",
Some(_) => "custom",
};
Some(pstr("color") == Some(cur))
}
_ => effectcraft_engine::menus::checked(&app.session, &e.command, &e.params),
}
}
fn entry_enabled(app: &EffectcraftApp, e: &MenuEntry) -> bool {
if e.command == "window.panel" {
return true;
}
app.session.is_enabled(&e.command)
}
/// Every menu entry, flattened with its submenu path.
pub fn menu_items(app: &EffectcraftApp) -> Vec<MenuItem> {
let mut out = Vec::new();
for c in effectcraft_engine::command_specs() {
if c.menu.is_empty() {
continue;
}
out.push(MenuItem {
id: c.id.into(),
label: c.label.into(),
path: c.menu.iter().map(|s| s.to_string()).collect(),
shortcut: c.shortcut.map(str::to_string),
enabled: app.session.is_enabled(c.id),
params: Value::Null,
});
}
for c in UI_COMMANDS {
if c.menu.is_empty() {
continue;
}
out.push(MenuItem {
id: c.id.into(),
label: c.label.into(),
path: c.menu.iter().map(|s| s.to_string()).collect(),
shortcut: c.shortcut.map(str::to_string),
enabled: true,
params: Value::Null,
});
}
for p in PanelKind::ALL {
out.push(MenuItem {
id: panel_command_id(p),
label: p.title().into(),
path: vec!["Window".into()],
shortcut: p.window_shortcut().map(str::to_string),
enabled: true,
params: Value::Null,
});
}
let has_layer = !app.session.state.selected_layers.is_empty();
for e in effectcraft_engine::effects::registry() {
out.push(MenuItem {
id: "effect.apply".into(),
label: e.name.into(),
path: vec!["Effect".into(), e.category.into()],
shortcut: None,
enabled: has_layer,
params: json!({"effect": e.id}),
});
}
out
effectcraft_engine::menus::entries()
.into_iter()
.map(|(path, e)| MenuItem {
id: e.command.clone(),
label: entry_label(app, e),
path,
shortcut: e.shortcut.clone(),
enabled: entry_enabled(app, e),
checked: entry_checked(app, e),
params: e.params.clone(),
})
.collect()
}
/// Shortcut text in this OS's notation (`⇧⌘K` on macOS, `Ctrl+Shift+K` elsewhere).
pub fn shortcut_text(s: &str) -> String {
if cfg!(target_os = "macos") {
let mut out = String::new();
let parts: Vec<&str> = s.split('+').collect();
let parts: Vec<&str> = match s.strip_suffix("++") {
Some(head) => vec![head, "+"],
None => s.split('+').collect(),
};
let (mods, key) = parts.split_at(parts.len().saturating_sub(1));
for m in mods {
out.push_str(match *m {
@@ -424,7 +637,12 @@ pub fn shortcut_text(s: &str) -> String {
x => x,
});
}
out.push_str(key.first().copied().unwrap_or(""));
out.push_str(match key.first().copied().unwrap_or("") {
"Space" => "Space",
"Escape" => "Esc",
"Home" => "↖",
k => k,
});
out
} else {
s.replace("Cmd", "Ctrl")
@@ -471,22 +689,53 @@ pub fn parse_shortcut(s: &str) -> Option<(egui::Modifiers, egui::Key)> {
key.map(|k| (m, k))
}
/// The key some keyboards report for a shifted punctuation key (`Shift+=` arrives as `+`).
fn shifted_alias(k: egui::Key) -> Option<egui::Key> {
use egui::Key::*;
Some(match k {
Equals => Plus,
Semicolon => Colon,
OpenBracket => OpenCurlyBracket,
CloseBracket => CloseCurlyBracket,
Slash => Questionmark,
Backslash => Pipe,
_ => return None,
})
}
/// A key binding: modifiers, key, command id and bound params.
pub type Binding = (egui::Modifiers, egui::Key, String, Value);
/// All active bindings, most modifiers first.
pub fn bindings() -> Vec<(egui::Modifiers, egui::Key, String)> {
let mut v: Vec<(egui::Modifiers, egui::Key, String)> = Vec::new();
pub fn bindings() -> Vec<Binding> {
let mut v: Vec<Binding> = Vec::new();
let mut push = |sc: &str, id: &str, params: Value| {
if let Some((m, k)) = parse_shortcut(sc) {
if m.shift
&& let Some(alias) = shifted_alias(k)
{
v.push((m, alias, id.to_string(), params.clone()));
}
v.push((m, k, id.to_string(), params));
}
};
// Menu entries (their shortcuts may bind params), then command defaults not in the menus.
let entries = effectcraft_engine::menus::entries();
for (_, e) in &entries {
if let Some(sc) = &e.shortcut {
push(sc, &e.command, e.params.clone());
}
}
for c in effectcraft_engine::command_specs() {
if let Some((m, k)) = c.shortcut.and_then(parse_shortcut) {
v.push((m, k, c.id.to_string()));
if let Some(sc) = c.shortcut
&& !entries.iter().any(|(_, e)| e.command == c.id)
{
push(sc, c.id, Value::Null);
}
}
for c in UI_COMMANDS {
if let Some((m, k)) = c.shortcut.and_then(parse_shortcut) {
v.push((m, k, c.id.to_string()));
}
}
for p in PanelKind::ALL {
if let Some((m, k)) = p.window_shortcut().and_then(parse_shortcut) {
v.push((m, k, panel_command_id(p)));
if let Some(sc) = c.shortcut {
push(sc, c.id, Value::Null);
}
}
v.sort_by_key(|(m, ..)| std::cmp::Reverse(m.command as u8 + m.shift as u8 + m.alt as u8 + m.ctrl as u8));
@@ -533,57 +782,30 @@ pub fn handle_shortcuts(app: &mut EffectcraftApp, ctx: &egui::Context) {
let _ = invoke(app, ctx, "edit.clear", json!({}));
continue;
}
if let Some((_, _, id)) = binds.iter().find(|(m, k, _)| *k == key && mods_match(*m, mods)) {
let id = id.clone();
if let Err(e) = invoke(app, ctx, &id, json!({})) {
if let Some((_, _, id, params)) = binds.iter().find(|(m, k, ..)| *k == key && mods_match(*m, mods)) {
let (id, params) = (id.clone(), if params.is_null() { json!({}) } else { params.clone() });
if let Err(e) = invoke(app, ctx, &id, params) {
app.ui.status = e;
}
}
}
}
/// Draw the in-window menu bar.
/// Draw the in-window menu bar (the engine's AE menu tree).
pub fn menu_bar(app: &mut EffectcraftApp, ui: &mut egui::Ui) {
let items = menu_items(app);
let ctx = ui.ctx().clone();
let mut clicked: Option<(String, Value)> = None;
ui.horizontal_centered(|ui| {
ui.add_space(6.0);
egui::MenuBar::new().ui(ui, |ui| {
for top in MENUS {
let mine: Vec<&MenuItem> = items.iter().filter(|i| i.path.first().map(String::as_str) == Some(top)).collect();
ui.menu_button(top, |ui| {
ui.set_min_width(260.0);
let mut subs: Vec<&str> = Vec::new();
let mut last_was_sub = false;
for it in &mine {
if it.path.len() > 1 {
let sub = it.path[1].as_str();
if !subs.contains(&sub) {
subs.push(sub);
ui.menu_button(sub, |ui| {
ui.set_min_width(220.0);
for s in mine.iter().filter(|x| x.path.get(1).map(String::as_str) == Some(sub)) {
if menu_entry(ui, s) {
clicked = Some((s.id.clone(), s.params.clone()));
ui.close();
}
}
});
last_was_sub = true;
}
} else {
if last_was_sub && top != "Effect" {
ui.separator();
last_was_sub = false;
}
if menu_entry(ui, it) {
clicked = Some((it.id.clone(), it.params.clone()));
ui.close();
}
}
}
});
for node in effectcraft_engine::menus::menu_bar() {
if let MenuNode::Submenu { label, children } = node {
let r = ui.menu_button(label, |ui| {
ui.set_min_width(if label == "Effect" { 200.0 } else { 280.0 });
menu_nodes(app, ui, children, &mut clicked);
});
app.auto.add(&format!("menu.{label}"), r.response.rect, label);
}
}
});
});
@@ -594,10 +816,81 @@ pub fn menu_bar(app: &mut EffectcraftApp, ui: &mut egui::Ui) {
}
}
fn menu_entry(ui: &mut egui::Ui, it: &MenuItem) -> bool {
let mut b = egui::Button::new(&it.label);
if let Some(s) = &it.shortcut {
fn menu_nodes(app: &mut EffectcraftApp, ui: &mut egui::Ui, nodes: &[MenuNode], clicked: &mut Option<(String, Value)>) {
for n in nodes {
match n {
MenuNode::Separator => {
ui.separator();
}
MenuNode::Submenu { label, children } => {
ui.menu_button((gutter(false), label.as_str()), |ui| {
ui.set_min_width(if children.len() > 30 { 200.0 } else { 240.0 });
// Long submenus (Blending Mode, effect categories) scroll instead of running
// off the screen.
let max_h = ui.ctx().content_rect().height() - 40.0;
egui::ScrollArea::vertical().max_height(max_h).show(ui, |ui| menu_nodes(app, ui, children, clicked));
});
}
MenuNode::Item(e) => {
if menu_entry(app, ui, e) {
*clicked = Some((e.command.clone(), e.params.clone()));
ui.close();
}
}
}
}
}
/// The check-mark column every menu row reserves (like macOS / After Effects menus).
fn gutter(checked: bool) -> egui::Atom<'static> {
use egui::AtomExt;
(if checked { "✔" } else { "" }).atom_size(egui::vec2(14.0, 14.0))
}
fn menu_entry(app: &EffectcraftApp, ui: &mut egui::Ui, e: &MenuEntry) -> bool {
let label = entry_label(app, e);
let mut b = egui::Button::new((gutter(entry_checked(app, e) == Some(true)), label));
if let Some(s) = &e.shortcut {
b = b.shortcut_text(shortcut_text(s));
}
ui.add_enabled(it.enabled, b).clicked()
ui.add_enabled(entry_enabled(app, e), b).clicked()
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn every_ui_shortcut_parses() {
for c in UI_COMMANDS {
if let Some(sc) = c.shortcut {
assert!(parse_shortcut(sc).is_some() || sc == "Num*", "{sc}");
}
}
for (_, e) in effectcraft_engine::menus::entries() {
if let Some(sc) = &e.shortcut {
assert!(parse_shortcut(sc).is_some() || sc == "Num*", "{} ({sc})", e.label);
}
}
}
#[test]
fn ui_and_menu_shortcuts_do_not_collide() {
let mut seen: std::collections::BTreeMap<(String, String), (String, String)> = Default::default();
for (m, k, id, params) in bindings() {
let key = (format!("{m:?}"), format!("{k:?}"));
let target = (id.clone(), params.to_string());
if let Some(prev) = seen.get(&key) {
assert_eq!(prev, &target, "{key:?} bound to {prev:?} and {target:?}");
} else {
seen.insert(key, target);
}
}
}
#[test]
fn menu_items_cover_the_tree() {
assert_eq!(menus().last(), Some(&"Help"));
assert!(effectcraft_engine::menus::entries().len() > 500);
}
}