f67d38b35cc5
menus.rs
//! Menus and UI-level commands. The menu bar is generated from the engine registry plus the UI
//! command table below (commands that only affect the frontend: tools, playback, zoom, panels).
//! `invoke` is the single entry point used by menus, shortcuts and the control channel.
use serde_json::{Value, json};
use crate::FilmcraftApp;
use crate::dock::PanelKind;
use crate::state::{Mode, Tool};
pub struct UiCommand {
pub id: &'static str,
pub label: &'static str,
pub menu: &'static [&'static str],
pub shortcut: Option<&'static str>,
}
macro_rules! uic {
($id:literal, $label:literal, [$($m:literal),*], $sc:expr) => {
UiCommand { id: $id, label: $label, menu: &[$($m),*], shortcut: $sc }
};
}
pub const UI_COMMANDS: &[UiCommand] = &[
uic!("window.dockAction", "Docking Action", [], None),
uic!("window.dockPanel", "Dock Panel", [], None),
uic!("window.floatPanel", "Undock Panel", [], None),
uic!("window.closePanel", "Close Panel", [], None),
uic!("app.language.english", "English", ["Edit", "Language"], None),
// Alphabetical by the language's own name after English; Cyrillic, then CJK names follow
// the Latin ones. Settings ▸ General ▸ Interface Language lists them the same way.
uic!("app.language.indonesian", "Bahasa Indonesia", ["Edit", "Language"], None),
uic!("app.language.bosnian", "Bosanski", ["Edit", "Language"], None),
uic!("app.language.czech", "Čeština", ["Edit", "Language"], None),
uic!("app.language.german", "Deutsch", ["Edit", "Language"], None),
uic!("app.language.spanish", "Español", ["Edit", "Language"], None),
uic!("app.language.french", "Français", ["Edit", "Language"], None),
uic!("app.language.italian", "Italiano", ["Edit", "Language"], None),
uic!("app.language.polish", "Polski", ["Edit", "Language"], None),
uic!("app.language.portuguese", "Português (Brasil)", ["Edit", "Language"], None),
uic!("app.language.swedish", "Svenska", ["Edit", "Language"], None),
uic!("app.language.turkish", "Türkçe", ["Edit", "Language"], None),
uic!("app.language.russian", "Русский", ["Edit", "Language"], None),
uic!("app.language.ukrainian", "Українська", ["Edit", "Language"], None),
uic!("app.language.japanese", "日本語", ["Edit", "Language"], None),
uic!("app.language.chinese", "简体中文", ["Edit", "Language"], None),
uic!("app.language.korean", "한국어", ["Edit", "Language"], None),
uic!("source.playback.toggle", "Source Play/Stop", [], None),
uic!("source.playback.play", "Play Source", [], None),
uic!("source.playback.stop", "Stop Source", [], None),
uic!("playback.toggle", "Play/Stop", [], Some("Space")),
uic!("playback.forward", "Shuttle Right", [], Some("L")),
uic!("playback.stop", "Shuttle Stop", [], Some("K")),
uic!("playback.reverse", "Shuttle Left", [], Some("J")),
uic!("playback.slowForward", "Shuttle Slow Right", [], None),
uic!("playback.slowReverse", "Shuttle Slow Left", [], None),
uic!("playback.playAround", "Play Around", [], None),
uic!("playback.inToOut", "Play In to Out", [], Some("Alt+K")),
uic!("playback.loop", "Loop", [], None),
uic!("view.zoomIn", "Zoom In", ["View"], Some("=")),
uic!("view.zoomOut", "Zoom Out", ["View"], Some("-")),
uic!("view.zoomToSequence", "Zoom to Sequence", ["View"], Some("\\")),
uic!("view.resetInterfaceZoom", "Reset Interface Zoom", ["View"], None),
uic!("view.playbackRes.full", "Full", ["View", "Playback Resolution"], None),
uic!("view.playbackRes.half", "1/2", ["View", "Playback Resolution"], None),
uic!("view.playbackRes.quarter", "1/4", ["View", "Playback Resolution"], None),
uic!("view.playbackRes.eighth", "1/8", ["View", "Playback Resolution"], None),
uic!("view.playbackRes.sixteenth", "1/16", ["View", "Playback Resolution"], None),
uic!("view.pausedRes.full", "Full", ["View", "Paused Resolution"], None),
uic!("view.pausedRes.half", "1/2", ["View", "Paused Resolution"], None),
uic!("view.pausedRes.quarter", "1/4", ["View", "Paused Resolution"], None),
uic!("view.pausedRes.eighth", "1/8", ["View", "Paused Resolution"], None),
uic!("view.pausedRes.sixteenth", "1/16", ["View", "Paused Resolution"], None),
uic!("view.highQualityPlayback", "High Quality Playback", ["View"], None),
uic!("view.display.composite", "Composite Video", ["View", "Display Mode"], None),
uic!("view.display.alpha", "Alpha", ["View", "Display Mode"], None),
uic!("view.display.red", "Red", ["View", "Display Mode"], None),
uic!("view.display.green", "Green", ["View", "Display Mode"], None),
uic!("view.display.blue", "Blue", ["View", "Display Mode"], None),
uic!("view.display.multicam", "Multi-Camera", ["View", "Display Mode"], None),
uic!("view.display.audioWaveform", "Audio Waveform", ["View", "Display Mode"], None),
uic!("view.display.comparison", "Comparison View", ["View", "Display Mode"], None),
uic!("view.display.videoAndWaveform", "Video and Audio Waveform Split", ["View", "Display Mode"], None),
uic!("view.magnification.fit", "Fit", ["View", "Magnification"], None),
uic!("view.magnification.10", "10%", ["View", "Magnification"], None),
uic!("view.magnification.25", "25%", ["View", "Magnification"], None),
uic!("view.magnification.50", "50%", ["View", "Magnification"], None),
uic!("view.magnification.75", "75%", ["View", "Magnification"], None),
uic!("view.magnification.100", "100%", ["View", "Magnification"], None),
uic!("view.magnification.150", "150%", ["View", "Magnification"], None),
uic!("view.magnification.200", "200%", ["View", "Magnification"], None),
uic!("view.magnification.400", "400%", ["View", "Magnification"], None),
uic!("view.magnification.800", "800%", ["View", "Magnification"], None),
uic!("view.magnification.1600", "1600%", ["View", "Magnification"], None),
uic!("view.showRulers", "Show Rulers", ["View"], None),
uic!("view.showGuides", "Show Guides", ["View"], None),
uic!("view.lockGuides", "Lock Guides", ["View"], None),
uic!("view.addGuide", "Add Guide…", ["View"], None),
uic!("view.clearGuides", "Clear Guides", ["View"], None),
uic!("view.snapInProgramMonitor", "Snap in Program Monitor", ["View"], None),
// the monitors' wrench menus only, as in the reference app
uic!("view.transparencyGrid", "Transparency Grid", [], None),
uic!("view.safeMargins", "Safe Margins", ["View", "Guide Templates"], None),
uic!("view.guideTemplates.save", "Save Guides as Template…", ["View", "Guide Templates"], None),
uic!("view.guideTemplates.manage", "Manage Guides…", ["View", "Guide Templates"], None),
uic!("view.guideTemplates.apply", "Apply Guide Template", [], None),
uic!("view.guideTemplates.delete", "Delete Guide Template", [], None),
uic!("view.compare.setReference", "Set Comparison Reference", [], None),
uic!("view.dynamicAudioWaveforms", "Dynamic Audio Waveforms", ["View"], None),
uic!("multicam.toggleView", "Multi-Camera View", ["View"], Some("Shift+0")),
uic!("multicam.recordToggle", "Multi-Camera Record On/Off Toggle", [], Some("0")),
uic!("multicam.editCamerasDialog", "Edit Cameras…", [], None),
uic!("voiceover.recordToggle", "Voice-over Record", [], None),
uic!("voiceover.settingsDialog", "Voice-Over Record Settings…", [], None),
// ids follow `ThemeKind`, labels follow Settings ▸ Appearance ▸ Color Theme (`darkest`, `dark`, `light`)
uic!("view.theme.dark", "Darkest", ["View", "Appearance"], None),
uic!("view.theme.medium", "Dark", ["View", "Appearance"], None),
uic!("view.theme.light", "Light", ["View", "Appearance"], None),
uic!("view.appearanceMode.next", "Next Appearance Mode", ["View", "Appearance"], None),
uic!("window.workspace.editing", "Editing", ["Window", "Workspaces"], Some("Alt+Shift+1")),
uic!("window.workspace.assembly", "Assembly", ["Window", "Workspaces"], Some("Alt+Shift+2")),
uic!("window.workspace.color", "Color", ["Window", "Workspaces"], Some("Alt+Shift+3")),
uic!("window.workspace.effects", "Effects", ["Window", "Workspaces"], Some("Alt+Shift+4")),
uic!("window.workspace.audio", "Audio", ["Window", "Workspaces"], Some("Alt+Shift+5")),
uic!("window.workspace.captionsandgraphics", "Captions and Graphics", ["Window", "Workspaces"], Some("Alt+Shift+6")),
uic!("window.workspace.allpanels", "All Panels", ["Window", "Workspaces"], None),
uic!("window.workspace.vertical", "Vertical", ["Window", "Workspaces"], None),
uic!("window.workspace.reset", "Reset to Saved Layout", ["Window", "Workspaces"], Some("Alt+Shift+0")),
uic!("window.workspace.saveChanges", "Save Changes to this Workspace", ["Window", "Workspaces"], None),
uic!("window.workspace.saveAs", "Save as New Workspace…", ["Window", "Workspaces"], None),
uic!("window.workspace.edit", "Edit Workspaces…", ["Window", "Workspaces"], None),
uic!("tool.selection", "Selection Tool", [], Some("V")),
uic!("tool.trackSelectForward", "Track Select Forward Tool", [], Some("A")),
uic!("tool.trackSelectBackward", "Track Select Backward Tool", [], Some("Shift+A")),
uic!("tool.ripple", "Ripple Edit Tool", [], Some("B")),
uic!("tool.rolling", "Rolling Edit Tool", [], Some("N")),
uic!("tool.rateStretch", "Rate Stretch Tool", [], Some("R")),
uic!("tool.remix", "Remix Tool", [], None),
uic!("tool.razor", "Razor Tool", [], Some("C")),
uic!("tool.slip", "Slip Tool", [], Some("Y")),
uic!("tool.slide", "Slide Tool", [], Some("U")),
uic!("tool.pen", "Pen Tool", [], Some("P")),
uic!("tool.hand", "Hand Tool", [], Some("H")),
uic!("tool.zoom", "Zoom Tool", [], Some("Z")),
uic!("tool.type", "Type Tool", [], Some("T")),
uic!("tool.verticalType", "Vertical Type Tool", [], None),
uic!("mode.import", "Import", [], None),
uic!("mode.edit", "Edit", [], None),
uic!("mode.export", "Export", ["File", "Export"], Some("Cmd+M")),
uic!("help.discord", "Join the ArtCraft Discord…", ["Help"], None),
uic!("help.website", "ArtCraft Website", ["Help"], None),
uic!("help.appPage", "FilmCraft on getartcraft.com", ["Help"], None),
uic!("help.github", "FilmCraft on GitHub", ["Help"], None),
uic!("help.reportIssue", "Report an Issue…", ["Help"], None),
uic!("help.revealLogFiles", "Reveal Log Files…", ["Help"], None),
uic!("help.systemCompatibilityReport", "System Compatibility Report…", ["Help"], None),
uic!("app.checkForUpdates", "Check for Updates…", ["Help"], None),
uic!("app.about", "About FilmCraft", ["Help"], None),
uic!("app.keyboardShortcuts", "Keyboard Shortcuts…", ["Edit"], Some("Cmd+Alt+K")),
uic!("app.settings.general", "General…", ["Edit", "Preferences"], Some("Cmd+,")),
uic!("app.settings.appearance", "Appearance…", ["Edit", "Preferences"], None),
uic!("app.settings.audio", "Audio…", ["Edit", "Preferences"], None),
uic!("app.settings.audioHardware", "Audio Hardware…", ["Edit", "Preferences"], None),
uic!("app.settings.autoSave", "Auto Save…", ["Edit", "Preferences"], None),
uic!("app.settings.color", "Color…", ["Edit", "Preferences"], None),
uic!("app.settings.graphics", "Graphics…", ["Edit", "Preferences"], None),
uic!("app.settings.labels", "Labels…", ["Edit", "Preferences"], None),
uic!("app.settings.media", "Media…", ["Edit", "Preferences"], None),
uic!("app.settings.mediaAnalysis", "Media Analysis & Transcription…", ["Edit", "Preferences"], None),
uic!("app.settings.mediaCache", "Media Cache…", ["Edit", "Preferences"], None),
uic!("app.settings.memory", "Memory…", ["Edit", "Preferences"], None),
uic!("app.settings.playback", "Playback…", ["Edit", "Preferences"], None),
uic!("app.settings.plugins", "Plugins…", ["Edit", "Preferences"], None),
uic!("app.settings.timeline", "Timeline…", ["Edit", "Preferences"], None),
uic!("app.settings.trim", "Trim…", ["Edit", "Preferences"], None),
];
pub fn panel_command_id(p: PanelKind) -> String {
format!("window.panel.{}", p.id())
}
/// Explicit monitor parameters override keyboard focus.
pub fn targets_source(app: &FilmcraftApp, params: &Value) -> bool {
match params.get("monitor").and_then(Value::as_str) {
Some("source") => true,
Some(_) => false,
None => app.ui.focused == PanelKind::Source,
}
}
/// Route menu editing to the focused text field; automation can explicitly invoke the engine.
pub fn invoke(app: &mut FilmcraftApp, ctx: &egui::Context, id: &str, params: Value) -> Result<Value, String> {
if text_field_window(ctx).is_some() && edits_text(id) {
return activate_native(app, ctx, id);
}
invoke_command(app, ctx, id, params)
}
pub(crate) fn menu_items_with_focus(app: &FilmcraftApp, ctx: &egui::Context) -> Vec<MenuItem> {
let mut items = menu_items(app);
if text_field_window(ctx).is_some() {
for item in items.iter_mut().filter(|item| edits_text(&item.id)) {
item.enabled = true;
}
}
items
}
/// Execute a UI or engine command by id.
pub fn invoke_command(app: &mut FilmcraftApp, ctx: &egui::Context, id: &str, mut params: Value) -> Result<Value, String> {
// Select All / Deselect All act on the Project panel's items when it has focus (#168), only
// what the shown bin lists (#456). Here and not in the key handling: on macOS the native menu
// takes Cmd+A once Select All is enabled (a sequence is open) and sends the id here (#1752).
if app.ui.focused == PanelKind::Project && id == "edit.selectAll" {
let (cmd, params) = crate::panels::project::select_all_command(app);
return invoke(app, ctx, &cmd, params);
}
if app.ui.focused == PanelKind::Project && id == "edit.deselectAll" {
return invoke(app, ctx, "project.deselectAll", params);
}
if id == "edit.selectAll" && (crate::panels::graphics::select_all_edited_text(app) || crate::panels::effect_controls::select_all_keyframes(app)) {
return Ok(Value::Null);
}
// Insert / Overwrite of a first clip that doesn't match the empty sequence: the Clip Mismatch
// Warning asks first, from a menu (`engine.execute` keeps the sequence settings, #1540)
if !app.engine_call && matches!(id, "source.insert" | "source.overwrite") && crate::panels::clip_mismatch::ask(app, id, ¶ms) {
return Ok(json!({"dialog": "clipMismatch"}));
}
// From a menu, a shortcut or `ui.menu.invoke`, a command without parameters opens the dialog
// that asks for them. From the control channel's `engine.execute` it does not (#1540): an agent
// can neither see nor close a dialog, so the engine command runs with what was given, and
// Sequence Settings… answers the settings it would have shown.
let menu = !app.engine_call;
if !menu && id == "sequence.settings" && (params.is_null() || params.as_object().is_some_and(|m| m.is_empty())) {
filmcraft_engine::find_command(id).map_or(Ok(()), |c| (c.enabled)(&app.session))?;
let seq = app.session.execute("sequence.inspect", json!({})).map_err(|e| e.to_string())?;
return Ok(json!({"settings": seq["settings"]}));
}
// New Item and File ▸ New make their item in the bin the Project panel shows (the dialogs of
// Sequence and Color Matte take it when they open); a `bin` of the caller wins, null included
// (the engine's root)
if matches!(id, "file.newAdjustmentLayer" | "file.newBarsAndTone" | "file.newBlackVideo" | "file.newCountingLeader" | "file.newTransparentVideo")
&& params.get("bin").is_none()
&& let Some(bin) = crate::panels::project::shown_bin(app)
&& let Some(object) = params.as_object_mut()
{
object.insert("bin".into(), json!(bin));
}
if filmcraft_engine::source_monitor::source_command(id) && targets_source(app, ¶ms) && params.get("target").is_none() {
let object = params.as_object_mut().ok_or("command parameters must be an object")?;
object.insert("target".into(), json!("source"));
}
if matches!(
id,
"app.language.english"
| "app.language.japanese"
| "app.language.spanish"
| "app.language.portuguese"
| "app.language.ukrainian"
| "app.language.chinese"
| "app.language.german"
| "app.language.russian"
| "app.language.french"
| "app.language.indonesian"
| "app.language.italian"
| "app.language.bosnian"
| "app.language.korean"
| "app.language.czech"
| "app.language.polish"
| "app.language.swedish"
| "app.language.turkish"
) {
// Japanese needs the craft-fonts (built with CRAFT_FONTS_DIR) or a font installed on the system
if id == "app.language.japanese" && !crate::i18n::install_japanese_font(ctx) {
return Err("no Japanese font is installed on this system (for example Noto Sans CJK JP); the interface stays in English".into());
}
// Chinese needs the Chinese fallback theme::install adds (craft-fonts or a system face)
if id == "app.language.chinese" && !crate::i18n::chinese_font_available() {
return Err("no Chinese font is installed on this system (for example Noto Sans CJK SC or Microsoft YaHei); the interface stays in English".into());
}
// Korean needs an installed Hangul font (none is bundled; see cjk.rs)
if id == "app.language.korean" && !crate::i18n::korean_font_available() {
return Err("no Korean font is installed on this system (for example Malgun Gothic or Noto Sans CJK KR); the interface stays in English".into());
}
let language = match id {
"app.language.japanese" => crate::i18n::Language::Ja,
"app.language.spanish" => crate::i18n::Language::Es,
"app.language.portuguese" => crate::i18n::Language::PtBr,
"app.language.ukrainian" => crate::i18n::Language::Uk,
"app.language.chinese" => crate::i18n::Language::ZhCn,
"app.language.german" => crate::i18n::Language::De,
"app.language.russian" => crate::i18n::Language::Ru,
"app.language.french" => crate::i18n::Language::Fr,
"app.language.indonesian" => crate::i18n::Language::Id,
"app.language.italian" => crate::i18n::Language::It,
"app.language.bosnian" => crate::i18n::Language::Bs,
"app.language.korean" => crate::i18n::Language::Ko,
"app.language.czech" => crate::i18n::Language::Cs,
"app.language.polish" => crate::i18n::Language::Pl,
"app.language.swedish" => crate::i18n::Language::Sv,
"app.language.turkish" => crate::i18n::Language::Tr,
_ => crate::i18n::Language::En,
};
// The preference is updated in memory before it is written, so a failed write (read-only
// or full disk) still switches the interface; it only can't be remembered for next time.
let saved = app.session.execute("prefs.set", json!({"key": "general.interfaceLanguage", "value": language.code()}));
app.ui.language = language;
crate::i18n::set_current(language);
if let Err(e) = saved {
app.ui.status = tlf!("The language changed but could not be saved: {e}", e);
}
ctx.request_repaint();
return Ok(json!(app.ui.language));
}
if id == "file.exportFrame" {
if params.get("target").is_none() {
let source = targets_source(app, ¶ms);
params.as_object_mut().ok_or("command parameters must be an object")?.insert("target".into(), json!(if source { "source" } else { "program" }));
}
if menu && params.get("path").is_none() {
return crate::panels::frame_export::open(app, ctx, ¶ms);
}
}
if id == "perf.stats" {
// the engine's counters plus playback, frame workers and UI timings
return Ok(crate::perf::stats(app));
}
if let Some(result) = crate::dock_ui::route(app, ctx, id, ¶ms) {
return result;
}
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);
}
if let Some(r) = crate::panels::workspaces::route(app, id, ¶ms) {
return r;
}
if let Some(t) = id.strip_prefix("tool.") {
let tool = Tool::from_name(t).ok_or_else(|| format!("unknown tool `{t}`"))?;
app.ui.tool = tool;
return Ok(json!({"tool": tool}));
}
if let Some(r) = crate::panels::keyboard::route(app, ctx, id, ¶ms) {
return r;
}
if !targets_source(app, ¶ms)
&& let Some(r) = crate::panels::trim_monitor::route_transport(app, ctx, id)
{
return r;
}
if let Some(r) = crate::panels::multicam::route(app, id, ¶ms) {
if let Err(e) = &r {
app.ui.status = e.clone();
}
return r;
}
if let Some(r) = crate::panels::voiceover::route(app, ctx, id, ¶ms, menu) {
return r;
}
// Close Project's and Quit's Save Project prompts are confirmations: `engine.execute` still gets them
if (menu || matches!(id, "file.closeProject" | "file.closeAllProjects" | "app.quit"))
&& let Some(r) = crate::panels::clip_dialogs::route(app, id, ¶ms)
{
return r;
}
if let Some(r) = crate::panels::misc::route_history(app, ctx, id, ¶ms) {
if let Err(e) = &r {
app.ui.status = e.clone();
}
return r;
}
if let Some(r) = crate::panels::settings::route(app, id) {
return r;
}
if let Some(r) = crate::panels::graphics_templates::route(app, ctx, id, ¶ms) {
if let Err(e) = &r {
app.ui.status = e.clone();
}
return r;
}
// The dialogs answer only the menu (#1540): from `engine.execute` the command goes on to the
// engine with the parameters given, and answers an error when it needs some. The UI commands
// the menu-dialog route also carries (dynamic waveforms, reveal, …) answer either way.
if let Some(r) = crate::panels::menu_dialogs::route(app, ctx, id, ¶ms, menu) {
if let Err(e) = &r {
app.ui.status = e.clone();
}
return r;
}
if menu && let Some(r) = crate::panels::media_dialogs::route(app, id, ¶ms) {
if let Err(e) = &r {
app.ui.status = e.clone();
}
return r;
}
if let Some(r) = crate::panels::monitor_view::route(app, id, ¶ms) {
if let Err(e) = &r {
app.ui.status = e.clone();
}
return r;
}
match id {
"playback.slowForward" | "playback.slowReverse" if targets_source(app, ¶ms) => {
app.shuttle_source(if id == "playback.slowForward" { 0.25 } else { -0.25 })?;
return Ok(json!({"playing": app.source_playback.clock.playing, "speed": app.source_playback.clock.speed}));
}
"playback.slowForward" | "playback.slowReverse" => {
app.play(if id == "playback.slowForward" { 0.25 } else { -0.25 });
return Ok(json!({"speed": app.playback.speed}));
}
"source.playback.toggle" => {
app.toggle_source_play()?;
return Ok(json!({"playing": app.source_playback.clock.playing}));
}
"source.playback.play" => {
app.play_source()?;
return Ok(json!({"playing": app.source_playback.clock.playing}));
}
"source.playback.stop" => {
app.stop_source();
return Ok(Value::Null);
}
"playback.toggle"
if params.get("monitor").and_then(Value::as_str) == Some("source") || (params.get("monitor").is_none() && app.ui.focused == PanelKind::Source) =>
{
app.toggle_source_play()?;
return Ok(json!({"playing": app.source_playback.clock.playing}));
}
"playback.toggle" => {
app.toggle_play(1.0);
return Ok(json!({"playing": app.playback.playing}));
}
// L / J: pressing again in the same direction doubles the speed, up to 8×
"playback.forward" | "playback.reverse" if targets_source(app, ¶ms) => {
let (clock, dir) = (&app.source_playback.clock, if id == "playback.forward" { 1.0 } else { -1.0 });
let speed = if clock.playing && clock.speed * dir > 0.0 { (clock.speed * 2.0).clamp(-8.0, 8.0) } else { dir };
app.shuttle_source(speed)?;
return Ok(json!({"playing": app.source_playback.clock.playing, "speed": app.source_playback.clock.speed}));
}
"playhead.stepBack" | "playhead.stepForward" | "playhead.stepBack5" | "playhead.stepForward5" if targets_source(app, ¶ms) => {
app.stop_source();
let item = app.session.state.source_item.ok_or("Open a Source clip first")?;
let view = filmcraft_engine::clip_ops::source_view(&app.session, item).ok_or("Source clip is unavailable")?;
let count = if id.ends_with('5') { i64::from(app.session.prefs.playback.step_many_frames) } else { 1 };
let direction = if id.contains("Back") { -1 } else { 1 };
let time = if view.rate.frame_duration().0 == 0 {
app.session.state.source_playhead
} else {
let frame = view.rate.frame_at(app.session.state.source_playhead).saturating_add(count.saturating_mul(direction));
view.rate.tick_of(frame)
};
return app.session.execute("source.setPlayhead", json!({"time": time.0})).map_err(|e| e.to_string());
}
"playback.forward" => {
let s = if app.playback.playing && app.playback.speed > 0.0 { (app.playback.speed * 2.0).min(8.0) } else { 1.0 };
app.play(s);
return Ok(json!({"speed": app.playback.speed}));
}
"playback.reverse" => {
let s = if app.playback.playing && app.playback.speed < 0.0 { (app.playback.speed * 2.0).max(-8.0) } else { -1.0 };
app.play(s);
return Ok(json!({"speed": app.playback.speed}));
}
"playback.stop" => {
if targets_source(app, ¶ms) {
app.stop_source();
} else {
app.stop();
}
return Ok(Value::Null);
}
"playback.inToOut" if targets_source(app, ¶ms) => {
app.play_source_range(false, false)?;
return Ok(Value::Null);
}
"playback.loop" if targets_source(app, ¶ms) => {
app.source_playback.clock.looping = !app.source_playback.clock.looping;
return Ok(json!({"loop": app.source_playback.clock.looping}));
}
"playback.inToOut" => {
let seq = app.session.active_sequence();
let marked_out = seq.and_then(|q| q.mark_out);
let out = seq.and_then(|q| q.mark_out.map(|o| o + q.settings.frame_rate.frame_duration()));
if let Some(i) = seq.and_then(|q| q.mark_in) {
app.session.set_playhead(i);
}
app.play(1.0);
app.playback.stop_at = out;
app.playback.rest_at = marked_out;
return Ok(Value::Null);
}
"playback.loop" => {
app.playback.looping = !app.playback.looping;
return Ok(json!({"loop": app.playback.looping}));
}
"view.zoomIn" | "view.zoomOut" => {
let f = if id == "view.zoomIn" { 1.6 } else { 1.0 / 1.6 };
let ph = app.session.playhead().seconds();
let seconds = app.session.active_sequence().map_or(0.0, |s| s.duration().seconds());
crate::panels::timeline::zoom_about(&mut app.ui.timeline, f, ph, app.last_timeline_width, seconds);
return Ok(Value::Null);
}
"view.zoomToSequence" => {
app.ui.timeline.fit_pending = true;
return Ok(Value::Null);
}
// the whole interface back to Settings ▸ Appearance ▸ UI Scale, after Ctrl+Plus or a mouse
// mapped to page zoom scaled it (#2253)
"view.resetInterfaceZoom" => {
let zoom = app.reset_ui_zoom(ctx);
return Ok(json!({"zoom": zoom}));
}
"mode.import" => {
app.ui.mode = Mode::Import;
return Ok(Value::Null);
}
"mode.edit" => {
app.ui.mode = Mode::Edit;
return Ok(Value::Null);
}
"mode.export" => {
app.ui.mode = Mode::Export;
return Ok(Value::Null);
}
"app.checkForUpdates" => {
let _ = crate::updates_ui::check(app);
return Ok(serde_json::Value::Null);
}
"app.about" => {
app.dialog = Some(crate::Dialog::About);
return Ok(Value::Null);
}
id if crate::links::url_for(id).is_some() => {
let url = crate::links::url_for(id).unwrap_or_default();
crate::links::open(app, ctx, url);
app.ui.status = tlf!("Opened {url}", url);
return Ok(json!({"url": url}));
}
"help.shortcuts" | "app.keyboardShortcuts" => {
crate::panels::shortcuts_dialog::open(app);
return Ok(Value::Null);
}
// File ▸ Export ▸ AAF… / OMF… open their settings dialogs; with a path they export directly.
"file.exportAaf" | "file.exportOmf" if menu && params.get("path").is_none() => {
filmcraft_engine::find_command(id).map_or(Ok(()), |c| (c.enabled)(&app.session))?;
return crate::panels::interchange_export::open(app, ctx, id);
}
// File ▸ Export ▸ Captions… (and the Text panel's Export captions…) opens the caption export
// settings (#3365); with a path it exports directly.
"captions.export" if menu && params.get("path").is_none() => {
filmcraft_engine::find_command(id).map_or(Ok(()), |c| (c.enabled)(&app.session))?;
return crate::panels::caption_export::open(app, ctx, ¶ms);
}
// File ▸ Export ▸ Media… opens the Export mode, like ⌘M (#382); with a path it exports directly.
"file.exportMedia" if menu && params.get("path").is_none() => {
filmcraft_engine::find_command(id).map_or(Ok(()), |c| (c.enabled)(&app.session))?;
app.ui.mode = Mode::Export;
return Ok(json!({"mode": "export"}));
}
// Audio Gain from the menu or G opens the dialog; with params it applies directly.
"clip.audioGain" if menu && params.as_object().is_none_or(|m| m.is_empty()) => {
filmcraft_engine::find_command("clip.audioGain").map_or(Ok(()), |c| (c.enabled)(&app.session))?;
if app.ui.audio_gain.mode.is_empty() {
app.ui.audio_gain.mode = "adjust".into();
}
app.dialog = Some(crate::Dialog::AudioGain);
return Ok(json!({"dialog": "audioGain"}));
}
// Colour dialogs from the menus; with params the engine command applies directly.
"clip.interpretFootage" if menu && params.get("colorSpace").is_none() && params.get("pixelAspect").is_none() => {
filmcraft_engine::find_command(id).map_or(Ok(()), |c| (c.enabled)(&app.session))?;
crate::panels::color_dialogs::open_interpret(app, ¶ms);
return Ok(json!({"dialog": "interpretFootage"}));
}
// Remix Properties… from the menu opens the dialog; with params it applies directly.
"clip.remix.properties" if menu && params.as_object().is_none_or(|m| m.is_empty()) => {
filmcraft_engine::find_command(id).map_or(Ok(()), |c| (c.enabled)(&app.session))?;
return crate::panels::remix::open(app, ctx);
}
// Add Tracks… from the menu opens the dialog (new tracks after the last ones, as Premiere
// offers); with params it adds the tracks directly.
"sequence.addTracks" if menu && params.as_object().is_none_or(|m| m.is_empty()) => {
filmcraft_engine::find_command(id).map_or(Ok(()), |c| (c.enabled)(&app.session))?;
let seq = app.session.active_sequence();
let count = |f: fn(&filmcraft_engine::project::Sequence) -> usize| seq.map_or(0, f);
app.ui.add_tracks = crate::state::AddTracksDraft {
video_after: count(|q| q.video_tracks.len()),
audio_after: count(|q| q.audio_tracks.len()),
submix_after: count(|q| q.submix_tracks.len()),
..Default::default()
};
app.dialog = Some(crate::Dialog::AddTracks);
return Ok(json!({"dialog": "addTracks"}));
}
"sequence.deleteTracks" if menu && params.as_object().is_none_or(|m| m.is_empty()) => {
filmcraft_engine::find_command(id).map_or(Ok(()), |c| (c.enabled)(&app.session))?;
app.ui.delete_tracks = Default::default();
app.dialog = Some(crate::Dialog::DeleteTracks);
return Ok(json!({"dialog": "deleteTracks"}));
}
// Sequence Settings… from the menu opens the dialog; with params the engine command applies
// them directly.
"sequence.settings" if menu && params.as_object().is_none_or(|m| m.is_empty()) => {
filmcraft_engine::find_command(id).map_or(Ok(()), |c| (c.enabled)(&app.session))?;
crate::panels::sequence_settings::open(app);
return Ok(json!({"dialog": "sequenceSettings"}));
}
// File ▸ New ▸ Sequence… (Cmd+N) opens New Sequence; with params (a clip, an agent) it
// makes the sequence directly
"file.newSequence" if menu && (params.as_object().is_none_or(|m| m.is_empty()) || crate::panels::project::bin_only(¶ms).is_some()) => {
crate::panels::sequence_settings::open_new(app, crate::panels::project::bin_only(¶ms));
return Ok(json!({"dialog": "newSequence"}));
}
"sequence.colorSettings" if menu && params.as_object().is_none_or(|m| m.is_empty()) => {
filmcraft_engine::find_command(id).map_or(Ok(()), |c| (c.enabled)(&app.session))?;
crate::panels::color_dialogs::open_sequence(app);
return Ok(json!({"dialog": "sequenceColor"}));
}
// New Bin from menus, shortcuts and the Project panel asks for the name (inline rename);
// agents pass `name` to name the bin directly
"file.newBin" if params.get("name").is_none() => {
let r = crate::panels::project::new_bin(app, ¶ms);
if let Err(e) = &r {
app.ui.status = e.clone();
}
return r;
}
// From menus/shortcuts (no params) these ask first; agents pass params to act directly.
"file.revert" if params.as_object().is_none_or(|m| m.is_empty()) && app.session.is_dirty() => {
if app.session.path.is_none() {
return Err("the project has not been saved yet".into());
}
app.dialog = Some(crate::Dialog::RevertConfirm);
return Ok(json!({"dialog": "revert"}));
}
// Files or an image on the system clipboard paste into the Timeline, or import into the
// Project panel (#611). Copying clips puts their names there as text, so media copied
// *after* them still wins; media that was already there when Copy/Cut ran is ignored so
// Paste of cut/copied clips still works (#697).
"edit.paste" | "edit.pasteInsert"
if params.as_object().is_none_or(|m| m.is_empty()) && matches!(app.ui.focused, PanelKind::Timeline | PanelKind::Project) =>
{
if let Some(r) = paste_clipboard_media(app, id == "edit.pasteInsert") {
return r;
}
}
"file.recover" if params.as_object().is_none_or(|m| m.is_empty()) => {
if app.session.recovery_candidates().is_empty() {
return Err("there are no unsaved changes to recover".into());
}
app.file_dialogs.recovery_choice = 0;
app.dialog = Some(crate::Dialog::Recovery);
return Ok(json!({"dialog": "recovery"}));
}
_ => {}
}
if id == "view.appearanceMode.next" {
crate::panels::settings::cycle_appearance(app, ctx);
return Ok(json!({"appearanceMode": app.session.prefs.appearance.appearance_mode}));
}
if let Some(th) = id.strip_prefix("view.theme.") {
let k = crate::theme::ThemeKind::from_name(th).ok_or("unknown theme")?;
crate::panels::settings::set_theme(app, ctx, k);
return Ok(Value::Null);
}
// File ▸ Open Recent
if let Some(i) = id.strip_prefix("file.openRecent.") {
let path = i.parse::<usize>().ok().and_then(|i| app.session.prefs.general.recent_projects.get(i).cloned()).ok_or("no such recent project")?;
return invoke(app, ctx, "file.open", json!({"path": path}));
}
if id == "file.clearRecent" {
let mut next = app.session.prefs.clone();
next.general.recent_projects.clear();
app.session.set_prefs(next).map_err(|e| e.to_string())?;
return Ok(Value::Null);
}
// File dialogs for commands that need a path.
if menu
&& ((id == "file.import" && params.get("paths").is_none() && params.get("path").is_none())
|| (id == "file.importImageSequence" && params.get("path").is_none())
|| (id == "file.saveAs" && params.get("path").is_none())
|| (id == "file.saveCopy" && 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())
|| (id == "captions.import" && params.get("path").is_none())
|| (id == "graphics.newFromFile" && params.get("path").is_none())
|| (crate::EXPORT_SAVE_DIALOGS.iter().any(|(c, ..)| *c == id) && params.get("path").is_none()))
{
return app.file_dialog(id, ¶ms);
}
let save_path = params.get("path").and_then(Value::as_str).map(str::to_string);
let r = app.session.execute(id, params).map_err(|e| e.to_string());
if let Err(e) = &r {
app.ui.status = e.clone();
}
if r.is_ok() {
let path = save_path.or_else(|| app.session.path.clone());
if let Some(path) = path {
saved_status(app, id, &path);
}
}
if r.is_ok() && matches!(id, "playhead.start" | "playhead.end") {
crate::panels::timeline::reveal_playhead(app, ctx);
}
let filled_clipboard = match id {
"edit.copy" | "edit.cut" => r.is_ok(),
// over a range with no clips on the targeted tracks they leave the clipboard (and the OS one) as it was
"sequence.lift" | "sequence.extract" => r.as_ref().is_ok_and(|v| v["copied"].as_u64().is_some_and(|n| n > 0)),
_ => false,
};
if filled_clipboard {
remember_stale_clipboard_media(app);
put_clip_names_on_system_clipboard(app, ctx);
}
r
}
/// Tell the status bar where a project save went (File ▸ Save, Save As, Save a Copy).
pub(crate) fn saved_status(app: &mut FilmcraftApp, id: &str, path: &str) {
match id {
"file.save" | "file.saveAs" | "file.saveAll" => app.ui.status = tlf!("Project saved to {path}", path),
"file.saveCopy" => app.ui.status = tlf!("Copy of the project saved to {path}", path),
_ => {}
}
}
/// Run the command `id` chosen in the native menu bar, which takes the Edit menu's key
/// equivalents (⌘C, ⌘X, ⌘V, ⌘A, ⌘Z, ⇧⌘Z) before any window sees them: while a text field has
/// the keyboard, in the main window or a floating panel's own window, the field gets the key
/// and the project command does not run.
pub fn activate_native(app: &mut FilmcraftApp, ctx: &egui::Context, id: &str) -> Result<Value, String> {
// Select All of a graphic's text is already handled in `invoke`
if text_field_window(ctx).is_none() && app.ui.gfx_edit.is_some() && matches!(id, "edit.copy" | "edit.cut" | "edit.paste") {
return Ok(graphic_text_command(app, ctx, id));
}
let (Some(window), Some(text)) = (text_field_window(ctx), text_command(id, ctx.os().is_mac())) else {
return invoke_command(app, ctx, id, json!({}));
};
// Retain main's immediate Select All for the root window, including Project search. Floating
// text fields keep the native event delivery that belongs to their own viewport.
if id == "edit.selectAll" && window == egui::ViewportId::ROOT && select_all_in_text_field(ctx) {
return Ok(Value::Null);
}
match text {
TextCommand::Paste => ctx.send_viewport_cmd_to(window, egui::ViewportCommand::RequestPaste),
TextCommand::Events(events) if window == egui::ViewportId::ROOT => {
app.synthetic.extend(events);
ctx.request_repaint();
}
TextCommand::Events(events) => craft_ui::docking::send_keys(ctx, window, events),
}
Ok(Value::Null)
}
fn select_all_in_text_field(ctx: &egui::Context) -> bool {
use egui::text::{CCursor, CCursorRange};
let Some(id) = ctx.memory(|m| m.focused()) else { return false };
let Some(mut state) = egui::TextEdit::load_state(ctx, id) else { return false };
state.cursor.set_char_range(Some(CCursorRange::two(CCursor::new(0), CCursor::new(usize::MAX))));
state.store(ctx, id);
true
}
/// The window whose text field has the keyboard: a floating panel's own OS window, or the main
/// window.
pub fn text_field_window(ctx: &egui::Context) -> Option<egui::ViewportId> {
craft_ui::docking::typing_window(ctx).or_else(|| ctx.text_edit_focused().then_some(egui::ViewportId::ROOT))
}
/// Whether a focused text field takes the menu command `id` ([`activate_native`]): the native
/// menu keeps such items enabled while one has the keyboard, whatever the project allows.
pub fn edits_text(id: &str) -> bool {
text_command(id, false).is_some()
}
/// Whether the graphic text being edited takes the menu command `id` ([`activate_native`]): Copy,
/// Cut and Paste go to its text instead of copying or pasting the graphic clip (#1953). Select All
/// already selects its text in [`invoke`], and Undo stays the project's, as each text edit is one.
/// The native menu keeps these enabled while a graphic's text is edited.
pub fn edits_graphic_text(id: &str) -> bool {
matches!(id, "edit.copy" | "edit.cut" | "edit.paste" | "edit.selectAll")
}
/// Hand a native Edit command to the graphic text being edited (main window). The editor gives the
/// keyboard to any other focused widget (a Text Properties button) and takes it back once nothing
/// has it, so that widget lets go first and the text receives the event in the next frame.
fn graphic_text_command(app: &mut FilmcraftApp, ctx: &egui::Context, id: &str) -> Value {
let editor = egui::Id::unique(crate::panels::graphics::TEXT_EDIT_ID);
if let Some(other) = ctx.memory(|m| m.focused()).filter(|f| *f != editor) {
ctx.memory_mut(|m| m.surrender_focus(other));
}
match id {
"edit.paste" => ctx.send_viewport_cmd_to(egui::ViewportId::ROOT, egui::ViewportCommand::RequestPaste),
"edit.cut" => app.synthetic.push(egui::Event::Cut),
_ => app.synthetic.push(egui::Event::Copy),
}
ctx.request_repaint();
Value::Null
}
/// What a focused text field does for an Edit menu command.
#[derive(Debug, PartialEq)]
enum TextCommand {
/// The clipboard text, which the window asks for and receives as `Paste`.
Paste,
Events(Vec<egui::Event>),
}
fn text_command(id: &str, mac: bool) -> Option<TextCommand> {
let key = |key, shift| {
let modifiers = egui::Modifiers { mac_cmd: mac, ctrl: !mac, command: true, shift, alt: false };
TextCommand::Events([true, false].map(|pressed| egui::Event::Key { key, physical_key: None, pressed, repeat: false, modifiers }).into())
};
Some(match id {
"edit.clear" => TextCommand::Events(
[true, false]
.map(|pressed| egui::Event::Key { key: egui::Key::Delete, physical_key: None, pressed, repeat: false, modifiers: egui::Modifiers::NONE })
.into(),
),
"edit.copy" => TextCommand::Events(vec![egui::Event::Copy]),
"edit.cut" => TextCommand::Events(vec![egui::Event::Cut]),
"edit.paste" => TextCommand::Paste,
"edit.undo" => key(egui::Key::Z, false),
"edit.redo" => key(egui::Key::Z, true),
"edit.selectAll" => key(egui::Key::A, false),
_ => return None,
})
}
/// Snapshot OS files/image present at Copy/Cut. Many platforms keep a previous file list after
/// `copy_text`, so Paste would otherwise import those files instead of the clips (#697).
fn remember_stale_clipboard_media(app: &mut FilmcraftApp) {
app.stale_clipboard_media = app.hooks.clipboard_media.as_mut().and_then(|f| f()).as_ref().map(clipboard_media_key);
}
fn clipboard_media_key(media: &crate::ClipboardMedia) -> String {
match media {
crate::ClipboardMedia::Files(paths) => format!("files:{}", paths.join("\0")),
crate::ClipboardMedia::Image { width, height, rgba } => {
let mut h: u64 = 0xcbf2_9ce4_8422_2325;
for b in rgba {
h ^= u64::from(*b);
h = h.wrapping_mul(0x0100_0000_01b3);
}
format!("img:{width}x{height}:{h}")
}
}
}
/// Copied clips live in the session, but on Windows and Linux Ctrl+V only reaches the app when the
/// system clipboard holds text (an empty clipboard sends no paste at all, #199). Put the copied
/// clip names there, like other editors put their own data on the clipboard, so Ctrl+V pastes the
/// clips right after a copy.
fn put_clip_names_on_system_clipboard(app: &FilmcraftApp, ctx: &egui::Context) {
let names: Vec<&str> = app.session.state.clipboard.iter().map(|(_, _, c)| c.name.as_str()).collect();
if names.is_empty() {
return;
}
let text = names.join("\n");
ctx.copy_text(if text.trim().is_empty() { format!("{} clips", names.len()) } else { text });
}
/// Paste the files or the image on the system clipboard: placed on the Timeline when it has focus,
/// imported into the shown bin in the Project panel. None when the clipboard holds neither.
fn paste_clipboard_media(app: &mut FilmcraftApp, insert: bool) -> Option<Result<Value, String>> {
// The paste event already carries the image bytes. Prefer them: re-reading the OS clipboard
// is racy (it may have changed) and the web host installs no clipboard-media hook.
let event_media = match app.pasted_image.take() {
Some(image) => match paste_image_media(&image) {
Ok(media) => Some(media),
Err(error) => return Some(Err(error)),
},
None => None,
};
let (media, from_event) = match event_media {
Some(media) => (media, true),
None => ((app.hooks.clipboard_media.as_mut()?)()?, false),
};
if !from_event && app.stale_clipboard_media.as_ref() == Some(&clipboard_media_key(&media)) {
return None;
}
let paths = match media {
crate::ClipboardMedia::Files(paths) => paths,
crate::ClipboardMedia::Image { width, height, rgba } => match encode_png(width, height, rgba)
.and_then(|png| filmcraft_engine::paste_media::save_pasted_image(&mut app.session, &png).map_err(|e| e.to_string()))
{
Ok(path) => vec![path],
Err(e) => return Some(Err(e)),
},
};
let params = json!({"paths": paths, "insert": insert, "place": app.ui.focused == PanelKind::Timeline, "bin": app.import_bin().0});
Some(app.session.execute("edit.pasteMedia", params).map_err(|e| e.to_string()))
}
/// Upper bound on the RGBA copy made from a clipboard image, matching the screenshot per-image
/// bound. The event's own `ColorImage` is still alive while this copy is encoded, so reject before
/// allocating it rather than only checking for integer overflow.
const MAX_CLIPBOARD_IMAGE_BYTES: usize = 128 * 1024 * 1024;
/// The RGBA bytes of an image delivered by `egui::Event::PasteImage`.
fn paste_image_media(image: &egui::ColorImage) -> Result<crate::ClipboardMedia, String> {
let [width, height] = image.size;
let len =
width.checked_mul(height).and_then(|pixels| pixels.checked_mul(4)).ok_or_else(|| "the pasted image size overflows the clipboard buffer".to_string())?;
if len > MAX_CLIPBOARD_IMAGE_BYTES {
return Err(format!(
"the pasted image is too large: {width}×{height} needs {len} RGBA bytes, over the {MAX_CLIPBOARD_IMAGE_BYTES}-byte clipboard image limit"
));
}
let mut rgba = Vec::with_capacity(len);
for c in &image.pixels {
// `Color32` is premultiplied internally; PNG/RgbaImage expect straight alpha. Round-trip
// through the straight sRGB form so a semi-transparent clipboard pixel keeps its colour.
let [r, g, b, a] = c.to_srgba_unmultiplied();
rgba.extend_from_slice(&[r, g, b, a]);
}
if rgba.len() != len {
return Err("the pasted image does not match its reported size".into());
}
Ok(crate::ClipboardMedia::Image { width, height, rgba })
}
/// A compressed PNG of a clipboard image; an error when its size and pixels disagree.
fn encode_png(width: usize, height: usize, rgba: Vec<u8>) -> Result<Vec<u8>, String> {
let (w, h) = (u32::try_from(width).map_err(|e| e.to_string())?, u32::try_from(height).map_err(|e| e.to_string())?);
let img = image::RgbaImage::from_raw(w, h, rgba).ok_or("the image on the clipboard is malformed")?;
let mut png = Vec::new();
img.write_to(&mut std::io::Cursor::new(&mut png), image::ImageFormat::Png).map_err(|e| e.to_string())?;
Ok(png)
}
/// A menu tree entry for display / `ui.menu.list`.
#[derive(Clone, Debug, PartialEq, serde::Serialize)]
pub struct MenuItem {
pub id: String,
pub label: String,
pub path: Vec<String>,
pub shortcut: Option<String>,
pub enabled: bool,
/// Checkmark state of toggle / radio items (None = not checkable).
#[serde(skip_serializing_if = "Option::is_none")]
pub checked: Option<bool>,
}
pub const MENUS: [&str; 9] = ["File", "Edit", "Clip", "Sequence", "Markers", "Graphics and Titles", "View", "Window", "Help"];
/// Labels only the native macOS app menu shows, with no command in the menu tree behind them: the
/// Settings submenu and the predefined Hide, Hide Others and Quit items. They are translated through
/// the catalogs like menu labels (`apps/filmcraft/src/native_menu.rs`).
pub const NATIVE_APP_MENU: [&str; 4] = ["Settings", "Hide FilmCraft", "Hide Others", "Quit FilmCraft"];
pub fn menu_items(app: &FilmcraftApp) -> Vec<MenuItem> {
let mut v = menu_items_for(&app.session);
for it in &mut v {
it.label = app.ui.language.tr(&it.label).to_string();
match it.id.as_str() {
"app.language.english" => it.checked = Some(app.ui.language == crate::i18n::Language::En),
"app.language.japanese" => it.checked = Some(app.ui.language == crate::i18n::Language::Ja),
"app.language.spanish" => it.checked = Some(app.ui.language == crate::i18n::Language::Es),
"app.language.portuguese" => it.checked = Some(app.ui.language == crate::i18n::Language::PtBr),
"app.language.ukrainian" => it.checked = Some(app.ui.language == crate::i18n::Language::Uk),
"app.language.chinese" => it.checked = Some(app.ui.language == crate::i18n::Language::ZhCn),
"app.language.german" => it.checked = Some(app.ui.language == crate::i18n::Language::De),
"app.language.russian" => it.checked = Some(app.ui.language == crate::i18n::Language::Ru),
"app.language.french" => it.checked = Some(app.ui.language == crate::i18n::Language::Fr),
"app.language.indonesian" => it.checked = Some(app.ui.language == crate::i18n::Language::Id),
"app.language.italian" => it.checked = Some(app.ui.language == crate::i18n::Language::It),
"app.language.bosnian" => it.checked = Some(app.ui.language == crate::i18n::Language::Bs),
"app.language.korean" => it.checked = Some(app.ui.language == crate::i18n::Language::Ko),
"app.language.czech" => it.checked = Some(app.ui.language == crate::i18n::Language::Cs),
"app.language.polish" => it.checked = Some(app.ui.language == crate::i18n::Language::Pl),
"app.language.swedish" => it.checked = Some(app.ui.language == crate::i18n::Language::Sv),
"app.language.turkish" => it.checked = Some(app.ui.language == crate::i18n::Language::Tr),
_ => {}
}
if let Some(on) = engine_toggle(&app.session, &it.id) {
it.checked = Some(on);
}
if it.id.starts_with("view.") {
it.checked = crate::panels::monitor_view::checked(app, &it.id).or(crate::panels::menu_dialogs::checked(app, &it.id));
it.enabled &= crate::panels::monitor_view::enabled(app, &it.id);
}
}
crate::panels::workspaces::menu(app, &mut v);
open_recent(&mut v, &app.session, |n| app.ui.language.tr(n).to_string());
v
}
/// Checkmark state of the engine's on/off menu commands (`{"on":bool?}`, toggled when invoked from a
/// menu without parameters). None = not one of them.
fn engine_toggle(session: &filmcraft_engine::Session, id: &str) -> Option<bool> {
let st = &session.state;
Some(match id {
"sequence.snap" => st.snapping,
"sequence.linkedSelection" => st.linked_selection,
"sequence.selectionFollowsPlayhead" => st.selection_follows_playhead,
"sequence.showThroughEdits" => st.show_through_edits,
"markers.rippleSequenceMarkers" => st.ripple_sequence_markers,
"markers.copyPasteIncludesSequenceMarkers" => st.copy_paste_sequence_markers,
_ => return None,
})
}
/// File ▸ Open Recent, after Open Project…: the recent projects (General ▸ `recentProjects`),
/// newest first, then Clear Recent Files. Clear is always there, so the submenu exists (in the
/// native menu too, built once at launch) before the first project is saved.
fn open_recent(items: &mut Vec<MenuItem>, session: &filmcraft_engine::Session, tr: impl Fn(&str) -> String) {
let Some(at) = items.iter().position(|it| it.id == "file.open") else { return };
let path: Vec<String> = vec!["File".into(), "Open Recent".into()];
let recent = &session.prefs.general.recent_projects;
let mut sub: Vec<MenuItem> = recent
.iter()
.enumerate()
.map(|(i, p)| MenuItem { id: format!("file.openRecent.{i}"), label: recent_name(p), path: path.clone(), shortcut: None, enabled: true, checked: None })
.collect();
sub.push(MenuItem { id: "file.clearRecent".into(), label: tr("Clear Recent Files"), path, shortcut: None, enabled: !recent.is_empty(), checked: None });
items.splice(at + 1..at + 1, sub);
}
/// A recent project as listed in File ▸ Open Recent and on Home: its file name.
pub fn recent_name(path: &str) -> String {
std::path::Path::new(path).file_name().map_or_else(|| path.to_string(), |n| n.to_string_lossy().into_owned())
}
/// The menu bar as it ships, without a running app: English labels, default preferences, the
/// built-in workspaces, nothing checked. Deterministic, so `cargo xtask parity` can measure it
/// (`examples/menu_tree.rs` prints it as JSON).
pub fn default_menu_items() -> Vec<MenuItem> {
let session = filmcraft_engine::Session::default();
let mut v = menu_items_for(&session);
crate::panels::workspaces::default_menu(&mut v, &session);
open_recent(&mut v, &session, str::to_string);
v
}
/// Menu entries with their live shortcuts and enablement.
pub fn menu_items_for(session: &filmcraft_engine::Session) -> Vec<MenuItem> {
let mut out = Vec::new();
for c in filmcraft_engine::command_specs() {
if c.menu.is_empty() {
continue;
}
// Edit ▸ Label items carry the names from Settings ▸ Labels
let label = match c.id.strip_prefix("edit.label.").and_then(filmcraft_project::Label::from_name) {
Some(l) => session.prefs.labels.name(l),
None => c.label.into(),
};
out.push(MenuItem {
id: c.id.into(),
label,
path: c.menu.iter().map(|s| s.to_string()).collect(),
shortcut: session.shortcuts.primary(c.id),
enabled: session.is_enabled(c.id),
checked: None,
});
}
for c in UI_COMMANDS.iter().chain(crate::panels::keyboard::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: session.shortcuts.primary(c.id),
enabled: true,
checked: None,
});
}
for p in PanelKind::ALL {
out.push(MenuItem {
id: panel_command_id(p),
label: p.title().into(),
path: vec!["Window".into()],
shortcut: session.shortcuts.primary(&panel_command_id(p)),
enabled: true,
checked: None,
});
}
out
}
/// Shortcut text for menus in this OS's notation (`⇧⌘K` on macOS, `Ctrl+Shift+K` elsewhere).
pub fn shortcut_text(s: &str) -> String {
use filmcraft_engine::shortcuts::{Chord, Platform};
Chord::parse(s).map(|c| c.display(Platform::current())).unwrap_or_else(|_| s.to_string())
}
/// Parse "Cmd+Shift+K" into the modifiers a chord requires + its key. Off macOS the Control key
/// is the primary modifier, so `Ctrl` and `Cmd` are one key and both mean `command` (the engine's
/// `Chord::effective` says the same): a physical Ctrl press carries `command` there, and
/// [`chord_held`] treats Ctrl and Cmd as one key, so a `Ctrl+…` chord matches a `Cmd+…` binding
/// and the other way round (#245). On a Mac they stay two keys.
pub fn parse_shortcut(s: &str) -> Option<(egui::Modifiers, egui::Key)> {
let mut m = egui::Modifiers::NONE;
let mut key = None;
let parts: Vec<&str> = if s == "+" { vec!["+"] } else { s.split('+').collect() };
let mac = cfg!(target_os = "macos");
for p in parts {
match p {
"Cmd" => m.command = true,
"Shift" => m.shift = true,
"Alt" => m.alt = true,
"Ctrl" if !mac => m.command = true,
"Ctrl" => m.ctrl = true,
k => {
key = match k {
";" => Some(egui::Key::Semicolon),
"'" => Some(egui::Key::Quote),
"," => Some(egui::Key::Comma),
"." => Some(egui::Key::Period),
"/" => Some(egui::Key::Slash),
"\\" => Some(egui::Key::Backslash),
"=" => Some(egui::Key::Equals),
"-" => Some(egui::Key::Minus),
"`" => Some(egui::Key::Backtick),
_ => egui::Key::from_name(k),
}
}
}
}
key.map(|k| (m, k))
}
/// One active key binding for the input loop: (modifiers, key, command id, panel or None).
pub type KeyBinding = (egui::Modifiers, egui::Key, String, Option<String>);
/// The active key bindings (from the engine's shortcut set), most specific first so Shift+I
/// doesn't also fire I.
pub fn bindings(app: &FilmcraftApp) -> Vec<KeyBinding> {
let mut v: Vec<KeyBinding> =
app.session.shortcuts.bindings.iter().filter_map(|b| parse_shortcut(&b.keys).map(|(m, k)| (m, k, b.command.clone(), b.panel.clone()))).collect();
// With Shift held, a punctuation key arrives as its shifted glyph (Shift+; is `:` on US layouts).
let shifted: Vec<KeyBinding> =
v.iter().filter(|b| b.0.shift).filter_map(|(m, k, id, p)| shifted_key(*k).map(|k2| (*m, k2, id.clone(), p.clone()))).collect();
v.extend(shifted);
// `+` is the `=` key with Shift on US layouts and its own key on others and the keypad: a chord
// on `=` takes it too, unless something is bound to `+` itself. Otherwise Ctrl+Plus fell
// through to egui's interface zoom while Ctrl+- and Ctrl+0 were taken by FilmCraft
// shortcuts, so the interface grew and could not shrink back (#2253).
let plus: Vec<KeyBinding> = v
.iter()
.filter(|(m, k, _, p)| *k == egui::Key::Equals && !v.iter().any(|(m2, k2, _, p2)| *k2 == egui::Key::Plus && m2 == m && p2 == p))
.map(|(m, _, id, p)| (*m, egui::Key::Plus, id.clone(), p.clone()))
.collect();
v.extend(plus);
v.sort_by_key(|(m, ..)| std::cmp::Reverse(specificity(*m)));
v
}
/// How many modifiers a chord names: chords naming more are matched first, because a punctuation
/// or digit key may arrive with an extra Shift or Alt the chord doesn't name (see [`chord_held`]).
pub fn specificity(m: egui::Modifiers) -> u8 {
m.command as u8 + m.shift as u8 + m.alt as u8 + m.ctrl as u8
}
/// Whether a press of `key` with `held` down is the chord `pattern` (from [`parse_shortcut`]) on
/// `key`. The held modifiers must be exactly the ones the chord names: Ctrl+Alt+Cmd+N is not
/// Cmd+Alt+N (File ▸ New ▸ Project), and Cmd+Shift+N is not Cmd+N. egui's own matching
/// (`Modifiers::matches_logically`, and `matches_exact` too) ignores an extra Ctrl on a Mac, and
/// `matches_logically` any extra Shift and Alt as well, so a chord the user didn't bind ran a
/// shorter one instead.
///
/// - Off macOS Ctrl and Cmd are one key (a physical Ctrl press carries both `ctrl` and `command`,
/// and [`parse_shortcut`] reads either as `command`, #245); on a Mac they are two.
/// - Shift and Alt must match exactly on letters and named keys (arrows, Space, Delete, F-keys…),
/// where they never change which key arrives. A punctuation or digit key is reached with Shift
/// or Alt on many layouts (`+` is Shift+= on US, `/` is Shift+7 on German, `[` is Option+5 on a
/// German Mac, digits need Shift on AZERTY), so there an extra Shift or Alt is allowed, as egui
/// allows it: Ctrl+Shift+= still arrives as Ctrl+Plus (#2253). [`bindings`] lists the most
/// specific chords first, so a chord that names that Shift or Alt still wins.
pub fn chord_held(held: egui::Modifiers, key: egui::Key, pattern: egui::Modifiers) -> bool {
chord_held_on(cfg!(target_os = "macos"), held, key, pattern)
}
/// [`chord_held`] for a Mac (`mac`) or another OS, so both are testable on either.
fn chord_held_on(mac: bool, held: egui::Modifiers, key: egui::Key, pattern: egui::Modifiers) -> bool {
let cmd = |m: egui::Modifiers| m.command || m.mac_cmd;
let primary = if mac { cmd(held) == cmd(pattern) && held.ctrl == pattern.ctrl } else { (cmd(held) || held.ctrl) == (cmd(pattern) || pattern.ctrl) };
let glyph = layout_may_add_shift_or_alt(key);
let shift = held.shift == pattern.shift || (glyph && held.shift);
let alt = held.alt == pattern.alt || (glyph && held.alt);
primary && shift && alt
}
/// Punctuation and digit keys: some keyboard layouts only reach them with Shift or Alt held.
fn layout_may_add_shift_or_alt(key: egui::Key) -> bool {
use egui::Key;
matches!(
key,
Key::Colon
| Key::Comma
| Key::Backslash
| Key::Slash
| Key::Pipe
| Key::Questionmark
| Key::Exclamationmark
| Key::OpenBracket
| Key::CloseBracket
| Key::OpenCurlyBracket
| Key::CloseCurlyBracket
| Key::Backtick
| Key::Minus
| Key::Period
| Key::Plus
| Key::Equals
| Key::Semicolon
| Key::Quote
| Key::Num0
| Key::Num1
| Key::Num2
| Key::Num3
| Key::Num4
| Key::Num5
| Key::Num6
| Key::Num7
| Key::Num8
| Key::Num9
)
}
/// The key a US layout reports for `k` with Shift held, when it differs.
fn shifted_key(k: egui::Key) -> Option<egui::Key> {
use egui::Key;
Some(match k {
Key::Semicolon => Key::Colon,
Key::Slash => Key::Questionmark,
Key::Equals => Key::Plus,
Key::Backslash => Key::Pipe,
Key::Num1 => Key::Exclamationmark,
Key::OpenBracket => Key::OpenCurlyBracket,
Key::CloseBracket => Key::CloseCurlyBracket,
_ => return None,
})
}
/// Frontend-owned commands, registered with the engine's shortcut set so they can be listed,
/// rebound and resolved alongside engine commands (the `shortcuts.` commands).
pub fn external_commands() -> Vec<filmcraft_engine::shortcuts::CommandInfo> {
use filmcraft_engine::shortcuts::CommandInfo;
let mut v: Vec<CommandInfo> =
UI_COMMANDS.iter().chain(crate::panels::keyboard::COMMANDS).map(|c| CommandInfo::new(c.id, c.label, c.menu, c.shortcut)).collect();
for p in PanelKind::ALL {
v.push(CommandInfo::new(&panel_command_id(p), p.title(), &["Window"], p.window_shortcut()));
}
v
}
/// Draw the in-window menu bar.
pub fn menu_bar(app: &mut FilmcraftApp, ui: &mut egui::Ui) {
ui.ctx().data_mut(|d| d.insert_temp(egui::Id::unique("interface-language"), app.ui.language));
let items = menu_items_with_focus(app, ui.ctx());
// Draw Edit ▸ Label with the user's live palette, not the baked-in label colours.
let labels = app.session.prefs.labels.clone();
let ctx = ui.ctx().clone();
let mut clicked: Option<String> = None;
// The bar's titles and their menus share one size and colour, as in a native menu bar.
egui::MenuBar::new().style(crate::theme::menu_style).config(egui::containers::menu::MenuConfig::new().style(crate::theme::menu_style)).ui(ui, |ui| {
let mut titles = Vec::with_capacity(MENUS.len());
for top in MENUS {
let mine: Vec<&MenuItem> = items.iter().filter(|i| i.path.first().map(String::as_str) == Some(top)).collect();
let r = ui.menu_button(app.ui.language.tr(top), |ui| {
ui.set_min_width(260.0);
if mine.is_empty() {
ui.add_enabled(false, egui::Button::new(tl!("(empty)")));
}
menu_level(ui, &mine, 1, &mut clicked, &mut app.auto, &labels);
});
app.auto.add(&format!("menu.{top}"), r.response.rect, top);
titles.push(r.response);
}
switch_on_hover(ui.ctx(), &titles);
});
if let Some(id) = clicked {
let _ = invoke(app, &ctx, &id, json!({}));
}
}
/// Like a native menu bar (Windows, macOS): while one top-level menu is open, hovering another
/// top-level title opens that menu instead (#1500). egui's `MenuBar` only switches on click.
fn switch_on_hover(ctx: &egui::Context, titles: &[egui::Response]) {
let ids: Vec<egui::Id> = titles.iter().map(egui::Popup::default_response_id).collect();
let Some(open) = ids.iter().position(|id| egui::Popup::is_id_open(ctx, *id)) else { return };
// `hovered` is false where a popup covers the title (a tall submenu pushed up over the
// bar), so the pointer inside an open menu never switches it.
if let Some(i) = titles.iter().position(egui::Response::hovered)
&& i != open
{
egui::Popup::open_id(ctx, ids[i]);
ctx.request_repaint();
}
}
/// One menu level: items whose path ends here, and a submenu (at its first item's position) for
/// each deeper path segment, recursively (e.g. Clip ▸ Video Options ▸ Time Interpolation).
fn menu_level(
ui: &mut egui::Ui,
items: &[&MenuItem],
depth: usize,
clicked: &mut Option<String>,
auto: &mut crate::automation::Registry,
labels: &filmcraft_engine::settings::LabelPrefs,
) {
let mut subs: Vec<&str> = Vec::new();
for it in items {
if let Some(sub) = it.path.get(depth).map(String::as_str) {
if subs.contains(&sub) {
continue;
}
subs.push(sub);
let inner: Vec<&MenuItem> = items.iter().copied().filter(|x| x.path.get(depth).map(String::as_str) == Some(sub)).collect();
let language = ui.ctx().data(|d| d.get_temp::<crate::i18n::Language>(egui::Id::unique("interface-language"))).unwrap_or_default();
ui.menu_button(language.tr(sub), |ui| {
ui.set_min_width(220.0);
menu_level(ui, &inner, depth + 1, clicked, auto, labels);
});
} else if menu_entry(ui, it, auto, labels) {
*clicked = Some(it.id.clone());
ui.close();
}
}
}
/// Only Edit ▸ Label choices get a swatch. Other commands keep their usual menu layout.
fn label_menu_swatch(id: &str, labels: &filmcraft_engine::settings::LabelPrefs) -> Option<egui::Color32> {
let label = id.strip_prefix("edit.label.").and_then(filmcraft_project::Label::from_name)?;
Some(label_color(labels, label))
}
/// The user's colour for label `l` (Preferences ▸ Labels), else the built-in one.
pub fn label_color(labels: &filmcraft_engine::settings::LabelPrefs, l: filmcraft_project::Label) -> egui::Color32 {
let [r, g, b] = labels.rgb(l);
egui::Color32::from_rgb(r, g, b)
}
/// The left gutter of a menu row that can carry a checkmark or a label swatch: the label is pushed
/// right by these spaces and the mark is painted in the room they leave, centred 10 points from the
/// row's left edge. The two agree at the default font and size; one constant keeps every gutter
/// (menu bar, right-click menus) the same width.
const GUTTER: &str = " ";
/// A label choice in a right-click Label menu (Project panel items, Timeline clips; #786), drawn
/// as Edit ▸ Label draws it: the user's name for the label with a swatch of its colour in the
/// left gutter. Registered for automation as `<prefix>.<Label>` (the label's built-in name).
pub fn label_choice(
ui: &mut egui::Ui,
auto: &mut crate::automation::Registry,
labels: &filmcraft_engine::settings::LabelPrefs,
l: filmcraft_project::Label,
prefix: &str,
) -> egui::Response {
let name = labels.name(l);
let text = crate::i18n::t(&name);
let r = ui.button(format!("{GUTTER}{text}"));
auto.add(&format!("{prefix}.{}", l.name()), r.rect, text);
paint_swatch(ui, r.rect, label_color(labels, l));
r
}
/// A checkmark in the left gutter of the menu row `row`.
fn paint_check(ui: &egui::Ui, row: egui::Rect) {
let c = row.left_center() + egui::vec2(10.0, 0.0);
let st = egui::Stroke::new(1.5, ui.visuals().text_color());
ui.painter().line_segment([c + egui::vec2(-4.0, 0.0), c + egui::vec2(-1.0, 3.0)], st);
ui.painter().line_segment([c + egui::vec2(-1.0, 3.0), c + egui::vec2(4.5, -3.5)], st);
}
/// A label swatch in the left gutter of the menu row `row`.
fn paint_swatch(ui: &egui::Ui, row: egui::Rect, color: egui::Color32) {
let rect = egui::Rect::from_center_size(row.left_center() + egui::vec2(10.0, 0.0), egui::vec2(12.0, 12.0));
ui.painter().rect_filled(rect, 2.0, color);
ui.painter().rect_stroke(rect, 2.0, egui::Stroke::new(1.0, ui.visuals().widgets.inactive.bg_stroke.color), egui::StrokeKind::Inside);
}
/// Where a menu item's hover hint opens, from the point the pointer entered the item: +x right,
/// +y down, in points. The hint's top-left corner goes there, so 0, 18 puts it just below the
/// arrow cursor, lined up with its tip.
const HINT_NUDGE: egui::Vec2 = egui::vec2(0.0, 18.0);
/// A hover hint that opens like a menu: below the pointer, where the hover began, and stays put
/// while the pointer moves within the same item.
fn hint_at_pointer(ui: &egui::Ui, r: &egui::Response, hint: String) {
let key = r.id.with("hint-at");
let Some(pointer) = r.hover_pos().filter(|_| r.hovered()) else {
ui.data_mut(|d| d.remove::<egui::Pos2>(key));
return;
};
let at = ui.data_mut(|d| *d.get_temp_mut_or_insert_with(key, || pointer));
let mut tip = egui::Tooltip::for_enabled(r);
tip.popup = tip.popup.at_position(at + HINT_NUDGE).align(egui::RectAlign::BOTTOM_START).gap(0.0);
tip.show(|ui| {
ui.set_max_width(ui.spacing().tooltip_width);
ui.label(hint);
});
}
fn menu_entry(ui: &mut egui::Ui, it: &MenuItem, auto: &mut crate::automation::Registry, labels: &filmcraft_engine::settings::LabelPrefs) -> bool {
// Checkmarks and label swatches each occupy the same left gutter.
let swatch = label_menu_swatch(&it.id, labels);
let label = if it.checked.is_some() || swatch.is_some() { format!("{GUTTER}{}", it.label) } else { it.label.clone() };
let mut b = egui::Button::new(label);
if let Some(s) = &it.shortcut {
b = b.shortcut_text(shortcut_text(s));
}
let r = ui.add_enabled(it.enabled, b);
// Help items show the address they open, or say what they do inside FilmCraft
if let Some(hint) = crate::links::menu_hint(&it.id) {
hint_at_pointer(ui, &r, hint);
}
auto.add(&format!("menu.{}", it.id), r.rect, &it.label);
if let Some(color) = swatch {
paint_swatch(ui, r.rect, color);
}
if it.checked == Some(true) {
paint_check(ui, r.rect);
}
r.clicked()
}
/// A row of a right-click menu with the menu bar's checkmark gutter, so it lines up with the
/// checkable rows around it. `checked` is as in [`MenuItem::checked`]: `None` is a command,
/// reported to accessibility as a plain button; `Some` is a checkable row, reported with its state
/// and showing the checkmark when true.
pub fn checked_entry(ui: &mut egui::Ui, label: &str, enabled: bool, checked: Option<bool>) -> egui::Response {
let r = ui.add_enabled(enabled, egui::Button::new(format!("{GUTTER}{label}")));
r.widget_info(|| match checked {
Some(checked) => egui::WidgetInfo::selected(egui::Role::Button, enabled, checked, label),
None => egui::WidgetInfo::labeled(egui::Role::Button, enabled, label),
});
if checked == Some(true) {
paint_check(ui, r.rect);
}
r
}
#[cfg(test)]
mod parse_shortcut_tests {
use super::{chord_held, label_menu_swatch, parse_shortcut};
#[test]
fn edit_label_menu_swatch_uses_current_custom_palette() {
use filmcraft_engine::settings::{LabelPrefs, label_id};
use filmcraft_project::Label;
let mut labels = LabelPrefs::default();
let label = Label::Iris;
let key = label_id(label);
labels.colors.get_mut(&key).unwrap().color = "#12ab34".into();
assert_eq!(label_menu_swatch("edit.label.Iris", &labels), Some(egui::Color32::from_rgb(0x12, 0xab, 0x34)));
assert_eq!(label_menu_swatch("edit.clear", &labels), None);
assert_eq!(label_menu_swatch("edit.label.invalid", &labels), None);
}
/// Off macOS `Ctrl` and `Cmd` are the same key, so either spelling parses to the modifiers a
/// physical Ctrl press carries and matches a binding written the other way (#245).
#[test]
fn ctrl_and_cmd_are_one_key_off_macos() {
let (ctrl, k) = parse_shortcut("Ctrl+Z").unwrap();
let (cmd, k2) = parse_shortcut("Cmd+Z").unwrap();
assert_eq!((k, k2), (egui::Key::Z, egui::Key::Z));
if cfg!(target_os = "macos") {
assert_eq!((ctrl.ctrl, ctrl.command), (true, false));
assert_eq!((cmd.ctrl, cmd.command), (false, true));
assert!(!chord_held(ctrl, k, cmd) && !chord_held(cmd, k, ctrl), "two different keys on a Mac");
} else {
assert_eq!((ctrl.ctrl, ctrl.command), (false, true));
assert_eq!(ctrl, cmd, "one key off a Mac");
// what egui-winit reports for the physical key, and a bare `command` as tests send it
let physical = egui::Modifiers { ctrl: true, command: true, ..Default::default() };
assert!(chord_held(physical, k, cmd) && chord_held(physical, k, ctrl));
assert!(chord_held(egui::Modifiers::COMMAND, k, ctrl));
}
assert!(!chord_held(parse_shortcut("Z").unwrap().0, k, cmd), "a bare key never stands in for the chord");
assert!(!chord_held(cmd, k, egui::Modifiers::NONE), "nor the chord for a bare key");
let (m, k) = parse_shortcut("Cmd+Shift+K").unwrap();
assert!(m.command && m.shift && !m.alt && k == egui::Key::K);
assert_eq!(parse_shortcut("+").map(|(_, k)| k), Some(egui::Key::Plus));
}
}
#[cfg(test)]
mod chord_held_tests {
use super::chord_held_on;
use egui::{Key, Modifiers};
/// What egui-winit reports on a Mac for Cmd (`mac_cmd` and `command`) and Control (`ctrl`).
const MAC_CMD: Modifiers = Modifiers { mac_cmd: true, command: true, ..Modifiers::NONE };
/// What egui-winit reports off a Mac for Ctrl: one key, both flags.
const PC_CTRL: Modifiers = Modifiers { ctrl: true, command: true, ..Modifiers::NONE };
/// The bug: Ctrl+Opt+Cmd+N ran File ▸ New ▸ Project (Cmd+Opt+N), because egui ignores an extra
/// Control on a Mac even in `matches_exact`.
#[test]
fn an_extra_ctrl_on_a_mac_is_not_the_cmd_chord() {
let new_project = Modifiers::COMMAND | Modifiers::ALT;
assert!(chord_held_on(true, MAC_CMD | Modifiers::ALT, Key::N, new_project), "Cmd+Opt+N still runs it");
assert!(!chord_held_on(true, MAC_CMD | Modifiers::ALT | Modifiers::CTRL, Key::N, new_project));
assert!(!chord_held_on(true, MAC_CMD | Modifiers::CTRL, Key::N, Modifiers::COMMAND), "Ctrl+Cmd+N is not Cmd+N");
assert!(!chord_held_on(true, MAC_CMD, Key::K, Modifiers::CTRL), "Cmd is not Control on a Mac");
assert!(chord_held_on(true, Modifiers::CTRL, Key::K, Modifiers::CTRL));
assert!(chord_held_on(true, MAC_CMD | Modifiers::CTRL, Key::K, Modifiers::COMMAND | Modifiers::CTRL));
}
/// An extra Shift or Alt on a letter or named key is a different chord, on every OS.
#[test]
fn an_extra_shift_or_alt_on_a_letter_or_named_key_is_another_chord() {
for mac in [true, false] {
let cmd = if mac { MAC_CMD } else { PC_CTRL };
assert!(!chord_held_on(mac, cmd | Modifiers::SHIFT, Key::N, Modifiers::COMMAND), "Cmd+Shift+N is not Cmd+N");
assert!(!chord_held_on(mac, cmd | Modifiers::ALT | Modifiers::SHIFT, Key::N, Modifiers::COMMAND | Modifiers::ALT));
assert!(!chord_held_on(mac, Modifiers::ALT, Key::I, Modifiers::NONE), "Alt+I is not I");
assert!(!chord_held_on(mac, Modifiers::SHIFT, Key::Delete, Modifiers::NONE), "Shift+Delete is not Delete (#680)");
assert!(!chord_held_on(mac, Modifiers::ALT, Key::ArrowLeft, Modifiers::NONE));
assert!(!chord_held_on(mac, Modifiers::SHIFT, Key::L, Modifiers::NONE), "Shift+L is its own shuttle key");
assert!(chord_held_on(mac, Modifiers::SHIFT, Key::L, Modifiers::SHIFT));
assert!(!chord_held_on(mac, Modifiers::NONE, Key::L, Modifiers::SHIFT), "a chord's Shift is required");
assert!(chord_held_on(mac, Modifiers::NONE, Key::Space, Modifiers::NONE));
}
}
/// Off a Mac Ctrl and Cmd are one key: a Ctrl press (both flags), a bare `command` and a bare
/// `ctrl` all match a `Cmd+…`/`Ctrl+…` binding (#245).
#[test]
fn ctrl_and_cmd_stay_one_key_off_a_mac() {
for held in [PC_CTRL, Modifiers::COMMAND, Modifiers::CTRL] {
assert!(chord_held_on(false, held, Key::Z, Modifiers::COMMAND), "{held:?}");
assert!(chord_held_on(false, held, Key::Z, Modifiers::CTRL), "{held:?}");
assert!(!chord_held_on(false, held, Key::Z, Modifiers::NONE), "{held:?} is not a bare Z");
}
assert!(!chord_held_on(false, Modifiers::NONE, Key::Z, Modifiers::COMMAND));
}
/// Punctuation and digits keep egui's allowance for the Shift or Alt a layout needs to type
/// them: Ctrl+Shift+= arrives as Ctrl+Plus (#2253), Shift+; as `:`.
#[test]
fn punctuation_and_digits_allow_the_shift_or_alt_a_layout_needs() {
for mac in [true, false] {
let cmd = if mac { MAC_CMD } else { PC_CTRL };
assert!(chord_held_on(mac, cmd | Modifiers::SHIFT, Key::Plus, Modifiers::COMMAND), "Ctrl+Shift+= is Ctrl+Plus");
assert!(chord_held_on(mac, Modifiers::SHIFT, Key::Colon, Modifiers::SHIFT));
assert!(chord_held_on(mac, Modifiers::ALT, Key::OpenBracket, Modifiers::NONE), "`[` is Option+5 on a German Mac");
assert!(chord_held_on(mac, Modifiers::SHIFT, Key::Num1, Modifiers::NONE), "digits need Shift on AZERTY");
assert!(!chord_held_on(mac, Modifiers::NONE, Key::Colon, Modifiers::SHIFT), "a chord's Shift is still required");
}
// ...but never an extra Ctrl or Cmd
assert!(!chord_held_on(true, MAC_CMD | Modifiers::CTRL, Key::Plus, Modifiers::COMMAND));
assert!(!chord_held_on(false, PC_CTRL, Key::Plus, Modifiers::NONE));
}
}
#[cfg(test)]
mod tests {
use super::*;
/// A clipboard image is premultiplied inside `Color32`. The PNG writer expects straight
/// alpha, so a semi-transparent pixel must come back with its colour, not darkened.
#[test]
fn pasted_images_keep_straight_alpha_colour() {
// Red at 50% opacity: premultiplied storage is about (128, 0, 0, 128).
let image = egui::ColorImage::from_rgba_unmultiplied([1, 1], &[255, 0, 0, 128]);
let media = paste_image_media(&image).expect("a 1×1 clipboard image");
let crate::ClipboardMedia::Image { width, height, rgba } = media else { panic!("not an image") };
assert_eq!((width, height), (1, 1));
assert_eq!(rgba, vec![255, 0, 0, 128], "straight RGBA, not the premultiplied bytes");
let png = encode_png(width, height, rgba).expect("a PNG");
let decoded = image::load_from_memory(&png).expect("decode the pasted PNG").to_rgba8();
assert_eq!(decoded.get_pixel(0, 0).0, [255, 0, 0, 128]);
}
/// An image whose RGBA size exceeds the 128 MiB cap is rejected before any RGBA copy is allocated.
#[test]
fn oversized_clipboard_images_are_rejected_before_allocating_rgba() {
let side = 100_000;
let image = egui::ColorImage { size: [side, side], source_size: egui::vec2(100_000.0, 100_000.0), pixels: Vec::new() };
let Err(error) = paste_image_media(&image) else { panic!("an oversized clipboard image was accepted") };
assert!(error.contains("too large"), "{error}");
}
/// What `cargo xtask parity` measures: every item sits under a top-level menu, and Window ▸
/// Workspaces lists the built-in workspaces before the layout commands, none checked.
#[test]
fn default_menu_items_are_the_shipped_menu_bar() {
let items = default_menu_items();
assert!(items.len() > 300, "{} items", items.len());
assert!(items.iter().all(|it| it.path.first().is_some_and(|top| MENUS.contains(&top.as_str()))));
let ws: Vec<&MenuItem> = items.iter().filter(|it| it.path == ["Window", "Workspaces"]).collect();
for name in crate::dock::WORKSPACES {
assert!(ws.iter().any(|it| it.label == name && it.checked.is_none()), "no workspace {name}");
}
let editing = ws.iter().position(|it| it.label == "Editing");
let reset = ws.iter().position(|it| it.id == "window.workspace.reset");
assert!(matches!((editing, reset), (Some(e), Some(r)) if e < r), "workspaces come before the layout commands");
assert!(items.iter().any(|it| it.id == "file.save" && it.path == ["File"]));
}
/// #1500: like a native menu bar, while one menu is open hovering another title opens it.
#[test]
fn hovering_another_title_switches_the_open_menu() {
use egui_kittest::{Harness, kittest::Queryable};
let app = FilmcraftApp::new(filmcraft_engine::Session::default());
let mut harness = Harness::builder().with_size(egui::vec2(1200.0, 700.0)).build_ui_state(|ui, app| menu_bar(app, ui), app);
// the app's fonts, which draw the macOS shortcut symbols (⇧⌘S) egui's default fonts lack
crate::theme::install(&harness.ctx, &harness.state().tokens);
harness.run_steps(3);
// Nothing open: hovering a title does not open it.
harness.get_by_label("Edit").hover();
harness.run_steps(3);
assert!(harness.query_by_label_contains("Save All").is_none() && harness.query_by_label_contains("Undo").is_none());
harness.get_by_label("File").click();
harness.run_steps(3);
assert!(harness.query_by_label_contains("Save All").is_some(), "File menu did not open");
harness.get_by_label("Edit").hover();
harness.run_steps(4);
assert!(harness.query_by_label_contains("Save All").is_none(), "File menu should close");
assert!(harness.query_by_label_contains("Undo").is_some(), "Edit menu should open on hover");
}
}
No file is open. Choose one in the tree, or press Ctrl+O (⌘O on a Mac) to find one by name.
Syntax highlighting could not load. The unmodified read-only text remains available above.