Skip to content

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

fix: マーカーの色変更と範囲帯の右クリックを修正 (#1143)

Ikki Nishihara9a3e7d38edb4
diff --git a/crates/ui-egui/src/panels/misc.rs b/crates/ui-egui/src/panels/misc.rs
index 63e494b84..0cb113880 100644
--- a/crates/ui-egui/src/panels/misc.rs
+++ b/crates/ui-egui/src/panels/misc.rs
@@ -57,6 +57,26 @@ pub const MARKER_FILTER: [filmcraft_project::Label; 7] = {
[Green, Rose, Purple, Mango, Yellow, Blue, Teal]
};
+/// A marker's Label menu. An explicit program target keeps panel focus and clip/project
+/// selections from routing colour changes to another item (#1143).
+pub(crate) fn marker_label_menu(app: &mut FilmcraftApp, ui: &mut egui::Ui, marker: &filmcraft_project::Marker, prefix: &str) {
+ let label = crate::i18n::t("Label");
+ let menu = ui.menu_button(label, |ui| {
+ for color in filmcraft_project::Label::ALL {
+ let name = crate::i18n::t(color.name());
+ let r = ui.selectable_label(color == marker.color, name);
+ app.auto.add_response(&format!("{prefix}.color.{}", color.name()), &r, name);
+ if r.clicked() {
+ if let Err(error) = app.session.execute("markers.edit", serde_json::json!({"target":"program","marker":marker.id.0,"color":color.name()})) {
+ app.ui.status = error.to_string();
+ }
+ ui.close();
+ }
+ }
+ });
+ app.auto.add_response(&format!("{prefix}.label"), &menu.response, label);
+}
+
/// Markers panel: a colour filter row (click a chip to show/hide that colour; automation ids
/// `markers.filter.<label>`), then the sequence markers (`markers.row.<id>`; click = go to it).
pub fn markers(app: &mut FilmcraftApp, ui: &mut egui::Ui, rect: Rect) {
@@ -113,6 +133,7 @@ pub fn markers(app: &mut FilmcraftApp, ui: &mut egui::Ui, rect: Rect) {
if resp.clicked() {
go = Some(m.start);
}
+ resp.context_menu(|ui| marker_label_menu(app, ui, m, "markers.menu"));
}
if seq.markers.is_empty() {
ui.label(egui::RichText::new(tl!("No markers. Press M to add one.")).color(t.text_faint));
diff --git a/crates/ui-egui/src/panels/timeline.rs b/crates/ui-egui/src/panels/timeline.rs
index d5766c0bb..e9b5ab8d1 100644
--- a/crates/ui-egui/src/panels/timeline.rs
+++ b/crates/ui-egui/src/panels/timeline.rs
@@ -13,7 +13,7 @@ use std::collections::HashMap;
use std::sync::{Arc, Mutex};
use egui::{Align2, Color32, CursorIcon, Pos2, Rect, Sense, Stroke, StrokeKind, pos2, vec2};
-use filmcraft_project::{ClipId, ItemId, Sequence, TrackId, TrackItem, TrackKind};
+use filmcraft_project::{ClipId, ItemId, MarkerId, Sequence, TrackId, TrackItem, TrackKind};
use filmcraft_time::{FrameRate, TICKS_PER_SECOND, Tick, TimeDisplay, format_time};
use serde_json::{Value, json};
@@ -22,7 +22,7 @@ use crate::icons::{self, Icon};
use crate::state::{ThumbnailMode, TimelineView, Tool};
use crate::theme::Tokens;
-use super::timeline_hit::{EdgeKind, Grab, edge_geometry, grab_at, marker_at, marker_rect};
+use super::timeline_hit::{EdgeKind, Grab, edge_geometry, grab_at, marker_at, marker_range_rect, marker_rect, marker_under_pointer};
pub use super::timeline_hit::{Hit, ROLL_PX, hit, selection_trim_kind};
const TOP_H: f32 = 58.0; // timecode + toolbar (left) / ruler (right)
@@ -1419,8 +1419,9 @@ fn draw_top(app: &mut FilmcraftApp, ui: &mut egui::Ui, rect: Rect, seq: &Sequenc
let c = Color32::from_rgb(c[0], c[1], c[2]);
let shape =
vec![pos2(x - 4.0, marker_y), pos2(x + 4.0, marker_y), pos2(x + 4.0, marker_y + 8.0), pos2(x, marker_y + 12.0), pos2(x - 4.0, marker_y + 8.0)];
- if m.duration > Tick::ZERO {
- clip.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));
+ if let Some(band) = marker_range_rect(layout, m).filter(|r| r.width() > 0.0) {
+ clip.rect_filled(band, 0.0, c.gamma_multiply(0.6));
+ app.auto.add(&format!("timeline.markerRange.{}", m.id.0), band, &m.name);
}
clip.add(egui::Shape::convex_polygon(shape, c, Stroke::NONE));
let mr = marker_rect(layout, m.start);
@@ -1714,13 +1715,19 @@ fn shift_track(seq: &Sequence, tid: TrackId, delta: i32) -> Option<TrackId> {
/// 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) {
+fn marker_menu(app: &mut FilmcraftApp, ctx: &egui::Context, ui: &mut egui::Ui, marker: MarkerId) {
+ let Some(marker) = app.session.active_sequence().and_then(|q| q.markers.iter().find(|m| m.id == marker)).cloned() else {
+ ui.close();
+ return;
+ };
+ super::misc::marker_label_menu(app, ui, &marker, "timeline.markerMenu");
+ ui.separator();
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!({})) {
+ if let Err(e) = crate::menus::invoke(app, ctx, cmd, json!({"target":"program"})) {
app.ui.status = e;
}
ui.close();
@@ -2410,15 +2417,15 @@ fn interact(app: &mut FilmcraftApp, ui: &mut egui::Ui, seq: &Sequence, layout: &
}
}
// 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");
+ let marker_menu_id = egui::Id::new("timeline.markerMenu.target");
if resp.secondary_clicked()
&& let Some(p) = resp.interact_pointer_pos()
{
- let marker = marker_at(seq, layout, p);
+ let marker = marker_under_pointer(seq, layout, p);
if let Some(m) = marker {
- app.session.set_playhead(m.max(Tick::ZERO));
+ app.session.set_playhead(m.start.max(Tick::ZERO));
}
- ctx.data_mut(|d| d.insert_temp(marker_menu_id, marker.is_some()));
+ ctx.data_mut(|d| d.insert_temp(marker_menu_id, marker.map(|m| m.id)));
}
if resp.secondary_clicked()
&& let Some(p) = resp.interact_pointer_pos()
@@ -2447,8 +2454,8 @@ 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);
+ if let Some(marker) = ctx.data(|d| d.get_temp::<Option<MarkerId>>(marker_menu_id)).flatten() {
+ marker_menu(app, &ctx, ui, marker);
return;
}
if let Some(target) = ctx.data(|d| d.get_temp::<Option<EditPointTarget>>(edit_point_menu_id)).flatten() {
@@ -2470,13 +2477,20 @@ fn interact(app: &mut FilmcraftApp, ui: &mut egui::Ui, seq: &Sequence, layout: &
}
for &(label, cmd) in *group {
if cmd == "edit.label" {
- ui.menu_button(crate::i18n::t(label), |ui| {
- for l in filmcraft_project::Label::ALL {
- if ui.button(crate::i18n::t(l.name())).clicked() {
- let _ = app.session.execute("edit.label", json!({"label": l.name()}));
- ui.close();
+ ui.add_enabled_ui(!picked.is_empty(), |ui| {
+ let menu = ui.menu_button(crate::i18n::t(label), |ui| {
+ for l in filmcraft_project::Label::ALL {
+ let r = ui.button(crate::i18n::t(l.name()));
+ app.auto.add_response(&format!("timeline.clipMenu.label.{}", l.name()), &r, crate::i18n::t(l.name()));
+ if r.clicked() {
+ if let Err(error) = app.session.execute("edit.label", json!({"label":l.name()})) {
+ app.ui.status = error.to_string();
+ }
+ ui.close();
+ }
}
- }
+ });
+ app.auto.add_response("timeline.clipMenu.edit.label", &menu.response, crate::i18n::t(label));
});
continue;
}
diff --git a/crates/ui-egui/src/panels/timeline_hit.rs b/crates/ui-egui/src/panels/timeline_hit.rs
index 9e311f50d..f94e9be92 100644
--- a/crates/ui-egui/src/panels/timeline_hit.rs
+++ b/crates/ui-egui/src/panels/timeline_hit.rs
@@ -74,11 +74,26 @@ 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).
+/// The visible duration band, shared by painting and hit testing (#1143).
+pub fn marker_range_rect(layout: &Layout, marker: &filmcraft_project::Marker) -> Option<Rect> {
+ (marker.duration > Tick::ZERO).then(|| {
+ let y = layout.ruler.min.y + 2.0;
+ Rect::from_min_max(pos2(layout.x_of(marker.start), y), pos2(layout.x_of(marker.start + marker.duration), y + 8.0)).intersect(layout.ruler)
+ })
+}
+
+/// The topmost marker under the pointer, including its range band. Preserve its id when opening
+/// a context menu: several markers can share a start, and the playhead can move while it is open.
+pub fn marker_under_pointer<'a>(seq: &'a Sequence, layout: &Layout, pos: Pos2) -> Option<&'a filmcraft_project::Marker> {
+ if !layout.ruler.contains(pos) {
+ return None;
+ }
+ seq.markers.iter().rev().find(|m| marker_rect(layout, m.start).contains(pos) || marker_range_rect(layout, m).is_some_and(|r| r.contains(pos)))
+}
+
+/// A press on a marker puts the playhead on its start for Clear Selected Marker (#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))
+ marker_under_pointer(seq, layout, pos).map(|m| m.start)
}
pub fn hit(seq: &Sequence, layout: &Layout, pos: Pos2) -> Hit {
@@ -437,6 +452,31 @@ mod tests {
assert_eq!(marker_at(&seq, &l, pos2(f32::NAN, 10.0)), None);
}
+ #[test]
+ fn range_bands_hit_the_last_drawn_marker_and_stay_on_the_ruler() {
+ let (mut seq, layout, _) = fixture(clips());
+ for id in [9, 10] {
+ seq.markers.push(filmcraft_project::Marker {
+ id: filmcraft_project::MarkerId(id),
+ start: Tick::from_seconds_f64(3.0),
+ duration: Tick::from_seconds_f64(2.0),
+ name: String::new(),
+ comment: String::new(),
+ kind: filmcraft_project::MarkerKind::Comment,
+ color: Label::Green,
+ });
+ }
+ let band = marker_range_rect(&layout, &seq.markers[1]).unwrap();
+ let at = band.center();
+ assert_eq!(marker_under_pointer(&seq, &layout, at).unwrap().id.0, 10);
+ assert_eq!(marker_at(&seq, &layout, at), Some(seq.markers[1].start));
+ assert!(marker_under_pointer(&seq, &layout, at + vec2(0.0, 20.0)).is_none());
+ assert!(marker_under_pointer(&seq, &layout, pos2(f32::NAN, at.y)).is_none());
+ seq.markers[1].duration = Tick(-1);
+ assert!(marker_range_rect(&layout, &seq.markers[1]).is_none());
+ assert_eq!(marker_under_pointer(&seq, &layout, at).unwrap().id.0, 9);
+ }
+
use egui::Modifiers;
use crate::state::Tool;
diff --git a/crates/ui-egui/tests/sequence_ui.rs b/crates/ui-egui/tests/sequence_ui.rs
index 460b7929a..dcf77bcc0 100644
--- a/crates/ui-egui/tests/sequence_ui.rs
+++ b/crates/ui-egui/tests/sequence_ui.rs
@@ -117,6 +117,22 @@ impl Driver {
img.save(dir.join(format!("{name}.png"))).unwrap();
}
+ fn right_marker(&mut self, marker: u64, range: bool) {
+ self.frames(3);
+ let id = format!("timeline.marker.{marker}");
+ let elements = self.ok("ui.elements", json!({"prefix":id}));
+ let rect = elements.as_array().unwrap().iter().find(|e| e["id"] == id).unwrap()["rect"].as_array().unwrap();
+ let mut x = rect[0].as_f64().unwrap() + rect[2].as_f64().unwrap() / 2.0;
+ let y = rect[1].as_f64().unwrap() + rect[3].as_f64().unwrap() / 2.0;
+ if range {
+ let app = self.app();
+ let m = app.session.active_sequence().unwrap().markers.iter().find(|m| m.id.0 == marker).unwrap();
+ x += m.duration.seconds() * app.ui.timeline.pps * 0.5;
+ }
+ self.ok("ui.click", json!({"x":x,"y":y,"button":"right"}));
+ self.frames(3);
+ }
+
fn ids(&mut self, prefix: &str) -> Vec<String> {
let v = self.ok("ui.elements", json!({"prefix": prefix}));
v.as_array().unwrap().iter().filter_map(|e| e["id"].as_str().map(str::to_string)).collect()
@@ -673,3 +689,111 @@ fn new_sequence_opens_its_dialog() {
assert!(d.ids("sequenceSettings.tab.color").iter().any(|i| i == "sequenceSettings.tab.color"));
d.click("sequenceSettings.cancel");
}
+
+#[test]
+fn timeline_marker_label_edits_only_the_clicked_marker_and_is_undoable() {
+ let mut d = Driver::demo();
+ d.exec("markers.clearAll", json!({}));
+ let target = d.exec("markers.add", json!({"frame":30,"name":"Review","comment":"Keep this note"}))["marker"].as_u64().unwrap();
+ d.exec("markers.add", json!({"frame":90,"name":"Other","color":"Rose"}));
+ let clip = d.app().session.active_sequence().unwrap().video_tracks[0].items[0].id;
+ d.exec("timeline.select", json!({"clips":[clip.0]}));
+ let before = d.app().session.active_sequence().unwrap().clone();
+ let undo = d.app().session.history.undo.len();
+ d.right_marker(target, false);
+ assert!(d.ids("timeline.markerMenu.").contains(&"timeline.markerMenu.label".into()), "a marker needs its own Label menu");
+ d.click("timeline.markerMenu.label");
+ assert_eq!(d.ids("timeline.markerMenu.color.").len(), filmcraft_project::Label::ALL.len());
+ d.snapshot("marker-colour-menu", "timeline.markerMenu.");
+ d.click("timeline.markerMenu.color.Blue");
+ let mut expected = before.clone();
+ expected.markers.iter_mut().find(|m| m.id.0 == target).unwrap().color = filmcraft_project::Label::Blue;
+ let actual = d.app().session.active_sequence().unwrap().clone();
+ assert_eq!(serde_json::to_value(&actual).unwrap(), serde_json::to_value(&expected).unwrap(), "only the chosen marker changes, not a selected clip");
+ assert_eq!(d.app().session.history.undo.len(), undo + 1);
+ d.exec("edit.undo", json!({}));
+ assert_eq!(serde_json::to_value(d.app().session.active_sequence().unwrap()).unwrap(), serde_json::to_value(&before).unwrap());
+ d.exec("edit.redo", json!({}));
+ assert_eq!(serde_json::to_value(d.app().session.active_sequence().unwrap()).unwrap(), serde_json::to_value(&expected).unwrap());
+}
+
+#[test]
+fn a_range_marker_band_opens_its_menu_and_keeps_the_marker_identity() {
+ let mut d = Driver::demo();
+ d.exec("markers.clearAll", json!({}));
+ let other = d.exec("markers.add", json!({"frame":30,"name":"Underneath","color":"Rose"}))["marker"].as_u64().unwrap();
+ let target = d.exec("markers.addRange", json!({"frame":30,"durationFrames":48,"name":"Review range"}))["marker"].as_u64().unwrap();
+ let before = d.app().session.active_sequence().unwrap().clone();
+ d.right_marker(target, true);
+ assert!(
+ d.ids("timeline.markerMenu.").contains(&"timeline.markerMenu.markers.clearCurrent".into()),
+ "right-clicking the range band must open the marker menu"
+ );
+ assert!(!d.ids("timeline.clipMenu.").iter().any(|id| id == "timeline.clipMenu.edit.cut"));
+ d.exec("playhead.set", json!({"frame":0}));
+ d.click("timeline.markerMenu.label");
+ d.click("timeline.markerMenu.color.Yellow");
+ let mut expected = before;
+ expected.markers.iter_mut().find(|m| m.id.0 == target).unwrap().color = filmcraft_project::Label::Yellow;
+ let actual = d.app().session.active_sequence().unwrap();
+ assert_eq!(actual.markers.iter().find(|m| m.id.0 == other).unwrap().color, filmcraft_project::Label::Rose);
+ assert_eq!(
+ serde_json::to_value(actual).unwrap(),
+ serde_json::to_value(&expected).unwrap(),
+ "target the clicked id, even when starts coincide or the playhead changes"
+ );
+ d.snapshot("marker-range-colour", "timeline.markerRange.");
+}
+
+#[test]
+fn markers_panel_label_refreshes_the_colour_filter_in_both_themes() {
+ for theme in ["dark", "light"] {
+ let mut d = Driver::demo();
+ d.ok("ui.set", json!({"theme":theme}));
+ d.exec("markers.clearAll", json!({}));
+ let target = d.exec("markers.add", json!({"frame":30,"name":"Review colour"}))["marker"].as_u64().unwrap();
+ d.exec("markers.filterColors", json!({"color":"Blue","visible":false}));
+ d.ok("ui.panel.show", json!({"panel":"Markers"}));
+ d.frames(3);
+ d.ok("ui.click", json!({"id":format!("markers.row.{target}"),"button":"right"}));
+ d.frames(3);
+ d.click("markers.menu.label");
+ assert_eq!(d.ids("markers.menu.color.").len(), filmcraft_project::Label::ALL.len());
+ d.snapshot(&format!("marker-panel-label-{theme}"), "markers.menu.");
+ let undo = d.app().session.history.undo.len();
+ d.click("markers.menu.color.Blue");
+ assert_eq!(d.app().session.active_sequence().unwrap().markers[0].color, filmcraft_project::Label::Blue);
+ assert_eq!(d.app().session.history.undo.len(), undo + 1);
+ assert!(d.ids("markers.row.").is_empty(), "the newly blue marker follows the existing filter");
+ d.exec("edit.undo", json!({}));
+ d.frames(3);
+ assert_eq!(d.ids("markers.row."), vec![format!("markers.row.{target}")]);
+ }
+}
+
+#[test]
+fn timeline_clip_label_is_disabled_without_a_selected_clip() {
+ let mut d = Driver::demo();
+ d.exec("markers.clearAll", json!({}));
+ d.exec("timeline.select", json!({"clips":[]}));
+ d.ok("ui.click", json!({"id":"timeline.ruler","fx":0.8,"fy":0.5,"button":"right"}));
+ d.frames(3);
+ let enabled = |d: &mut Driver| {
+ let elements = d.ok("ui.elements", json!({"prefix":"timeline.clipMenu.edit.label"}));
+ elements[0]["enabled"].clone()
+ };
+ assert_eq!(enabled(&mut d), json!(false));
+ let undo = d.app().session.history.undo.len();
+ d.click("timeline.clipMenu.edit.label");
+ assert!(d.ids("timeline.clipMenu.label.").is_empty());
+ assert_eq!(d.app().session.history.undo.len(), undo);
+ d.ok("ui.key", json!({"key":"Escape"}));
+ let clip = d.app().session.active_sequence().unwrap().video_tracks[0].items[0].id;
+ d.ok("ui.click", json!({"id":format!("timeline.clip.{}",clip.0),"fx":0.5,"button":"right"}));
+ d.frames(3);
+ assert_eq!(enabled(&mut d), json!(true));
+ d.click("timeline.clipMenu.edit.label");
+ d.click("timeline.clipMenu.label.Blue");
+ assert_eq!(d.app().session.active_sequence().unwrap().find_item(clip).unwrap().1.label, filmcraft_project::Label::Blue);
+ assert!(d.app().session.active_sequence().unwrap().markers.is_empty());
+}