Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

feat(desktop-gpui): add timeline start and end shortcuts

Himanshu Kumarca225ccfe5d7
diff --git a/apps/desktop-gpui/src/editor_clips.rs b/apps/desktop-gpui/src/editor_clips.rs
index 81cc72e22..865968054 100644
--- a/apps/desktop-gpui/src/editor_clips.rs
+++ b/apps/desktop-gpui/src/editor_clips.rs
@@ -379,6 +379,10 @@ impl Default for ClipsState {
}
impl ClipsState {
+ pub(crate) fn has_open_menu(&self) -> bool {
+ self.import_menu.is_some()
+ }
+
pub(crate) fn is_importing(&self) -> bool {
self.importing
}
diff --git a/apps/desktop-gpui/src/editor_window.rs b/apps/desktop-gpui/src/editor_window.rs
index ecbc4b425..dbfa36f75 100644
--- a/apps/desktop-gpui/src/editor_window.rs
+++ b/apps/desktop-gpui/src/editor_window.rs
@@ -3448,6 +3448,15 @@ impl EditorWindow {
if !self.visual_ready() {
return;
}
+ self.on_ready_key(event, window, cx);
+ }
+
+ fn on_ready_key(
+ &mut self,
+ event: &gpui::KeyDownEvent,
+ window: &mut Window,
+ cx: &mut Context<Self>,
+ ) {
if self.frame_controls.is_open() && event.keystroke.key == "escape" {
self.close_frame_controls(window, cx);
cx.stop_propagation();
@@ -3670,6 +3679,33 @@ impl EditorWindow {
return;
}
match keystroke.key.as_str() {
+ "up" | "down" | "home" | "end" => {
+ if keystroke.modifiers.function
+ || self.crop.is_some()
+ || self.canvas_selection.is_some()
+ || self.sidebar.menu.is_some()
+ || self.toolbar_menu.is_some()
+ || self.text_menu.is_some()
+ || self.add_track.is_some()
+ || self.clip_speed.is_some()
+ || self.sidebar.color_picker.is_some()
+ || self.presets_menu.is_some()
+ || self.preset_dialog.is_some()
+ || self.clips.has_open_menu()
+ || self.audio_picker.is_some()
+ || self.camera3d_picker
+ || self.frame_controls.is_open()
+ {
+ return;
+ }
+ cx.stop_propagation();
+ self.view.preview_time = None;
+ self.scene_preview_time = None;
+ match keystroke.key.as_str() {
+ "up" | "home" => self.jump_to_start(cx),
+ _ => self.jump_to_end(cx),
+ }
+ }
// `e.code === "Backspace" || (e.code === "Delete" &&
// hasNoModifiers)` (`TL/index.tsx:963`). gpui reports the main
// delete key as `backspace` and forward-delete as `delete`, which
@@ -12256,6 +12292,193 @@ fn hex_to_color(rgba: [u8; 4]) -> cap_project::Color {
mod tests {
use super::*;
+ #[cfg(target_os = "linux")]
+ mod boundary_navigation {
+ use super::*;
+
+ struct KeyboardView {
+ editor: Entity<EditorWindow>,
+ check_readiness: bool,
+ _project: tempfile::TempDir,
+ }
+
+ impl Render for KeyboardView {
+ fn render(&mut self, window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
+ let theme = Theme::for_window(window, cx, false);
+ let check_readiness = self.check_readiness;
+ self.editor.update(cx, |editor, cx| {
+ div()
+ .size_full()
+ .track_focus(&editor.focus)
+ .on_key_down(cx.listener(move |editor, event, window, cx| {
+ if check_readiness {
+ editor.on_key(event, window, cx);
+ } else {
+ editor.on_ready_key(event, window, cx);
+ }
+ }))
+ .on_key_up(cx.listener(|editor, event: &gpui::KeyUpEvent, _, cx| {
+ editor.crop_key_up(event, cx);
+ }))
+ .child(ui::TextInput::plain(&theme, "name", &editor.name_input))
+ })
+ }
+ }
+
+ // The ready-handler fixture bypasses media loading; the gated fixture separately
+ // checks that a loading project cannot issue transport commands.
+ fn fixture(
+ cx: &mut gpui::TestAppContext,
+ check_readiness: bool,
+ ) -> (
+ Entity<EditorWindow>,
+ TransportDriver,
+ &mut gpui::VisualTestContext,
+ ) {
+ let project = tempfile::tempdir().unwrap();
+ let (handle, driver, _) = transport();
+ let (view, cx) = cx.add_window_view(move |window, cx| {
+ ui::bind_text_input_keys(cx);
+ let editor = cx.new(|cx| EditorWindow::new(project.path().into(), window, cx));
+ editor.update(cx, |editor, cx| {
+ editor.transport = Some(handle);
+ editor.total = 30.;
+ editor.name_input.update(cx, |input, cx| {
+ input.set_disabled(false, cx);
+ input.set_text("Recording", cx);
+ });
+ window.focus(&editor.focus, cx);
+ });
+ KeyboardView {
+ editor,
+ check_readiness,
+ _project: project,
+ }
+ });
+ let editor = view.read_with(cx, |view, _| view.editor.clone());
+ (editor, driver, cx)
+ }
+
+ fn desired(driver: &TransportDriver) -> Desired {
+ *driver.desired.lock().unwrap()
+ }
+
+ #[gpui::test]
+ fn boundary_keys_pause_and_seek_the_existing_transport(cx: &mut gpui::TestAppContext) {
+ let (editor, driver, cx) = fixture(cx, false);
+ for (key, seconds) in [("up", 0.), ("down", 30.), ("home", 0.), ("end", 30.)] {
+ editor.update(cx, |editor, _| {
+ editor.playing = true;
+ editor.view.playing = true;
+ editor.playhead = 12.;
+ editor.view.preview_time = Some(12.);
+ editor.scene_preview_time = Some(12.);
+ editor.transport.as_ref().unwrap().play_from(720);
+ });
+ let generation = desired(&driver).seek_gen;
+ cx.simulate_keystrokes(key);
+ let requested = desired(&driver);
+ assert_eq!(requested.seek, Some((seconds * 60.) as u32), "{key}");
+ assert_eq!(requested.seek_gen, generation + 1, "{key}");
+ assert!(!requested.playing, "{key}");
+ editor.read_with(cx, |editor, _| {
+ assert_eq!(editor.playhead, seconds, "{key}");
+ assert_eq!(editor.view.playhead, seconds, "{key}");
+ assert_eq!(editor.preview_or_playhead(), seconds, "{key}");
+ assert!(editor.view.preview_time.is_none(), "{key}");
+ assert!(editor.scene_preview_time.is_none(), "{key}");
+ assert!(!editor.playing && !editor.view.playing, "{key}");
+ });
+ }
+ }
+
+ #[gpui::test]
+ fn boundary_keys_preserve_input_menu_and_canvas_ownership(cx: &mut gpui::TestAppContext) {
+ let (editor, driver, cx) = fixture(cx, false);
+ let initial = desired(&driver);
+ let (input, focus) = editor.read_with(cx, |editor, cx| {
+ (
+ editor.name_input.read(cx).focus_handle(),
+ editor.focus.clone(),
+ )
+ });
+ cx.update(|window, cx| window.focus(&input, cx));
+ cx.simulate_keystrokes("up down home end");
+ assert_eq!(desired(&driver), initial);
+
+ cx.update(|window, cx| window.focus(&focus, cx));
+ editor.update(cx, |editor, _| {
+ editor.sidebar.menu = open_sidebar_menu_for_test(
+ crate::editor_tabs::SidebarMenu::BackgroundCornerStyle,
+ );
+ });
+ cx.simulate_keystrokes("up down home end");
+ assert_eq!(desired(&driver), initial);
+ editor.read_with(cx, |editor, _| {
+ assert_eq!(
+ editor.sidebar.menu.as_ref().unwrap().state.highlighted,
+ Some(1)
+ );
+ });
+
+ editor.update(cx, |editor, _| {
+ editor.sidebar.menu = None;
+ editor.canvas_selection = Some(crate::editor_canvas::CanvasSelection::Text(99));
+ });
+ cx.simulate_keystrokes("up down home end");
+ assert_eq!(desired(&driver), initial);
+
+ editor.update(cx, |editor, _| {
+ editor.canvas_selection = None;
+ editor.crop = Some(crate::editor_crop::CropState::new(
+ (640, 480),
+ (802., 602.),
+ crate::editor_crop::CROP_ZERO,
+ ));
+ });
+ cx.simulate_keystrokes("home end");
+ assert_eq!(desired(&driver), initial);
+
+ editor.update(cx, |editor, _| {
+ editor.crop = None;
+ editor.camera3d_picker = true;
+ });
+ cx.simulate_keystrokes("up down home end");
+ assert_eq!(desired(&driver), initial);
+ }
+
+ #[gpui::test]
+ fn boundary_keys_ignore_modifiers_and_repeats(cx: &mut gpui::TestAppContext) {
+ let (_, driver, cx) = fixture(cx, false);
+ let initial = desired(&driver);
+ cx.simulate_keystrokes("shift-up ctrl-down alt-home cmd-end");
+ for key in ["up", "down", "home", "end"] {
+ let keystroke = gpui::Keystroke::parse(key).unwrap();
+ cx.simulate_event(gpui::KeyDownEvent {
+ keystroke: keystroke.clone(),
+ is_held: true,
+ prefer_character_input: false,
+ });
+ let mut modified = keystroke;
+ modified.modifiers.function = true;
+ cx.simulate_event(gpui::KeyDownEvent {
+ keystroke: modified,
+ is_held: false,
+ prefer_character_input: false,
+ });
+ }
+ assert_eq!(desired(&driver), initial);
+ }
+
+ #[gpui::test]
+ fn boundary_keys_wait_for_a_ready_editor(cx: &mut gpui::TestAppContext) {
+ let (_, driver, cx) = fixture(cx, true);
+ let initial = desired(&driver);
+ cx.simulate_keystrokes("up down home end");
+ assert_eq!(desired(&driver), initial);
+ }
+ }
+
#[cfg(target_os = "linux")]
#[gpui::test]
#[ignore = "manual preset menu repaint benchmark"]