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
fix(gui): fit UI text by advance everywhere and share the ellipsis check (ready for 0.1.2✊)
RoversX42e16fc49d72
diff --git a/wezterm-gui/src/settings_window.rs b/wezterm-gui/src/settings_window.rsindex db50f705..2dcf796b 100644--- a/wezterm-gui/src/settings_window.rs+++ b/wezterm-gui/src/settings_window.rs@@ -8388,16 +8388,15 @@ impl SettingsWindow {text_width,)?;let note_y = self.settings_row_description_y(y) + self.description_line_step() + extra;- self.draw_text(- layers,- &body_font,- x,- note_y,- &crate::i18n::tr("settings-app-icon-note"),- palette.muted_text,- text_width,- )?;- let bottom = (note_y + cell_height).max(label_y + cell_height) + self.ui_px(6.0);+ let last_note_y = note_y+ + self.draw_row_note(+ layers,+ x,+ note_y,+ &crate::i18n::tr("settings-app-icon-note"),+ text_width,+ )?;+ let bottom = (last_note_y + cell_height).max(label_y + cell_height) + self.ui_px(6.0);Ok((bottom - (y + self.settings_row_visual_height())).max(0.0))}@@ -9296,20 +9295,10 @@ impl SettingsWindow {lines.push(chunk.join(":"));}}- let step = self.description_line_step();let top = bottom + self.ui_px(20.0);- for (index, line) in lines.iter().enumerate() {- self.draw_text(- layers,- &body_font,- x,- top + step * index as f32,- line,- palette.text,- width,- )?;- }- bottom = top + step * (lines.len() - 1) as f32 + cell_height;+ bottom = top+ + self.draw_text_lines(layers, &body_font, x, top, &lines, palette.text, width)?+ + cell_height;}Ok((bottom + self.ui_px(6.0) - (y + self.settings_row_visual_height())).max(extra))}@@ -14876,19 +14865,50 @@ impl SettingsWindow {) -> anyhow::Result<f32> {let palette = self.palette();let body_font = Rc::clone(&self.body_font);- let y = self.settings_row_description_y(label_y);- let step = self.description_line_step();let lines = self.description_lines(&body_font, description, width);+ self.draw_text_lines(+ layers,+ &body_font,+ x,+ self.settings_row_description_y(label_y),+ &lines,+ palette.secondary_text,+ width,+ )+ }++ /// A row's note under its description, wrapped as the description is:+ /// one line in English is often two in French or German.+ fn draw_row_note(+ &self,+ layers: &mut TripleLayerQuadAllocator<'_>,+ x: f32,+ y: f32,+ note: &str,+ width: f32,+ ) -> anyhow::Result<f32> {+ let palette = self.palette();+ let body_font = Rc::clone(&self.body_font);+ let lines = self.description_lines(&body_font, note, width);+ self.draw_text_lines(layers, &body_font, x, y, &lines, palette.muted_text, width)+ }++ /// `lines` one under another from `y`, a description line apart. Returns+ /// how far below `y` the last of them starts.+ #[allow(clippy::too_many_arguments)]+ fn draw_text_lines(+ &self,+ layers: &mut TripleLayerQuadAllocator<'_>,+ font: &Rc<LoadedFont>,+ x: f32,+ y: f32,+ lines: &[String],+ color: LinearRgba,+ width: f32,+ ) -> anyhow::Result<f32> {+ let step = self.description_line_step();for (index, line) in lines.iter().enumerate() {- self.draw_text(- layers,- &body_font,- x,- y + step * index as f32,- line,- palette.secondary_text,- width,- )?;+ self.draw_text(layers, font, x, y + step * index as f32, line, color, width)?;}Ok(step * lines.len().saturating_sub(1) as f32)}@@ -15542,15 +15562,23 @@ impl SettingsWindow {let center_y = control_y + control_height / 2.0;let knob = self.ui_px(22.0);let groove = self.ui_px(4.0);- // As wide as the widest value it can show, with a gap from the knob:- // a fixed width cut "Default" short in English and longer in French.- let value_width = [window_opacity_label(None), window_opacity_label(Some(100))]+ // The value beside the track is as wide as the widest it can show --+ // a fixed width cut "Default" short in English, and more in French --+ // with a gap from the knob, but never takes the track below its+ // least, where the knob at opaque would run into it; then it is cut.+ let value_gap = self.ui_px(12.0);+ let track_least = self.ui_px(48.0);+ let widest_value = [window_opacity_label(None), window_opacity_label(Some(100))].iter().map(|label| self.measure_text_width(&ui_font, label))- .fold(self.ui_px(60.0), f32::max)- + self.ui_px(12.0);+ .fold(self.ui_px(60.0), f32::max);+ let value_width =+ (widest_value + value_gap).min((column_width - knob - track_least).max(0.0));+ let value_room = (value_width - value_gap).max(0.0);let track_left = control_x + knob / 2.0;- let track_width = (column_width - value_width - knob).max(self.ui_px(48.0));+ // Never past the column either, however narrow it gets.+ let track_width = (column_width - value_width - knob)+ .max(track_least.min((column_width - knob).max(0.0)));let along =f32::from(percent - WINDOW_OPACITY_LEAST) / f32::from(100 - WINDOW_OPACITY_LEAST);let knob_x = track_left + track_width * along;@@ -15591,8 +15619,10 @@ impl SettingsWindow {palette.control_border,knob / 2.0,)?;- let value = window_opacity_label(chosen);- let value_text_width = self.measure_text_width(&ui_font, &value).min(value_width);+ // Cut first and then measured, so a value that had to be cut still+ // sits flush right with the others.+ let value = self.text_with_ellipsis(&ui_font, &window_opacity_label(chosen), value_room);+ let value_text_width = self.measure_text_width(&ui_font, &value);self.draw_text(layers,&ui_font,@@ -15604,7 +15634,7 @@ impl SettingsWindow {} else {palette.secondary_text},- value_width,+ value_room,)?;if supported {self.ui.window_opacity_track = Some((track_left, track_width));@@ -15644,23 +15674,16 @@ impl SettingsWindow {}// Where the slider is not offered, and where it is not yet reliable,- // said plainly, as the app icon's row says where it works. Wrapped as- // the description is: in French and German it outgrows one line.+ // said plainly, as the app icon's row says where it works.let note_y = self.settings_row_description_y(y) + step + description_extra;- let note = crate::i18n::tr("settings-window-opacity-platforms");- let note_lines = self.description_lines(&body_font, ¬e, text_width);- for (index, line) in note_lines.iter().enumerate() {- self.draw_text(+ let last_note_y = note_y+ + self.draw_row_note(layers,- &body_font,x,- note_y + step * index as f32,- line,- palette.muted_text,+ note_y,+ &crate::i18n::tr("settings-window-opacity-platforms"),text_width,)?;- }- let last_note_y = note_y + step * note_lines.len().saturating_sub(1) as f32;let cell_height = self.metrics.cell_size.height as f32;let bottom = (last_note_y + cell_height).max(control_bottom) + self.ui_px(6.0);Ok((bottom - (y + self.settings_row_visual_height())).max(0.0))@@ -17667,9 +17690,8 @@ impl SettingsWindow {let right_edge = x + max_width;for glyph in &shaped.glyphs {- // Fitted by advance, as the measurement and the ellipsis are; see- // `DrawContext::draw_text_on_layer`.- if pos_x + glyph.x_advance.get() as f32 > right_edge + 0.5 {+ let advance = glyph.x_advance.get() as f32;+ if !crate::ui::draw::glyph_fits(pos_x, advance, right_edge) {break;}if let Some(texture) = glyph.texture.as_ref() {@@ -17691,10 +17713,7 @@ impl SettingsWindow {quad.set_fg_color(color);quad.set_hsv(None);}- pos_x += glyph.x_advance.get() as f32;- if pos_x > right_edge {- break;- }+ pos_x += advance;}Ok(())@@ -17853,7 +17872,11 @@ impl SettingsWindow {end = idx + text[idx..].chars().next().map_or(0, char::len_utf8);}- format!("{}{}", text[..end].trim_end(), ellipsis)+ // Checked whole, as it is drawn -- a string `draw_text` shapes anyway,+ // so the check leaves nothing extra in the cache unless it steps back.+ crate::ui::draw::fit_ellipsized(text, end, max_width, |candidate| {+ self.measure_text_width(font, candidate)+ })}fn native_settings_path() -> PathBuf {diff --git a/wezterm-gui/src/termwindow/box_model.rs b/wezterm-gui/src/termwindow/box_model.rsindex 407547a4..2e2c85e1 100644--- a/wezterm-gui/src/termwindow/box_model.rs+++ b/wezterm-gui/src/termwindow/box_model.rs@@ -632,7 +632,9 @@ impl super::TermWindow {.next().ok_or_else(|| anyhow!("info.cluster didn't map into string"))?;if let Some(key) = BlockKey::from_str(grapheme) {- if pixel_width + context.width.pixel_cell >= max_x {+ // From the pen, as for text glyphs: `max_x` is where+ // the element may end, not a width.+ if !crate::ui::draw::glyph_fits(x_pos, context.width.pixel_cell, max_x) {break;}pixel_width += context.width.pixel_cell;@@ -652,13 +654,10 @@ impl super::TermWindow {num_cells as u8,)?;- if let Some(texture) = glyph.texture.as_ref() {- let x_pos = x_pos + (glyph.x_offset + glyph.bearing_x).get() as f32;- let width = texture.coords.size.width as f32 * glyph.scale as f32;- if x_pos + width >= max_x {- break;- }- } else if x_pos + glyph.x_advance.get() as f32 >= max_x {+ // By advance, so text laid out to its measured width+ // keeps its last glyph; see `glyph_fits`.+ if !crate::ui::draw::glyph_fits(x_pos, glyph.x_advance.get() as f32, max_x)+ {break;}@@ -893,17 +892,18 @@ impl super::TermWindow {match &element.content {ComputedElementContent::Text(cells) => {let mut pos_x = element.content_rect.min_x();+ // Each cell is fitted by its advance, as layout fitted it+ // (see `glyph_fits`), so a mark with no advance at the edge+ // stays with its glyph.+ let max_x = element.content_rect.max_x();for cell in cells {- if pos_x >= element.content_rect.max_x() {- break;- }match cell {ElementCell::Sprite(sprite) => {let width = sprite.coords.width();let height = sprite.coords.height();let pos_y = top + element.content_rect.min_y();- if pos_x + width as f32 > element.content_rect.max_x() {+ if !crate::ui::draw::glyph_fits(pos_x, width as f32, max_x) {break;}@@ -925,9 +925,11 @@ impl super::TermWindow {- (glyph.y_offset + glyph.bearing_y).get() as f32+ element.baseline;- if pos_x + glyph.x_advance.get() as f32- > element.content_rect.max_x()- {+ if !crate::ui::draw::glyph_fits(+ pos_x,+ glyph.x_advance.get() as f32,+ max_x,+ ) {break;}let pos_x = pos_x + (glyph.x_offset + glyph.bearing_x).get() as f32;diff --git a/wezterm-gui/src/termwindow/render/fancy_tab_bar.rs b/wezterm-gui/src/termwindow/render/fancy_tab_bar.rsindex a7601615..5fc8482c 100644--- a/wezterm-gui/src/termwindow/render/fancy_tab_bar.rs+++ b/wezterm-gui/src/termwindow/render/fancy_tab_bar.rs@@ -1574,7 +1574,10 @@ impl crate::TermWindow {/// Draw a cached glyph run starting at pixel `start_x` (which may be left of/// `clip_left`, e.g. for a horizontally-scrolled line), clipping to/// `[clip_left, clip_right]`: glyphs fully left of `clip_left` advance the- /// pen but draw nothing, and drawing stops at `clip_right`. With+ /// pen but draw nothing, and drawing stops at the first whole glyph whose+ /// advance does not fit before `clip_right` (see `glyph_fits`). The last+ /// glyph's ink may reach a pixel or two past it; a surface that must not+ /// be drawn past uses `paint_cached_ui_shape_pixel_clipped`. With/// `start_x == clip_left` this is identical to the previous fixed-origin/// painter, so existing callers are unaffected.#[allow(clippy::too_many_arguments)]@@ -1615,7 +1618,7 @@ impl crate::TermWindow {let color = color_for(info);if let Some(key) = info.block_key {- if x_pos + advance > clip_right {+ if !crate::ui::draw::glyph_fits(x_pos, advance, clip_right) {break;}let sprite = glyph_cache.cached_block(key, metrics)?;@@ -1635,7 +1638,9 @@ impl crate::TermWindow {}let glyph = &info.glyph;- if x_pos + advance > clip_right {+ // By advance, so a label sized to its measured width keeps its+ // last glyph; see `glyph_fits`.+ if !crate::ui::draw::glyph_fits(x_pos, advance, clip_right) {break;}@@ -1644,9 +1649,6 @@ impl crate::TermWindow {let glyph_y = y - (glyph.y_offset + glyph.bearing_y).get() as f32 + baseline;let glyph_width = texture.coords.size.width as f32 * glyph.scale as f32;let glyph_height = texture.coords.size.height as f32 * glyph.scale as f32;- if glyph_x + glyph_width > clip_right {- break;- }let mut quad = layers.allocate(2)?;quad.set_position(glyph_x + left_offset,diff --git a/wezterm-gui/src/termwindow/ui/plugin_panel.rs b/wezterm-gui/src/termwindow/ui/plugin_panel.rsindex e97cec69..0fa0f90f 100644--- a/wezterm-gui/src/termwindow/ui/plugin_panel.rs+++ b/wezterm-gui/src/termwindow/ui/plugin_panel.rs@@ -1606,28 +1606,20 @@ impl TermWindow {if !spend(budget, showing) {return Ok(());}- match text.font {- Font::Ui => self.paint_cached_ui_shape_clipped(- layers,- metrics,- &shaped,- left,- top,- clip_left,- clip_right,- |_| color,- ),- Font::Mono => self.paint_cached_ui_shape_pixel_clipped(- layers,- metrics,- &shaped,- left,- top,- clip_left,- clip_right,- |_| color,- ),- }+ // The panel is a real clip -- lines scroll under its edges -- so text+ // of either font is cut there by pixel and never drawn past it,+ // whatever ink a glyph has beyond its advance. What shows is what+ // was charged above.+ self.paint_cached_ui_shape_pixel_clipped(+ layers,+ metrics,+ &shaped,+ left,+ top,+ clip_left,+ clip_right,+ |_| color,+ ).map(drop)}diff --git a/wezterm-gui/src/termwindow/ui/sidebar.rs b/wezterm-gui/src/termwindow/ui/sidebar.rsindex 20961d4a..b8c46cbf 100644--- a/wezterm-gui/src/termwindow/ui/sidebar.rs+++ b/wezterm-gui/src/termwindow/ui/sidebar.rs@@ -3675,10 +3675,14 @@ impl crate::TermWindow {.collect();let cut_byte = ellipsize_cut_byte(&glyphs, text.len(), budget).min(text.len());- let mut output = String::with_capacity(cut_byte + ELLIPSIS.len());- output.push_str(&text[..cut_byte]);- output.push_str(ELLIPSIS);- Ok(Cow::Owned(output))+ // Checked whole, as it will be drawn; a run that cannot be measured+ // keeps the cut as made.+ Ok(Cow::Owned(crate::ui::draw::fit_ellipsized(+ text,+ cut_byte,+ max_width,+ |candidate| self.sidebar_text_width(font, candidate).unwrap_or(0.0),+ )))}pub(crate) fn sidebar_text_width(diff --git a/wezterm-gui/src/ui/draw.rs b/wezterm-gui/src/ui/draw.rsindex 6833a65a..0c8efad2 100644--- a/wezterm-gui/src/ui/draw.rs+++ b/wezterm-gui/src/ui/draw.rs@@ -803,12 +803,8 @@ impl<'a> DrawContext<'a> {for info in infos {let glyph = glyph_cache.cached_glyph(&info, style, false, font, self.metrics, 1)?;- // Whether a glyph fits is a question for its advance, as it is- // for `measure_text_width` and the ellipsis. The ink a glyph- // draws past its advance -- rounding on a 1x screen, a heavier- // face -- is no reason to drop it, and a box sized to the- // measured text lost its last glyph to exactly that.- if pos_x + glyph.x_advance.get() as f32 > right_edge + 0.5 {+ let advance = glyph.x_advance.get() as f32;+ if !glyph_fits(pos_x, advance, right_edge) {break;}if let Some(texture) = glyph.texture.as_ref() {@@ -830,10 +826,7 @@ impl<'a> DrawContext<'a> {quad.set_fg_color(color);quad.set_hsv(None);}- pos_x += glyph.x_advance.get() as f32;- if pos_x > right_edge {- break;- }+ pos_x += advance;}Ok(())@@ -904,7 +897,9 @@ impl<'a> DrawContext<'a> {}}- format!("{}{}", text[..boundaries[best]].trim_end(), ellipsis)+ fit_ellipsized(text, boundaries[best], max_width, |candidate| {+ self.measure_text_width(font, candidate)+ })}}@@ -912,6 +907,43 @@ fn color_with_alpha(color: LinearRgba, alpha: f32) -> LinearRgba {LinearRgba(color.0, color.1, color.2, alpha.clamp(0.0, 1.0))}+/// Whether a glyph that starts at `pos_x` and moves the pen by `advance`+/// fits text that ends at `right_edge`.+///+/// Decided by the advance alone, as `measure_text_width` and the ellipsis+/// decide, so a box sized to its measured text keeps its last glyph: some+/// fonts draw a pixel or two of ink past their advance, and that ink is+/// drawn into the box's padding. Half a pixel of slack absorbs rounding+/// between measuring and drawing. Every whole-glyph painter of UI text+/// decides this way; a surface that must not draw past its edge clips by+/// pixel instead.+pub(crate) fn glyph_fits(pos_x: f32, advance: f32, right_edge: f32) -> bool {+ pos_x + advance <= right_edge + 0.5+}++/// The last step every ellipsizer shares: `text` cut at byte `end`, trailing+/// spaces dropped, "..." after it, measured whole as it will be drawn --+/// kerning at the join can make the two wider together than measured apart+/// -- and stepped back a character at a time until it fits `max_width`,+/// with the half pixel `glyph_fits` allows. `end` is a character boundary.+pub(crate) fn fit_ellipsized(+ text: &str,+ mut end: usize,+ max_width: f32,+ mut measure: impl FnMut(&str) -> f32,+) -> String {+ loop {+ let candidate = format!("{}...", text[..end].trim_end());+ if end == 0 || measure(&candidate) <= max_width + 0.5 {+ return candidate;+ }+ end = text[..end]+ .char_indices()+ .next_back()+ .map_or(0, |(idx, _)| idx);+ }+}+/// Rounded corners are rasterized as separate integer-sized sprites. Keep the/// rectangle that joins those sprites on the same physical-pixel grid or a/// fractional DPI scale can leave a one-pixel gap between the two halves of a@@ -1131,7 +1163,37 @@ impl RoundedFramePainter for DrawContext<'_> {#[cfg(test)]mod tests {- use super::pixel_snap_rounded_rect;+ use super::{fit_ellipsized, glyph_fits, pixel_snap_rounded_rect};++ #[test]+ fn an_ellipsized_string_is_checked_whole() {+ // Each character one wide, but "x." kerns two wider at the join: the+ // cut made on separate measurements does not fit and steps back.+ let kerned = |s: &str| s.chars().count() as f32 + if s.contains("x.") { 2.0 } else { 0.0 };+ assert_eq!(fit_ellipsized("abcxyz", 4, 8.0, kerned), "abc...");+ // Trailing spaces go before the ellipsis does.+ assert_eq!(+ fit_ellipsized("ab cd", 4, 8.0, |s: &str| s.len() as f32),+ "ab..."+ );+ // Nothing left of the text: the ellipsis alone.+ assert_eq!(+ fit_ellipsized("abc", 0, 1.0, |s: &str| s.len() as f32),+ "..."+ );+ }++ #[test]+ fn the_last_glyph_of_measured_text_is_kept() {+ // A label measured to end at 100: its last glyph is kept whatever ink+ // it draws past that, and so is rounding between the two passes.+ assert!(glyph_fits(92.0, 8.0, 100.0));+ assert!(glyph_fits(92.3, 8.0, 100.0));+ // An advance that does not fit is left out.+ assert!(!glyph_fits(93.0, 8.0, 100.0));+ // A mark with no advance at the edge stays with the glyph it marks.+ assert!(glyph_fits(100.0, 0.0, 100.0));+ }#[test]fn fractional_dpi_pill_keeps_a_filled_center_strip() {