A modern terminal with a built-in multiplexer: close it and your sessions keep running. Run shells and tons of AI agents across your machines, then pick them up from desktop, browser, TUI and more. Written in Rust with no UI framework: interface drawn straight on the GPU. No Electron, GPUI or webview.
Rust agentagent-orchestrationaiai-agentsclaude-codecodexcoding-agentlinuxmacosmultiplayermultiplexerrustterminalterminal-emulatorterminal-multiplexertmuxtuiweztermwindows
Improve Note scrolling and Space-aware sidebar reflow
RoversX78be73b68ab1
diff --git a/wezterm-gui/src/markdown_editor/host.rs b/wezterm-gui/src/markdown_editor/host.rsindex bd82882c..a0003913 100644--- a/wezterm-gui/src/markdown_editor/host.rs+++ b/wezterm-gui/src/markdown_editor/host.rs@@ -110,7 +110,15 @@ pub(crate) struct NoteHostState {pub spelling_context: Option<Range<usize>>,pub spellcheck_scheduled_revision: Option<u64>,pub spellcheck_in_flight_revision: Option<u64>,- pub native_text_input_snapshot_key: Option<(u64, usize, usize, u32, usize)>,+ /// (revision, selection start, selection end, wrap key). Scroll position+ /// is deliberately NOT part of the key: rebuilding the snapshot per+ /// scrolled frame copied up to 64 KB of text and thousands of hit rects.+ pub native_text_input_snapshot_key: Option<(u64, usize, usize, usize)>,+ /// Scroll offset (bits) the current snapshot's hit rects were built at,+ /// so a settle refresh can catch them up after scrolling stops.+ pub native_text_input_snapshot_scroll: Option<u32>,+ /// Last time the Note body scroll offset actually changed.+ pub last_scroll_change: Option<Instant>,pub native_text_input_token: u64,pub parse_requested_revision: Option<u64>,pub parse_in_flight_revision: Option<u64>,@@ -118,6 +126,11 @@ pub(crate) struct NoteHostState {/// link had no cached resolution; a background parse round trip owes us/// the authoritative vault lookup.links_resolution_pending: bool,+ /// Memoized `caret_reveal_start` for the current projection, keyed by+ /// (revision, caret). `active_syntax` is a linear scan over every syntax+ /// node and refresh runs once per paint; a pure scroll frame must not pay+ /// for it.+ reveal_key_cache: Option<(u64, usize, usize)>,background_wrap_preferred: bool,background_wrap_requested_key: Option<NoteBackgroundWrapKey>,background_wrap_generation: u64,@@ -171,10 +184,13 @@ impl Default for NoteHostState {spellcheck_scheduled_revision: None,spellcheck_in_flight_revision: None,native_text_input_snapshot_key: None,+ native_text_input_snapshot_scroll: None,+ last_scroll_change: None,native_text_input_token: 0,parse_requested_revision: None,parse_in_flight_revision: None,links_resolution_pending: false,+ reveal_key_cache: None,background_wrap_preferred: false,background_wrap_requested_key: None,background_wrap_generation: 0,@@ -227,6 +243,7 @@ impl NoteHostState {self.parse_requested_revision = None;self.parse_in_flight_revision = None;self.links_resolution_pending = false;+ self.reveal_key_cache = None;self.background_wrap_preferred = false;self.background_wrap_requested_key = None;let snapshot = self@@ -248,6 +265,9 @@ impl NoteHostState {let end = progressive_preview_end(&snapshot.source, PROGRESSIVE_PREVIEW_BYTES);let source = &snapshot.source[..end];self.projection = MarkdownProjection::parse(source);+ // Deliberately uncached: this is a partial seed projection that the+ // full background parse replaces.+ self.reveal_key_cache = None;let active_start = self.projection.caret_reveal_start(0);self.visual = Arc::new(build_visual_document(source,@@ -277,6 +297,7 @@ impl NoteHostState {self.parse_requested_revision = None;self.parse_in_flight_revision = None;self.links_resolution_pending = false;+ self.reveal_key_cache = None;self.background_wrap_preferred = false;self.background_wrap_requested_key = None;}@@ -414,6 +435,7 @@ impl NoteHostState {self.links_resolution_pending = links_missing_resolution|| (any_links && matches!(self.display_source, NoteDisplaySource::Published));self.projection = projection;+ self.reveal_key_cache = None;let code_starts = self.projection.objects@@ -434,9 +456,19 @@ impl NoteHostState {}self.visual_key = None;}- let active_start = self- .projection- .caret_reveal_start(self.view.selection.focus.byte);+ let caret = self.view.selection.focus.byte;+ let active_start = match self.reveal_key_cache {+ Some((cached_revision, cached_caret, result))+ if cached_revision == revision && cached_caret == caret =>+ {+ result+ }+ _ => {+ let result = self.projection.caret_reveal_start(caret);+ self.reveal_key_cache = Some((revision, caret, result));+ result+ }+ };let key = (revision, self.view.mode, active_start);if self.visual_key != Some(key) {if projection_stage.is_none() {@@ -516,6 +548,7 @@ impl NoteHostState {}let active_start = projection.caret_reveal_start(caret);self.projection = projection;+ self.reveal_key_cache = Some((revision, caret, active_start));// The worker performed the authoritative vault-link resolution.self.links_resolution_pending = false;self.visual = Arc::new(visual);@@ -984,7 +1017,11 @@ impl NoteHostState {let old = self.view.scroll_offset;self.view.scroll_offset += delta;self.clamp_scroll();- (old - self.view.scroll_offset).abs() > f32::EPSILON+ let changed = (old - self.view.scroll_offset).abs() > f32::EPSILON;+ if changed {+ self.last_scroll_change = Some(Instant::now());+ }+ changed}pub(crate) fn toggle_code_block(&mut self, source_start: usize) -> bool {@@ -1072,6 +1109,24 @@ mod state_tests {assert!(host.background_parse_request().is_some());}+ #[test]+ fn reveal_cache_tracks_caret_moves_within_one_revision() {+ let source = "A **bold** word";+ let mut host = host_with_source(source);+ let key_outside = host.visual_key;++ host.view.selection = SourceSelection::caret(source.find("bold").unwrap());+ host.refresh_projection();+ assert_ne!(host.visual_key, key_outside, "caret inside strong reveals");++ host.view.selection = SourceSelection::caret(0);+ host.refresh_projection();+ assert_eq!(+ host.visual_key, key_outside,+ "memoized reveal key must not stick to the previous caret"+ );+ }+#[test]fn small_note_wiki_links_resolve_via_background_parse_not_ui_thread() {let source = "See [[Other Note]] for details.";diff --git a/wezterm-gui/src/termwindow/mod.rs b/wezterm-gui/src/termwindow/mod.rsindex 6e5ba46e..5b1585b2 100644--- a/wezterm-gui/src/termwindow/mod.rs+++ b/wezterm-gui/src/termwindow/mod.rs@@ -1137,6 +1137,14 @@ pub struct TermWindow {right_sidebar_file_preview_width: usize,right_sidebar_note_pane_expanded: bool,right_sidebar_note_pane_width: usize,+ /// Cached `space_note_vault(active_space_id).is_some()` so the hot layout+ /// predicate does not lock the workspace store; refreshed on space/vault+ /// changes and self-healed once per Note paint.+ active_space_has_note_vault: bool,+ /// Sidebar width from before a Space switch whose reflow must wait until+ /// the destination mux window is adopted (`switch_to_mux_window`), so the+ /// resize/SIGWINCH never hits the Space being left.+ pending_sidebar_reflow_width: Option<usize>,right_sidebar_file_preview_generation: u64,right_sidebar_file_preview_highlight_cancel: Arc<AtomicUsize>,right_sidebar_file_preview_lines: Vec<RightSidebarFilePreviewLine>,@@ -1579,6 +1587,8 @@ impl TermWindow {});let workspace_layout_structure_fingerprint =crate::workspace_threads::window_layout_structure_fingerprint(mux_window_id);+ let active_space_has_note_vault =+ crate::workspace_threads::space_note_vault(&active_space_id).is_some();let myself = Self {created: Instant::now(),@@ -1783,6 +1793,8 @@ impl TermWindow {right_sidebar_note_pane_expanded:crate::native_settings::right_sidebar_note_pane_expanded(),right_sidebar_note_pane_width: ui::right_sidebar_note_pane_width_for_dpi(dpi as usize),+ active_space_has_note_vault,+ pending_sidebar_reflow_width: None,right_sidebar_file_preview_generation: 0,right_sidebar_file_preview_highlight_cancel: Arc::new(AtomicUsize::new(0)),right_sidebar_file_preview_lines: Vec::new(),@@ -2583,7 +2595,10 @@ impl TermWindow {.gui_window_for_mux_window(mux_window_id).is_some(){- // Already showing it and the mapping is current; nothing to do.+ // Already showing it and the mapping is current; a deferred+ // Space-switch reflow can settle now since there is no other+ // window to protect from the resize.+ self.consume_pending_sidebar_reflow();return;}@@ -2603,6 +2618,11 @@ impl TermWindow {self.invalidate_fancy_tab_bar();self.invalidate_modal();+ // The destination window is adopted; a Space switch that changed the+ // sidebar width can resize the terminal now without touching the+ // window being left.+ self.consume_pending_sidebar_reflow();+// Tab::resize updates the local split tree but deliberately skips// remote mirror panes. Force the per-pane GUI-sized resize now rather// than waiting for a later paint (a connection view may still be up).@@ -2967,27 +2987,60 @@ impl TermWindow {self.invalidate_window_if(crate::workspace_threads::refresh_all_thread_work());}- fn switch_space(&mut self, space_id: String, window: &Window) {+ pub(crate) fn switch_space(&mut self, space_id: String, window: &Window) {+ self.switch_space_to_thread(space_id, None, window);+ }++ /// Switch this window to another Space, activating `preferred_thread`+ /// when given (a notification jump) instead of the Space's recorded+ /// active thread — starting both activations can leave the window on the+ /// wrong thread when the first one materializes asynchronously.+ ///+ /// Returns false when no navigation happened (the destination Space is+ /// already shown in another window).+ pub(crate) fn switch_space_to_thread(+ &mut self,+ space_id: String,+ preferred_thread: Option<String>,+ window: &Window,+ ) -> bool {if self.active_space_id == space_id {- window.invalidate();- return;+ if let Some(thread_id) = preferred_thread {+ self.activate_workspace_thread(thread_id, window);+ } else {+ window.invalidate();+ }+ return true;}self.snapshot_active_workspace_thread_layout();self.workspace_sidebar_pending_thread_selection = None;if !crate::workspace_threads::switch_window_space(self.space_owner_id, &space_id) {window.invalidate();- return;+ return false;}+ let previous_sidebar_width = self.right_sidebar_width();self.set_content_view_active(false);self.active_space_id = space_id.clone();+ self.refresh_active_space_note_vault_flag();self.sync_content_view_surfaces_with_mux();self.workspace_sidebar_scroll_offset = 0.0;- if let Some(thread_id) = crate::workspace_threads::ensure_active_thread_for_space(&space_id)- {+ // Vault availability differs between Spaces; an expanded Note pane+ // can activate or deactivate here and the terminal must follow. The+ // reflow is deferred until switch_to_mux_window adopts the+ // destination (activation may attach or materialize asynchronously)+ // so the resize and its SIGWINCH reach the new Space's PTYs, not the+ // Space being left.+ self.pending_sidebar_reflow_width = Some(previous_sidebar_width);+ let target_thread = preferred_thread+ .or_else(|| crate::workspace_threads::ensure_active_thread_for_space(&space_id));+ if let Some(thread_id) = target_thread {self.activate_workspace_thread(thread_id, window);} else {+ // Nothing to adopt; settle the reflow immediately.+ self.consume_pending_sidebar_reflow();window.invalidate();}+ true}/// Delete a remote Space AND end its sessions on the server: send a@@ -3034,9 +3087,15 @@ impl TermWindow {Ok(deleted) => {let deleted_active_space = self.active_space_id == space_id;if deleted_active_space {+ let previous_sidebar_width = self.right_sidebar_width();self.active_space_id = deleted.fallback_space_id.clone();+ self.refresh_active_space_note_vault_flag();self.sync_content_view_surfaces_with_mux();self.workspace_sidebar_scroll_offset = 0.0;+ // Deferred until the fallback Space's mux window is+ // adopted so the resize does not hit the deleted Space's+ // panes.+ self.pending_sidebar_reflow_width = Some(previous_sidebar_width);if let Some(window) = window {if let Some(thread_id) =crate::workspace_threads::ensure_active_thread_for_space(@@ -3045,8 +3104,11 @@ impl TermWindow {{self.activate_workspace_thread(thread_id, window);} else {+ self.consume_pending_sidebar_reflow();window.invalidate();}+ } else {+ self.consume_pending_sidebar_reflow();}}diff --git a/wezterm-gui/src/termwindow/ui/right_sidebar.rs b/wezterm-gui/src/termwindow/ui/right_sidebar.rsindex a4ff1ac8..d8928604 100644--- a/wezterm-gui/src/termwindow/ui/right_sidebar.rs+++ b/wezterm-gui/src/termwindow/ui/right_sidebar.rs@@ -878,8 +878,17 @@ impl crate::TermWindow {&& self.right_sidebar_mode == RightSidebarMode::Tasks&& self.right_sidebar_note_pane_expanded// Without a vault there is no editor to expand; the sidebar shows- // the choose/create-vault UI inline instead of a blank pane.- && workspace_threads::space_note_vault(&self.active_space_id).is_some()+ // the choose/create-vault UI inline instead of a blank pane. This+ // is a cached flag: the predicate sits inside right_sidebar_width+ // and must not lock the workspace store.+ && self.active_space_has_note_vault+ }++ /// Re-derive the cached vault flag from the store. Call after anything+ /// that can change the active Space or its vault binding.+ pub(crate) fn refresh_active_space_note_vault_flag(&mut self) {+ self.active_space_has_note_vault =+ workspace_threads::space_note_vault(&self.active_space_id).is_some();}pub(crate) fn right_sidebar_note_pane_width(&self) -> Option<usize> {@@ -2883,6 +2892,11 @@ impl crate::TermWindow {};let result = workspace_threads::set_space_note_vault(&space_id, path, managed);notify_window.notify(TermWindowNotif::Apply(Box::new(move |term_window| {+ let previous_sidebar_width = term_window.right_sidebar_width();+ term_window.refresh_active_space_note_vault_flag();+ // Binding a vault can activate an expanded Note pane in a+ // Space that previously had none.+ term_window.reflow_right_sidebar_if_width_changed(previous_sidebar_width);term_window.right_sidebar_note_open_generation = term_window.right_sidebar_note_open_generation.wrapping_add(1);@@ -4203,15 +4217,28 @@ impl crate::TermWindow {};let selection = self.right_sidebar_note.view.selection.range();let revision = session.lock().revision();- let key = (- revision,- selection.start,- selection.end,- self.right_sidebar_note.view.scroll_offset.to_bits(),- wrap_key,- );+ let key = (revision, selection.start, selection.end, wrap_key);+ let scroll_bits = self.right_sidebar_note.view.scroll_offset.to_bits();if self.right_sidebar_note.native_text_input_snapshot_key == Some(key) {- return;+ if self.right_sidebar_note.native_text_input_snapshot_scroll == Some(scroll_bits) {+ return;+ }+ // Only the scroll moved: the text/selection are still correct and+ // just the hit rects are stale. Rebuilding costs a large context+ // copy plus per-glyph rects, so skip it while the user is still+ // scrolling; the caret-blink repaint performs the settle refresh+ // shortly after the scroll stops.+ const SNAPSHOT_SCROLL_SETTLE: Duration = Duration::from_millis(150);+ if self+ .right_sidebar_note+ .last_scroll_change+ .is_some_and(|changed| changed.elapsed() < SNAPSHOT_SCROLL_SETTLE)+ {+ // Guarantee a repaint shortly after scrolling settles so the+ // refresh below runs even if nothing else invalidates.+ self.update_next_frame_time(Some(Instant::now() + SNAPSHOT_SCROLL_SETTLE));+ return;+ }}const MAX_NATIVE_CONTEXT_BYTES: usize = 64 * 1024;@@ -4288,6 +4315,7 @@ impl crate::TermWindow {hits,}));self.right_sidebar_note.native_text_input_snapshot_key = Some(key);+ self.right_sidebar_note.native_text_input_snapshot_scroll = Some(scroll_bits);}fn schedule_right_sidebar_note_autosave_wakeup(window: window::Window, delay: Duration) {@@ -4642,6 +4670,29 @@ impl crate::TermWindow {}}+ /// Reflow the terminal when a state change (space switch, vault binding,+ /// pane activation) altered the computed sidebar width; a bare invalidate+ /// leaves the terminal sized for the old width and the difference shows+ /// as dead space.+ pub(crate) fn reflow_right_sidebar_if_width_changed(&mut self, previous_width: usize) {+ if self.right_sidebar_width() == previous_width {+ return;+ }+ if let Some(window) = self.window.as_ref().cloned() {+ let dimensions = self.dimensions;+ self.apply_dimensions(&dimensions, None, &window);+ window.invalidate();+ }+ }++ /// Settle a Space-switch reflow that was deferred until the destination+ /// mux window was adopted.+ pub(crate) fn consume_pending_sidebar_reflow(&mut self) {+ if let Some(previous_width) = self.pending_sidebar_reflow_width.take() {+ self.reflow_right_sidebar_if_width_changed(previous_width);+ }+ }+pub(crate) fn toggle_right_sidebar_note_pane(&mut self) {self.right_sidebar_note_pane_expanded = !self.right_sidebar_note_pane_expanded;if let Err(err) = crate::native_settings::save_right_sidebar_note_pane_expanded(@@ -5529,7 +5580,8 @@ impl crate::TermWindow {return Ok(());}RightSidebarMode::Tasks => {- self.paint_note_sidebar(+ let stage = crate::input_diagnostics::StageTimer::begin("note_paint");+ let result = self.paint_note_sidebar(layers,&ui_font,ui_metrics,@@ -5541,7 +5593,9 @@ impl crate::TermWindow {content_width,rect.y.saturating_add(rect.height),base_font_size,- )?;+ );+ stage.finish(result.is_ok());+ result?;return Ok(());}}@@ -5883,7 +5937,9 @@ impl crate::TermWindow {base_font_size: f64,) -> anyhow::Result<()> {self.right_sidebar_note_table_layouts.clear();- if let Some(vault) = workspace_threads::space_note_vault(&self.active_space_id) {+ let vault = workspace_threads::space_note_vault(&self.active_space_id);+ self.active_space_has_note_vault = vault.is_some();+ if let Some(vault) = vault {self.refresh_note_vault_index_if_needed(&vault.root);}if !self.ensure_active_right_sidebar_note_document() {@@ -6622,7 +6678,14 @@ impl crate::TermWindow {|| self.right_sidebar_note.line_geometry.len() != visual.lines.len(){let mut top = padding;- let mut geometry = Vec::with_capacity(visual.lines.len());+ // Reuse the previous geometry allocation when this host holds the+ // only reference (paint-local clones are dropped each frame).+ let mut geometry = Arc::try_unwrap(std::mem::take(+ &mut self.right_sidebar_note.line_geometry,+ ))+ .unwrap_or_default();+ geometry.clear();+ geometry.reserve(visual.lines.len());for (line_index, line) in visual.lines.iter().enumerate() {let metrics = match line.block {BlockKind::Heading(1) => h1_metrics,diff --git a/window/src/os/macos/window.rs b/window/src/os/macos/window.rsindex cbf3d22d..0bf56d77 100644--- a/window/src/os/macos/window.rs+++ b/window/src/os/macos/window.rs@@ -2895,22 +2895,22 @@ impl WindowView {let mut inner = myself.inner.borrow_mut();inner.ime_text = s.to_string();- /*- let key_is_down = inner.key_is_down.take().unwrap_or(true);-- let key = KeyCode::composed(s);-- let event = KeyEvent {- key,- modifiers: Modifiers::NONE,- repeat_count: 1,- key_is_down,- }- .normalize_shift();+ // Advise the GUI right here rather than relying on the keyDown+ // handler's post-interpretKeyEvents dispatch: keyboard IMEs are+ // always driven by a key event, but dictation updates the marked+ // text spontaneously with no key event at all, and without this+ // the live transcription is invisible until the final insertText.+ // The keyDown path may advise the same status again; that is a+ // harmless duplicate.+ let status = if inner.ime_text.is_empty() {+ DeadKeyStatus::None+ } else {+ DeadKeyStatus::Composing(inner.ime_text.clone())+ };+ inner+ .events+ .dispatch(WindowEvent::AdviseDeadKeyStatus(status));- inner.ime_last_event.replace(event.clone());- inner.events.dispatch(WindowEvent::KeyEvent(event));- */inner.ime_last_event.take();inner.ime_state = ImeDisposition::Acted;}@@ -2924,6 +2924,9 @@ impl WindowView {// but iterm doesn't... and we've never seen// this get called so far?inner.ime_text.clear();+ inner+ .events+ .dispatch(WindowEvent::AdviseDeadKeyStatus(DeadKeyStatus::None));inner.ime_last_event.take();inner.ime_state = ImeDisposition::Acted;}@@ -3394,9 +3397,18 @@ impl WindowView {let precise_scroll_delta = if precise&& (raw_vert_delta.abs() > f64::EPSILON || raw_horz_delta.abs() > f64::EPSILON){+ // scrollingDeltaX/Y are in points; mouse coordinates and all of+ // the chrome geometry consuming this payload are in backing+ // pixels (see the convertRectToBacking in mouse_common). Convert+ // here so every consumer receives uniform physical pixels.+ let rect = NSRect::new(+ NSPoint::new(0., 0.),+ NSSize::new(raw_horz_delta, raw_vert_delta),+ );+ let backing = unsafe { NSView::convertRectToBacking(this as id, rect) };Some(PreciseScrollDelta {- x: raw_horz_delta as f32,- y: raw_vert_delta as f32,+ x: f64::copysign(backing.size.width, raw_horz_delta) as f32,+ y: f64::copysign(backing.size.height, raw_vert_delta) as f32,})} else {None