Skip to content

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

dock: switch tabs with the mouse wheel over a tab strip

ReReddo76e7bfb8c919
diff --git a/crates/ui-egui/src/dock.rs b/crates/ui-egui/src/dock.rs
index 23b4b6cc1..bd6e73a2f 100644
--- a/crates/ui-egui/src/dock.rs
+++ b/crates/ui-egui/src/dock.rs
@@ -687,6 +687,22 @@ pub fn draw_group_chrome_details(
list_opened = !open;
}
}
+ // The wheel over the strip steps through the tabs: down = next, up = previous, one tab
+ // per wheel notch (trackpad scrolling adds up to a notch first), stopping at the ends.
+ let wheel_id = egui::Id::new(("tab-wheel", g.path.clone()));
+ if ui.rect_contains_pointer(strip) && !tabs.is_empty() {
+ const NOTCH: f32 = 40.0;
+ let total = ui.ctx().data(|d| d.get_temp::<f32>(wheel_id)).unwrap_or(0.0) + crate::panels::timeline::wheel_input(ui.ctx()).delta.y;
+ let steps = (total / NOTCH).trunc();
+ ui.ctx().data_mut(|d| d.insert_temp(wheel_id, total - steps * NOTCH));
+ let to = (active_at as i64).saturating_sub(steps as i64).clamp(0, tabs.len() as i64 - 1) as usize;
+ if let Some(tab) = tabs.get(to).filter(|_| to != active_at) {
+ activate(tab, &mut actions);
+ actions.push(DockAction::Focus(tab.panel));
+ }
+ } else {
+ ui.ctx().data_mut(|d| d.remove::<f32>(wheel_id));
+ }
let mut x = strip.min.x + 12.0;
// sequence tabs as drawn (for dragging one past its neighbours), and the one being dragged
let mut seq_rects: Vec<(u64, Rect)> = Vec::new();
diff --git a/crates/ui-egui/tests/panels_ui.rs b/crates/ui-egui/tests/panels_ui.rs
index 8f1b10136..ed2cf4584 100644
--- a/crates/ui-egui/tests/panels_ui.rs
+++ b/crates/ui-egui/tests/panels_ui.rs
@@ -209,6 +209,63 @@ fn panel_menu_maximize_restores_and_an_empty_dock_reopens() {
assert!(d.has("panel.tab.Program") && d.has("panel.tab.Timeline"));
}
+/// The wheel over a tab strip switches that group's tab: down = next, up = previous, one tab per
+/// notch, smaller trackpad steps adding up to a notch, stopping at the ends. Over the panel's
+/// content the wheel leaves the tabs alone.
+#[test]
+fn wheel_over_a_tab_strip_switches_tabs() {
+ use filmcraft_ui_egui::dock::{DockNode, PanelKind};
+ fn shown(d: &mut Driver) -> PanelKind {
+ fn find(n: &DockNode) -> Option<PanelKind> {
+ match n {
+ DockNode::Tabs { panels, active } if panels.contains(&PanelKind::Project) => panels.get(*active).copied(),
+ DockNode::Tabs { .. } => None,
+ DockNode::Split { a, b, .. } => find(a).or_else(|| find(b)),
+ }
+ }
+ find(&d.app().ui.dock).unwrap()
+ }
+ fn wheel(d: &mut Driver, dy: f64) {
+ let id = format!("panel.tab.{}", shown(d).id());
+ d.ok("ui.scroll", json!({"id": id, "dy": dy}));
+ d.frames(2);
+ }
+ let mut d = Driver::demo();
+ assert_eq!(shown(&mut d), PanelKind::Project);
+ wheel(&mut d, -40.0);
+ assert_eq!(shown(&mut d), PanelKind::MediaBrowser);
+ wheel(&mut d, 40.0);
+ assert_eq!(shown(&mut d), PanelKind::Project);
+ wheel(&mut d, 120.0);
+ assert_eq!(shown(&mut d), PanelKind::Project, "no wrap past the first tab");
+
+ // a trackpad swipe in small steps moves one tab once the steps add up to a notch
+ let r = d.ok("ui.elements", json!({"prefix": "panel.tab.Project"}))[0]["rect"].clone();
+ let at = egui::pos2((r[0].as_f64().unwrap() + r[2].as_f64().unwrap() / 2.0) as f32, (r[1].as_f64().unwrap() + r[3].as_f64().unwrap() / 2.0) as f32);
+ d.harness.input_mut().events.push(egui::Event::PointerMoved(at));
+ d.frames(1);
+ for step in 0..5 {
+ d.harness.input_mut().events.push(egui::Event::MouseWheel {
+ unit: egui::MouseWheelUnit::Point,
+ delta: egui::vec2(0.0, -10.0),
+ modifiers: egui::Modifiers::NONE,
+ phase: egui::TouchPhase::Move,
+ });
+ d.frames(1);
+ let expect = if step < 3 { PanelKind::Project } else { PanelKind::MediaBrowser };
+ assert_eq!(shown(&mut d), expect, "after {} pt", (step + 1) * 10);
+ }
+
+ d.ok("ui.scroll", json!({"id": "panel.MediaBrowser", "dy": -40.0}));
+ d.frames(2);
+ assert_eq!(shown(&mut d), PanelKind::MediaBrowser, "the wheel over the content keeps the tab");
+
+ for _ in 0..10 {
+ wheel(&mut d, -40.0);
+ }
+ assert_eq!(shown(&mut d), PanelKind::History, "no wrap past the last tab");
+}
+
#[test]
fn lumetri_scopes_layouts_menu_and_footer() {
let mut d = Driver::demo();