Skip to content

armgit / zed

Public Imported from GitHub
0

Code at the speed of thought – Zed is a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.

gpui: Add helper methods for em width and em advance (#24036)

Marshall Bowers990bdde5e889
diff --git a/crates/editor/src/editor.rs b/crates/editor/src/editor.rs
index f83cc53c516..0f387e4c704 100644
--- a/crates/editor/src/editor.rs
+++ b/crates/editor/src/editor.rs
@@ -14124,13 +14124,7 @@ impl Editor {
let font_id = window.text_system().resolve_font(&style.text.font());
let font_size = style.text.font_size.to_pixels(window.rem_size());
let line_height = style.text.line_height_in_pixels(window.rem_size());
-
- let em_width = window
- .text_system()
- .typographic_bounds(font_id, font_size, 'm')
- .unwrap()
- .size
- .width;
+ let em_width = window.text_system().em_width(font_id, font_size).unwrap();
gpui::Point::new(em_width, line_height)
}
diff --git a/crates/editor/src/element.rs b/crates/editor/src/element.rs
index 2a98ea4d6b9..7c37416bfc2 100644
--- a/crates/editor/src/element.rs
+++ b/crates/editor/src/element.rs
@@ -836,12 +836,7 @@ impl EditorElement {
let style = editor.style.clone().unwrap_or_default();
let font_id = window.text_system().resolve_font(&style.text.font());
let font_size = style.text.font_size.to_pixels(window.rem_size());
- let em_width = window
- .text_system()
- .typographic_bounds(font_id, font_size, 'm')
- .unwrap()
- .size
- .width;
+ let em_width = window.text_system().em_width(font_id, font_size).unwrap();
let scroll_margin_x = EditorSettings::get_global(cx).horizontal_scroll_margin;
@@ -6398,12 +6393,8 @@ impl Element for EditorElement {
window.text_system().resolve_font(&style.text.font());
let font_size =
style.text.font_size.to_pixels(window.rem_size());
- let em_width = window
- .text_system()
- .typographic_bounds(font_id, font_size, 'm')
- .unwrap()
- .size
- .width;
+ let em_width =
+ window.text_system().em_width(font_id, font_size).unwrap();
size(line.width + em_width, height)
},
@@ -6480,17 +6471,8 @@ impl Element for EditorElement {
let font_id = window.text_system().resolve_font(&style.text.font());
let font_size = style.text.font_size.to_pixels(window.rem_size());
let line_height = style.text.line_height_in_pixels(window.rem_size());
- let em_width = window
- .text_system()
- .typographic_bounds(font_id, font_size, 'm')
- .unwrap()
- .size
- .width;
- let em_advance = window
- .text_system()
- .advance(font_id, font_size, 'm')
- .unwrap()
- .width;
+ let em_width = window.text_system().em_width(font_id, font_size).unwrap();
+ let em_advance = window.text_system().em_advance(font_id, font_size).unwrap();
let letter_size = size(em_width, line_height);
@@ -8060,17 +8042,8 @@ fn compute_auto_height_layout(
let font_id = window.text_system().resolve_font(&style.text.font());
let font_size = style.text.font_size.to_pixels(window.rem_size());
let line_height = style.text.line_height_in_pixels(window.rem_size());
- let em_width = window
- .text_system()
- .typographic_bounds(font_id, font_size, 'm')
- .unwrap()
- .size
- .width;
- let em_advance = window
- .text_system()
- .advance(font_id, font_size, 'm')
- .unwrap()
- .width;
+ let em_width = window.text_system().em_width(font_id, font_size).unwrap();
+ let em_advance = window.text_system().em_advance(font_id, font_size).unwrap();
let mut snapshot = editor.snapshot(window, cx);
let gutter_dimensions = snapshot.gutter_dimensions(
diff --git a/crates/gpui/src/text_system.rs b/crates/gpui/src/text_system.rs
index 9c3b574442c..6b9256e6e7c 100644
--- a/crates/gpui/src/text_system.rs
+++ b/crates/gpui/src/text_system.rs
@@ -195,6 +195,20 @@ impl TextSystem {
Ok(result * font_size)
}
+ /// Returns the width of an `em`.
+ ///
+ /// Uses the width of the `m` character in the given font and size.
+ pub fn em_width(&self, font_id: FontId, font_size: Pixels) -> Result<Pixels> {
+ Ok(self.typographic_bounds(font_id, font_size, 'm')?.size.width)
+ }
+
+ /// Returns the advance width of an `em`.
+ ///
+ /// Uses the advance width of the `m` character in the given font and size.
+ pub fn em_advance(&self, font_id: FontId, font_size: Pixels) -> Result<Pixels> {
+ Ok(self.advance(font_id, font_size, 'm')?.width)
+ }
+
/// Get the number of font size units per 'em square',
/// Per MDN: "an abstract square whose height is the intended distance between
/// lines of type in the same type size"
diff --git a/crates/markdown/src/markdown.rs b/crates/markdown/src/markdown.rs
index 5112a680635..ceebc3547ef 100644
--- a/crates/markdown/src/markdown.rs
+++ b/crates/markdown/src/markdown.rs
@@ -528,12 +528,7 @@ impl MarkdownElement {
let text_style = self.style.base_text_style.clone();
let font_id = window.text_system().resolve_font(&text_style.font());
let font_size = text_style.font_size.to_pixels(window.rem_size());
- let em_width = window
- .text_system()
- .typographic_bounds(font_id, font_size, 'm')
- .unwrap()
- .size
- .width;
+ let em_width = window.text_system().em_width(font_id, font_size).unwrap();
window.request_autoscroll(Bounds::from_corners(
point(position.x - 3. * em_width, position.y - 3. * line_height),
point(position.x + 3. * em_width, position.y + 3. * line_height),