Skip to content

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

timeline, text editing, program monitor: clicking empty space deselects (#683)

ReReddo1ed57e6d4ac5
diff --git a/crates/ui-egui/src/panels/graphics.rs b/crates/ui-egui/src/panels/graphics.rs
index 09aead4f5..3f38c565f 100644
--- a/crates/ui-egui/src/panels/graphics.rs
+++ b/crates/ui-egui/src/panels/graphics.rs
@@ -386,6 +386,19 @@ const TEXT_EDIT_ID: &str = "gfx-text-edit";
fn end_edit(app: &mut FilmcraftApp, ui: &egui::Ui) {
app.ui.gfx_edit = None;
ui.memory_mut(|m| m.surrender_focus(egui::Id::new(TEXT_EDIT_ID)));
+ ui.data_mut(|d| d.remove::<crate::dock::PanelKind>(egui::Id::new(TEXT_EDIT_ID).with("panel")));
+}
+
+/// A click on the Program monitor's empty space (the picture's, or around it): end the text edit
+/// and let go of the selected graphic layers and mask (#683).
+pub(crate) fn deselect_on_monitor(app: &mut FilmcraftApp, ui: &egui::Ui) {
+ end_edit(app, ui);
+ app.session.state.graphic_layers.clear();
+ if app.session.state.selected_mask.is_some()
+ && let Err(e) = app.session.execute("masks.select", json!({"none": true}))
+ {
+ app.ui.status = e.to_string();
+ }
}
/// Draw the overlay and handle graphics tools on the Program monitor picture `pic`.
@@ -678,10 +691,8 @@ pub fn monitor_overlay(app: &mut FilmcraftApp, ui: &mut egui::Ui, pic: Rect, fra
end_edit(app, ui);
}
actions.push(("graphics.selectLayer".into(), json!({"clip": v.clip.0, "layers": [v.layer]})));
- } else if app.ui.gfx_edit.is_some() {
- end_edit(app, ui);
- } else if sel_clip.is_some() && !sel_layers.is_empty() {
- app.session.state.graphic_layers.clear();
+ } else {
+ deselect_on_monitor(app, ui);
}
}
Tool::Pen => {
@@ -806,10 +817,26 @@ fn start_edit_at(app: &mut FilmcraftApp, ui: &egui::Ui, v: &LayerView, p: Pos2,
/// Keyboard handling while a text layer is being edited.
fn text_edit(app: &mut FilmcraftApp, ui: &mut egui::Ui, v: &LayerView, mut ed: GfxEdit, resp: &egui::Response, actions: &mut Vec<(String, Value)>) {
let _ = resp;
+ // moving to another panel ends the edit; Properties and Essential Graphics style the text
+ // being edited, so working there keeps it (#683). Only a move counts: an edit started with
+ // another panel focused (an agent's `ui.set`) goes on until the focus moves.
+ use crate::dock::PanelKind;
+ let panel_id = egui::Id::new(TEXT_EDIT_ID).with("panel");
+ let before = ui.data(|d| d.get_temp::<PanelKind>(panel_id));
+ ui.data_mut(|d| d.insert_temp(panel_id, app.ui.focused));
+ if before.is_some_and(|b| b != app.ui.focused) && !matches!(app.ui.focused, PanelKind::Program | PanelKind::Properties | PanelKind::EssentialGraphics) {
+ end_edit(app, ui);
+ return;
+ }
let eid = egui::Id::new(TEXT_EDIT_ID);
// a real (non-clickable) widget holds keyboard focus so shortcuts pause while typing
let fr = ui.interact(Rect::from_points(&v.quad()), eid, Sense::focusable_noninteractive());
fr.widget_info(|| egui::WidgetInfo::labeled(egui::WidgetType::TextEdit, true, "Text layer"));
+ // while another field has the keyboard (a value typed in Properties) the keys are its own;
+ // the text takes the keyboard back once nothing has it (#683)
+ if ui.memory(|m| m.focused().is_some_and(|f| f != eid)) {
+ return;
+ }
ui.memory_mut(|m| {
m.request_focus(eid);
m.set_focus_lock_filter(eid, egui::EventFilter { tab: true, horizontal_arrows: true, vertical_arrows: true, escape: true });
diff --git a/crates/ui-egui/src/panels/monitor.rs b/crates/ui-egui/src/panels/monitor.rs
index 11f4bc2d8..8476a32b4 100644
--- a/crates/ui-egui/src/panels/monitor.rs
+++ b/crates/ui-egui/src/panels/monitor.rs
@@ -279,6 +279,11 @@ pub fn show(app: &mut FilmcraftApp, ui: &mut egui::Ui, rect: Rect, which: Which)
let c = if app.playback.meter.counts().1 > 0 { t.render_yellow } else { t.render_green };
ui.painter().circle_filled(pos2(video_area.min.x + 10.0, video_area.min.y + 10.0), 4.0, c);
}
+ // A click on the Program monitor around the picture lets go of what is selected on it (#683;
+ // in the picture the overlays do that). Under the picture and the overlays, so they come first.
+ if which == Which::Program && show_picture && ui.interact(video_area, egui::Id::new((prefix, "background")), Sense::click()).clicked() {
+ crate::panels::graphics::deselect_on_monitor(app, ui);
+ }
// Click/drag in the picture: the Hand tool pans a magnified picture, otherwise focus.
let pic_resp = ui.interact(pic, egui::Id::new((prefix, "pic")), if show_picture { Sense::click_and_drag() } else { Sense::hover() });
app.auto.add(&format!("{prefix}.picture"), pic.intersect(video_area), "picture");
diff --git a/crates/ui-egui/src/panels/timeline.rs b/crates/ui-egui/src/panels/timeline.rs
index d59978393..4a99b725d 100644
--- a/crates/ui-egui/src/panels/timeline.rs
+++ b/crates/ui-egui/src/panels/timeline.rs
@@ -1996,7 +1996,9 @@ fn interact(app: &mut FilmcraftApp, ui: &mut egui::Ui, seq: &Sequence, layout: &
}
}
(_, Grab::Other(Hit::Transition { .. })) => None,
- (_, Grab::Other(Hit::Empty { .. })) => {
+ // empty space: an empty stretch of a track, or no track at all (below the tracks, between
+ // the video and audio tracks, #683)
+ (_, Grab::Other(Hit::Empty { .. } | Hit::None)) if layout.content.contains(p) => {
if resp.drag_started() {
Some(Drag::Marquee { start: p })
} else {
@@ -2006,7 +2008,7 @@ fn interact(app: &mut FilmcraftApp, ui: &mut egui::Ui, seq: &Sequence, layout: &
None
}
}
- (_, Grab::Other(Hit::None)) => None,
+ (_, Grab::Other(Hit::Empty { .. } | Hit::None)) => None,
};
if let Some(d) = started {
if matches!(d, Drag::Scrub) {
diff --git a/crates/ui-egui/tests/deselect_ui.rs b/crates/ui-egui/tests/deselect_ui.rs
new file mode 100644
index 000000000..66095e652
--- /dev/null
+++ b/crates/ui-egui/tests/deselect_ui.rs
@@ -0,0 +1,154 @@
+//! Clicking empty space deselects, consistently (#683): the Timeline's empty space below and
+//! between the tracks, a text layer being edited when another panel is clicked, and the Program
+//! monitor's empty space around the picture (graphic layers, masks).
+
+use std::sync::mpsc::{Sender, channel};
+
+use egui_kittest::Harness;
+use filmcraft_engine::Session;
+use filmcraft_ui_egui::FilmcraftApp;
+use filmcraft_ui_egui::control::ControlRequest;
+use filmcraft_ui_egui::state::GfxEdit;
+use serde_json::{Value, json};
+
+struct Driver {
+ harness: Harness<'static, FilmcraftApp>,
+ tx: Sender<ControlRequest>,
+}
+
+impl Driver {
+ fn demo() -> Self {
+ let mut session = Session::default();
+ session.execute("file.openDemoProject", json!({})).expect("demo project");
+ let (tx, rx) = channel();
+ let app = FilmcraftApp::new(session).with_control(rx);
+ let harness = Harness::builder().with_size(egui::vec2(1600.0, 980.0)).with_max_steps(10_000).build_eframe(move |_cc| app);
+ let mut d = Driver { harness, tx };
+ d.frames(4);
+ d
+ }
+ fn frames(&mut self, n: usize) {
+ for _ in 0..n {
+ let ctx = self.harness.ctx.clone();
+ let mut raw = std::mem::take(self.harness.input_mut());
+ eframe::App::raw_input_hook(self.harness.state_mut(), &ctx, &mut raw);
+ *self.harness.input_mut() = raw;
+ self.harness.step();
+ }
+ }
+ fn ok(&mut self, method: &str, params: Value) -> Value {
+ let (req, reply) = ControlRequest::new(method, params.clone());
+ self.tx.send(req).unwrap();
+ for _ in 0..600 {
+ self.frames(1);
+ if let Ok(v) = reply.try_recv() {
+ assert_eq!(v["ok"], json!(true), "{method} {params} failed: {v}");
+ return v["result"].clone();
+ }
+ }
+ panic!("no reply to {method} {params}");
+ }
+ fn exec(&mut self, command: &str, params: Value) -> Value {
+ self.ok("engine.execute", json!({"command": command, "params": params}))
+ }
+ fn rect(&mut self, id: &str) -> [f64; 4] {
+ let v = self.ok("ui.elements", json!({"prefix": id}));
+ let e = v.as_array().unwrap().iter().find(|e| e["id"] == id).unwrap_or_else(|| panic!("no element {id}")).clone();
+ let r: Vec<f64> = e["rect"].as_array().unwrap().iter().map(|x| x.as_f64().unwrap()).collect();
+ [r[0], r[1], r[2], r[3]]
+ }
+ fn click_at(&mut self, x: f64, y: f64) {
+ self.ok("ui.click", json!({"x": x, "y": y}));
+ self.frames(3);
+ }
+ fn app(&mut self) -> &mut FilmcraftApp {
+ self.harness.state_mut()
+ }
+}
+
+/// A click on the Timeline below the tracks deselects, as one on an empty stretch of a track does;
+/// a drag from there draws a box selection.
+#[test]
+fn empty_timeline_space_below_the_tracks_deselects_and_starts_a_marquee() {
+ let mut d = Driver::demo();
+ d.ok("ui.set", json!({"focused": "Timeline"}));
+ let seq = d.exec("sequence.inspect", json!({}));
+ let clip = seq["video"][0]["items"][0]["clip"].as_u64().unwrap();
+ let area = d.rect("timeline.tracks");
+ // a point in the track area that is on no track at all: going up from the area's bottom
+ let x = area[0] + area[2] * 0.5;
+ let y = (0..60)
+ .map(|k| area[1] + area[3] - 3.0 - 4.0 * k as f64)
+ .find(|y| d.ok("ui.timeline.hit", json!({"x": x, "y": y}))["hit"] == "none")
+ .expect("the track area has space on no track");
+ let below = (x, y);
+ d.exec("timeline.select", json!({"clips": [clip]}));
+ d.frames(2);
+ d.click_at(below.0, below.1);
+ assert!(d.app().session.state.selection.is_empty(), "a click below the tracks deselects");
+ // a drag from below the tracks up over the clips selects them
+ let top = d.rect("timeline.track.V1.locked");
+ d.ok("ui.drag", json!({"from": {"x": area[0] + 4.0, "y": below.1}, "to": {"x": area[0] + area[2] * 0.6, "y": top[1] + top[3] / 2.0}, "steps": 8}));
+ d.frames(3);
+ assert!(!d.app().session.state.selection.is_empty(), "the box from below the tracks selected clips");
+}
+
+/// A text layer being edited stops being edited when another panel is clicked (its keys went into
+/// the text before, #227); working in Properties, which styles the text, keeps the edit.
+#[test]
+fn clicking_another_panel_ends_text_editing() {
+ let mut d = Driver::demo();
+ let seq = d.exec("sequence.inspect", json!({}));
+ let at = seq["playhead"].as_i64().unwrap();
+ let clip = d.exec("graphics.newText", json!({"text": "Title", "time": at}))["clip"].as_u64().unwrap();
+ d.ok("ui.panel.show", json!({"panel": "Properties"}));
+ let edit = |d: &mut Driver| {
+ d.app().ui.gfx_edit = Some(GfxEdit { clip, layer: 0, caret: 0, anchor: 0 });
+ d.app().ui.focused = filmcraft_ui_egui::dock::PanelKind::Program;
+ d.frames(3);
+ };
+ edit(&mut d);
+ assert!(d.app().ui.gfx_edit.is_some());
+ let props = d.rect("panel.Properties");
+ d.click_at(props[0] + props[2] / 2.0, props[1] + props[3] - 10.0);
+ assert!(d.app().ui.gfx_edit.is_some(), "a click in Properties keeps the edit");
+ let tl = d.rect("timeline.tracks");
+ d.click_at(tl[0] + tl[2] / 2.0, tl[1] + tl[3] - 6.0);
+ assert!(d.app().ui.gfx_edit.is_none(), "a click in the Timeline ends it");
+}
+
+/// A click on the Program monitor around the picture lets go of the selected graphic layer and
+/// mask; so does a click on the picture's empty space.
+#[test]
+fn empty_monitor_space_deselects_layers_and_masks() {
+ let mut d = Driver::demo();
+ let pic = d.rect("program.picture");
+ let panel = d.rect("panel.Program");
+ // the empty space beside or above the picture, a few points away from it
+ let outside = if pic[0] - panel[0] > 20.0 { (pic[0] - 8.0, pic[1] + pic[3] / 2.0) } else { (pic[0] + pic[2] / 2.0, pic[1] + pic[3] + 8.0) };
+ let seq = d.exec("sequence.inspect", json!({}));
+ let clip = seq["video"][0]["items"][0]["clip"].as_u64().unwrap();
+ // a graphic layer
+ let at = seq["playhead"].as_i64().unwrap();
+ let gclip = d.exec("graphics.newText", json!({"text": "Title", "time": at}))["clip"].as_u64().unwrap();
+ d.exec("graphics.selectLayer", json!({"clip": gclip, "layers": [0]}));
+ d.frames(2);
+ assert!(!d.app().session.state.graphic_layers.is_empty());
+ d.click_at(outside.0, outside.1);
+ assert!(d.app().session.state.graphic_layers.is_empty(), "a click around the picture deselects the layer");
+ // a mask: around the picture, and on the picture's empty space
+ d.exec("timeline.select", json!({"clips": [clip]}));
+ d.exec("effects.apply", json!({"clips": [clip], "effect": "gaussian_blur"}));
+ let fx = {
+ let s = &d.harness.state().session;
+ let it = s.active_sequence().unwrap().find_item(filmcraft_project::ClipId(clip)).unwrap().1;
+ it.effects.iter().position(|e| e.effect == "gaussian_blur").unwrap()
+ };
+ d.exec("masks.add", json!({"clip": clip, "effect": fx, "shape": "ellipse"}));
+ for (x, y) in [outside, (pic[0] + 6.0, pic[1] + 6.0)] {
+ d.exec("masks.select", json!({"clip": clip, "effect": fx, "mask": 0}));
+ d.frames(2);
+ d.click_at(x, y);
+ assert!(d.app().session.state.selected_mask.is_none(), "a click at ({x}, {y}) deselects the mask");
+ }
+}