How do I detect clicks on the background of a layout with buttons and labels inside? #4856
-
Hi! I'm struggling to create an area with buttons/labels inside, whose background I can also click (to do something custom). I know about Do you know how I can achieve this? Thanks! |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 5 replies
-
I accidentally stumbled upon You can call it with a
Keep in mind that |
Beta Was this translation helpful? Give feedback.
-
apparently this is now deprecated, an example showing how to detect clicks on the background of a frame using the non-deprecated alternative would be nice. Here's the PR that deprecates it: #5054 |
Beta Was this translation helpful? Give feedback.
-
just following up, this example shows how to detect clicks on en egui::Sides. let response = ui.scope_builder(
UiBuilder::new()
.sense(Sense::click()),
|ui| {
egui::Frame::NONE
.inner_margin(Margin::symmetric(4, 0))
.show(ui, |ui| {
egui::Sides::new()
.height(ui.available_height())
.show(
ui,
|ui| {
Label::new(RichText::new("LEFT")
.monospace().color(Color32::RED)).selectable(false).ui(ui);
},
|ui| {
Label::new(RichText::new("RIGHT")
.monospace().color(Color32::GREEN)).selectable(false).ui(ui);
}
);
});
}).response;
if response.clicked() {
debug!("clicked");
} hope this helps someone, it took me about 30 attempts yesterday to make this work. I found accomplishing this extremely non-intuitive. |
Beta Was this translation helpful? Give feedback.
I accidentally stumbled upon
interact_bg
just now 😅You can call it with a
Sense
inside the widget you are drawing, and the background will detect thatSense
:Keep in mind that
interact_bg
works with the ID of the parent element you are drawing, so if you have multiple ones they will each require a custom ID, which you can give them withpush_id
.