Cursor height stays lined with text instead of line

This commit is contained in:
James Magahern
2026-11-17 18:23:56 -08:00
parent 119df745fc
commit 2fa33e5e8c
4 changed files with 46 additions and 22 deletions
+2 -3
View File
@@ -113,9 +113,8 @@ impl CursorAnimator {
Self::default()
}
/// Cursor bounds are in window coordinates and cover a full terminal cell
/// (two cells for a wide block cursor). Pass the desired fill alpha in color;
/// an overlay renderer can use a translucent color to leave text readable.
/// Cursor bounds cover the text height and the width of one or two columns
/// in window coordinates. The fill alpha controls how much text shows through.
pub fn set_target(
&mut self,
bounds: Bounds<Pixels>,
+12 -1
View File
@@ -23,23 +23,34 @@ pub struct TerminalFont {
pub size: f32,
pub cell_width: f32,
pub line_height: f32,
pub text_height: f32,
}
impl TerminalFont {
fn new(face: Font, size: f32, text_system: &TextSystem) -> Self {
let id = text_system.resolve_font(&face);
let cell_width = text_system
.advance(text_system.resolve_font(&face), px(size), 'M')
.advance(id, px(size), 'M')
.map(|advance| f32::from(advance.width))
.unwrap_or(size * 0.6)
.max(1.);
// Match the shaped line's ascent and descent, excluding interline spacing.
// The native font backends can report descent as a signed distance.
let text_height = f32::from(text_system.ascent(id, px(size)))
+ f32::from(text_system.descent(id, px(size))).abs();
Self {
face,
size,
cell_width,
line_height: (size * 1.8).round(),
text_height,
}
}
pub fn text_top(&self) -> f32 {
(self.line_height - self.text_height) / 2.
}
pub fn adjust_size(&mut self, delta: f32, text_system: &TextSystem) -> bool {
let size = (self.size + delta).clamp(6., 72.);
if size == self.size {
+29 -15
View File
@@ -93,7 +93,7 @@ impl Renderer {
if cached.cells == cells {
continue;
}
let (backgrounds, decorations, cursors) = assemble_quads(&cells, self.font.line_height);
let (backgrounds, decorations, cursors) = assemble_quads(&cells, &self.font);
let text = assemble_text(&cells)
.into_iter()
.map(|segment| {
@@ -303,8 +303,10 @@ impl Renderer {
fn assemble_quads(
cells: &[RenderCell],
line_height: f32,
font: &TerminalFont,
) -> (Vec<Span>, Vec<Decoration>, Vec<Decoration>) {
let top = font.text_top();
let bottom = top + font.text_height;
let mut backgrounds: Vec<Span> = Vec::new();
let mut decorations: Vec<Decoration> = Vec::new();
let mut cursors = Vec::new();
@@ -328,7 +330,7 @@ fn assemble_quads(
column,
columns,
cell.foreground,
line_height - 2.,
bottom - 2.,
);
}
if flags.contains(Flags::STRIKEOUT) {
@@ -337,13 +339,13 @@ fn assemble_quads(
column,
columns,
cell.foreground,
line_height / 2.,
top + font.text_height / 2.,
);
}
}
let (y, height, width) = match cell.cursor {
CursorShape::Beam => (0., line_height, Some(2.)),
CursorShape::Underline => (line_height - 2., 2., None),
CursorShape::Beam => (top, font.text_height, Some(2.)),
CursorShape::Underline => (bottom - 2., 2., None),
_ => continue,
};
cursors.push(Decoration {
@@ -492,6 +494,16 @@ fn assemble_text(cells: &[RenderCell]) -> Vec<TextSegment> {
mod tests {
use super::*;
fn test_font(line_height: f32) -> TerminalFont {
TerminalFont {
face: gpui::font("Test Mono"),
size: 15.,
cell_width: 9.,
line_height,
text_height: 18.,
}
}
fn cell(c: char) -> RenderCell {
RenderCell {
cell: Cell {
@@ -508,7 +520,7 @@ mod tests {
fn blank_screen_has_no_text_or_background_work() {
let cells = vec![cell(' '); 250];
assert!(assemble_text(&cells).is_empty());
let (backgrounds, decorations, cursors) = assemble_quads(&cells, 20.);
let (backgrounds, decorations, cursors) = assemble_quads(&cells, &test_font(20.));
assert!(backgrounds.is_empty() && decorations.is_empty() && cursors.is_empty());
}
@@ -563,7 +575,7 @@ mod tests {
}
#[test]
fn decorations_and_cursors_follow_the_terminal_line_height() {
fn cursors_and_decorations_stay_with_the_text_as_row_spacing_increases() {
let mut cells = vec![cell('a'), cell('b')];
cells[0]
.cell
@@ -571,12 +583,14 @@ mod tests {
.insert(Flags::UNDERLINE | Flags::STRIKEOUT);
cells[0].cursor = CursorShape::Underline;
cells[1].cursor = CursorShape::Beam;
for height in [8., 20., 96.] {
let (_, decorations, cursors) = assemble_quads(&cells, height);
assert_eq!(decorations[0].y, height - 2.);
assert_eq!(decorations[1].y, height / 2.);
assert_eq!(cursors[0].y + cursors[0].height, height);
assert_eq!(cursors[1].height, height);
for (row_height, top) in [(18., 0.), (27., 4.5), (40., 11.)] {
let font = test_font(row_height);
let (_, decorations, cursors) = assemble_quads(&cells, &font);
let bottom = top + font.text_height;
assert_eq!(decorations[0].y, bottom - 2.);
assert_eq!(decorations[1].y, row_height / 2.);
assert_eq!((cursors[0].y, cursors[0].height), (bottom - 2., 2.));
assert_eq!((cursors[1].y, cursors[1].height), (top, font.text_height));
}
}
@@ -587,7 +601,7 @@ mod tests {
c.background = rgb(0x123456).into();
c.cell.flags.insert(Flags::UNDERLINE | Flags::STRIKEOUT);
}
let (backgrounds, decorations, _) = assemble_quads(&cells, 20.);
let (backgrounds, decorations, _) = assemble_quads(&cells, &test_font(20.));
assert_eq!(backgrounds.len(), 1);
assert_eq!((backgrounds[0].column, backgrounds[0].columns), (2, 5));
assert_eq!(decorations.len(), 2);
+3 -3
View File
@@ -1154,11 +1154,11 @@ impl TerminalPane {
bounds.origin
+ point(
px(PADDING + f32::from(cursor.column) * font.cell_width),
px(PADDING + cursor.row as f32 * font.line_height),
px(PADDING + cursor.row as f32 * font.line_height + font.text_top()),
),
size(
px(font.cell_width * if wide { 2. } else { 1. }),
px(font.line_height),
px(font.text_height),
),
);
let mut cursor_color = colors::resolve(Color::Named(NamedColor::Cursor), &palette);
@@ -1281,7 +1281,7 @@ impl Render for TerminalPane {
pane.preedit.clone().into(), px(font.size), &[run], None,
);
let origin = pane.cursor_bounds.origin;
let height = px(font.line_height);
let height = pane.cursor_bounds.size.height;
let _ = line.paint_background(origin, height, window, cx);
let _ = line.paint(origin, height, window, cx);
}