git.lucas.co / cce-ui
GPU-accelerated UI toolkit (Vulkan)
git clone https://git.lucas.co/cce-ui.git

commitedcd41a8ccb6d82e4b4f88c25e0d29ff3a186207
parentdbd075364d
authorLucas Galante <[email protected]>
date2026-06-15 21:42
Standardize vertical text alignment and center TextBox vertically with TEXT_LINE_HEIGHT_MULTIPLIER

 src/layout.rs                | 14 ++++++++++++++
 src/widget/input/text_box.rs |  7 +++----
 2 files changed, 17 insertions(+), 4 deletions(-)

diff --git a/src/layout.rs b/src/layout.rs
index b8f62f0..6c3eca3 100644
--- a/src/layout.rs
+++ b/src/layout.rs
@@ -54,6 +54,20 @@ static NESTED_SECTION_LABEL_ALIGNMENT: RwLock<u8> = RwLock::new(0);
 static LABEL_MARGIN: RwLock<f32> = RwLock::new(6.0);
 static BUTTON_CORNER_RADIUS: RwLock<f32> = RwLock::new(4.0);
 
+/// Standard line height multiplier for text layout in clear-ui.
+pub const TEXT_LINE_HEIGHT_MULTIPLIER: f32 = 1.4;
+
+/// Standard line height based on font size.
+pub fn line_height(font_size: f32) -> f32 {
+    font_size * TEXT_LINE_HEIGHT_MULTIPLIER
+}
+
+/// Aligns a text label's top coordinate (`y`) so it is centered vertically
+/// inside a container of height `container_h` starting at `y`.
+pub fn center_text_y(y: f32, container_h: f32, font_size: f32) -> f32 {
+    y + (container_h - line_height(font_size)) / 2.0
+}
+
 pub fn reload_config() {
     if let Some(content) = read_config() {
         let mut menubar_font_changed = false;
diff --git a/src/widget/input/text_box.rs b/src/widget/input/text_box.rs
index fdd58c3..089a4a5 100644
--- a/src/widget/input/text_box.rs
+++ b/src/widget/input/text_box.rs
@@ -804,16 +804,15 @@ impl Element for TextBox {
                 quads.push((cursor_x, cursor_y, 1.5, caret_h, cursor_color));
             } else {
                 let caret_h = self.font_size * 1.15;
-                let line_h = self.font_size * 1.333;
                 if start != end {
                     let highlight_x = self.base.x + 8.0 + (start as f32 * char_width);
                     let max_x = self.base.x + self.base.w - 6.0;
                     let highlight_w = ((end - start) as f32 * char_width).min(max_x - highlight_x).max(0.0);
                     quads.push((
                         highlight_x,
-                        self.base.y + top + (visual_h - line_h) / 2.0,
+                        crate::layout::center_text_y(self.base.y + top, visual_h, self.font_size),
                         highlight_w,
-                        line_h,
+                        crate::layout::line_height(self.font_size),
                         highlight_color,
                     ));
                 }
@@ -907,7 +906,7 @@ impl Element for TextBox {
             labels.push(TextLabel {
                 text: display_text,
                 x: self.base.x + 8.0,
-                y: self.base.y + top + (visual_h - self.font_size) / 2.0,
+                y: crate::layout::center_text_y(self.base.y + top, visual_h, self.font_size),
                 font_size: self.font_size,
                 color: label_color,
             });