Skip to content

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

Merge pull request #1015 from MarekSpodBiedry/issue-960

Brandon Thomas920b6c40e33e
diff --cc crates/ui-egui/src/lib.rs
index c1506aaf2,9a62b1d08..2e3a7bebe
--- a/crates/ui-egui/src/lib.rs
+++ b/crates/ui-egui/src/lib.rs
@@@ -1108,9 -1084,10 +1113,10 @@@ impl FilmcraftApp
(Tick::ZERO, dur)
};
if let Some(end) = self.playback.stop_at.filter(|e| t >= *e && self.playback.speed > 0.0 && !self.playback.looping) {
- self.session.set_playhead(end);
+ self.session.set_playhead(self.playback.rest_at.unwrap_or(end));
self.stop();
- } else if t >= hi && self.playback.speed > 0.0 {
+ } else if t >= hi && self.playback.speed > 0.0 && (self.playback.looping || !self.session.voiceover.recording()) {
+ // a voice-over take keeps playing (and recording) into the empty timeline past the end
if self.playback.looping {
self.session.set_playhead(lo);
self.play(self.playback.speed);
@@@ -2409,108 -2230,40 +2415,140 @@@ mod audio_recovery_tests
assert!(app.ui.status.contains("Audio output failed"));
app.stop();
}
+
+ /// A stream the device dropped, which `recover` restarts on the same clock.
+ struct Restartable {
+ reading: Option<u64>,
+ }
+
+ impl AudioOut for Restartable {
+ fn start(&mut self, _: Box<dyn FnMut(&mut [f32], usize) + Send>) -> Result<u32, String> {
+ Ok(48000)
+ }
+ fn stop(&mut self) {}
+ fn sample_rate(&self) -> u32 {
+ 48000
+ }
+ fn played_frames(&self) -> Option<u64> {
+ self.reading
+ }
+ fn recover(&mut self) {
+ self.reading = self.reading.or(Some(48_000));
+ }
+ }
+
+ #[test]
+ fn a_restarted_stream_keeps_the_device_clock_and_the_sound() {
+ let mut session = Session::default();
+ session.execute("file.openDemoProject", json!({})).unwrap();
+ let ctx = egui::Context::default();
+ let mut app = FilmcraftApp::new(session);
+ app.audio = Some(Box::new(Restartable { reading: None }));
+ app.playback.playing = true;
+ app.playback.audio_clock = true;
+ app.playback.anchor_tick = Tick::ZERO;
+ app.playback.anchor_time = 0.0;
+ app.playback.audio_seen = (0, 10.0);
+ let mut output = ctx.run_ui(egui::RawInput { time: Some(10.0), ..Default::default() }, |ui| app.advance_playback(ui.ctx()));
+ output.textures_delta.clear();
+ assert!(app.playback.audio_clock, "the restarted stream is still the master clock");
+ assert!(!app.ui.status.contains("Audio output failed"));
+ // 48 000 frames played at 48 kHz: one second in, on a frame boundary
+ let (shown, second) = (app.session.playhead(), Tick::from_seconds_f64(1.0));
+ assert!(
+ shown <= second && shown + app.session.sequence_rate().frame_duration() > second,
+ "the playhead follows the frames the device played: {shown:?}"
+ );
+ app.stop();
+ }
+}
+
+/// Whether a shortcut chord must be left alone because a text field is focused.
+///
+/// A focused text field owns every unmodified key: an input method composes there from plain
+/// letters (Vietnamese Telex builds `á` from `a` + `s`, `ả` from `a` + `r`, `ạ` from `a` + `j`),
+/// and our single-key tool shortcuts would swallow them before the field sees them (#243). Chords
+/// with a modifier are not what a text field or an IME consumes, so they keep working.
+fn lets_text_field_keep(modifiers: egui::Modifiers, text_focused: bool) -> bool {
+ text_focused && menus::specificity(modifiers) == 0
+}
+
+#[cfg(test)]
+mod text_focus_shortcut_tests {
+ use super::lets_text_field_keep;
+ use egui::{Key, Modifiers};
+
+ /// Vietnamese Telex composes diacritics from unmodified letters (`a` + `s` → `á`, `a` + `r` →
+ /// `ả`, `a` + `j` → `ạ`), and the editor binds `J`, `Z`, `R`, `A`, `C`, `T`, `K`, `L`, `B`, `N`,
+ /// `P`, `V`, `Y`, `U`, `H` as single-key tool shortcuts. While a text field is focused those
+ /// keys must reach the field, or no diacritic can ever be typed (#243).
+ #[test]
+ fn an_unmodified_key_reaches_a_focused_text_field() {
+ assert!(lets_text_field_keep(Modifiers::NONE, true), "the IME needs the plain letter keys");
+ }
+
+ /// Without a text field the editor keeps its single-key tool shortcuts.
+ #[test]
+ fn an_unmodified_key_still_fires_without_a_text_field() {
+ assert!(!lets_text_field_keep(Modifiers::NONE, false));
+ }
+
+ /// A modifier chord is not what a text field or an IME consumes, so it keeps firing either way.
+ #[test]
+ fn a_modified_chord_still_fires_while_text_is_focused() {
+ for m in [Modifiers::COMMAND, Modifiers::CTRL, Modifiers::SHIFT, Modifiers::ALT, Modifiers::COMMAND | Modifiers::SHIFT] {
+ assert!(!lets_text_field_keep(m, true), "{m:?} is not an IME key");
+ }
+ }
+
+ /// The keys a Vietnamese Telex IME needs must be exactly the ones we now hand to the field.
+ #[test]
+ fn the_telex_trigger_keys_are_left_to_the_field() {
+ // s=acute, f=grave, r=hook above, x=tilde, j=dot below, w=horn, z=remove diacritic
+ for key in [Key::S, Key::F, Key::R, Key::X, Key::J, Key::W, Key::Z] {
+ assert!(lets_text_field_keep(Modifiers::NONE, true), "{key:?} must reach the IME");
+ }
+ // The letters the editor binds as tools are the ones Telex composes with, so they must be
+ // left to the field too: `A` (Track Select Forward), `R` (Rate Stretch), `J` (Shuttle Left),
+ // `Z` (Zoom), `C` (Razor), `T` (Type).
+ for key in [Key::A, Key::R, Key::J, Key::Z, Key::C, Key::T] {
+ assert!(lets_text_field_keep(Modifiers::NONE, true), "{key:?} is a Telex key the tools stole");
+ }
+ }
}
+ #[cfg(test)]
+ mod voiceover_playback_tests {
+ use super::*;
+
+ #[test]
+ fn a_voice_over_take_records_into_the_empty_timeline_past_the_end() {
+ let mut session = Session::default();
+ session.execute("file.openDemoProject", json!({})).unwrap();
+ session.execute("audio.voiceover.settings", json!({"prerollSeconds": 0.0})).unwrap();
+ session.prefs.timeline.return_to_beginning = true;
+ let ctx = egui::Context::default();
+ let mut app = FilmcraftApp::new(session);
+ app.audio = None;
+ let dur = app.session.active_sequence().map(|q| q.duration()).unwrap();
+ let from = app.session.sequence_rate().snap(dur + Tick::from_seconds_f64(60.0));
+ app.session.set_playhead(from);
+ panels::voiceover::toggle(&mut app, None).unwrap();
+ // play() keeps the record point instead of returning to the beginning
+ assert!(app.playback.playing);
+ assert_eq!(app.session.playhead(), from);
+ app.end_preroll(0.0);
+ // and one second later the take is still playing and recording, not stopped at the end
+ let mut output = ctx.run_ui(egui::RawInput { time: Some(1.0), ..Default::default() }, |ui| app.advance_playback(ui.ctx()));
+ output.textures_delta.clear();
+ assert!(app.playback.playing);
+ assert!(app.session.voiceover.recording());
+ assert!(app.session.playhead() > from, "{:?} <= {from:?}", app.session.playhead());
+ app.session.execute("audio.voiceover.stop", json!({"discard": true})).unwrap();
+ app.stop();
+ }
+ }
+
/// A physical-pixel screen position as a point in the window's UI space, given the window's content origin in points.
///
/// Assumes the whole virtual screen uses the window's `pixels_per_point`: with monitors of mixed