diff --git a/egui/src/style.rs b/egui/src/style.rs index 3f03a9f3..d1e884b2 100644 --- a/egui/src/style.rs +++ b/egui/src/style.rs @@ -138,6 +138,9 @@ pub struct Visuals { /// The color used for `Hyperlink`, pub hyperlink_color: Color32, + /// Background color behind code-styled monospaced labels. + pub code_bg_color: Color32, + pub window_corner_radius: f32, pub window_shadow: Shadow, @@ -283,6 +286,7 @@ impl Default for Visuals { selection: Default::default(), dark_bg_color: Color32::from_gray(10), hyperlink_color: Color32::from_rgb(90, 170, 255), + code_bg_color: Color32::from_gray(64), window_corner_radius: 10.0, window_shadow: Shadow::big(), resize_corner_size: 12.0, @@ -500,6 +504,7 @@ impl Visuals { selection, dark_bg_color, hyperlink_color, + code_bg_color, window_corner_radius, window_shadow, resize_corner_size, @@ -514,6 +519,7 @@ impl Visuals { ui.collapsing("selection", |ui| selection.ui(ui)); ui_color(ui, dark_bg_color, "dark_bg_color"); ui_color(ui, hyperlink_color, "hyperlink_color"); + ui_color(ui, code_bg_color, "code_bg_color"); ui.add(Slider::f32(window_corner_radius, 0.0..=20.0).text("window_corner_radius")); shadow_ui(ui, window_shadow, "Window shadow:"); ui.add(Slider::f32(resize_corner_size, 0.0..=20.0).text("resize_corner_size")); diff --git a/egui/src/widgets/label.rs b/egui/src/widgets/label.rs index d084e094..d80898af 100644 --- a/egui/src/widgets/label.rs +++ b/egui/src/widgets/label.rs @@ -8,6 +8,7 @@ pub struct Label { pub(crate) multiline: Option, pub(crate) text_style: Option, pub(crate) background_color: Color32, + use_code_bg_color: bool, pub(crate) text_color: Option, } @@ -18,6 +19,7 @@ impl Label { multiline: None, text_style: None, background_color: Color32::TRANSPARENT, + use_code_bg_color: false, text_color: None, } } @@ -52,9 +54,9 @@ impl Label { } /// Monospace label with gray background - pub fn code(self) -> Self { + pub fn code(mut self) -> Self { + self.use_code_bg_color = true; self.text_style(TextStyle::Monospace) - .background_color(Color32::from_gray(64)) // TODO: style } pub fn small(self) -> Self { @@ -101,11 +103,15 @@ impl Label { // This should be the easiest method of putting text anywhere. pub fn paint_galley(&self, ui: &mut Ui, pos: Pos2, galley: Galley) { - if self.background_color != Color32::TRANSPARENT { + let mut background_color = self.background_color; + if self.use_code_bg_color { + background_color = ui.style().visuals.code_bg_color; + } + if background_color != Color32::TRANSPARENT { for row in &galley.rows { let rect = row.rect().translate(pos.to_vec2()); let rect = rect.expand(1.0); // looks better - ui.painter().rect_filled(rect, 0.0, self.background_color); + ui.painter().rect_filled(rect, 0.0, background_color); } }