Skip to main content

journey/widgets/
heading.rs

1//! A tiny section heading for the commit screen.
2//!
3//! saudade's `Label` honors `layout()` but anchors its text at the slot's
4//! top-left corner. `Heading` is a minimal left-aligned, *vertically-centered*
5//! text widget that honors `layout()` and whose text can be updated (e.g. to
6//! show a live file count).
7
8use saudade::{Painter, Rect, Theme, Widget};
9
10pub struct Heading {
11    rect: Rect,
12    text: String,
13    size: f32,
14}
15
16impl Heading {
17    pub fn new(text: impl Into<String>) -> Self {
18        Self {
19            rect: Rect::new(0, 0, 0, 0),
20            text: text.into(),
21            size: 12.0,
22        }
23    }
24
25    pub fn set_text(&mut self, text: impl Into<String>) {
26        self.text = text.into();
27    }
28}
29
30impl Widget for Heading {
31    fn bounds(&self) -> Rect {
32        self.rect
33    }
34
35    fn paint(&mut self, painter: &mut Painter, theme: &Theme) {
36        let y = self.rect.y + (self.rect.h - self.size as i32).max(0) / 2;
37        painter.text(self.rect.x, y, &self.text, self.size, theme.text);
38    }
39
40    fn layout(&mut self, bounds: Rect) {
41        self.rect = bounds;
42    }
43}