diff --git a/crates/eframe/src/web/events.rs b/crates/eframe/src/web/events.rs index c61a80012..88bedab35 100644 --- a/crates/eframe/src/web/events.rs +++ b/crates/eframe/src/web/events.rs @@ -196,11 +196,13 @@ pub(crate) fn on_keydown(event: web_sys::KeyboardEvent, runner: &mut AppRunner) let prevent_default = should_prevent_default_for_key(runner, &modifiers, egui_key); - // log::debug!( - // "On keydown {:?} {egui_key:?}, has_focus: {has_focus}, egui_wants_keyboard: {}, prevent_default: {prevent_default}", - // event.key().as_str(), - // runner.egui_ctx().wants_keyboard_input() - // ); + if false { + log::debug!( + "On keydown {:?} {egui_key:?}, has_focus: {has_focus}, egui_wants_keyboard: {}, prevent_default: {prevent_default}", + event.key().as_str(), + runner.egui_ctx().egui_wants_keyboard_input() + ); + } if prevent_default { event.prevent_default(); diff --git a/crates/egui-winit/src/lib.rs b/crates/egui-winit/src/lib.rs index 7660e3cef..6663ba40f 100644 --- a/crates/egui-winit/src/lib.rs +++ b/crates/egui-winit/src/lib.rs @@ -309,21 +309,21 @@ impl State { self.on_mouse_button_input(*state, *button); EventResponse { repaint: true, - consumed: self.egui_ctx.wants_pointer_input(), + consumed: self.egui_ctx.egui_wants_pointer_input(), } } WindowEvent::MouseWheel { delta, phase, .. } => { self.on_mouse_wheel(window, *delta, *phase); EventResponse { repaint: true, - consumed: self.egui_ctx.wants_pointer_input(), + consumed: self.egui_ctx.egui_wants_pointer_input(), } } WindowEvent::CursorMoved { position, .. } => { self.on_cursor_moved(window, *position); EventResponse { repaint: true, - consumed: self.egui_ctx.is_using_pointer(), + consumed: self.egui_ctx.egui_is_using_pointer(), } } WindowEvent::CursorLeft { .. } => { @@ -340,8 +340,10 @@ impl State { let consumed = match touch.phase { winit::event::TouchPhase::Started | winit::event::TouchPhase::Ended - | winit::event::TouchPhase::Cancelled => self.egui_ctx.wants_pointer_input(), - winit::event::TouchPhase::Moved => self.egui_ctx.is_using_pointer(), + | winit::event::TouchPhase::Cancelled => { + self.egui_ctx.egui_wants_pointer_input() + } + winit::event::TouchPhase::Moved => self.egui_ctx.egui_is_using_pointer(), }; EventResponse { repaint: true, @@ -392,7 +394,7 @@ impl State { EventResponse { repaint: true, - consumed: self.egui_ctx.wants_keyboard_input(), + consumed: self.egui_ctx.egui_wants_keyboard_input(), } } WindowEvent::KeyboardInput { @@ -413,7 +415,7 @@ impl State { self.on_keyboard_input(event); // When pressing the Tab key, egui focuses the first focusable element, hence Tab always consumes. - let consumed = self.egui_ctx.wants_keyboard_input() + let consumed = self.egui_ctx.egui_wants_keyboard_input() || event.logical_key == winit::keyboard::Key::Named(winit::keyboard::NamedKey::Tab); EventResponse { @@ -528,7 +530,7 @@ impl State { self.egui_input.events.push(egui::Event::Zoom(zoom_factor)); EventResponse { repaint: true, - consumed: self.egui_ctx.wants_pointer_input(), + consumed: self.egui_ctx.egui_wants_pointer_input(), } } @@ -541,7 +543,7 @@ impl State { .push(egui::Event::Rotate(-delta.to_radians())); EventResponse { repaint: true, - consumed: self.egui_ctx.wants_pointer_input(), + consumed: self.egui_ctx.egui_wants_pointer_input(), } } @@ -556,7 +558,7 @@ impl State { }); EventResponse { repaint: true, - consumed: self.egui_ctx.wants_pointer_input(), + consumed: self.egui_ctx.egui_wants_pointer_input(), } } } diff --git a/crates/egui/src/containers/area.rs b/crates/egui/src/containers/area.rs index 99b8e56a7..35afab35a 100644 --- a/crates/egui/src/containers/area.rs +++ b/crates/egui/src/containers/area.rs @@ -712,7 +712,7 @@ fn automatic_area_position(ctx: &Context, layer_id: LayerId) -> Pos2 { // NOTE: for the benefit of the egui demo, we position the windows so they don't // cover the side panels, which means we use `available_rect` here instead of `constrain_rect` or `screen_rect`. - let available_rect = ctx.available_rect(); + let available_rect = ctx.globally_available_rect(); let spacing = 16.0; let left = available_rect.left() + spacing; diff --git a/crates/egui/src/containers/panel.rs b/crates/egui/src/containers/panel.rs index 6b1fabfd7..2816a9f01 100644 --- a/crates/egui/src/containers/panel.rs +++ b/crates/egui/src/containers/panel.rs @@ -754,7 +754,7 @@ impl Panel { add_contents: Box R + 'c>, ) -> InnerResponse { let side = self.side; - let available_rect = ctx.available_rect(); + let available_rect = ctx.globally_available_rect(); let mut panel_ui = Ui::new( ctx.clone(), self.id, @@ -1050,7 +1050,7 @@ impl CentralPanel { id, UiBuilder::new() .layer_id(LayerId::background()) - .max_rect(ctx.available_rect().round_ui()), + .max_rect(ctx.globally_available_rect().round_ui()), ); panel_ui.set_clip_rect(ctx.content_rect()); diff --git a/crates/egui/src/context.rs b/crates/egui/src/context.rs index 0f23178e5..9695c0622 100644 --- a/crates/egui/src/context.rs +++ b/crates/egui/src/context.rs @@ -2798,12 +2798,18 @@ impl Context { } /// How much space is still available after panels have been added. - pub fn available_rect(&self) -> Rect { + pub fn globally_available_rect(&self) -> Rect { self.pass_state(|s| s.available_rect()).round_ui() } + /// How much space is still available after panels have been added. + #[deprecated = "Renamed to globally_available_rect"] + pub fn available_rect(&self) -> Rect { + self.globally_available_rect() + } + /// How much space is used by panels and windows. - pub fn used_rect(&self) -> Rect { + pub fn globally_used_rect(&self) -> Rect { self.write(|ctx| { let mut used = ctx.viewport().this_pass.used_by_panels; for (_id, window) in ctx.memory.areas().visible_windows() { @@ -2813,17 +2819,31 @@ impl Context { }) } + /// How much space is used by panels and windows. + #[deprecated = "Renamed to globally_used_rect"] + pub fn used_rect(&self) -> Rect { + self.globally_used_rect() + } + /// How much space is used by panels and windows. /// /// You can shrink your egui area to this size and still fit all egui components. + pub fn globally_used_size(&self) -> Vec2 { + (self.globally_used_rect().max - Pos2::ZERO).round_ui() + } + + /// How much space is used by panels and windows. + /// + /// You can shrink your egui area to this size and still fit all egui components. + #[deprecated = "Renamed to globally_used_size"] pub fn used_size(&self) -> Vec2 { - (self.used_rect().max - Pos2::ZERO).round_ui() + (self.globally_used_rect().max - Pos2::ZERO).round_ui() } // --------------------------------------------------------------------- /// Is the pointer (mouse/touch) over any egui area? - pub fn is_pointer_over_area(&self) -> bool { + pub fn is_pointer_over_egui(&self) -> bool { let pointer_pos = self.input(|i| i.pointer.interact_pos()); if let Some(pointer_pos) = pointer_pos { if let Some(layer) = self.layer_id_at(pointer_pos) { @@ -2840,29 +2860,60 @@ impl Context { } } + /// Is the pointer (mouse/touch) over any egui area? + #[deprecated = "Renamed to is_pointer_over_egui"] + pub fn is_pointer_over_area(&self) -> bool { + self.is_pointer_over_egui() + } + /// True if egui is currently interested in the pointer (mouse or touch). /// /// Could be the pointer is hovering over a [`crate::Window`] or the user is dragging a widget. /// If `false`, the pointer is outside of any egui area and so /// you may be interested in what it is doing (e.g. controlling your game). /// Returns `false` if a drag started outside of egui and then moved over an egui area. + pub fn egui_wants_pointer_input(&self) -> bool { + self.egui_is_using_pointer() + || (self.is_pointer_over_egui() && !self.input(|i| i.pointer.any_down())) + } + + /// True if egui is currently interested in the pointer (mouse or touch). + /// + /// Could be the pointer is hovering over a [`crate::Window`] or the user is dragging a widget. + /// If `false`, the pointer is outside of any egui area and so + /// you may be interested in what it is doing (e.g. controlling your game). + /// Returns `false` if a drag started outside of egui and then moved over an egui area. + #[deprecated = "Renamed to egui_wants_pointer_input"] pub fn wants_pointer_input(&self) -> bool { - self.is_using_pointer() - || (self.is_pointer_over_area() && !self.input(|i| i.pointer.any_down())) + self.egui_wants_pointer_input() } /// Is egui currently using the pointer position (e.g. dragging a slider)? /// /// NOTE: this will return `false` if the pointer is just hovering over an egui area. - pub fn is_using_pointer(&self) -> bool { + pub fn egui_is_using_pointer(&self) -> bool { self.memory(|m| m.interaction().is_using_pointer()) } + /// Is egui currently using the pointer position (e.g. dragging a slider)? + /// + /// NOTE: this will return `false` if the pointer is just hovering over an egui area. + #[deprecated = "Renamed to egui_is_using_pointer"] + pub fn is_using_pointer(&self) -> bool { + self.egui_is_using_pointer() + } + /// If `true`, egui is currently listening on text input (e.g. typing text in a [`crate::TextEdit`]). - pub fn wants_keyboard_input(&self) -> bool { + pub fn egui_wants_keyboard_input(&self) -> bool { self.memory(|m| m.focused().is_some()) } + /// If `true`, egui is currently listening on text input (e.g. typing text in a [`crate::TextEdit`]). + #[deprecated = "Renamed to egui_wants_keyboard_input"] + pub fn wants_keyboard_input(&self) -> bool { + self.egui_wants_keyboard_input() + } + /// Highlight this widget, to make it look like it is hovered, even if it isn't. /// /// If you call this after the widget has been fully rendered, @@ -2877,7 +2928,7 @@ impl Context { /// /// This only works with the old, deprecated [`crate::menu`] API. #[expect(deprecated)] - #[deprecated = "Use `is_popup_open` instead"] + #[deprecated = "Use `any_popup_open` instead"] pub fn is_context_menu_open(&self) -> bool { self.data(|d| { d.get_temp::(crate::menu::CONTEXT_MENU_ID_STR.into()) @@ -2888,6 +2939,18 @@ impl Context { /// Is a popup or (context) menu open? /// /// Will return false for [`crate::Tooltip`]s (which are technically popups as well). + pub fn any_popup_open(&self) -> bool { + self.pass_state_mut(|fs| { + fs.layers + .values() + .any(|layer| !layer.open_popups.is_empty()) + }) + } + + /// Is a popup or (context) menu open? + /// + /// Will return false for [`crate::Tooltip`]s (which are technically popups as well). + #[deprecated = "Renamed to any_popup_open"] pub fn is_popup_open(&self) -> bool { self.pass_state_mut(|fs| { fs.layers @@ -3217,16 +3280,16 @@ impl Context { ui.label("Is using pointer") .on_hover_text("Is egui currently using the pointer actively (e.g. dragging a slider)?"); - ui.monospace(self.is_using_pointer().to_string()); + ui.monospace(self.egui_is_using_pointer().to_string()); ui.end_row(); ui.label("Wants pointer input") .on_hover_text("Is egui currently interested in the location of the pointer (either because it is in use, or because it is hovering over a window)."); - ui.monospace(self.wants_pointer_input().to_string()); + ui.monospace(self.egui_wants_pointer_input().to_string()); ui.end_row(); ui.label("Wants keyboard input").on_hover_text("Is egui currently listening for text input?"); - ui.monospace(self.wants_keyboard_input().to_string()); + ui.monospace(self.egui_wants_keyboard_input().to_string()); ui.end_row(); ui.label("Keyboard focus widget").on_hover_text("Is egui currently listening for text input?"); diff --git a/crates/egui_extras/src/datepicker/button.rs b/crates/egui_extras/src/datepicker/button.rs index a6e278286..98aceefe2 100644 --- a/crates/egui_extras/src/datepicker/button.rs +++ b/crates/egui_extras/src/datepicker/button.rs @@ -194,7 +194,7 @@ impl Widget for DatePickerButton<'_> { // We don't want to close our popup if any other popup is open, since other popups would // most likely be the combo boxes in the date picker. - let any_popup_open = ui.ctx().is_popup_open(); + let any_popup_open = ui.any_popup_open(); if !button_response.clicked() && !any_popup_open && (ui.input(|i| i.key_pressed(Key::Escape)) || area_response.clicked_elsewhere()) diff --git a/examples/custom_keypad/src/keypad.rs b/examples/custom_keypad/src/keypad.rs index 2e8e9809e..028ce5834 100644 --- a/examples/custom_keypad/src/keypad.rs +++ b/examples/custom_keypad/src/keypad.rs @@ -179,7 +179,7 @@ impl Keypad { ) }); - let is_first_show = ctx.wants_keyboard_input() && state.focus != focus; + let is_first_show = ctx.egui_wants_keyboard_input() && state.focus != focus; if is_first_show { let y = ctx.global_style().spacing.interact_size.y * 1.25; state.open = true;