Add ability to override text color with `visuals.override_text_color`

This is kind of hacky; I may redesign this later
This commit is contained in:
Emil Ernerfeldt 2020-10-14 16:32:29 +02:00
parent 1bd45c35eb
commit c2a0705c6f
4 changed files with 46 additions and 15 deletions

View File

@ -94,15 +94,15 @@ fn button_frame(
let mut response = ui.interact(outer_rect, id, sense);
response.active |= button_active;
let style = ui.style().interact(&response);
let visuals = ui.style().interact(&response);
ui.painter().set(
where_to_put_background,
PaintCmd::Rect {
rect: outer_rect,
corner_radius: style.corner_radius,
fill: style.bg_fill,
stroke: style.bg_stroke,
corner_radius: visuals.corner_radius,
fill: visuals.bg_fill,
stroke: visuals.bg_stroke,
},
);

View File

@ -99,6 +99,22 @@ pub struct Interaction {
#[derive(Clone, Debug, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
pub struct Visuals {
/// Override default text color for all text.
///
/// This is great for setting the color of text for any widget.
///
/// If `text_color` is `None` (default), then the text color will be the same as the
/// foreground stroke color (`WidgetVisuals::fg_stroke`)
/// and will depend on wether or not the widget is being interacted with.
///
/// In the future we may instead modulate
/// the `text_color` based on wether or not it is interacted with
/// so that `visuals.text_color` is always used,
/// but its alpha may be different based on whether or not
/// it is disabled, non-interactive, hovered etc.
pub override_text_color: Option<Srgba>,
/// Visual styles of widgets
pub widgets: Widgets,
/// e.g. the background of the slider or text edit,
@ -128,7 +144,8 @@ impl Visuals {
}
pub fn text_color(&self) -> Srgba {
self.widgets.noninteractive.text_color()
self.override_text_color
.unwrap_or_else(|| self.widgets.noninteractive.text_color())
}
}
@ -230,6 +247,7 @@ impl Default for Interaction {
impl Default for Visuals {
fn default() -> Self {
Self {
override_text_color: None,
widgets: Default::default(),
dark_bg_color: Srgba::black_alpha(140),
window_corner_radius: 10.0,
@ -409,6 +427,7 @@ impl Visuals {
}
let Self {
override_text_color: _,
widgets,
dark_bg_color,
window_corner_radius,

View File

@ -197,7 +197,7 @@ impl Widget for Hyperlink {
ui.ctx().output().open_url = Some(url.clone());
}
let style = ui.style().interact(&response);
let visuals = ui.style().interact(&response);
if response.hovered {
// Underline:
@ -209,7 +209,7 @@ impl Widget for Hyperlink {
let max_x = pos.x + line.max_x();
ui.painter().line_segment(
[pos2(min_x, y), pos2(max_x, y)],
(style.fg_stroke.width, color),
(visuals.fg_stroke.width, color),
);
}
}
@ -301,16 +301,22 @@ impl Widget for Button {
let rect = ui.allocate_space(desired_size);
let response = ui.interact(rect, id, sense);
let style = ui.style().interact(&response);
let visuals = ui.style().interact(&response);
// let text_cursor = response.rect.center() - 0.5 * galley.size; // centered-centered (looks bad for justified drop-down menus
let text_cursor = pos2(
response.rect.left() + button_padding.x,
response.rect.center().y - 0.5 * galley.size.y,
); // left-centered
let fill = fill.unwrap_or(style.bg_fill);
ui.painter()
.rect(response.rect, style.corner_radius, fill, style.bg_stroke);
let text_color = text_color.unwrap_or_else(|| style.text_color());
let fill = fill.unwrap_or(visuals.bg_fill);
ui.painter().rect(
response.rect,
visuals.corner_radius,
fill,
visuals.bg_stroke,
);
let text_color = text_color
.or(ui.style().visuals.override_text_color)
.unwrap_or_else(|| visuals.text_color());
ui.painter()
.galley(text_cursor, galley, text_style, text_color);
response
@ -397,7 +403,9 @@ impl<'a> Widget for Checkbox<'a> {
});
}
let text_color = text_color.unwrap_or_else(|| visuals.text_color());
let text_color = text_color
.or(ui.style().visuals.override_text_color)
.unwrap_or_else(|| visuals.text_color());
ui.painter()
.galley(text_cursor, galley, text_style, text_color);
response
@ -481,7 +489,9 @@ impl Widget for RadioButton {
});
}
let text_color = text_color.unwrap_or_else(|| visuals.text_color());
let text_color = text_color
.or(ui.style().visuals.override_text_color)
.unwrap_or_else(|| visuals.text_color());
painter.galley(text_cursor, galley, text_style, text_color);
response
}

View File

@ -216,7 +216,9 @@ impl<'t> Widget for TextEdit<'t> {
}
}
let text_color = text_color.unwrap_or_else(|| visuals.text_color());
let text_color = text_color
.or(ui.style().visuals.override_text_color)
.unwrap_or_else(|| visuals.text_color());
painter.galley(response.rect.min, galley, text_style, text_color);
ui.memory().text_edit.insert(id, state);
response