Skip to content

Commit

Permalink
Also change warning_label to match
Browse files Browse the repository at this point in the history
  • Loading branch information
emilk committed Nov 13, 2024
1 parent 9f6d3fc commit b9140c4
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 11 deletions.
9 changes: 9 additions & 0 deletions crates/viewer/re_ui/src/context_ext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,17 +59,26 @@ pub trait ContextExt {
// egui::Stroke::new(stroke_width, color)
}

/// Text colored to indicate success.
#[must_use]
fn success_text(&self, text: impl Into<String>) -> egui::RichText {
egui::RichText::new(text).color(SUCCESS_COLOR)
}

/// Text colored to indicate a warning.
///
/// For most cases, you should use [`crate::UiExt::warning_label`] instead,
/// which has a nice fat border around it.
#[must_use]
fn warning_text(&self, text: impl Into<String>) -> egui::RichText {
let style = self.ctx().style();
egui::RichText::new(text).color(style.visuals.warn_fg_color)
}

/// Text colored to indicate an error.
///
/// For most cases, you should use [`crate::UiExt::error_label`] instead,
/// which has a nice fat border around it.
#[must_use]
fn error_text(&self, text: impl Into<String>) -> egui::RichText {
let style = self.ctx().style();
Expand Down
1 change: 1 addition & 0 deletions crates/viewer/re_ui/src/design_tokens.rs
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,7 @@ impl DesignTokens {
egui_style.visuals.image_loading_spinners = false;

egui_style.visuals.error_fg_color = egui::Color32::from_rgb(0xAB, 0x01, 0x16);
egui_style.visuals.warn_fg_color = egui::Color32::from_rgb(0xFF, 0x7A, 0x0C);

ctx.set_style(egui_style);
}
Expand Down
27 changes: 16 additions & 11 deletions crates/viewer/re_ui/src/ui_ext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,23 +53,26 @@ pub trait UiExt {
fn ui(&self) -> &egui::Ui;
fn ui_mut(&mut self) -> &mut egui::Ui;

/// Show the label with a success color.
fn success_label(&mut self, text: &str) -> egui::Response {
let label = egui::Label::new(self.ui().ctx().success_text(text));
self.ui_mut().add(label)
}

/// Shows a warning label.
/// Shows a warning label with a large border.
///
/// If you don't want a border, use [`crate::ContextExt::warning_text`].
fn warning_label(&mut self, warning_text: &str) -> egui::Response {
let label = egui::Label::new(self.ui().ctx().warning_text(warning_text));
self.ui_mut().add(label)
let ui = self.ui_mut();
warning_or_error_label(
ui,
ui.style().visuals.warn_fg_color,
warning_text,
warning_text,
)
}

/// Shows a small error label with the given text on hover and copies the text to the clipboard on click.
/// Shows a small error label with the given text on hover and copies the text to the clipboard on click with a large border.
///
/// This has a large border! If you don't want a border, use [`crate::ContextExt::error_text`].
fn error_with_details_on_hover(&mut self, error_text: &str) -> egui::Response {
let ui = self.ui_mut();
warning_or_error_label(ui, ui.style().visuals.error_fg_color, "Error", error_text)
}
}

fn error_label_background_color(&self) -> egui::Color32 {
error_label_bg_color(self.ui().style().visuals.error_fg_color)
Expand All @@ -79,6 +82,8 @@ pub trait UiExt {
///
/// Use this only if the error message is short, or you have a lot of room.
/// Otherwise, use [`Self::error_with_details_on_hover`].
///
/// This has a large border! If you don't want a border, use [`crate::ContextExt::error_text`].
fn error_label(&mut self, error_text: &str) -> egui::Response {
let ui = self.ui_mut();
warning_or_error_label(
Expand Down

0 comments on commit b9140c4

Please sign in to comment.