Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Button::image_tint_follows_text_color #5430

Merged
merged 2 commits into from
Dec 4, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 20 additions & 1 deletion crates/egui/src/widgets/button.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ pub struct Button<'a> {
min_size: Vec2,
rounding: Option<Rounding>,
selected: bool,
image_tint_follows_text_color: bool,
}

impl<'a> Button<'a> {
Expand Down Expand Up @@ -70,6 +71,7 @@ impl<'a> Button<'a> {
min_size: Vec2::ZERO,
rounding: None,
selected: false,
image_tint_follows_text_color: false,
}
}

Expand Down Expand Up @@ -156,6 +158,18 @@ impl<'a> Button<'a> {
self
}

/// If true, the tint of the image is the same as the text color.
///
/// This makes sense for images that are white, that should have the same color as the text color.
/// This will also make the icon color depend on hover state.
///
/// Default: `false`.
#[inline]
pub fn image_tint_follows_text_color(mut self, image_tint_follows_text_color: bool) -> Self {
self.image_tint_follows_text_color = image_tint_follows_text_color;
self
}

/// Show some text on the right side of the button, in weak color.
///
/// Designed for menu buttons, for setting a keyboard shortcut text (e.g. `Ctrl+S`).
Expand Down Expand Up @@ -190,6 +204,7 @@ impl Widget for Button<'_> {
min_size,
rounding,
selected,
image_tint_follows_text_color,
} = self;

let frame = frame.unwrap_or_else(|| ui.visuals().button_frame);
Expand Down Expand Up @@ -319,12 +334,16 @@ impl Widget for Button<'_> {
let image_rect = Rect::from_min_size(image_pos, image_size);
cursor_x += image_size.x;
let tlr = image.load_for_size(ui.ctx(), image_size);
let mut image_options = image.image_options().clone();
if image_tint_follows_text_color {
image_options.tint = visuals.text_color();
}
widgets::image::paint_texture_load_result(
ui,
&tlr,
image_rect,
image.show_loading_spinner,
image.image_options(),
&image_options,
);
response = widgets::image::texture_load_result_response(
&image.source(ui.ctx()),
Expand Down
Loading