FilmCraft: An open-source, nonlinear video editor written by scratch in pure Rust
use super::{FilmcraftApp, menus};
#[cfg(test)]
use filmcraft_engine::Session;
#[cfg(test)]
use serde_json::json;
impl FilmcraftApp {
// ---------------------------------------------------------------- input
/// Sends the menu tree to the native menu bar when it changed, so its items show the same
/// labels, shortcuts and enablement as the in-window menus (Undo with nothing to undo is
/// disabled). The items a focused text field takes stay enabled while one has the keyboard.
/// Without a native menu bar this does nothing.
pub(super) fn sync_native_menu(&mut self, ctx: &egui::Context) {
if self.hooks.menu_changed.is_none() {
return;
}
let mut items = menus::menu_items(self);
if menus::text_field_window(ctx).is_some() {
for it in items.iter_mut().filter(|it| menus::edits_text(&it.id)) {
it.enabled = true;
}
}
if items == self.menu_sent {
return;
}
if let Some(hook) = self.hooks.menu_changed.as_mut() {
hook(&items);
}
self.menu_sent = items;
}
}
impl FilmcraftApp {
/// Drain native menu/control command ids, preserving the control-only Quit exception.
pub(super) fn logic_command_inbox(&mut self, ctx: &egui::Context) {
if let Some(rx) = self.command_inbox.take() {
while let Ok(id) = rx.try_recv() {
// control-only mode ignores native menu commands, but Quit stays available (#1943)
if self.control_input_only && id != "app.quit" {
continue;
}
if let Err(e) = menus::activate_native(self, ctx, &id) {
self.ui.status = e;
}
}
self.command_inbox = Some(rx);
}
}
}
#[cfg(test)]
mod native_menu_tests {
use super::*;
use std::cell::RefCell;
use std::rc::Rc;
fn enabled(items: &[menus::MenuItem], id: &str) -> Option<bool> {
items.iter().find(|it| it.id == id).map(|it| it.enabled)
}
/// The native menu bar (macOS) gets the menu tree whenever it changes, so Edit ▸ Undo and Redo
/// follow the history instead of staying enabled (#902).
#[test]
fn native_menu_follows_undo_and_redo_availability() {
let sent: Rc<RefCell<Vec<Vec<menus::MenuItem>>>> = Rc::default();
let mut app = FilmcraftApp::new(Session::default());
let sink = sent.clone();
app.hooks.menu_changed = Some(Box::new(move |items: &[menus::MenuItem]| sink.borrow_mut().push(items.to_vec())));
let ctx = egui::Context::default();
app.sync_native_menu(&ctx);
let last = |sent: &Rc<RefCell<Vec<Vec<menus::MenuItem>>>>, id: &str| sent.borrow().last().and_then(|items| enabled(items, id));
assert_eq!(last(&sent, "edit.undo"), Some(false), "nothing to undo in an empty project");
assert_eq!(last(&sent, "edit.redo"), Some(false), "nothing to redo in an empty project");
assert_eq!(last(&sent, "file.newProject"), Some(true));
app.sync_native_menu(&ctx);
assert_eq!(sent.borrow().len(), 1, "an unchanged menu is not sent again");
app.session.execute("file.newSequence", json!({"width":16,"height":16})).unwrap();
app.sync_native_menu(&ctx);
assert_eq!(last(&sent, "edit.undo"), Some(true), "an edit can be undone");
assert_eq!(last(&sent, "edit.redo"), Some(false));
app.session.execute("edit.undo", json!({})).unwrap();
app.sync_native_menu(&ctx);
assert_eq!(last(&sent, "edit.undo"), Some(false));
assert_eq!(last(&sent, "edit.redo"), Some(true), "the undone edit can be redone");
}
}