Add `Visuals::weak_text_alpha` and `weak_text_color` (#7285)

* Closes https://github.com/emilk/egui/issues/7262

This also makes the default weak color slightly less weak in most cases.
This commit is contained in:
Emil Ernerfeldt 2025-07-01 20:42:54 +02:00 committed by GitHub
parent 9d1dce51eb
commit 0857527f1d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
10 changed files with 129 additions and 70 deletions

View File

@ -936,6 +936,17 @@ pub struct Visuals {
/// it is disabled, non-interactive, hovered etc. /// it is disabled, non-interactive, hovered etc.
pub override_text_color: Option<Color32>, pub override_text_color: Option<Color32>,
/// How strong "weak" text is.
///
/// Ignored if [`Self::weak_text_color`] is set.
pub weak_text_alpha: f32,
/// Color of "weak" text.
///
/// If `None`, the color is [`Self::text_color`]
/// multiplied by [`Self::weak_text_alpha`].
pub weak_text_color: Option<Color32>,
/// Visual styles of widgets /// Visual styles of widgets
pub widgets: Widgets, pub widgets: Widgets,
@ -1043,7 +1054,8 @@ impl Visuals {
} }
pub fn weak_text_color(&self) -> Color32 { pub fn weak_text_color(&self) -> Color32 {
self.gray_out(self.text_color()) self.weak_text_color
.unwrap_or_else(|| self.text_color().gamma_multiply(self.weak_text_alpha))
} }
#[inline(always)] #[inline(always)]
@ -1363,6 +1375,8 @@ impl Visuals {
Self { Self {
dark_mode: true, dark_mode: true,
override_text_color: None, override_text_color: None,
weak_text_alpha: 0.6,
weak_text_color: None,
widgets: Widgets::default(), widgets: Widgets::default(),
selection: Selection::default(), selection: Selection::default(),
hyperlink_color: Color32::from_rgb(90, 170, 255), hyperlink_color: Color32::from_rgb(90, 170, 255),
@ -2055,6 +2069,8 @@ impl Visuals {
let Self { let Self {
dark_mode, dark_mode,
override_text_color: _, override_text_color: _,
weak_text_alpha,
weak_text_color,
widgets, widgets,
selection, selection,
hyperlink_color, hyperlink_color,
@ -2098,7 +2114,45 @@ impl Visuals {
disabled_alpha, disabled_alpha,
} = self; } = self;
fn ui_optional_color(
ui: &mut Ui,
color: &mut Option<Color32>,
default_value: Color32,
label: impl Into<WidgetText>,
) -> Response {
let label_response = ui.label(label);
ui.horizontal(|ui| {
let mut set = color.is_some();
ui.checkbox(&mut set, "");
if set {
let color = color.get_or_insert(default_value);
ui.color_edit_button_srgba(color);
} else {
*color = None;
};
});
ui.end_row();
label_response
}
ui.collapsing("Background colors", |ui| { ui.collapsing("Background colors", |ui| {
Grid::new("background_colors")
.num_columns(2)
.show(ui, |ui| {
fn ui_color(
ui: &mut Ui,
color: &mut Color32,
label: impl Into<WidgetText>,
) -> Response {
let label_response = ui.label(label);
ui.color_edit_button_srgba(color);
ui.end_row();
label_response
}
ui_color(ui, &mut widgets.inactive.weak_bg_fill, "Buttons"); ui_color(ui, &mut widgets.inactive.weak_bg_fill, "Buttons");
ui_color(ui, window_fill, "Windows"); ui_color(ui, window_fill, "Windows");
ui_color(ui, panel_fill, "Panels"); ui_color(ui, panel_fill, "Panels");
@ -2108,16 +2162,21 @@ impl Visuals {
ui_color(ui, extreme_bg_color, "Extreme") ui_color(ui, extreme_bg_color, "Extreme")
.on_hover_text("Background of plots and paintings"); .on_hover_text("Background of plots and paintings");
ui_color( ui_optional_color(ui, text_edit_bg_color, *extreme_bg_color, "TextEdit")
ui,
text_edit_bg_color.get_or_insert(*extreme_bg_color),
"TextEdit",
)
.on_hover_text("Background of TextEdit"); .on_hover_text("Background of TextEdit");
}); });
});
ui.collapsing("Text color", |ui| { ui.collapsing("Text color", |ui| {
fn ui_text_color(ui: &mut Ui, color: &mut Color32, label: impl Into<RichText>) {
ui.label(label.into().color(*color));
ui.color_edit_button_srgba(color);
ui.end_row();
}
Grid::new("text_color").num_columns(2).show(ui, |ui| {
ui_text_color(ui, &mut widgets.noninteractive.fg_stroke.color, "Label"); ui_text_color(ui, &mut widgets.noninteractive.fg_stroke.color, "Label");
ui_text_color( ui_text_color(
ui, ui,
&mut widgets.inactive.fg_stroke.color, &mut widgets.inactive.fg_stroke.color,
@ -2131,16 +2190,32 @@ impl Visuals {
ui_text_color(ui, hyperlink_color, "hyperlink_color"); ui_text_color(ui, hyperlink_color, "hyperlink_color");
ui_color(ui, code_bg_color, RichText::new("Code background").code()).on_hover_ui( ui.label(RichText::new("Code background").code())
|ui| { .on_hover_ui(|ui| {
ui.horizontal(|ui| { ui.horizontal(|ui| {
ui.spacing_mut().item_spacing.x = 0.0; ui.spacing_mut().item_spacing.x = 0.0;
ui.label("For monospaced inlined text "); ui.label("For monospaced inlined text ");
ui.code("like this"); ui.code("like this");
ui.label("."); ui.label(".");
}); });
}, });
ui.color_edit_button_srgba(code_bg_color);
ui.end_row();
ui.label("Weak text alpha");
ui.add_enabled(
weak_text_color.is_none(),
DragValue::new(weak_text_alpha).speed(0.01).range(0.0..=1.0),
); );
ui.end_row();
ui_optional_color(
ui,
weak_text_color,
widgets.noninteractive.text_color(),
"Weak text color",
);
});
}); });
ui.collapsing("Text cursor", |ui| { ui.collapsing("Text cursor", |ui| {
@ -2364,22 +2439,6 @@ fn two_drag_values(value: &mut Vec2, range: std::ops::RangeInclusive<f32>) -> im
} }
} }
fn ui_color(ui: &mut Ui, color: &mut Color32, label: impl Into<WidgetText>) -> Response {
ui.horizontal(|ui| {
ui.color_edit_button_srgba(color);
ui.label(label);
})
.response
}
fn ui_text_color(ui: &mut Ui, color: &mut Color32, label: impl Into<RichText>) -> Response {
ui.horizontal(|ui| {
ui.color_edit_button_srgba(color);
ui.label(label.into().color(*color));
})
.response
}
impl HandleShape { impl HandleShape {
pub fn ui(&mut self, ui: &mut Ui) { pub fn ui(&mut self, ui: &mut Ui) {
ui.horizontal(|ui| { ui.horizontal(|ui| {

View File

@ -1,3 +1,3 @@
version https://git-lfs.github.com/spec/v1 version https://git-lfs.github.com/spec/v1
oid sha256:2849afd01ec3dae797b15893e28908f6b037588b3712fb6dec556edb7b230b5d oid sha256:f62d5375ff784e333e01a31b84d9caadf2dcbd2b19647a08977dab6550b48828
size 179082 size 179654

View File

@ -1,3 +1,3 @@
version https://git-lfs.github.com/spec/v1 version https://git-lfs.github.com/spec/v1
oid sha256:5fc9e2ec3253a30ac9649995b019b6b23d745dba07a327886f574a15c0e99e84 oid sha256:e0a49139611dd5f4e97874e8f7b0e12b649da5f373ff7ee80a7ff678f7f8ecc7
size 50082 size 50321

View File

@ -1,3 +1,3 @@
version https://git-lfs.github.com/spec/v1 version https://git-lfs.github.com/spec/v1
oid sha256:df1e4a1e355100056713e751a8979d4201d0e4aab5513ba2f7a3e4852e1347dd oid sha256:cfc5dd77728ee0b3d319c5851698305851b6713eb054a6eb5b618e9670f58ae5
size 264340 size 277018

View File

@ -1,3 +1,3 @@
version https://git-lfs.github.com/spec/v1 version https://git-lfs.github.com/spec/v1
oid sha256:cdff6256488f3a40c65a3d73c0635377bf661c57927bce4c853b2a5f3b33274e oid sha256:fdf3535530c1abb1262383ff9a3f2a740ad2c62ccec33ec5fb435be11625d139
size 35121 size 35125

View File

@ -1,3 +1,3 @@
version https://git-lfs.github.com/spec/v1 version https://git-lfs.github.com/spec/v1
oid sha256:4a347875ef98ebbd606774e03baffdb317cb0246882db116fee1aa7685efbb88 oid sha256:1bd15215f3ec1b365b8c51987f629d5653e4f40e84c34756aea0dc863af27c1e
size 179653 size 179906

View File

@ -1,3 +1,3 @@
version https://git-lfs.github.com/spec/v1 version https://git-lfs.github.com/spec/v1
oid sha256:ee129f0542f21e12f5aa3c2f9746e7cadd73441a04d580f57c12c1cdd40d8b07 oid sha256:3f5a7397601cb718d5529842a428d2d328d4fe3d1a9cf1a3ca6d583d8525f75e
size 153136 size 153190

View File

@ -1,3 +1,3 @@
version https://git-lfs.github.com/spec/v1 version https://git-lfs.github.com/spec/v1
oid sha256:ad14068e60fa678ee749925dd3713ee2b12a83ec1bca9c413bdeb9bc27d8ac20 oid sha256:d59882afca42e766dddc36450a3331ca247a130e3796f99d0335ac370a7c3610
size 407795 size 425517

View File

@ -1,3 +1,3 @@
version https://git-lfs.github.com/spec/v1 version https://git-lfs.github.com/spec/v1
oid sha256:f74f5ff20b842c1990c50d8a66ab5b34e248786f01b1592485620d31426ce5ae oid sha256:8ff776897760d300a4f26c10578be0d9afed7b4ae9f95f941914e641c2a10cb8
size 13302 size 13798

View File

@ -1,3 +1,3 @@
version https://git-lfs.github.com/spec/v1 version https://git-lfs.github.com/spec/v1
oid sha256:df84f3fce07a45a208f6169f0df701b7971fc7d467151870d56d90ce49a2c819 oid sha256:9cd6a7f38c876cc345eae1a5e01f7668d4642b70181198fe0f09570815e47da8
size 13522 size 13489