Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

fix(desktop-gpui): keep library actions tied to their recording

Aditya Garudc28b63b2dc9f
diff --git a/apps/desktop-gpui/src/main_window.rs b/apps/desktop-gpui/src/main_window.rs
index 48989b3b0..c9fd7b3b4 100644
--- a/apps/desktop-gpui/src/main_window.rs
+++ b/apps/desktop-gpui/src/main_window.rs
@@ -4541,14 +4541,11 @@ impl MainWindow {
}
match (kind, &self.library) {
(LibraryKind::Recordings, Some(LibraryItems::Recordings(rows))) => {
- for (index, row) in rows.iter().enumerate() {
+ for row in rows {
if !self.matches_search(&row.item.pretty_name) {
continue;
}
- cards.push(
- self.render_recording_card(index, row, cx)
- .into_any_element(),
- );
+ cards.push(self.render_recording_card(row, cx).into_any_element());
}
}
(LibraryKind::Screenshots, Some(LibraryItems::Screenshots(rows))) => {
@@ -4747,7 +4744,6 @@ impl MainWindow {
fn render_recording_card(
&self,
- index: usize,
row: &LibraryRow<RecordingItem>,
cx: &mut Context<Self>,
) -> impl IntoElement {
@@ -4767,10 +4763,10 @@ impl MainWindow {
.unwrap_or_else(|| upload.label().to_string())
},
);
- let actions = self.render_recording_card_actions(index, item, cx);
+ let actions = self.render_recording_card_actions(item, cx);
self.library_card(
- ("library-recording", index),
+ gpui::ElementId::Path(item.path.as_path().into()),
row.thumbnail.clone(),
"icons/square-play.svg",
item.pretty_name.clone(),
@@ -4793,7 +4789,6 @@ impl MainWindow {
fn render_recording_card_actions(
&self,
- index: usize,
item: &RecordingItem,
cx: &mut Context<Self>,
) -> gpui::Div {
@@ -4811,7 +4806,7 @@ impl MainWindow {
if item.opens_editor() {
let editor = path.clone();
row = row.child(self.library_action(
- ("lib-rec-edit", index),
+ "lib-rec-edit",
"icons/edit.svg",
cx.listener(move |_, _, _window, cx| {
let editor = editor.clone();
@@ -4821,7 +4816,7 @@ impl MainWindow {
}
if let Some(url) = sharing {
row = row.child(self.library_action(
- ("lib-rec-link", index),
+ "lib-rec-link",
"icons/link.svg",
move |_, _, cx| cx.open_url(&url),
));
@@ -4833,7 +4828,7 @@ impl MainWindow {
{
let retry_path = path.clone();
row = row.child(self.library_action(
- ("lib-rec-retry", index),
+ "lib-rec-retry",
"icons/rotate-ccw.svg",
cx.listener(move |_, _, window, cx| {
let path = retry_path.clone();
@@ -4858,12 +4853,12 @@ impl MainWindow {
}
let folder = path.clone();
row = row.child(self.library_action(
- ("lib-rec-folder", index),
+ "lib-rec-folder",
"icons/folder.svg",
move |_, _, cx| library::open_recording_folder(&folder, mode, cx),
));
row.child(self.library_action(
- ("lib-rec-delete", index),
+ "lib-rec-delete",
"icons/trash.svg",
cx.listener(move |this, _, window, cx| {
this.delete_library_recording(path.clone(), window, cx);
diff --git a/apps/desktop-gpui/src/settings_window.rs b/apps/desktop-gpui/src/settings_window.rs
index 19b3740a4..8d82f051a 100644
--- a/apps/desktop-gpui/src/settings_window.rs
+++ b/apps/desktop-gpui/src/settings_window.rs
@@ -2810,7 +2810,7 @@ impl SettingsWindow {
// `not-last:border-b` counts the *rendered* rows, so the border
// stops at the last visible one rather than the last matching one.
items.push(
- self.render_recording_row(index, row, index + 1 == visible, cx)
+ self.render_recording_row(row, index + 1 == visible, cx)
.into_any_element(),
);
}
@@ -2864,7 +2864,6 @@ impl SettingsWindow {
/// w-full`, bordered from every row but the last.
fn render_recording_row(
&self,
- index: usize,
row: &RecordingRow,
last: bool,
cx: &mut Context<Self>,
@@ -2924,7 +2923,7 @@ impl SettingsWindow {
);
div()
- .id(("recording-row", index))
+ .id(gpui::ElementId::Path(item.path.as_path().into()))
.flex()
.flex_row()
.justify_between()
@@ -2943,7 +2942,7 @@ impl SettingsWindow {
}
})
.child(left)
- .child(self.render_recording_actions(index, item, cx))
+ .child(self.render_recording_actions(item, cx))
.when(opens_editor, |this| {
this.on_click(cx.listener(move |_this, _, _window, cx| {
let path = path.clone();
@@ -3049,7 +3048,6 @@ impl SettingsWindow {
/// Reupload and Open link; both get Open recording bundle and Delete.
fn render_recording_actions(
&self,
- index: usize,
item: &RecordingItem,
cx: &mut Context<Self>,
) -> impl IntoElement {
@@ -3064,7 +3062,7 @@ impl SettingsWindow {
if mode == RecordingMode::Studio {
if let Some(link) = sharing.clone() {
actions = actions.child(self.row_button(
- ("recording-link", index),
+ "recording-link",
"icons/link.svg",
"Open link",
false,
@@ -3074,7 +3072,7 @@ impl SettingsWindow {
}
let editor_path = path.clone();
actions = actions.child(self.row_button(
- ("recording-edit", index),
+ "recording-edit",
"icons/edit.svg",
"Edit",
// `disabled={status === "InProgress"}`
@@ -3090,7 +3088,7 @@ impl SettingsWindow {
let retry_path = path.clone();
actions = actions.child(
self.row_button(
- ("recording-reupload", index),
+ "recording-reupload",
"icons/rotate-ccw.svg",
"Retry upload",
!item
@@ -3121,7 +3119,7 @@ impl SettingsWindow {
);
if let Some(link) = sharing {
actions = actions.child(self.row_button(
- ("recording-instant-link", index),
+ "recording-instant-link",
"icons/link.svg",
"Open link",
false,
@@ -3133,7 +3131,7 @@ impl SettingsWindow {
let folder_path = path.clone();
actions = actions.child(self.row_button(
- ("recording-folder", index),
+ "recording-folder",
"icons/folder.svg",
"Open recording bundle",
false,
@@ -3141,7 +3139,7 @@ impl SettingsWindow {
move |_this, _window, cx| library::open_recording_folder(&folder_path, mode, cx),
));
actions.child(self.row_button(
- ("recording-delete", index),
+ "recording-delete",
"icons/trash.svg",
"Delete",
false,
@@ -5349,3 +5347,6 @@ mod tests {
assert_eq!(format_project_name("{nope}"), "{nope}");
}
}
+
+#[cfg(test)]
+mod recording_action_tests;
diff --git a/apps/desktop-gpui/src/settings_window/recording_action_tests.rs b/apps/desktop-gpui/src/settings_window/recording_action_tests.rs
new file mode 100644
index 000000000..7ff584f07
--- /dev/null
+++ b/apps/desktop-gpui/src/settings_window/recording_action_tests.rs
@@ -0,0 +1,151 @@
+use super::*;
+use gpui::{Modifiers, MouseButton, TestAppContext, VisualTestContext, point, size};
+
+struct RecordingActions {
+ settings: Entity<SettingsWindow>,
+}
+
+impl Render for RecordingActions {
+ fn render(&mut self, _: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
+ self.settings.update(cx, |settings, cx| {
+ settings.render_recordings_list(cx).into_any_element()
+ })
+ }
+}
+
+fn settings_fixture(window: &mut Window, cx: &mut Context<SettingsWindow>) -> SettingsWindow {
+ let input = cx.new(|cx| ui::TextInputState::single_line(window, cx));
+ let scroll = ScrollHandle::new();
+ SettingsWindow {
+ theme: Theme::light(),
+ page: Page::Recordings,
+ settings: GeneralSettings::default(),
+ menu: None,
+ page_scroll: scroll.clone(),
+ instant_quality_anchor: ScrollAnchor::for_handle(scroll.clone()),
+ studio_quality_anchor: ScrollAnchor::for_handle(scroll),
+ quality_scroll_request: None,
+ has_cap_pro: false,
+ plan_refresh_pending: false,
+ plan_refresh_failed: false,
+ instant_quality_notice: None,
+ studio_quality_save_failed: false,
+ pages: crate::settings_pages::PagesState::new(window, cx),
+ project_name: String::new(),
+ server_url: String::new(),
+ project_name_input: input.clone(),
+ server_url_input: input.clone(),
+ _field_events: std::array::from_fn(|_| gpui::Subscription::new(|| {})),
+ recordings: Recordings::new(input.clone()),
+ screenshots: Screenshots::new(input),
+ placeholders: ui::CollapsibleState::new(false),
+ placeholders_task: None,
+ slider_track: Rc::new(Cell::new(None)),
+ slider_dragging: false,
+ windows: Vec::new(),
+ focus: cx.focus_handle(),
+ _sign_in_subscriptions: std::array::from_fn(|_| gpui::Subscription::new(|| {})),
+ user_profile: None,
+ profile_image: None,
+ keyboard_nav: false,
+ _keyboard_nav: gpui::Subscription::new(|| {}),
+ }
+}
+
+fn row(root: &str) -> RecordingRow {
+ RecordingRow {
+ item: RecordingItem {
+ path: PathBuf::from(root).join("same-name.cap"),
+ pretty_name: "Same name".into(),
+ mode: RecordingMode::Studio,
+ status: library::RecordingStatus::Complete,
+ upload: None,
+ clip_count: 1,
+ sharing: Some(format!("https://cap.test/{root}")),
+ sort_time_millis: 0.,
+ thumbnail: None,
+ thumbnail_version: None,
+ },
+ thumbnail: None,
+ thumbnail_stale: false,
+ }
+}
+
+fn draw(cx: &mut VisualTestContext) {
+ cx.update(|window, cx| {
+ window.refresh();
+ window.draw(cx).clear();
+ });
+}
+
+/// Settings' recordings list showing "first" above "second", drawn once.
+fn open_two_recordings(cx: &mut TestAppContext) -> (Entity<SettingsWindow>, VisualTestContext) {
+ let window = cx.open_window(size(px(600.), px(300.)), |window, cx| RecordingActions {
+ settings: cx.new(|cx| {
+ let mut settings = settings_fixture(window, cx);
+ settings.recordings.items = Some(vec![row("first"), row("second")]);
+ settings
+ }),
+ });
+ let settings = window
+ .read_with(cx, |view, _| view.settings.clone())
+ .unwrap();
+ let mut visual = VisualTestContext::from_window(window.into(), cx);
+ draw(&mut visual);
+ (settings, visual)
+}
+
+#[gpui::test]
+fn held_recording_action_does_not_move_to_a_different_recording(cx: &mut TestAppContext) {
+ let (settings, mut visual) = open_two_recordings(cx);
+ let link = point(px(432.), px(40.));
+ visual.simulate_mouse_move(link, None, Modifiers::none());
+ visual.simulate_mouse_down(link, MouseButton::Left, Modifiers::none());
+ settings.update(&mut visual, |settings, _| {
+ settings.recordings.items.as_mut().unwrap().remove(0);
+ });
+ draw(&mut visual);
+ visual.simulate_mouse_up(link, MouseButton::Left, Modifiers::none());
+ assert_eq!(visual.opened_url(), None);
+ visual.simulate_click(link, Modifiers::none());
+ assert_eq!(
+ visual.opened_url().as_deref(),
+ Some("https://cap.test/second")
+ );
+}
+
+#[gpui::test]
+fn held_recording_action_survives_an_unchanged_refresh(cx: &mut TestAppContext) {
+ let (settings, mut visual) = open_two_recordings(cx);
+ let link = point(px(432.), px(40.));
+ visual.simulate_mouse_move(link, None, Modifiers::none());
+ visual.simulate_mouse_down(link, MouseButton::Left, Modifiers::none());
+ settings.update(&mut visual, |settings, _| {
+ settings.recordings.items = Some(vec![row("first"), row("second")]);
+ });
+ draw(&mut visual);
+ visual.simulate_mouse_up(link, MouseButton::Left, Modifiers::none());
+ assert_eq!(
+ visual.opened_url().as_deref(),
+ Some("https://cap.test/first")
+ );
+}
+
+#[gpui::test]
+fn held_recording_action_follows_the_same_recording_after_reordering(cx: &mut TestAppContext) {
+ let (settings, mut visual) = open_two_recordings(cx);
+ let old_link = point(px(432.), px(116.));
+ let new_link = point(px(432.), px(40.));
+ visual.simulate_mouse_move(old_link, None, Modifiers::none());
+ visual.simulate_mouse_down(old_link, MouseButton::Left, Modifiers::none());
+ settings.update(&mut visual, |settings, _| {
+ settings.recordings.items.as_mut().unwrap().remove(0);
+ });
+ draw(&mut visual);
+ visual.simulate_mouse_move(new_link, Some(MouseButton::Left), Modifiers::none());
+ visual.simulate_mouse_up(new_link, MouseButton::Left, Modifiers::none());
+ assert_eq!(
+ visual.opened_url().as_deref(),
+ Some("https://cap.test/second")
+ );
+}