Add Response::request_focus and surrender_focus

This commit is contained in:
Emil Ernerfeldt 2021-04-15 09:45:12 +02:00
parent daf2e13238
commit 96a2732735
3 changed files with 15 additions and 1 deletions

View file

@ -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)

View file

@ -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) {

View file

@ -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`]