Skip to content

FilmCraft: An open-source, nonlinear video editor written by scratch in pure Rust

NEST13: the panel menu closes on a click elsewhere and on Escape

justjohn12345cdd98367058e
diff --git a/crates/ui-egui/src/lib.rs b/crates/ui-egui/src/lib.rs
index 508ee965e..9421e7181 100644
--- a/crates/ui-egui/src/lib.rs
+++ b/crates/ui-egui/src/lib.rs
@@ -1313,7 +1313,12 @@ impl FilmcraftApp {
}
}
dock::DockAction::PanelMenu(p, pos) => {
- ui.ctx().data_mut(|d| d.insert_temp(egui::Id::new("panel-menu"), (p, pos)));
+ // (with the frame it opened in: the click that opens it is not a click elsewhere)
+ let frame = ui.ctx().cumulative_frame_nr();
+ ui.ctx().data_mut(|d| {
+ d.insert_temp(egui::Id::new("panel-menu"), (p, pos));
+ d.insert_temp(egui::Id::new("panel-menu-opened"), frame);
+ });
}
}
}
diff --git a/crates/ui-egui/src/panels/mod.rs b/crates/ui-egui/src/panels/mod.rs
index 1b7423a57..94a5e8b49 100644
--- a/crates/ui-egui/src/panels/mod.rs
+++ b/crates/ui-egui/src/panels/mod.rs
@@ -233,7 +233,10 @@ pub fn panel_menu_popup(app: &mut FilmcraftApp, ui: &mut egui::Ui) {
}
});
});
- if close || (area.response.clicked_elsewhere() && ui.input(|i| i.pointer.any_pressed())) {
+ // A click elsewhere or Escape closes the menu. The click that opened it is over the tab, not
+ // the menu, and must not close it again in the same frame.
+ let fresh = ui.ctx().data(|d| d.get_temp::<u64>(egui::Id::new("panel-menu-opened"))) == Some(ui.ctx().cumulative_frame_nr());
+ if close || (!fresh && area.response.clicked_elsewhere()) || ui.input(|i| i.key_pressed(egui::Key::Escape)) {
ui.ctx().data_mut(|d| d.remove::<(PanelKind, egui::Pos2)>(id));
}
}
diff --git a/crates/ui-egui/tests/timeline_tabs_ui.rs b/crates/ui-egui/tests/timeline_tabs_ui.rs
index d2c5be614..f2026eca5 100644
--- a/crates/ui-egui/tests/timeline_tabs_ui.rs
+++ b/crates/ui-egui/tests/timeline_tabs_ui.rs
@@ -175,6 +175,26 @@ fn a_right_click_on_a_tab_shows_it_and_opens_its_menu() {
d.click("panel.menu.Timeline.closeOthers");
assert_eq!((d.open(), d.active()), (vec![b], b));
assert!(!d.has("panel.menu.Timeline.closeOthers"), "the menu closes");
+ // the menu goes away on a click elsewhere, and on Escape
+ d.show(main);
+ d.ok("ui.click", json!({"id": format!("timeline.tab.{b}"), "button": "right"}));
+ d.frames(3);
+ assert!(d.has("panel.menu.Timeline.close"));
+ d.click("panel.Program");
+ assert!(!d.has("panel.menu.Timeline.close"), "a click elsewhere closes the menu");
+ d.ok("ui.click", json!({"id": format!("timeline.tab.{main}"), "button": "right"}));
+ d.frames(3);
+ assert!(d.has("panel.menu.Timeline.close"));
+ d.ok("ui.key", json!({"key": "Escape"}));
+ d.frames(3);
+ assert!(!d.has("panel.menu.Timeline.close"), "Escape closes the menu");
+ // a right-click on another tab while the menu is open moves the menu to that tab
+ d.ok("ui.click", json!({"id": format!("timeline.tab.{b}"), "button": "right"}));
+ d.frames(3);
+ d.ok("ui.click", json!({"id": format!("timeline.tab.{main}"), "button": "right"}));
+ d.frames(3);
+ assert!(d.has("panel.menu.Timeline.close") && d.active() == main);
+ d.click("panel.Program");
// Close Panel from the menu closes the tab it was opened on
d.show(main);
d.ok("ui.click", json!({"id": format!("timeline.tab.{b}"), "button": "right"}));