git.lucas.co / cce-ui
GPU-accelerated UI toolkit (Vulkan)
git clone https://git.lucas.co/cce-ui.git

commit0a34e0786fb8c56329a72f30c9eb66d52e296a23
parentef2d668722
authorLucas Galante <lsgalante12@gmail.com>
date2026-10-08 20:01
feat(context): focus_id / unfocus_id, focus_widget and unfocus_widget by id

For a widget the context owns: a handle cannot lend the widget and the
context to `focus_widget(&mut w)` at once. Neither hands the widget the
context, so neither needs lending.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

 src/context.rs | 23 +++++++++++++++++++++++
 1 file changed, 23 insertions(+)

diff --git a/src/context.rs b/src/context.rs
index 9e7d05e..962480b 100644
--- a/src/context.rs
+++ b/src/context.rs
@@ -740,6 +740,29 @@ impl UiContext {
         w.unfocus();
     }
 
+    /// [`focus_widget`](Self::focus_widget) by id — for a widget the context owns.
+    pub fn focus_id(&mut self, id: WidgetId) {
+        if let Some(old) = self.focused_widget.filter(|old| *old != id) {
+            if let Some(w) = self.get_widget_mut(old) {
+                w.unfocus();
+            }
+        }
+        self.focused_widget = Some(id);
+        if let Some(w) = self.get_widget_mut(id) {
+            w.focus();
+        }
+    }
+
+    /// [`unfocus_widget`](Self::unfocus_widget) by id — for a widget the context owns.
+    pub fn unfocus_id(&mut self, id: WidgetId) {
+        if self.focused_widget == Some(id) {
+            self.focused_widget = None;
+        }
+        if let Some(w) = self.get_widget_mut(id) {
+            w.unfocus();
+        }
+    }
+
     pub fn clear_if_matches(&mut self, w: &dyn WidgetHost) {
         if self.focused_widget == Some(w.base().id()) {
             self.focused_widget = None;