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:
parent
1bd45c35eb
commit
c2a0705c6f
|
|
@ -94,15 +94,15 @@ fn button_frame(
|
||||||
|
|
||||||
let mut response = ui.interact(outer_rect, id, sense);
|
let mut response = ui.interact(outer_rect, id, sense);
|
||||||
response.active |= button_active;
|
response.active |= button_active;
|
||||||
let style = ui.style().interact(&response);
|
let visuals = ui.style().interact(&response);
|
||||||
|
|
||||||
ui.painter().set(
|
ui.painter().set(
|
||||||
where_to_put_background,
|
where_to_put_background,
|
||||||
PaintCmd::Rect {
|
PaintCmd::Rect {
|
||||||
rect: outer_rect,
|
rect: outer_rect,
|
||||||
corner_radius: style.corner_radius,
|
corner_radius: visuals.corner_radius,
|
||||||
fill: style.bg_fill,
|
fill: visuals.bg_fill,
|
||||||
stroke: style.bg_stroke,
|
stroke: visuals.bg_stroke,
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -99,6 +99,22 @@ pub struct Interaction {
|
||||||
#[derive(Clone, Debug, PartialEq)]
|
#[derive(Clone, Debug, PartialEq)]
|
||||||
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
|
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
|
||||||
pub struct Visuals {
|
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,
|
pub widgets: Widgets,
|
||||||
|
|
||||||
/// e.g. the background of the slider or text edit,
|
/// e.g. the background of the slider or text edit,
|
||||||
|
|
@ -128,7 +144,8 @@ impl Visuals {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn text_color(&self) -> Srgba {
|
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 {
|
impl Default for Visuals {
|
||||||
fn default() -> Self {
|
fn default() -> Self {
|
||||||
Self {
|
Self {
|
||||||
|
override_text_color: None,
|
||||||
widgets: Default::default(),
|
widgets: Default::default(),
|
||||||
dark_bg_color: Srgba::black_alpha(140),
|
dark_bg_color: Srgba::black_alpha(140),
|
||||||
window_corner_radius: 10.0,
|
window_corner_radius: 10.0,
|
||||||
|
|
@ -409,6 +427,7 @@ impl Visuals {
|
||||||
}
|
}
|
||||||
|
|
||||||
let Self {
|
let Self {
|
||||||
|
override_text_color: _,
|
||||||
widgets,
|
widgets,
|
||||||
dark_bg_color,
|
dark_bg_color,
|
||||||
window_corner_radius,
|
window_corner_radius,
|
||||||
|
|
|
||||||
|
|
@ -197,7 +197,7 @@ impl Widget for Hyperlink {
|
||||||
ui.ctx().output().open_url = Some(url.clone());
|
ui.ctx().output().open_url = Some(url.clone());
|
||||||
}
|
}
|
||||||
|
|
||||||
let style = ui.style().interact(&response);
|
let visuals = ui.style().interact(&response);
|
||||||
|
|
||||||
if response.hovered {
|
if response.hovered {
|
||||||
// Underline:
|
// Underline:
|
||||||
|
|
@ -209,7 +209,7 @@ impl Widget for Hyperlink {
|
||||||
let max_x = pos.x + line.max_x();
|
let max_x = pos.x + line.max_x();
|
||||||
ui.painter().line_segment(
|
ui.painter().line_segment(
|
||||||
[pos2(min_x, y), pos2(max_x, y)],
|
[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 rect = ui.allocate_space(desired_size);
|
||||||
|
|
||||||
let response = ui.interact(rect, id, sense);
|
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 = response.rect.center() - 0.5 * galley.size; // centered-centered (looks bad for justified drop-down menus
|
||||||
let text_cursor = pos2(
|
let text_cursor = pos2(
|
||||||
response.rect.left() + button_padding.x,
|
response.rect.left() + button_padding.x,
|
||||||
response.rect.center().y - 0.5 * galley.size.y,
|
response.rect.center().y - 0.5 * galley.size.y,
|
||||||
); // left-centered
|
); // left-centered
|
||||||
let fill = fill.unwrap_or(style.bg_fill);
|
let fill = fill.unwrap_or(visuals.bg_fill);
|
||||||
ui.painter()
|
ui.painter().rect(
|
||||||
.rect(response.rect, style.corner_radius, fill, style.bg_stroke);
|
response.rect,
|
||||||
let text_color = text_color.unwrap_or_else(|| style.text_color());
|
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()
|
ui.painter()
|
||||||
.galley(text_cursor, galley, text_style, text_color);
|
.galley(text_cursor, galley, text_style, text_color);
|
||||||
response
|
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()
|
ui.painter()
|
||||||
.galley(text_cursor, galley, text_style, text_color);
|
.galley(text_cursor, galley, text_style, text_color);
|
||||||
response
|
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);
|
painter.galley(text_cursor, galley, text_style, text_color);
|
||||||
response
|
response
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -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);
|
painter.galley(response.rect.min, galley, text_style, text_color);
|
||||||
ui.memory().text_edit.insert(id, state);
|
ui.memory().text_edit.insert(id, state);
|
||||||
response
|
response
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue