gpui_editor/
meta_line.rs

1use gpui::{
2    div, prelude::FluentBuilder, px, rgb, IntoElement, ParentElement, Point, RenderOnce,
3    SharedString, Styled,
4};
5
6#[derive(Default, Debug, Clone)]
7pub enum Language {
8    #[default]
9    PlainText,
10    Rust,
11}
12
13impl Language {
14    pub fn label(self) -> SharedString {
15        let string: &'static str = match self {
16            Language::PlainText => "Plain Text",
17            Language::Rust => "Rust",
18        };
19
20        string.into()
21    }
22}
23
24pub struct Selection {
25    pub lines: usize,
26    pub chars: usize,
27}
28
29#[derive(IntoElement)]
30pub struct MetaLine {
31    cursor_position: Point<usize>,
32    language: Language,
33    selection: Option<Selection>,
34}
35
36impl MetaLine {
37    pub fn new(
38        cursor_position: Point<usize>,
39        language: Language,
40        selection: Option<Selection>,
41    ) -> Self {
42        Self {
43            cursor_position,
44            language,
45            selection,
46        }
47    }
48}
49
50impl RenderOnce for MetaLine {
51    fn render(self, _window: &mut gpui::Window, _cx: &mut gpui::App) -> impl gpui::IntoElement {
52        div()
53            .absolute()
54            .right(px(0.0))
55            .bottom(px(0.0))
56            .h(px(24.0))
57            .flex()
58            .px_3()
59            .child(
60                div()
61                    .flex()
62                    .gap_2()
63                    .text_sm()
64                    .text_color(rgb(0xaaaaaa))
65                    .child(self.language.label())
66                    .child(SharedString::from(format!(
67                        "{}:{}",
68                        self.cursor_position.y + 1,
69                        self.cursor_position.x + 1
70                    )))
71                    .when_some(self.selection, |this, selection| {
72                        this.child(SharedString::from(format!("{} chars", selection.chars)))
73                    }),
74            )
75    }
76}