replace oneshot channel with sendable future

David Kleingeld created

Change summary

crates/gpui/src/platform/linux/platform.rs       | 13 ++------
crates/gpui/src/platform/linux/wayland/client.rs | 27 +++++++----------
crates/gpui/src/platform/linux/x11/client.rs     | 15 +++------
3 files changed, 20 insertions(+), 35 deletions(-)

Detailed changes

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

@@ -77,10 +77,8 @@ pub trait LinuxClient {
     #[cfg(any(feature = "wayland", feature = "x11"))]
     fn window_identifier(
         &self,
-    ) -> futures::channel::oneshot::Receiver<Option<ashpd::WindowIdentifier>> {
-        let (sources_tx, sources_rx) = futures::channel::oneshot::channel();
-        sources_tx.send(None).ok();
-        sources_rx
+    ) -> impl Future<Output = Option<ashpd::WindowIdentifier>> + Send + 'static {
+        std::future::ready::<Option<ashpd::WindowIdentifier>>(None)
     }
 }
 
@@ -310,10 +308,9 @@ impl<P: LinuxClient + 'static> Platform for P {
                 } else {
                     "Open File"
                 };
-                let identifier = identifier.await.ok().flatten();
 
                 let request = match ashpd::desktop::file_chooser::OpenFileRequest::default()
-                    .identifier(identifier)
+                    .identifier(identifier.await)
                     .modal(true)
                     .title(title)
                     .accept_label(options.prompt.as_ref().map(crate::SharedString::as_str))
@@ -370,11 +367,9 @@ impl<P: LinuxClient + 'static> Platform for P {
                 let suggested_name = suggested_name.map(|s| s.to_owned());
 
                 async move {
-                    let identifier = identifier.await.ok().flatten();
-
                     let mut request_builder =
                         ashpd::desktop::file_chooser::SaveFileRequest::default()
-                            .identifier(identifier)
+                            .identifier(identifier.await)
                             .modal(true)
                             .title("Save File")
                             .current_folder(directory)

crates/gpui/src/platform/linux/wayland/client.rs 🔗

@@ -860,23 +860,18 @@ impl LinuxClient for WaylandClient {
         "Wayland"
     }
 
-    fn window_identifier(&self) -> futures::channel::oneshot::Receiver<Option<WindowIdentifier>> {
-        let (done_tx, done_rx) = futures::channel::oneshot::channel();
-        let client_state = self.0.borrow();
-        let executor = &client_state.common.foreground_executor;
-
-        if let Some(active_window) = client_state.keyboard_focused_window.as_ref() {
-            let surface = active_window.surface();
-            executor
-                .spawn(async move {
-                    let window_identifier = ashpd::WindowIdentifier::from_wayland(&surface).await;
-                    done_tx.send(window_identifier).ok();
-                })
-                .detach();
-        } else {
-            done_tx.send(None).ok();
+    fn window_identifier(&self) -> impl Future<Output = Option<WindowIdentifier>> + Send + 'static {
+        async fn inner(surface: Option<wl_surface::WlSurface>) -> Option<WindowIdentifier> {
+            if let Some(surface) = surface {
+                ashpd::WindowIdentifier::from_wayland(&surface).await
+            } else {
+                None
+            }
         }
-        done_rx
+
+        let client_state = self.0.borrow();
+        let active_window = client_state.keyboard_focused_window.as_ref();
+        inner(active_window.map(|aw| aw.surface()))
     }
 }
 

crates/gpui/src/platform/linux/x11/client.rs 🔗

@@ -1662,19 +1662,14 @@ impl LinuxClient for X11Client {
         Some(handles)
     }
 
-    fn window_identifier(&self) -> futures::channel::oneshot::Receiver<Option<WindowIdentifier>> {
-        let (done_tx, done_rx) = futures::channel::oneshot::channel();
+    fn window_identifier(&self) -> impl Future<Output = Option<WindowIdentifier>> + Send + 'static {
         let state = self.0.borrow();
-        if let Some(window) = state
+        state
             .keyboard_focused_window
             .and_then(|focused_window| state.windows.get(&focused_window))
-        {
-            let window_identifier = WindowIdentifier::from_xid(window.window.x_window as u64);
-            done_tx.send(Some(window_identifier)).ok();
-        } else {
-            done_tx.send(None).ok();
-        }
-        done_rx
+            .map(|window| window.window.x_window as u64)
+            .map(|x_window| std::future::ready(Some(WindowIdentifier::from_xid(x_window))))
+            .unwrap_or(std::future::ready(None))
     }
 }