[style] Add code_bg_color

This commit is contained in:
Emil Ernerfeldt 2021-01-30 15:48:17 +01:00
parent 18e1ea1d63
commit 26d47eabf0
2 changed files with 16 additions and 4 deletions

View file

@ -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"));

View file

@ -8,6 +8,7 @@ pub struct Label {
pub(crate) multiline: Option<bool>,
pub(crate) text_style: Option<TextStyle>,
pub(crate) background_color: Color32,
use_code_bg_color: bool,
pub(crate) text_color: Option<Color32>,
}
@ -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);
}
}