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 Area::sense and improve hit-testing of buttons in menus #4234

Merged
merged 2 commits into from
Mar 26, 2024
Merged
Show file tree
Hide file tree
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
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
Loading