FilmCraft: An open-source, nonlinear video editor written by scratch in pure Rust
#695: Timeline markers can be picked and cleared
vangel76c13f2aeddb7b
diff --git a/crates/ui-egui/src/panels/timeline.rs b/crates/ui-egui/src/panels/timeline.rsindex d59978393..7fc8f8b5f 100644--- a/crates/ui-egui/src/panels/timeline.rs+++ b/crates/ui-egui/src/panels/timeline.rs@@ -22,7 +22,7 @@ use crate::icons::{self, Icon};use crate::state::{TimelineView, Tool};use crate::theme::Tokens;-use super::timeline_hit::{EdgeKind, Grab, edge_geometry, grab_at};+use super::timeline_hit::{EdgeKind, Grab, edge_geometry, grab_at, marker_at, marker_rect};pub use super::timeline_hit::{Hit, ROLL_PX, hit, selection_trim_kind};const TOP_H: f32 = 58.0; // timecode + toolbar (left) / ruler (right)@@ -1304,7 +1304,7 @@ fn draw_top(app: &mut FilmcraftApp, ui: &mut egui::Ui, rect: Rect, seq: &Sequencclip.rect_filled(Rect::from_min_max(pos2(x, marker_y), pos2(layout.x_of(m.start + m.duration), marker_y + 8.0)), 0.0, c.gamma_multiply(0.6));}clip.add(egui::Shape::convex_polygon(shape, c, Stroke::NONE));- let mr = Rect::from_center_size(pos2(x, marker_y + 6.0), vec2(10.0, 13.0));+ let mr = marker_rect(layout, m.start);app.auto.add(&format!("timeline.marker.{}", m.id.0), mr, &m.name);if ui.rect_contains_pointer(mr) && !m.name.is_empty() {egui::Tooltip::always_open(ui.ctx().clone(), ui.layer_id(), egui::Id::new(("mk", m.id.0)), egui::PopupAnchor::Pointer).show(|ui| {@@ -1578,6 +1578,22 @@ fn shift_track(seq: &Sequence, tid: TrackId, delta: i32) -> Option<TrackId> {/// The clip context menu: groups (separated by rules) of (label, command id). Entries marked `…`/// open their dialog through `menus::invoke`, like the same item in the Clip menu.+/// The menu of a right-clicked Timeline marker (automation ids `timeline.markerMenu.<command>`);+/// the marker is under the playhead, where Clear Selected Marker looks for it (#695).+fn marker_menu(app: &mut FilmcraftApp, ctx: &egui::Context, ui: &mut egui::Ui) {+ for (label, cmd) in [("Clear Selected Marker", "markers.clearCurrent"), ("Clear Markers", "markers.clearAll")] {+ let label = crate::i18n::t(label);+ let r = ui.button(label);+ app.auto.add(&format!("timeline.markerMenu.{cmd}"), r.rect, label);+ if r.clicked() {+ if let Err(e) = crate::menus::invoke(app, ctx, cmd, json!({})) {+ app.ui.status = e;+ }+ ui.close();+ }+ }+}+pub(crate) const CLIP_MENU: &[&[(&str, &str)]] = &[&[("Cut", "edit.cut"),@@ -2015,6 +2031,10 @@ fn interact(app: &mut FilmcraftApp, ui: &mut egui::Ui, seq: &Sequence, layout: &if mods.shift || app.session.prefs.timeline.snap_playhead {tt = snap_playhead(app, seq, layout, tt);}+ // a press on a marker selects it: the playhead goes exactly onto it (#695)+ if let Some(m) = marker_at(seq, layout, p) {+ tt = m.max(Tick::ZERO);+ }app.session.set_playhead(tt);}if resp.drag_started() {@@ -2199,6 +2219,17 @@ fn interact(app: &mut FilmcraftApp, ui: &mut egui::Ui, seq: &Sequence, layout: &let edit_point_menu_id = egui::Id::new("timeline.editPointMenu.target");// the clip right-clicked, remembered while its menu is open: Unlink leaves only it selectedlet menu_clip_id = egui::Id::new("timeline.clipMenu.clip");+ // on a marker of the ruler, the marker menu instead (right-clicking a marker selects it first)+ let marker_menu_id = egui::Id::new("timeline.markerMenu.open");+ if resp.secondary_clicked()+ && let Some(p) = resp.interact_pointer_pos()+ {+ let marker = marker_at(seq, layout, p);+ if let Some(m) = marker {+ app.session.set_playhead(m.max(Tick::ZERO));+ }+ ctx.data_mut(|d| d.insert_temp(marker_menu_id, marker.is_some()));+ }if resp.secondary_clicked()&& let Some(p) = resp.interact_pointer_pos()&& let Hit::Clip { clip, edge, track } = hit(seq, layout, p)@@ -2226,6 +2257,10 @@ fn interact(app: &mut FilmcraftApp, ui: &mut egui::Ui, seq: &Sequence, layout: &}resp.context_menu(|ui| {ui.set_min_width(220.0);+ if ctx.data(|d| d.get_temp::<bool>(marker_menu_id)).unwrap_or(false) {+ marker_menu(app, &ctx, ui);+ return;+ }if let Some(target) = ctx.data(|d| d.get_temp::<Option<EditPointTarget>>(edit_point_menu_id)).flatten() {edit_point_menu(app, &ctx, ui, seq, target);return;diff --git a/crates/ui-egui/src/panels/timeline_hit.rs b/crates/ui-egui/src/panels/timeline_hit.rsindex 16dd5f712..9e311f50d 100644--- a/crates/ui-egui/src/panels/timeline_hit.rs+++ b/crates/ui-egui/src/panels/timeline_hit.rs@@ -5,9 +5,10 @@use std::cmp::Ordering;-use egui::{Modifiers, Pos2};+use egui::{Modifiers, Pos2, Rect, pos2, vec2};use filmcraft_edit::Edge;use filmcraft_project::{ClipId, Sequence, Track, TrackId, TransitionId};+use filmcraft_time::Tick;use super::timeline::Layout;use crate::state::Tool;@@ -68,6 +69,18 @@ pub enum Hit {None,}+/// Where the Timeline ruler draws (and hit-tests) the pentagon of a marker at `start`.+pub fn marker_rect(layout: &Layout, start: Tick) -> Rect {+ Rect::from_center_size(pos2(layout.x_of(start), layout.ruler.min.y + 8.0), vec2(10.0, 13.0))+}++/// The start of the marker whose pentagon is under `pos` on the ruler (the topmost, i.e. last+/// drawn, when several overlap). A press there puts the playhead on the marker, so Markers ▸ Clear+/// Selected Marker removes it (#695).+pub fn marker_at(seq: &Sequence, layout: &Layout, pos: Pos2) -> Option<Tick> {+ seq.markers.iter().rev().map(|m| m.start).find(|t| marker_rect(layout, *t).contains(pos))+}+pub fn hit(seq: &Sequence, layout: &Layout, pos: Pos2) -> Hit {if layout.ruler.contains(pos) {return Hit::Ruler;@@ -404,6 +417,26 @@ mod tests {}}+ #[test]+ fn a_press_on_a_marker_lands_on_its_start() {+ let (mut seq, l, _) = fixture(clips());+ let off = Tick::from_seconds_f64(3.017);+ seq.markers.push(filmcraft_project::Marker {+ id: filmcraft_project::MarkerId(9),+ start: off,+ duration: Tick::ZERO,+ name: String::new(),+ comment: String::new(),+ kind: filmcraft_project::MarkerKind::Comment,+ color: Label::Green,+ });+ // 3 px beside the pentagon's tip, where the nearest frame is not the marker's time+ assert_eq!(marker_at(&seq, &l, pos2(304.7, 10.0)), Some(off));+ assert_eq!(marker_at(&seq, &l, pos2(310.0, 10.0)), None);+ assert_eq!(marker_at(&seq, &l, pos2(301.7, 30.0)), None);+ assert_eq!(marker_at(&seq, &l, pos2(f32::NAN, 10.0)), None);+ }+use egui::Modifiers;use crate::state::Tool;diff --git a/crates/ui-egui/tests/sequence_ui.rs b/crates/ui-egui/tests/sequence_ui.rsindex 0a16547f8..460b7929a 100644--- a/crates/ui-egui/tests/sequence_ui.rs+++ b/crates/ui-egui/tests/sequence_ui.rs@@ -321,6 +321,32 @@ fn markers_panel_colour_filter() {assert_eq!(d.ids("markers.row.").len(), 2);}+/// #695: a marker on the Timeline could not be cleared: with snapping off a click on it left the+/// playhead on the frame under the pointer, not on the marker Clear Selected Marker looks for, and+/// right-clicking it opened the clip menu.+#[test]+fn a_clicked_timeline_marker_can_be_cleared() {+ let mut d = Driver::demo();+ d.exec("markers.clearAll", json!({}));+ let first = d.exec("markers.add", json!({"frame": 30}))["marker"].as_u64().unwrap();+ let second = d.exec("markers.add", json!({"frame": 90}))["marker"].as_u64().unwrap();+ d.exec("sequence.snap", json!({"on": false}));+ d.exec("playhead.set", json!({"frame": 0}));+ // zoomed out so that a few px beside a marker is another frame+ let v = &mut d.app().ui.timeline;+ (v.pps, v.target_pps, v.scroll, v.target_scroll) = (60.0, 60.0, 0.0, 0.0);+ d.frames(4);+ let markers = |d: &mut Driver| d.app().session.active_sequence().unwrap().markers.iter().map(|m| m.id.0).collect::<Vec<_>>();+ d.ok("ui.click", json!({"id": format!("timeline.marker.{first}"), "fx": 0.95}));+ d.frames(2);+ d.exec("markers.clearCurrent", json!({}));+ assert_eq!(markers(&mut d), vec![second], "the clicked marker is cleared");+ d.ok("ui.click", json!({"id": format!("timeline.marker.{second}"), "fx": 0.95, "button": "right"}));+ d.frames(3);+ d.click("timeline.markerMenu.markers.clearCurrent");+ assert!(markers(&mut d).is_empty(), "cleared from its context menu");+}+#[test]fn shift_semicolon_goes_to_the_next_gap() {let mut d = Driver::demo();