From a88f0788b1b81d94028d13141db7ce3844a798bc Mon Sep 17 00:00:00 2001 From: "Aatman Jain (AJ)" <257399449+AatmanAJ@users.noreply.github.com> Date: Wed, 14 Oct 2026 12:17:52 +0100 Subject: [PATCH 1/2] perf(desktop-gpui): repaint the recording bar only when it changes The bar repainted 4 times a second for an M:SS timer, and on every 0.5dB meter change for a 20pt level track. Wake the timer once per displayed second (1s while paused), and repaint for the meter only when its fill moves by a device pixel and the bar is shown. --- apps/desktop-gpui/src/app_windows.rs | 6 +- apps/desktop-gpui/src/controls_window.rs | 84 +++++++++++++++++++----- 2 files changed, 74 insertions(+), 16 deletions(-) diff --git a/apps/desktop-gpui/src/app_windows.rs b/apps/desktop-gpui/src/app_windows.rs index ac747c962ad..ba6ae9f7ab5 100644 --- a/apps/desktop-gpui/src/app_windows.rs +++ b/apps/desktop-gpui/src/app_windows.rs @@ -6314,9 +6314,13 @@ pub fn arm_target_mode(kind: crate::main_window::TargetType, cx: &mut App) { /// Repaint the bar between its own 250ms ticks -- the mic meter updates at /// ~20Hz and the bar is never the active window. pub fn refresh_controls_window(cx: &mut App) { + let level_db = crate::feeds::Feeds::global(cx).read(cx).mic_level_db; if let Some(handle) = cx.global::().controls { handle - .update(cx, |_, window, cx| { + .update(cx, |controls, window, cx| { + if !controls.level_bar_moves(level_db) { + return; + } cx.notify(); window.refresh(); }) diff --git a/apps/desktop-gpui/src/controls_window.rs b/apps/desktop-gpui/src/controls_window.rs index acb02cbc8b1..4a6568d562f 100644 --- a/apps/desktop-gpui/src/controls_window.rs +++ b/apps/desktop-gpui/src/controls_window.rs @@ -32,6 +32,9 @@ pub struct ControlsWindow { /// Repaints the timer. An inactive window is repainted lazily by the /// platform, so the tick both notifies and asks for a frame explicitly. _tick: gpui::Task<()>, + /// The mic level bar's fill in device pixels as last drawn, `None` while + /// the bar is hidden. + drawn_level_px: std::cell::Cell>, } #[derive(Clone, Copy)] @@ -60,23 +63,26 @@ impl ControlsWindow { let theme = Theme::for_window(window, cx, false); cx.observe(&session, |_, _, cx| cx.notify()).detach(); - // 4 ticks a second keeps the seconds display honest without burning a - // frame budget; `refresh` (not just `notify`) because this window is - // usually not the active one while a recording runs. + // The timer shows whole seconds, so wake just after each one ticks + // over; `refresh` (not just `notify`) because this window is usually + // not the active one while a recording runs. let tick = cx.spawn_in(window, async move |this, cx| { + let mut delay = Duration::from_secs(1); loop { - cx.background_executor() - .timer(Duration::from_millis(250)) - .await; - if this - .update_in(cx, |_, window, cx| { - cx.notify(); - window.refresh(); - }) - .is_err() - { + cx.background_executor().timer(delay).await; + let Ok(next) = this.update_in(cx, |this, window, cx| { + cx.notify(); + window.refresh(); + let session = this.session.read(cx); + if matches!(session.phase, Phase::Recording { paused: false }) { + until_next_second(session.elapsed()) + } else { + Duration::from_secs(1) + } + }) else { return; - } + }; + delay = next; } }); @@ -87,9 +93,17 @@ impl ControlsWindow { #[cfg(target_os = "linux")] confirmation_pending: false, _tick: tick, + drawn_level_px: std::cell::Cell::new(None), } } + /// Whether a new mic level would move the bar by at least a device pixel. + pub fn level_bar_moves(&self, db: f64) -> bool { + self.drawn_level_px + .get() + .is_some_and(|drawn| drawn != level_bar_px(db)) + } + /// `formatTime`: `M:SS`, hours folded into minutes the same way. fn format_elapsed(elapsed: Duration) -> String { let total = elapsed.as_secs(); @@ -200,7 +214,10 @@ impl ControlsWindow { let can_mute = session.mode() == Some(RecordingMode::Instant) && session.has_microphone() && matches!(session.phase, Phase::Recording { .. }); - let level = feeds::bar_level(Feeds::global(cx).read(cx).mic_level_db); + let level_db = Feeds::global(cx).read(cx).mic_level_db; + let level = feeds::bar_level(level_db); + self.drawn_level_px + .set((self.has_microphone && !muted).then(|| level_bar_px(level_db))); let icon = if !self.has_microphone || muted { "icons/mic-off.svg" @@ -540,3 +557,40 @@ fn recording_issue_text(error: String) -> impl IntoElement + Styled { error }) } + +/// The bar's track is 20pt wide; 40 steps covers a 2x display. +fn level_bar_px(db: f64) -> u32 { + (feeds::bar_level(db) * 40.0).round() as u32 +} + +fn until_next_second(elapsed: Duration) -> Duration { + Duration::from_secs(1) + .saturating_sub(Duration::from_nanos(u64::from(elapsed.subsec_nanos()))) + .max(Duration::from_millis(20)) +} + +#[cfg(test)] +mod repaint_tests { + use super::*; + + #[test] + fn the_timer_wakes_once_per_displayed_second() { + assert_eq!( + until_next_second(Duration::from_millis(3_250)), + Duration::from_millis(750) + ); + assert_eq!( + until_next_second(Duration::from_millis(3_995)), + Duration::from_millis(20) + ); + assert_eq!(until_next_second(Duration::ZERO), Duration::from_secs(1)); + } + + #[test] + fn level_changes_under_a_device_pixel_do_not_move_the_bar() { + assert_eq!(level_bar_px(-30.0), level_bar_px(-30.5)); + assert_ne!(level_bar_px(-30.0), level_bar_px(-32.0)); + assert_eq!(level_bar_px(-96.0), 0); + assert_eq!(level_bar_px(0.0), 40); + } +} From 04c21d5ec6de8543778fe2453b558d7b7f303a93 Mon Sep 17 00:00:00 2001 From: "Aatman Jain (AJ)" <257399449+AatmanAJ@users.noreply.github.com> Date: Thu, 15 Oct 2026 07:24:24 +0100 Subject: [PATCH 2/2] fix(desktop-gpui): size the bar meter to the display and re-time the clock on resume Count meter pixels with the window's scale factor instead of assuming a 2x display, and respawn the timer tick whenever the clock starts or stops so a resume wakes at the next displayed second rather than after a full paused-state second. --- apps/desktop-gpui/src/app_windows.rs | 6 +- apps/desktop-gpui/src/controls_window.rs | 127 ++++++++++++++++------- 2 files changed, 90 insertions(+), 43 deletions(-) diff --git a/apps/desktop-gpui/src/app_windows.rs b/apps/desktop-gpui/src/app_windows.rs index ba6ae9f7ab5..7956e0e7a71 100644 --- a/apps/desktop-gpui/src/app_windows.rs +++ b/apps/desktop-gpui/src/app_windows.rs @@ -6311,14 +6311,14 @@ pub fn arm_target_mode(kind: crate::main_window::TargetType, cx: &mut App) { .ok(); } -/// Repaint the bar between its own 250ms ticks -- the mic meter updates at -/// ~20Hz and the bar is never the active window. +/// Repaint the bar between its once-a-second ticks -- the mic meter updates at +/// up to ~20Hz and the bar is never the active window. pub fn refresh_controls_window(cx: &mut App) { let level_db = crate::feeds::Feeds::global(cx).read(cx).mic_level_db; if let Some(handle) = cx.global::().controls { handle .update(cx, |controls, window, cx| { - if !controls.level_bar_moves(level_db) { + if !controls.level_bar_moves(level_db, window.scale_factor()) { return; } cx.notify(); diff --git a/apps/desktop-gpui/src/controls_window.rs b/apps/desktop-gpui/src/controls_window.rs index 4a6568d562f..4636cf7e03b 100644 --- a/apps/desktop-gpui/src/controls_window.rs +++ b/apps/desktop-gpui/src/controls_window.rs @@ -32,6 +32,7 @@ pub struct ControlsWindow { /// Repaints the timer. An inactive window is repainted lazily by the /// platform, so the tick both notifies and asks for a frame explicitly. _tick: gpui::Task<()>, + ticking_live: bool, /// The mic level bar's fill in device pixels as last drawn, `None` while /// the bar is hidden. drawn_level_px: std::cell::Cell>, @@ -61,47 +62,70 @@ impl ControlsWindow { ) -> Self { crate::theme::bind_window(window, cx); let theme = Theme::for_window(window, cx, false); - cx.observe(&session, |_, _, cx| cx.notify()).detach(); - - // The timer shows whole seconds, so wake just after each one ticks - // over; `refresh` (not just `notify`) because this window is usually - // not the active one while a recording runs. - let tick = cx.spawn_in(window, async move |this, cx| { - let mut delay = Duration::from_secs(1); - loop { - cx.background_executor().timer(delay).await; - let Ok(next) = this.update_in(cx, |this, window, cx| { - cx.notify(); - window.refresh(); - let session = this.session.read(cx); - if matches!(session.phase, Phase::Recording { paused: false }) { - until_next_second(session.elapsed()) - } else { - Duration::from_secs(1) - } - }) else { - return; - }; - delay = next; + cx.observe_in(&session, window, |this, _, window, cx| { + let live = this.timer_is_live(cx); + if live != this.ticking_live { + this.ticking_live = live; + this._tick = Self::spawn_tick(window, cx); } - }); + cx.notify(); + }) + .detach(); - Self { + let mut this = Self { session, theme, has_microphone, #[cfg(target_os = "linux")] confirmation_pending: false, - _tick: tick, + _tick: gpui::Task::ready(()), + ticking_live: false, drawn_level_px: std::cell::Cell::new(None), - } + }; + this.ticking_live = this.timer_is_live(cx); + this._tick = Self::spawn_tick(window, cx); + this + } + + fn timer_is_live(&self, cx: &gpui::App) -> bool { + matches!( + self.session.read(cx).phase, + Phase::Recording { paused: false } + ) + } + + /// The timer shows whole seconds, so wake just after each one ticks over; + /// `refresh` (not just `notify`) because this window is usually not the + /// active one while a recording runs. Respawned whenever the timer starts + /// or stops running, so a resume lines up with the next second. + fn spawn_tick(window: &mut Window, cx: &mut Context) -> gpui::Task<()> { + cx.spawn_in(window, async move |this, cx| { + loop { + let Ok(delay) = this.update_in(cx, |this, _, cx| { + let session = this.session.read(cx); + tick_delay(this.timer_is_live(cx), session.elapsed()) + }) else { + return; + }; + cx.background_executor().timer(delay).await; + if this + .update_in(cx, |_, window, cx| { + cx.notify(); + window.refresh(); + }) + .is_err() + { + return; + } + } + }) } /// Whether a new mic level would move the bar by at least a device pixel. - pub fn level_bar_moves(&self, db: f64) -> bool { + pub fn level_bar_moves(&self, db: f64, scale: f32) -> bool { self.drawn_level_px .get() - .is_some_and(|drawn| drawn != level_bar_px(db)) + .is_some_and(|drawn| drawn != level_bar_px(db, scale)) } /// `formatTime`: `M:SS`, hours folded into minutes the same way. @@ -207,7 +231,7 @@ impl ControlsWindow { /// gray-10 track). In instant mode with a live mic it doubles as the mute /// button -- studio deliberately does not expose mute, since the mic is an /// editable track and muted spans would bake zeros in. - fn render_microphone(&self, cx: &mut Context) -> impl IntoElement { + fn render_microphone(&self, scale: f32, cx: &mut Context) -> impl IntoElement { let theme = self.theme; let session = self.session.read(cx); let muted = session.mic_muted; @@ -217,7 +241,7 @@ impl ControlsWindow { let level_db = Feeds::global(cx).read(cx).mic_level_db; let level = feeds::bar_level(level_db); self.drawn_level_px - .set((self.has_microphone && !muted).then(|| level_bar_px(level_db))); + .set((self.has_microphone && !muted).then(|| level_bar_px(level_db, scale))); let icon = if !self.has_microphone || muted { "icons/mic-off.svg" @@ -352,7 +376,7 @@ impl ControlsWindow { .detach(); } - fn render_bar(&mut self, cx: &mut Context) -> impl IntoElement { + fn render_bar(&mut self, scale: f32, cx: &mut Context) -> impl IntoElement { let theme = self.theme; let session = self.session.read(cx); let paused = session.is_paused(); @@ -410,7 +434,7 @@ impl ControlsWindow { .items_center() .gap(px(2.)) .flex_shrink_0() - .child(self.render_microphone(cx)) + .child(self.render_microphone(scale, cx)) .child( self.action_button( "pause", @@ -535,7 +559,7 @@ impl Render for ControlsWindow { .child(recording_issue_text(issue)), ) }) - .child(self.render_bar(cx)) + .child(self.render_bar(window.scale_factor(), cx)) } } @@ -558,9 +582,17 @@ fn recording_issue_text(error: String) -> impl IntoElement + Styled { }) } -/// The bar's track is 20pt wide; 40 steps covers a 2x display. -fn level_bar_px(db: f64) -> u32 { - (feeds::bar_level(db) * 40.0).round() as u32 +/// The bar's track is 20pt wide. +fn level_bar_px(db: f64, scale: f32) -> u32 { + (feeds::bar_level(db) * 20.0 * f64::from(scale)).round() as u32 +} + +fn tick_delay(live: bool, elapsed: Duration) -> Duration { + if live { + until_next_second(elapsed) + } else { + Duration::from_secs(1) + } } fn until_next_second(elapsed: Duration) -> Duration { @@ -586,11 +618,26 @@ mod repaint_tests { assert_eq!(until_next_second(Duration::ZERO), Duration::from_secs(1)); } + #[test] + fn a_resumed_timer_wakes_at_the_next_second_and_a_paused_one_idles() { + assert_eq!( + tick_delay(true, Duration::from_millis(3_900)), + Duration::from_millis(100) + ); + assert_eq!( + tick_delay(false, Duration::from_millis(3_900)), + Duration::from_secs(1) + ); + } + #[test] fn level_changes_under_a_device_pixel_do_not_move_the_bar() { - assert_eq!(level_bar_px(-30.0), level_bar_px(-30.5)); - assert_ne!(level_bar_px(-30.0), level_bar_px(-32.0)); - assert_eq!(level_bar_px(-96.0), 0); - assert_eq!(level_bar_px(0.0), 40); + assert_eq!(level_bar_px(-30.0, 2.0), level_bar_px(-30.5, 2.0)); + assert_ne!(level_bar_px(-30.0, 2.0), level_bar_px(-32.0, 2.0)); + assert_eq!(level_bar_px(-30.0, 1.0), level_bar_px(-31.0, 1.0)); + assert_ne!(level_bar_px(-30.0, 1.0), level_bar_px(-33.0, 1.0)); + assert_eq!(level_bar_px(-96.0, 2.0), 0); + assert_eq!(level_bar_px(0.0, 1.0), 20); + assert_eq!(level_bar_px(0.0, 2.0), 40); } }