BlueGit

ict rusty-teams

Files

rusty-teams / crates / app-state / src / task_dialog / hosted.rs

hosted.rs

hosted.rs 5.0 KiB Download Raw
1use std::sync::{Arc, OnceLock};
2
3use gpui_kit::*;
4use serde_json::json;
5use teams_core::DialogIdentity;
6use webview::{DialogEvent, DialogHandle, DialogMode, DialogSpec, WebViewTransport};
7
8use super::sdk::{self, Effect, HostContext, HostPage};
9use super::{UrlDialog, UrlDialogNext};
10use crate::app_state::{AppHandle, AppState};
11use crate::backend::Engine;
12use crate::{notify, runtime, theme};
13
14static HOST: OnceLock<Arc<WebViewTransport>> = OnceLock::new();
15const CALL_WINDOW_WIDTH: u32 = 1100;
16const CALL_WINDOW_HEIGHT: u32 = 760;
17
18pub fn install_host(transport: Arc<WebViewTransport>) {
19 let _ = HOST.set(transport);
20}
21
22pub fn open(dialog: UrlDialog, engine: Option<Arc<Engine>>, cx: &mut App) {
23 let Some(host) = HOST.get().cloned() else {
24 cx.open_url(&dialog.fallback_url);
25 return;
26 };
27 let state = cx.global::<AppHandle>().0.clone();
28 let owner = cx
29 .active_window()
30 .and_then(|window| {
31 window
32 .update(cx, |_, window, _| notify::native_handle(window))
33 .ok()
34 })
35 .flatten();
36 cx.spawn(async move |cx| run(host, dialog, state, engine, owner, cx).await)
37 .detach();
38}
39
40/// Teams on the web in its own window, signed in through the shared WebView2 profile.
41pub fn open_call_window(url: String, title: String, cx: &mut App) -> bool {
42 let Some(host) = HOST.get().cloned() else {
43 return false;
44 };
45 let (_handle, mut events) = host.open_dialog(DialogSpec {
46 mode: DialogMode::Call,
47 title,
48 width: CALL_WINDOW_WIDTH,
49 height: CALL_WINDOW_HEIGHT,
50 background: theme::background_hex(),
51 owner: None,
52 page_url: url,
53 page_html: String::new(),
54 });
55 cx.spawn(async move |cx| {
56 while let Some(DialogEvent::Message(raw)) = events.recv().await {
57 let link = serde_json::from_str::<serde_json::Value>(&raw)
58 .ok()
59 .and_then(|message| message.get("url")?.as_str().map(str::to_owned));
60 if let Some(link) = link {
61 cx.update(|cx| cx.open_url(&link));
62 }
63 }
64 })
65 .detach();
66 true
67}
68
69async fn run(
70 host: Arc<WebViewTransport>,
71 dialog: UrlDialog,
72 state: Entity<AppState>,
73 engine: Option<Arc<Engine>>,
74 owner: Option<isize>,
75 cx: &mut AsyncApp,
76) {
77 let identity = fetch_identity(engine, &dialog.scope.conversation_id).await;
78 let context = HostContext {
79 user_id: identity.user_id,
80 user_display_name: identity.display_name,
81 tenant_id: identity.tenant_id,
82 conversation_id: dialog.scope.conversation_id.clone(),
83 app_id: dialog.app_id.clone(),
84 session_id: uuid::Uuid::new_v4().to_string(),
85 dark: true,
86 web_application_resource: dialog.web_application_resource.clone(),
87 };
88 let page_html = HostPage {
89 task_url: &dialog.url,
90 background: theme::background_hex(),
91 token_acquirer_script: &session::token_acquirer_script(),
92 }
93 .html();
94 let (handle, mut events) = host.open_dialog(DialogSpec {
95 mode: DialogMode::Hosted,
96 title: dialog.title.clone(),
97 width: dialog.width,
98 height: dialog.height,
99 background: theme::background_hex(),
100 owner,
101 page_url: sdk::host_page_url(),
102 page_html,
103 });
104 while let Some(DialogEvent::Message(raw)) = events.recv().await {
105 let handled = sdk::handle(&raw, &context);
106 for directive in &handled.directives {
107 handle.post(directive.to_string());
108 }
109 if let Some(effect) = handled.effect {
110 apply(effect, &dialog, &state, &handle, cx).await;
111 }
112 }
113}
114
115async fn fetch_identity(engine: Option<Arc<Engine>>, conversation_id: &str) -> DialogIdentity {
116 let empty = DialogIdentity {
117 user_id: String::new(),
118 display_name: String::new(),
119 tenant_id: String::new(),
120 };
121 let Some(engine) = engine else {
122 return empty;
123 };
124 let conversation_id = conversation_id.to_owned();
125 let lookup = runtime::spawn(async move { engine.dialog_identity(&conversation_id).await.ok() });
126 lookup.await.ok().flatten().unwrap_or(empty)
127}
128
129async fn apply(
130 effect: Effect,
131 dialog: &UrlDialog,
132 state: &Entity<AppState>,
133 handle: &DialogHandle,
134 cx: &mut AsyncApp,
135) {
136 match effect {
137 Effect::Resize { width, height } => handle.resize(width, height),
138 Effect::OpenLink(link) => {
139 cx.update(|cx| cx.open_url(&link));
140 }
141 Effect::Submit(result) => {
142 let scope = dialog.scope.clone();
143 let next = state
144 .update(cx, |state, cx| state.submit_url_dialog(scope, result, cx))
145 .await;
146 match next {
147 UrlDialogNext::Close => handle.close(),
148 UrlDialogNext::Navigate { url, width, height } => {
149 handle.resize(Some(width), Some(height));
150 handle.post(json!({"kind": "load", "url": url}).to_string());
151 }
152 }
153 }
154 }
155}