From 81d642b1f1ed8b1926ce2f13e188c4e680c90a87 Mon Sep 17 00:00:00 2001 From: Emil Ernerfeldt Date: Wed, 5 Aug 2020 13:59:33 +0200 Subject: [PATCH] [text] highlight the text edit widget being edited --- egui/src/context.rs | 9 +++++++++ egui/src/style.rs | 6 +++--- egui/src/types.rs | 9 +++++++++ egui/src/ui.rs | 2 ++ 4 files changed, 23 insertions(+), 3 deletions(-) diff --git a/egui/src/context.rs b/egui/src/context.rs index c55d0633d..637fbd5f3 100644 --- a/egui/src/context.rs +++ b/egui/src/context.rs @@ -337,6 +337,9 @@ impl Context { ) -> InteractInfo { let interact_rect = rect.expand2(0.5 * self.style().item_spacing); // make it easier to click. TODO: nice way to do this let hovered = self.contains_mouse(layer, clip_rect, interact_rect); + let has_kb_focus = interaction_id + .map(|id| self.memory().has_kb_focus(id)) + .unwrap_or(false); if interaction_id.is_none() || sense == Sense::nothing() { // Not interested in input: @@ -347,6 +350,7 @@ impl Context { clicked: false, double_clicked: false, active: false, + has_kb_focus, }; } let interaction_id = interaction_id.unwrap(); @@ -368,6 +372,7 @@ impl Context { clicked: false, double_clicked: false, active: false, + has_kb_focus, }; if sense.click && memory.interaction.click_id.is_none() { @@ -396,6 +401,7 @@ impl Context { clicked: false, double_clicked: false, active: false, + has_kb_focus, } } } else if self.input.mouse.released { @@ -407,6 +413,7 @@ impl Context { clicked, double_clicked: clicked && self.input.mouse.double_click, active, + has_kb_focus, } } else if self.input.mouse.down { InteractInfo { @@ -416,6 +423,7 @@ impl Context { clicked: false, double_clicked: false, active, + has_kb_focus, } } else { InteractInfo { @@ -425,6 +433,7 @@ impl Context { clicked: false, double_clicked: false, active, + has_kb_focus, } } } diff --git a/egui/src/style.rs b/egui/src/style.rs index 972e8fe44..414471f66 100644 --- a/egui/src/style.rs +++ b/egui/src/style.rs @@ -149,10 +149,10 @@ impl Default for Interact { impl Interact { pub fn style(&self, interact: &InteractInfo) -> &WidgetStyle { - if interact.sense == Sense::nothing() { - &self.disabled - } else if interact.active { + if interact.active || interact.has_kb_focus { &self.active + } else if interact.sense == Sense::nothing() { + &self.disabled } else if interact.hovered { &self.hovered } else { diff --git a/egui/src/types.rs b/egui/src/types.rs index 0adbf2a46..20a3afc13 100644 --- a/egui/src/types.rs +++ b/egui/src/types.rs @@ -66,6 +66,9 @@ pub struct InteractInfo { /// The mouse is interacting with this thing (e.g. dragging it or holding it) pub active: bool, + /// This widget has the keyboard focus (i.e. is receiving key pressed) + pub has_kb_focus: bool, + /// The region of the screen we are talking about pub rect: Rect, } @@ -78,6 +81,7 @@ impl InteractInfo { clicked: false, double_clicked: false, active: false, + has_kb_focus: false, rect: Rect::nothing(), } } @@ -89,6 +93,7 @@ impl InteractInfo { clicked: self.clicked || other.clicked, double_clicked: self.double_clicked || other.double_clicked, active: self.active || other.active, + has_kb_focus: self.has_kb_focus || other.has_kb_focus, rect: self.rect.union(other.rect), } } @@ -115,6 +120,9 @@ pub struct GuiResponse { /// The mouse is interacting with this thing (e.g. dragging it) pub active: bool, + /// This widget has the keyboard focus (i.e. is receiving key pressed) + pub has_kb_focus: bool, + /// The area of the screen we are talking about pub rect: Rect, @@ -147,6 +155,7 @@ impl Into for GuiResponse { clicked: self.clicked, double_clicked: self.double_clicked, active: self.active, + has_kb_focus: self.has_kb_focus, rect: self.rect, } } diff --git a/egui/src/ui.rs b/egui/src/ui.rs index aa99564c8..18fbb3b40 100644 --- a/egui/src/ui.rs +++ b/egui/src/ui.rs @@ -336,6 +336,7 @@ impl Ui { clicked, double_clicked, active, + has_kb_focus, rect, } = interact; GuiResponse { @@ -344,6 +345,7 @@ impl Ui { clicked, double_clicked, active, + has_kb_focus, rect, ctx: self.ctx().clone(), }