| 1 | //! De sessiebrowser van rusty-teams als [`SessionTabs`] voor het koppelen |
| 2 | //! (Linux/macOS: de beheerde Chrome/Firefox uit ADR 0003). Op Windows kan de |
| 3 | //! WebView2-host nog geen los tabblad openen; daar blijft de systeembrowser. |
| 4 | |
| 5 | use std::sync::Arc; |
| 6 | |
| 7 | use kvt_sleutels::session::SessionTabs; |
| 8 | |
| 9 | #[cfg(any(target_os = "linux", target_os = "macos"))] |
| 10 | mod managed { |
| 11 | use kvt_sleutels::session::{BoxFuture, SessionTabs}; |
| 12 | use serde_json::Value; |
| 13 | |
| 14 | pub(super) struct ManagedTabs(pub(super) browser::Browser); |
| 15 | |
| 16 | impl SessionTabs for ManagedTabs { |
| 17 | fn open<'a>(&'a self, url: &'a str) -> BoxFuture<'a, Result<String, String>> { |
| 18 | Box::pin(async move { |
| 19 | self.0 |
| 20 | .kvt_open_tab(url) |
| 21 | .await |
| 22 | .map_err(|error| error.to_string()) |
| 23 | }) |
| 24 | } |
| 25 | |
| 26 | fn evaluate<'a>( |
| 27 | &'a self, |
| 28 | tab: &'a str, |
| 29 | expression: &'a str, |
| 30 | ) -> BoxFuture<'a, Result<Value, String>> { |
| 31 | Box::pin(async move { |
| 32 | self.0 |
| 33 | .kvt_evaluate(tab, expression) |
| 34 | .await |
| 35 | .map_err(|error| error.to_string()) |
| 36 | }) |
| 37 | } |
| 38 | |
| 39 | fn close<'a>(&'a self, tab: &'a str) -> BoxFuture<'a, ()> { |
| 40 | Box::pin(async move { |
| 41 | let _ = self.0.kvt_close_tab(tab).await; |
| 42 | }) |
| 43 | } |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | pub(super) fn session_tabs() -> Option<Arc<dyn SessionTabs>> { |
| 48 | #[cfg(any(target_os = "linux", target_os = "macos"))] |
| 49 | { |
| 50 | crate::linux_browser::managed() |
| 51 | .map(|browser| Arc::new(managed::ManagedTabs(browser)) as Arc<dyn SessionTabs>) |
| 52 | } |
| 53 | #[cfg(not(any(target_os = "linux", target_os = "macos")))] |
| 54 | { |
| 55 | None |
| 56 | } |
| 57 | } |