From 6fd7c422ab8383ebb3c76c009a42145666a2177b Mon Sep 17 00:00:00 2001 From: Emil Ernerfeldt Date: Sun, 7 Mar 2021 13:06:47 +0100 Subject: [PATCH] Refactor: remove has_kb_focus/lost_kb_focus bools from Reponse Just forward the queries to Memory --- egui/src/context.rs | 5 ----- egui/src/response.rs | 21 +++++++-------------- egui/src/style.rs | 2 +- egui/src/widgets/text_edit.rs | 7 ++----- 4 files changed, 10 insertions(+), 25 deletions(-) diff --git a/egui/src/context.rs b/egui/src/context.rs index 58c054f9..4f34c6bb 100644 --- a/egui/src/context.rs +++ b/egui/src/context.rs @@ -179,9 +179,6 @@ impl CtxRef { ) -> Response { let hovered = hovered && enabled; // can't even hover disabled widgets - let has_kb_focus = self.memory().has_kb_focus(id); - let lost_kb_focus = self.memory().lost_kb_focus(id); - let mut response = Response { ctx: self.clone(), layer_id, @@ -196,8 +193,6 @@ impl CtxRef { drag_released: false, is_pointer_button_down_on: false, interact_pointer_pos: None, - has_kb_focus, - lost_kb_focus, changed: false, // must be set by the widget itself }; diff --git a/egui/src/response.rs b/egui/src/response.rs index c3014258..83eae239 100644 --- a/egui/src/response.rs +++ b/egui/src/response.rs @@ -60,12 +60,6 @@ pub struct Response { /// `None` if the widget is not being interacted with. pub(crate) interact_pointer_pos: Option, - /// This widget has the keyboard focus (i.e. is receiving key presses). - pub(crate) has_kb_focus: bool, - - /// The widget had keyboard focus and lost it. - pub(crate) lost_kb_focus: bool, - /// What the underlying data changed? /// e.g. the slider was dragged, text was entered in a `TextEdit` etc. /// Always `false` for something like a `Button`. @@ -88,8 +82,6 @@ impl std::fmt::Debug for Response { drag_released, is_pointer_button_down_on, interact_pointer_pos, - has_kb_focus, - lost_kb_focus, changed, } = self; f.debug_struct("Response") @@ -105,8 +97,6 @@ impl std::fmt::Debug for Response { .field("drag_released", drag_released) .field("is_pointer_button_down_on", is_pointer_button_down_on) .field("interact_pointer_pos", interact_pointer_pos) - .field("has_kb_focus", has_kb_focus) - .field("lost_kb_focus", lost_kb_focus) .field("changed", changed) .finish() } @@ -162,7 +152,12 @@ impl Response { /// This widget has the keyboard focus (i.e. is receiving key presses). pub fn has_kb_focus(&self) -> bool { - self.has_kb_focus + self.ctx.memory().has_kb_focus(self.id) + } + + /// True if this widget has keyboard focus this frame, but didn't last frame. + pub fn gained_kb_focus(&self) -> bool { + self.ctx.memory().gained_kb_focus(self.id) } /// The widget had keyboard focus and lost it, @@ -179,7 +174,7 @@ impl Response { /// } /// ``` pub fn lost_kb_focus(&self) -> bool { - self.lost_kb_focus + self.ctx.memory().lost_kb_focus(self.id) } /// The widgets is being dragged. @@ -374,8 +369,6 @@ impl Response { is_pointer_button_down_on: self.is_pointer_button_down_on || other.is_pointer_button_down_on, interact_pointer_pos: self.interact_pointer_pos.or(other.interact_pointer_pos), - has_kb_focus: self.has_kb_focus || other.has_kb_focus, - lost_kb_focus: self.lost_kb_focus || other.lost_kb_focus, changed: self.changed || other.changed, } } diff --git a/egui/src/style.rs b/egui/src/style.rs index f033acac..15692821 100644 --- a/egui/src/style.rs +++ b/egui/src/style.rs @@ -243,7 +243,7 @@ pub struct Widgets { impl Widgets { pub fn style(&self, response: &Response) -> &WidgetVisuals { - if response.is_pointer_button_down_on() || response.has_kb_focus { + if response.is_pointer_button_down_on() || response.has_kb_focus() { &self.active } else if response.hovered() { &self.hovered diff --git a/egui/src/widgets/text_edit.rs b/egui/src/widgets/text_edit.rs index 0339cf4f..96bbe817 100644 --- a/egui/src/widgets/text_edit.rs +++ b/egui/src/widgets/text_edit.rs @@ -248,7 +248,7 @@ impl<'t> Widget for TextEdit<'t> { if frame { let visuals = ui.style().interact(&response); let frame_rect = response.rect.expand(visuals.expansion); - let shape = if response.has_kb_focus { + let shape = if response.has_kb_focus() { Shape::Rect { rect: frame_rect, corner_radius: visuals.corner_radius, @@ -541,10 +541,7 @@ impl<'t> TextEdit<'t> { ui.memory().text_edit.insert(id, state); - Response { - lost_kb_focus: ui.memory().lost_kb_focus(id), // we may have lost it during the course of this function - ..response - } + response } }