1use crate::common::Size;
5use egui::{FontId, Response, RichText, Ui, Widget};
6use egui_components_theme::Theme;
7
8#[derive(Clone, Copy, Debug, PartialEq, Eq, Default)]
9pub enum LabelTone {
10 #[default]
11 Default,
12 Muted,
13 Primary,
14 Secondary,
15 Danger,
16 Success,
17 Warning,
18}
19
20pub struct Label {
21 text: String,
22 tone: LabelTone,
23 size: Size,
24 strong: bool,
25 italic: bool,
26 underline: bool,
27}
28
29impl Label {
30 pub fn new(text: impl Into<String>) -> Self {
31 Self {
32 text: text.into(),
33 tone: LabelTone::Default,
34 size: Size::Medium,
35 strong: false,
36 italic: false,
37 underline: false,
38 }
39 }
40 pub fn tone(mut self, t: LabelTone) -> Self {
41 self.tone = t;
42 self
43 }
44 pub fn muted(self) -> Self {
45 self.tone(LabelTone::Muted)
46 }
47 pub fn size(mut self, s: Size) -> Self {
48 self.size = s;
49 self
50 }
51 pub fn strong(mut self) -> Self {
52 self.strong = true;
53 self
54 }
55 pub fn italic(mut self) -> Self {
56 self.italic = true;
57 self
58 }
59 pub fn underline(mut self) -> Self {
60 self.underline = true;
61 self
62 }
63}
64
65impl Widget for Label {
66 fn ui(self, ui: &mut Ui) -> Response {
67 let theme = Theme::get(ui.ctx());
68 let c = theme.colors;
69 let color = match self.tone {
70 LabelTone::Default => c.foreground,
71 LabelTone::Muted => c.muted_foreground,
72 LabelTone::Primary => c.primary_background,
73 LabelTone::Secondary => c.secondary_foreground,
74 LabelTone::Danger => c.danger_background,
75 LabelTone::Success => c.success_background,
76 LabelTone::Warning => c.warning_background,
77 };
78 let mut rich = RichText::new(self.text)
79 .color(color)
80 .font(FontId::proportional(self.size.font_size(&theme.metrics)));
81 if self.strong {
82 rich = rich.strong();
83 }
84 if self.italic {
85 rich = rich.italics();
86 }
87 if self.underline {
88 rich = rich.underline();
89 }
90 ui.add(egui::Label::new(rich))
91 }
92}