Track active window id in test platform

Antonio Scandurra and Nathan Sobo created

Co-Authored-By: Nathan Sobo <nathan@zed.dev>

Change summary

crates/gpui/src/app.rs                   |  2 
crates/gpui/src/platform.rs              |  2 
crates/gpui/src/platform/mac/platform.rs |  2 
crates/gpui/src/platform/test.rs         | 37 ++++++++++++++++++-------
4 files changed, 30 insertions(+), 13 deletions(-)

Detailed changes

crates/gpui/src/app.rs 🔗

@@ -1315,7 +1315,7 @@ impl AppContext {
     {
         self.update(|this| {
             let window_id = post_inc(&mut this.next_window_id);
-            let platform_window = this.platform.add_status_item();
+            let platform_window = this.platform.add_status_item(window_id);
             let window = this.build_window(window_id, platform_window, build_root_view);
             let root_view = window.root_view().clone().downcast::<V>().unwrap();
 

crates/gpui/src/platform.rs 🔗

@@ -63,7 +63,7 @@ pub trait Platform: Send + Sync {
     ) -> Box<dyn Window>;
     fn main_window_id(&self) -> Option<usize>;
 
-    fn add_status_item(&self) -> Box<dyn Window>;
+    fn add_status_item(&self, id: usize) -> Box<dyn Window>;
 
     fn write_to_clipboard(&self, item: ClipboardItem);
     fn read_from_clipboard(&self) -> Option<ClipboardItem>;

crates/gpui/src/platform/mac/platform.rs 🔗

@@ -601,7 +601,7 @@ impl platform::Platform for MacPlatform {
         Window::main_window_id()
     }
 
-    fn add_status_item(&self) -> Box<dyn platform::Window> {
+    fn add_status_item(&self, _id: usize) -> Box<dyn platform::Window> {
         Box::new(StatusItem::add(self.fonts()))
     }
 

crates/gpui/src/platform/test.rs 🔗

@@ -102,6 +102,7 @@ pub struct Platform {
     fonts: Arc<dyn super::FontSystem>,
     current_clipboard_item: Mutex<Option<ClipboardItem>>,
     cursor: Mutex<CursorStyle>,
+    active_window_id: Arc<Mutex<Option<usize>>>,
 }
 
 impl Platform {
@@ -111,6 +112,7 @@ impl Platform {
             fonts: Arc::new(super::current::FontSystem::new()),
             current_clipboard_item: Default::default(),
             cursor: Mutex::new(CursorStyle::Arrow),
+            active_window_id: Default::default(),
         }
     }
 }
@@ -144,22 +146,31 @@ impl super::Platform for Platform {
 
     fn open_window(
         &self,
-        _: usize,
+        id: usize,
         options: super::WindowOptions,
         _executor: Rc<super::executor::Foreground>,
     ) -> Box<dyn super::Window> {
-        Box::new(Window::new(match options.bounds {
-            WindowBounds::Maximized | WindowBounds::Fullscreen => vec2f(1024., 768.),
-            WindowBounds::Fixed(rect) => rect.size(),
-        }))
+        *self.active_window_id.lock() = Some(id);
+        Box::new(Window::new(
+            id,
+            match options.bounds {
+                WindowBounds::Maximized | WindowBounds::Fullscreen => vec2f(1024., 768.),
+                WindowBounds::Fixed(rect) => rect.size(),
+            },
+            self.active_window_id.clone(),
+        ))
     }
 
     fn main_window_id(&self) -> Option<usize> {
-        None
+        self.active_window_id.lock().clone()
     }
 
-    fn add_status_item(&self) -> Box<dyn crate::platform::Window> {
-        Box::new(Window::new(vec2f(24., 24.)))
+    fn add_status_item(&self, id: usize) -> Box<dyn crate::platform::Window> {
+        Box::new(Window::new(
+            id,
+            vec2f(24., 24.),
+            self.active_window_id.clone(),
+        ))
     }
 
     fn write_to_clipboard(&self, item: ClipboardItem) {
@@ -245,6 +256,7 @@ impl super::Screen for Screen {
 }
 
 pub struct Window {
+    id: usize,
     pub(crate) size: Vector2F,
     scale_factor: f32,
     current_scene: Option<crate::Scene>,
@@ -258,11 +270,13 @@ pub struct Window {
     pub(crate) title: Option<String>,
     pub(crate) edited: bool,
     pub(crate) pending_prompts: RefCell<VecDeque<oneshot::Sender<usize>>>,
+    active_window_id: Arc<Mutex<Option<usize>>>,
 }
 
 impl Window {
-    pub fn new(size: Vector2F) -> Self {
+    pub fn new(id: usize, size: Vector2F, active_window_id: Arc<Mutex<Option<usize>>>) -> Self {
         Self {
+            id,
             size,
             event_handlers: Default::default(),
             resize_handlers: Default::default(),
@@ -276,6 +290,7 @@ impl Window {
             title: None,
             edited: false,
             pending_prompts: Default::default(),
+            active_window_id,
         }
     }
 
@@ -326,7 +341,9 @@ impl super::Window for Window {
         done_rx
     }
 
-    fn activate(&self) {}
+    fn activate(&self) {
+        *self.active_window_id.lock() = Some(self.id);
+    }
 
     fn set_title(&mut self, title: &str) {
         self.title = Some(title.to_string())