1
0
mirror of https://github.com/emilk/egui.git synced 2026-09-01 14:20:04 -04:00

Change ui.disable() to modify opacity (#7282)

* Closes https://github.com/emilk/egui/pull/6765


Branched off of https://github.com/emilk/egui/pull/6765 by @tye-exe.

I needed to branch off to update snapshot images

---------

Co-authored-by: tye-exe <tye@mailbox.org>
Co-authored-by: Tye <131195812+tye-exe@users.noreply.github.com>
This commit is contained in:
Emil Ernerfeldt
2025-07-01 14:05:53 +02:00
committed by GitHub
parent b2995dcb83
commit 9e021f78da
22 changed files with 67 additions and 44 deletions

View File

@@ -83,6 +83,7 @@ impl Painter {
}
/// If set, colors will be modified to look like this
#[deprecated = "Use `multiply_opacity` instead"]
pub fn set_fade_to_color(&mut self, fade_to_color: Option<Color32>) {
self.fade_to_color = fade_to_color;
}

View File

@@ -1019,6 +1019,9 @@ pub struct Visuals {
/// How to display numeric color values.
pub numeric_color_space: NumericColorSpace,
/// How much to modify the alpha of a disabled widget.
pub disabled_alpha: f32,
}
impl Visuals {
@@ -1054,17 +1057,32 @@ impl Visuals {
}
/// When fading out things, we fade the colors towards this.
// TODO(emilk): replace with an alpha
#[inline(always)]
#[deprecated = "Use disabled_alpha(). Fading is now handled by modifying the alpha channel."]
pub fn fade_out_to_color(&self) -> Color32 {
self.widgets.noninteractive.weak_bg_fill
}
/// Returned a "grayed out" version of the given color.
/// Disabled widgets have their alpha modified by this.
#[inline(always)]
pub fn disabled_alpha(&self) -> f32 {
self.disabled_alpha
}
/// Returns a "disabled" version of the given color.
///
/// This function modifies the opcacity of the given color.
/// If this is undesirable use [`gray_out`](Self::gray_out).
#[inline(always)]
pub fn disable(&self, color: Color32) -> Color32 {
color.gamma_multiply(self.disabled_alpha())
}
/// Returns a "grayed out" version of the given color.
#[doc(alias = "grey_out")]
#[inline(always)]
pub fn gray_out(&self, color: Color32) -> Color32 {
crate::ecolor::tint_color_towards(color, self.fade_out_to_color())
crate::ecolor::tint_color_towards(color, self.widgets.noninteractive.weak_bg_fill)
}
}
@@ -1384,6 +1402,7 @@ impl Visuals {
image_loading_spinners: true,
numeric_color_space: NumericColorSpace::GammaByte,
disabled_alpha: 0.5,
}
}
@@ -2063,6 +2082,7 @@ impl Visuals {
image_loading_spinners,
numeric_color_space,
disabled_alpha,
} = self;
ui.collapsing("Background Colors", |ui| {
@@ -2191,6 +2211,8 @@ impl Visuals {
ui.label("Color picker type");
numeric_color_space.toggle_button_ui(ui);
});
ui.add(Slider::new(disabled_alpha, 0.0..=1.0).text("Disabled element alpha"));
});
ui.vertical_centered(|ui| reset_button(ui, self, "Reset visuals"));

View File

@@ -522,7 +522,7 @@ impl Ui {
self.enabled = false;
if self.is_visible() {
self.painter
.set_fade_to_color(Some(self.visuals().fade_out_to_color()));
.multiply_opacity(self.visuals().disabled_alpha());
}
}
@@ -2963,8 +2963,8 @@ impl Ui {
if is_anything_being_dragged && !can_accept_what_is_being_dragged {
// When dragging something else, show that it can't be dropped here:
fill = self.visuals().gray_out(fill);
stroke.color = self.visuals().gray_out(stroke.color);
fill = self.visuals().disable(fill);
stroke.color = self.visuals().disable(stroke.color);
}
frame.frame.fill = fill;