Skip to main content

egui_charts/ui_kit/status_message/
mod.rs

1//! Status message display helpers.
2//!
3//! Provides consistent error/success/warning/info message rendering
4//! used in dialogs and forms.
5//!
6//! # Example
7//! ```
8//! # use egui_charts::ui_kit::status_message::{MessageKind, status_message};
9//! # egui::__run_test_ui(|ui| {
10//! # let error: Option<String> = Some("Symbol not found".into());
11//! # let success: Option<String> = None;
12//! if let Some(ref error) = error {
13//!     status_message(ui, MessageKind::Error, error);
14//! }
15//! if let Some(ref msg) = success {
16//!     status_message(ui, MessageKind::Success, msg);
17//! }
18//! # });
19//! ```
20
21use crate::localization as l10n;
22use crate::theme::ThemeExt;
23use egui::Ui;
24
25/// Kind of status message, determines color.
26#[derive(Clone, Copy, Debug, PartialEq, Eq)]
27pub enum MessageKind {
28    /// An operation failed.
29    Error,
30    /// Successful completion.
31    Success,
32    /// A warning requiring attention.
33    Warning,
34    /// Informational status.
35    Info,
36}
37
38/// Show a padded, colored status message.
39pub fn status_message(ui: &mut Ui, kind: MessageKind, text: &str) {
40    let text = l10n::text(ui.ctx(), text);
41    translated_status_message(ui, kind, text);
42}
43
44/// Show an already translated message without copying it through another lookup.
45pub(crate) fn translated_status_message(ui: &mut Ui, kind: MessageKind, text: String) {
46    let theme = ui.active_theme();
47    let color = match kind {
48        MessageKind::Error => ui.style().visuals.error_fg_color,
49        MessageKind::Success => theme.ui.success,
50        MessageKind::Warning => ui.style().visuals.warn_fg_color,
51        MessageKind::Info => theme.ui.info,
52    };
53
54    ui.add_space(theme.spacing.sm);
55    ui.colored_label(color, text);
56}