Change filled button background on hover #7627
-
I am trying to change a button background from a custom one to another custom one on hover.
It feels like there should be a way to repaint based on a egui::CentralPanel::default().show(ctx, |ui| {
let button = Button::new("test")
.fill(Color32::DARK_RED) // this disables hover effect
.min_size(vec2(50.0, 50.0));
let response = ui.add(button);
if response.hovered() {
let visuals = ui.style().interact(&response);
let mut job = LayoutJob::default();
job.append(
"test",
0.0,
TextFormat {
color: visuals.text_color(),
..Default::default()
},
);
let galley = WidgetText::LayoutJob(job.into()).into_galley(
ui,
None,
response.rect.width(),
TextStyle::Button,
);
// Change background color on hover
ui.painter()
.rect_filled(response.rect, 0.0, Color32::DARK_BLUE);
// repaint text, because it's gone now
ui.painter().galley(
response
.rect
.shrink2(ui.spacing().button_padding)
.left_center(), // this does not place it in the center vertically
galley,
Color32::TRANSPARENT,
);
}
}); |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
@YgorSouza's answer to #3356 should apply (maybe you are still using {
let styles = ui.styles_mut();
styles.visuals.widgets.inactive.weak_bg_fill = Color32::DARK_RED; // button at rest
styles.visuals.widgets.hovered.weak_bg_fill = Color32::DARK_BLUE; // button on hover
}
let button = Button::new("test")
.min_size(vec2(50., 50.)); |
Beta Was this translation helpful? Give feedback.
@YgorSouza's answer to #3356 should apply (maybe you are still using
Button::fill()
, which overrides on-hover effects):