mirror of
https://github.com/emilk/egui.git
synced 2026-09-01 06:10:06 -04:00
Track the global focus state of the UI (#1859)
* Track the global focus state of the UI * Fix changelog entries * Document the new difference between `Response::has_focus` and `Memory::has_focus`
This commit is contained in:
@@ -62,6 +62,9 @@ pub struct RawInput {
|
||||
/// Note: when using `eframe` on Windows you need to enable
|
||||
/// drag-and-drop support using `eframe::NativeOptions`.
|
||||
pub dropped_files: Vec<DroppedFile>,
|
||||
|
||||
/// The window has the keyboard focus (i.e. is receiving key presses).
|
||||
pub has_focus: bool,
|
||||
}
|
||||
|
||||
impl Default for RawInput {
|
||||
@@ -76,6 +79,7 @@ impl Default for RawInput {
|
||||
events: vec![],
|
||||
hovered_files: Default::default(),
|
||||
dropped_files: Default::default(),
|
||||
has_focus: true, // integrations opt into global focus tracking
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -96,6 +100,7 @@ impl RawInput {
|
||||
events: std::mem::take(&mut self.events),
|
||||
hovered_files: self.hovered_files.clone(),
|
||||
dropped_files: std::mem::take(&mut self.dropped_files),
|
||||
has_focus: self.has_focus,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -111,6 +116,7 @@ impl RawInput {
|
||||
mut events,
|
||||
mut hovered_files,
|
||||
mut dropped_files,
|
||||
has_focus,
|
||||
} = newer;
|
||||
|
||||
self.screen_rect = screen_rect.or(self.screen_rect);
|
||||
@@ -122,6 +128,7 @@ impl RawInput {
|
||||
self.events.append(&mut events);
|
||||
self.hovered_files.append(&mut hovered_files);
|
||||
self.dropped_files.append(&mut dropped_files);
|
||||
self.has_focus = has_focus;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -541,6 +548,7 @@ impl RawInput {
|
||||
events,
|
||||
hovered_files,
|
||||
dropped_files,
|
||||
has_focus,
|
||||
} = self;
|
||||
|
||||
ui.label(format!("screen_rect: {:?} points", screen_rect));
|
||||
@@ -558,6 +566,7 @@ impl RawInput {
|
||||
ui.label(format!("modifiers: {:#?}", modifiers));
|
||||
ui.label(format!("hovered_files: {}", hovered_files.len()));
|
||||
ui.label(format!("dropped_files: {}", dropped_files.len()));
|
||||
ui.label(format!("has_focus: {}", has_focus));
|
||||
ui.scope(|ui| {
|
||||
ui.set_min_height(150.0);
|
||||
ui.label(format!("events: {:#?}", events))
|
||||
|
||||
@@ -350,6 +350,11 @@ impl Memory {
|
||||
}
|
||||
|
||||
/// Does this widget have keyboard focus?
|
||||
///
|
||||
/// This function does not consider whether the UI as a whole (e.g. window)
|
||||
/// has the keyboard focus. That makes this function suitable for deciding
|
||||
/// widget state that should not be disrupted if the user moves away
|
||||
/// from the window and back.
|
||||
#[inline(always)]
|
||||
pub fn has_focus(&self, id: Id) -> bool {
|
||||
self.interaction.focus.id == Some(id)
|
||||
|
||||
@@ -212,8 +212,14 @@ impl Response {
|
||||
}
|
||||
|
||||
/// This widget has the keyboard focus (i.e. is receiving key presses).
|
||||
///
|
||||
/// This function only returns true if the UI as a whole (e.g. window)
|
||||
/// also has the keyboard focus. That makes this function suitable
|
||||
/// for style choices, e.g. a thicker border around focused widgets.
|
||||
pub fn has_focus(&self) -> bool {
|
||||
self.ctx.memory().has_focus(self.id)
|
||||
// Access input and memory in separate statements to prevent deadlock.
|
||||
let has_global_focus = self.ctx.input().raw.has_focus;
|
||||
has_global_focus && self.ctx.memory().has_focus(self.id)
|
||||
}
|
||||
|
||||
/// True if this widget has keyboard focus this frame, but didn't last frame.
|
||||
|
||||
Reference in New Issue
Block a user