Skip to content

Commit

Permalink
Add Area::sense and improve hit-testing of buttons in menus (#4234)
Browse files Browse the repository at this point in the history
Previously, putting the cursor in the gap between two menu buttons would
not hover any of the buttons, but the click-sensitive menu itself. Now
the menu is no longer click-sensitive.
  • Loading branch information
emilk authored Mar 26, 2024
1 parent 9cfaf8b commit 8a880d6
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 9 deletions.
30 changes: 22 additions & 8 deletions crates/egui/src/containers/area.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ impl State {
#[derive(Clone, Copy, Debug)]
pub struct Area {
pub(crate) id: Id,
sense: Option<Sense>,
movable: bool,
interactable: bool,
enabled: bool,
Expand All @@ -78,6 +79,7 @@ impl Area {
pub fn new(id: Id) -> Self {
Self {
id,
sense: None,
movable: true,
interactable: true,
constrain: false,
Expand Down Expand Up @@ -114,7 +116,7 @@ impl Area {
self
}

/// moveable by dragging the area?
/// Moveable by dragging the area?
#[inline]
pub fn movable(mut self, movable: bool) -> Self {
self.movable = movable;
Expand All @@ -139,6 +141,15 @@ impl Area {
self
}

/// Explicitly set a sense.
///
/// If not set, this will default to `Sense::drag()` if movable, `Sense::click()` if interactable, and `Sense::hover()` otherwise.
#[inline]
pub fn sense(mut self, sense: Sense) -> Self {
self.sense = Some(sense);
self
}

/// `order(Order::Foreground)` for an Area that should always be on top
#[inline]
pub fn order(mut self, order: Order) -> Self {
Expand Down Expand Up @@ -255,6 +266,7 @@ impl Area {
pub(crate) fn begin(self, ctx: &Context) -> Prepared {
let Self {
id,
sense,
movable,
order,
interactable,
Expand Down Expand Up @@ -299,13 +311,15 @@ impl Area {
// interact right away to prevent frame-delay
let mut move_response = {
let interact_id = layer_id.id.with("move");
let sense = if movable {
Sense::drag()
} else if interactable {
Sense::click() // allow clicks to bring to front
} else {
Sense::hover()
};
let sense = sense.unwrap_or_else(|| {
if movable {
Sense::drag()
} else if interactable {
Sense::click() // allow clicks to bring to front
} else {
Sense::hover()
}
});

let move_response = ctx.create_widget(WidgetRect {
id: interact_id,
Expand Down
3 changes: 2 additions & 1 deletion crates/egui/src/menu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,8 @@ pub(crate) fn menu_ui<'c, R>(
.order(Order::Foreground)
.fixed_pos(pos)
.constrain_to(ctx.screen_rect())
.interactable(true);
.interactable(true)
.sense(Sense::hover());

let area_response = area.show(ctx, |ui| {
set_menu_style(ui.style_mut());
Expand Down

0 comments on commit 8a880d6

Please sign in to comment.