egui_pop_viewport/graph/status.rs
1//! A node's status in its title bar ([`GraphEditor::status`](super::GraphEditor::status)):
2//! a short word in a colour, its detail in a tooltip, read aloud as part
3//! of the node's description.
4
5use egui::Color32;
6use egui_pop_core::kit::Tone;
7use egui_pop_core::theme::Tokens;
8
9/// The colour of a node's status.
10#[derive(Clone, Debug, PartialEq)]
11pub enum StatusColor {
12 /// A kit tone, as text in that tone is drawn
13 /// (`egui_pop_core::ui::tone_color`).
14 Tone(Tone),
15 /// A colour the application gives.
16 Color(Color32),
17 /// A theme token's colour by its path (`success`, `state.running`); the muted
18 /// text's colour when the theme has no such token.
19 Token(String),
20}
21
22impl Default for StatusColor {
23 fn default() -> Self {
24 StatusColor::Tone(Tone::Muted)
25 }
26}
27
28impl StatusColor {
29 /// The colour it draws in, with the theme's tokens.
30 pub fn resolve(&self, tokens: &Tokens) -> Color32 {
31 match self {
32 StatusColor::Tone(tone) => egui_pop_core::ui::tone_color(tokens, *tone),
33 StatusColor::Color(c) => *c,
34 StatusColor::Token(path) => tokens.color(path).unwrap_or(tokens.text_role.muted.color),
35 }
36 }
37}
38
39/// A node's status: a short text drawn small at its title bar's right
40/// end (left of the [`header`](super::GraphEditor::header) slot), in its
41/// colour, its detail shown as a tooltip; both are part of the node's
42/// accessible description.
43///
44/// ```
45/// use egui_pop_core::kit::Tone;
46/// use egui_pop_viewport::graph::{NodeStatus, StatusColor};
47///
48/// let s = NodeStatus::new("error")
49/// .tone(Tone::Danger)
50/// .detail("The input file is missing");
51/// assert_eq!(s.color, StatusColor::Tone(Tone::Danger));
52/// assert_eq!(s.description(), "error, The input file is missing");
53/// ```
54#[derive(Clone, Debug, Default, PartialEq)]
55pub struct NodeStatus {
56 /// The short text ("running", "stale").
57 pub text: String,
58 /// Its colour (muted text by default).
59 pub color: StatusColor,
60 /// What it means in full, shown as a tooltip.
61 pub detail: Option<String>,
62}
63
64impl NodeStatus {
65 /// A status saying `text`, in muted text.
66 pub fn new(text: impl Into<String>) -> Self {
67 NodeStatus {
68 text: text.into(),
69 ..NodeStatus::default()
70 }
71 }
72
73 /// In a kit tone.
74 pub fn tone(mut self, tone: Tone) -> Self {
75 self.color = StatusColor::Tone(tone);
76 self
77 }
78
79 /// In a colour of the application's.
80 pub fn color(mut self, color: Color32) -> Self {
81 self.color = StatusColor::Color(color);
82 self
83 }
84
85 /// In a theme token's colour, by its path (`success`, `state.running`).
86 pub fn token(mut self, path: impl Into<String>) -> Self {
87 self.color = StatusColor::Token(path.into());
88 self
89 }
90
91 /// Its detail, shown as a tooltip.
92 pub fn detail(mut self, detail: impl Into<String>) -> Self {
93 self.detail = Some(detail.into());
94 self
95 }
96
97 /// What it adds to the node's accessible description: its text, then
98 /// its detail.
99 pub fn description(&self) -> String {
100 match &self.detail {
101 Some(d) if !d.is_empty() => format!("{}, {d}", self.text),
102 _ => self.text.clone(),
103 }
104 }
105}