From 96a273273563018feec00687ae56dd49f033fcb1 Mon Sep 17 00:00:00 2001 From: Emil Ernerfeldt Date: Thu, 15 Apr 2021 09:45:12 +0200 Subject: [PATCH] Add Response::request_focus and surrender_focus --- CHANGELOG.md | 1 + egui/src/memory.rs | 5 ++++- egui/src/response.rs | 10 ++++++++++ 3 files changed, 15 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a77a297d..abe2c1e9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,7 @@ NOTE: [`eframe`](eframe/CHANGELOG.md), [`egui_web`](egui_web/CHANGELOG.md) and [ ### Added ⭐ * Make labels interactive with `Label::sense(Sense::click())`. +* Add `Response::request_focus` and `Response::surrender_focus`. ### Fixed 🐛 * Fix [defocus-bug on touch screens](https://github.com/emilk/egui/issues/288) diff --git a/egui/src/memory.rs b/egui/src/memory.rs index b3f8dead..117b66f3 100644 --- a/egui/src/memory.rs +++ b/egui/src/memory.rs @@ -300,11 +300,14 @@ impl Memory { self.interaction.focus.id == Some(id) } - /// Give keyboard focus to a specific widget + /// Give keyboard focus to a specific widget. + /// See also [`crate::Response::request_focus`]. pub fn request_focus(&mut self, id: Id) { self.interaction.focus.id = Some(id); } + /// Surrender keyboard focus for a specific widget. + /// See also [`crate::Response::surrender_focus`]. #[inline(always)] pub fn surrender_focus(&mut self, id: Id) { if self.interaction.focus.id == Some(id) { diff --git a/egui/src/response.rs b/egui/src/response.rs index cbae32e9..8d56cf12 100644 --- a/egui/src/response.rs +++ b/egui/src/response.rs @@ -199,6 +199,16 @@ impl Response { self.lost_focus() } + /// Request that this widget get keyboard focus. + pub fn request_focus(&self) { + self.ctx.memory().request_focus(self.id) + } + + /// Surrender keyboard focus for this widget. + pub fn surrender_focus(&self) { + self.ctx.memory().surrender_focus(self.id) + } + /// The widgets is being dragged. /// /// To find out which button(s), query [`crate::PointerState::button_down`]