git.lucas.co / cce-weather
weather: now, the next 24 hours and the week
git clone https://git.lucas.co/cce-weather.git

commit2219f80b90e4d5c76f0ecaaeafd382d495a15d2c
parent90801f6519
authorLucas Galante <lsgalante12@gmail.com>
date2026-10-07 15:30
refactor: register widgets by reference (UiContext::register_host)

register_widget(id, ptr) took a raw pointer in a safe function; cce-ui is
making it an unsafe fn now that every app registers through
register_host(&mut w), which takes the widget's own id and a live borrow.
Registration loops that existed only to hand the registry raw pointers
(and the unsafe blocks and self_ptr casts around them) are gone.

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

 src/main.rs | 18 +++++-------------
 1 file changed, 5 insertions(+), 13 deletions(-)

diff --git a/src/main.rs b/src/main.rs
index 2d245a7..60ef131 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -191,13 +191,6 @@ impl WeatherApp {
         ids
     }
 
-    fn roots(&mut self) -> Vec<*mut (dyn WidgetHost + 'static)> {
-        let mut v: Vec<*mut (dyn WidgetHost + 'static)> =
-            vec![self.search.as_ptr_mut(), self.units_btn.as_ptr_mut(), self.refresh_btn.as_ptr_mut()];
-        v.extend(self.results_btns.iter_mut().map(|b| b.as_ptr_mut()));
-        v
-    }
-
     fn picking(&self) -> bool {
         !self.results.is_empty()
     }
@@ -762,12 +755,11 @@ impl Application for WeatherApp {
     fn display_list(&mut self, size: LogicalSize, scale: f64) -> Option<DisplayList> {
         if !self.widgets_registered {
             self.widgets_registered = true;
-            let self_ptr = self as *mut Self;
-            unsafe {
-                for w in (*self_ptr).roots() {
-                    let id = (*w).base().id();
-                    self.ui_context.register_widget(id, w);
-                }
+            self.ui_context.register_host(&mut self.search);
+            self.ui_context.register_host(&mut self.units_btn);
+            self.ui_context.register_host(&mut self.refresh_btn);
+            for b in self.results_btns.iter_mut() {
+                self.ui_context.register_host(b);
             }
         }
         let size_changed = self.size != (size.width, size.height) || self.scale != scale;