Open source Loom alternative. Beautiful, shareable screen recordings.
Rust appcapcossloommacnextjsnextjs15open-sourceossreactrecordrustscreen-capturescreen-recorderscreenshotsolidjstauritauri-apptypescriptvite
perf(desktop-gpui): rebuild only the sidebar on caption download ticks and sync editor fields without cloning
Richie McIlroy8d42a1c06bd2
diff --git a/apps/desktop-gpui/src/editor_panels.rs b/apps/desktop-gpui/src/editor_panels.rsindex 17ed08d00..ec9bdbc5d 100644--- a/apps/desktop-gpui/src/editor_panels.rs+++ b/apps/desktop-gpui/src/editor_panels.rs@@ -2732,31 +2732,41 @@ impl EditorWindow {/// while it has focus. Runs from `render`, like `sync_hex_inputs`, because/// the focus test needs a `&Window`.pub(crate) fn sync_fields(&mut self, window: &mut Window, cx: &mut Context<Self>) {- for (key, input) in self.fields.clone() {- if input.read(cx).focus_handle().is_focused(window) {+ // Every field ever created is checked on every render, so the value is+ // written into one reused buffer and only copied out when it moved.+ let mut value = String::new();+ for (key, input) in &self.fields {+ let state = input.read(cx);+ if state.is_focused(window) {continue;}- let Some(value) = self.field_value(key) else {+ value.clear();+ if self.write_field_value(*key, &mut value).is_none() {continue;- };- if input.read(cx).text() != value {- input.update(cx, |input, cx| input.set_text(value, cx));+ }+ if state.text() != value {+ let text = value.clone();+ input.update(cx, |input, cx| input.set_text(text, cx));}}}- /// What a field should read when it is not being typed into.- fn field_value(&self, key: FieldKey) -> Option<String> {+ /// What a field should read when it is not being typed into, appended to+ /// `out`. `None` leaves the field alone.+ fn write_field_value(&self, key: FieldKey, out: &mut String) -> Option<()> {+ use std::fmt::Write as _;+// The crop boxes read the open cropper, not the project: their value// is `crop()[props.field]`, i.e. `realBounds` (`Editor.tsx:1182`).if let FieldKey::Crop(field) = key {let state = self.crop.as_ref()?;- return Some(ui::format_number(field.read(state.real())));+ out.push_str(&ui::format_number(field.read(state.real())));+ return Some(());}let timeline = self.project.timeline.as_ref()?;- Some(match key {- FieldKey::StyleName(index) => timeline.style_segments.get(index)?.name.clone(),- FieldKey::ImageName(index) => timeline.image_segments.get(index)?.name.clone(),+ match key {+ FieldKey::StyleName(index) => out.push_str(&timeline.style_segments.get(index)?.name),+ FieldKey::ImageName(index) => out.push_str(&timeline.image_segments.get(index)?.name),FieldKey::StyleCrop(index, axis) => {let background = timeline.style_segments@@ -2769,37 +2779,40 @@ impl EditorWindow {position: XY::new(0, 0),size: XY::new(width, height),});- match axis {+ let value = match axis {0 => crop.position.x,1 => crop.position.y,2 => crop.size.x,_ => crop.size.y,+ };+ let _ = write!(out, "{value}");+ }+ FieldKey::TextContent(index) => {+ out.push_str(&timeline.text_segments.get(index)?.content)+ }+ FieldKey::CaptionText(index) => {+ out.push_str(&timeline.caption_segments.get(index)?.text)+ }+ FieldKey::AudioName(index) => {+ if let Some(name) = &timeline.audio_segments.get(index)?.name {+ out.push_str(name);}- .to_string()}- FieldKey::TextContent(index) => timeline.text_segments.get(index)?.content.clone(),- FieldKey::CaptionText(index) => timeline.caption_segments.get(index)?.text.clone(),- FieldKey::AudioName(index) => timeline- .audio_segments- .get(index)?- .name- .clone()- .unwrap_or_default(),FieldKey::KeyboardText(index) => {- timeline.keyboard_segments.get(index)?.display_text.clone()+ out.push_str(&timeline.keyboard_segments.get(index)?.display_text)}// `value={props.segment.start.toFixed(2)}` on all four.FieldKey::CaptionStart(index) => {- format!("{:.2}", timeline.caption_segments.get(index)?.start)+ let _ = write!(out, "{:.2}", timeline.caption_segments.get(index)?.start);}FieldKey::CaptionEnd(index) => {- format!("{:.2}", timeline.caption_segments.get(index)?.end)+ let _ = write!(out, "{:.2}", timeline.caption_segments.get(index)?.end);}FieldKey::KeyboardStart(index) => {- format!("{:.2}", timeline.keyboard_segments.get(index)?.start)+ let _ = write!(out, "{:.2}", timeline.keyboard_segments.get(index)?.start);}FieldKey::KeyboardEnd(index) => {- format!("{:.2}", timeline.keyboard_segments.get(index)?.end)+ let _ = write!(out, "{:.2}", timeline.keyboard_segments.get(index)?.end);}// `Math.round((props.value ?? 0) * 1000)` -- the offset field is in// milliseconds (`:6182`).@@ -2810,10 +2823,11 @@ impl EditorWindow {.iter().find(|item| item.index as usize == clip).map_or(0., |item| kind.read(&item.offsets));- ui::format_number(f64::from((offset * 1000.).round()))+ out.push_str(&ui::format_number(f64::from((offset * 1000.).round())));}_ => return None,- })+ }+ Some(())}/// A live keystroke in a segment field.diff --git a/apps/desktop-gpui/src/editor_tabs.rs b/apps/desktop-gpui/src/editor_tabs.rsindex 5dec243e4..9ab09bb06 100644--- a/apps/desktop-gpui/src/editor_tabs.rs+++ b/apps/desktop-gpui/src/editor_tabs.rs@@ -3140,9 +3140,10 @@ impl EditorWindow {.detach();}- /// One tick of [`Self::spawn_caption_download_poll`].+ /// One tick of [`Self::spawn_caption_download_poll`]. Only the captions+ /// tab reads the download, so only the sidebar is rebuilt.pub(crate) fn repaint_caption_download(&mut self, cx: &mut Context<Self>) {- cx.notify();+ self.invalidate_sidebar(cx);}/// `downloadModel` (`CaptionsTab.tsx:643-690`). The download runs on thediff --git a/apps/desktop-gpui/src/editor_window.rs b/apps/desktop-gpui/src/editor_window.rsindex 64a71db98..c9c031c45 100644--- a/apps/desktop-gpui/src/editor_window.rs+++ b/apps/desktop-gpui/src/editor_window.rs@@ -3909,6 +3909,13 @@ impl EditorWindow {self.transport_controls.update(cx, |_, cx| cx.notify());}+ /// State only the sidebar draws, such as a caption model download's+ /// progress. Same idea as [`Self::invalidate_hover_chrome`]: the header,+ /// toolbar, transport and timeline stay cached.+ pub(crate) fn invalidate_sidebar(&mut self, cx: &mut Context<Self>) {+ self.sidebar_view.update(cx, |_, cx| cx.notify());+ }+fn set_hovered_track(&mut self,track: Option<TrackKind>,@@ -12985,6 +12992,141 @@ mod tests {cx.update(|window, _| assert!(input_focus.is_focused(window)));}+ // gpui's test windows have no platform window handle on macOS or Windows, which the editor needs.+ #[cfg(target_os = "linux")]+ #[gpui::test]+ fn caption_download_tick_rebuilds_only_the_sidebar(cx: &mut gpui::TestAppContext) {+ use std::cell::Cell;++ let project = tempfile::tempdir().unwrap();+ let project_path = project.path().to_path_buf();+ let (editor, cx) = cx+ .add_window_view(move |window, cx| EditorWindow::new(project_path.clone(), window, cx));+ editor.update_in(cx, |editor, window, cx| {+ editor.set_error("captions".into(), window, cx);+ });+ let sidebar = editor.read_with(cx, |editor, _| editor.sidebar_view.clone());+ let editor_notifies = Rc::new(Cell::new(0));+ let sidebar_notifies = Rc::new(Cell::new(0));+ let _subscriptions = cx.update(|_, cx| {+ let editor_count = editor_notifies.clone();+ let sidebar_count = sidebar_notifies.clone();+ [+ cx.observe(&editor, move |_, _| {+ editor_count.set(editor_count.get() + 1)+ }),+ cx.observe(&sidebar, move |_, _| {+ sidebar_count.set(sidebar_count.get() + 1)+ }),+ ]+ });+ cx.run_until_parked();+ editor_notifies.set(0);+ sidebar_notifies.set(0);++ editor.update(cx, |editor, cx| editor.repaint_caption_download(cx));++ assert_eq!(editor_notifies.get(), 0);+ assert_eq!(sidebar_notifies.get(), 1);+ }++ // gpui's test windows have no platform window handle on macOS or Windows, which the editor needs.+ #[cfg(target_os = "linux")]+ #[gpui::test]+ fn field_sync_rewrites_only_unfocused_fields_whose_value_moved(cx: &mut gpui::TestAppContext) {+ use crate::{editor_panels::FieldKey, editor_tabs::OffsetKind};++ fn text(+ editor: &Entity<EditorWindow>,+ key: FieldKey,+ cx: &mut gpui::VisualTestContext,+ ) -> String {+ editor.read_with(cx, |editor, cx| {+ editor.field(key).unwrap().read(cx).text().to_string()+ })+ }++ let project = tempfile::tempdir().unwrap();+ let project_path = project.path().to_path_buf();+ let (editor, cx) = cx+ .add_window_view(move |window, cx| EditorWindow::new(project_path.clone(), window, cx));+ let keys = [+ FieldKey::CaptionText(0),+ FieldKey::CaptionStart(0),+ FieldKey::CaptionEnd(0),+ FieldKey::CaptionText(1),+ FieldKey::KeyboardText(0),+ FieldKey::KeyboardEnd(0),+ FieldKey::SyncOffset(0, OffsetKind::Mic),+ ];+ editor.update_in(cx, |editor, window, cx| {+ editor.set_error("fields".into(), window, cx);+ editor.project.timeline = Some(TimelineConfiguration {+ style_segments: Vec::new(),+ image_segments: Vec::new(),+ segments: Vec::new(),+ transitions: Vec::new(),+ zoom_segments: Vec::new(),+ scene_segments: Vec::new(),+ mask_segments: Vec::new(),+ text_segments: Vec::new(),+ caption_segments: vec![+ serde_json::from_value(serde_json::json!({+ "id": "caption",+ "start": 1.25,+ "end": 3.5,+ "text": "Hello",+ }))+ .unwrap(),+ ],+ keyboard_segments: vec![+ serde_json::from_value(serde_json::json!({+ "id": "keys",+ "start": 0.5,+ "end": 2.0,+ "displayText": "Cmd K",+ }))+ .unwrap(),+ ],+ audio_segments: Vec::new(),+ camera3d_segments: Vec::new(),+ });+ for key in keys {+ editor.ensure_field(key, window, cx);+ }+ editor.sync_fields(window, cx);+ });+ let texts = keys.map(|key| text(&editor, key, cx));+ assert_eq!(texts, ["Hello", "1.25", "3.50", "", "Cmd K", "2.00", "0"]);++ // An unchanged value leaves the field, and its selection, alone.+ let caption = editor.read_with(cx, |editor, _| {+ editor.field(FieldKey::CaptionText(0)).unwrap().clone()+ });+ caption.update(cx, |input, cx| input.select_all_text(cx));+ editor.update_in(cx, |editor, window, cx| editor.sync_fields(window, cx));+ assert_eq!(caption.read_with(cx, |input, _| input.selection()), 0..5);++ // A moved value is rewritten, except into the field being typed in.+ let start_focus = editor.read_with(cx, |editor, cx| {+ editor+ .field(FieldKey::CaptionStart(0))+ .unwrap()+ .read(cx)+ .focus_handle()+ });+ editor.update_in(cx, |editor, window, cx| {+ window.focus(&start_focus, cx);+ let segment = &mut editor.project.timeline.as_mut().unwrap().caption_segments[0];+ segment.start = 2.;+ segment.text = "Bye".into();+ editor.sync_fields(window, cx);+ });+ assert_eq!(text(&editor, FieldKey::CaptionText(0), cx), "Bye");+ assert_eq!(caption.read_with(cx, |input, _| input.selection()), 3..3);+ assert_eq!(text(&editor, FieldKey::CaptionStart(0), cx), "1.25");+ }+/// `default_editor_preview_resolution()` is asserted to be 1248x702 in the/// Tauri app itself (`lib.rs:192-194`); the render size of a display/// recording follows from it.diff --git a/apps/desktop-gpui/src/ui/text_input.rs b/apps/desktop-gpui/src/ui/text_input.rsindex cbf4628ab..b63f87883 100644--- a/apps/desktop-gpui/src/ui/text_input.rs+++ b/apps/desktop-gpui/src/ui/text_input.rs@@ -598,6 +598,11 @@ impl TextInputState {self.focus.clone()}+ /// `focus_handle().is_focused(window)` without cloning the handle.+ pub fn is_focused(&self, window: &Window) -> bool {+ self.focus.is_focused(window)+ }+fn invalidate_layout(&self) {*self.layout.borrow_mut() = None;}