BlueGit

ict rusty-teams

Files

rusty-teams / crates / app / src / inline_video / native_windows.rs

native_windows.rs

33b53d7d History
native_windows.rs 2.8 KiB Download Raw
1use std::cell::{Cell, OnceCell, RefCell};
2use std::path::PathBuf;
3use std::sync::OnceLock;
4
5use gpui_kit::*;
6use tokio::sync::mpsc::{UnboundedReceiver, unbounded_channel};
7use webview::{EmbedBounds, EmbedEvent, EmbedHost, EmbedSpec, MainEmbed};
8
9use crate::notify::platform;
10use crate::theme;
11
12static USER_DATA_FOLDER: OnceLock<PathBuf> = OnceLock::new();
13
14thread_local! {
15 static HOST: OnceCell<EmbedHost> = const { OnceCell::new() };
16}
17
18pub fn configure(user_data_folder: PathBuf) {
19 let _ = USER_DATA_FOLDER.set(user_data_folder);
20}
21
22pub fn available() -> bool {
23 USER_DATA_FOLDER.get().is_some()
24}
25
26fn host() -> Option<EmbedHost> {
27 let folder = USER_DATA_FOLDER.get()?;
28 Some(HOST.with(|host| host.get_or_init(|| EmbedHost::new(folder.clone())).clone()))
29}
30
31fn physical(bounds: Bounds<Pixels>, scale: f32) -> EmbedBounds {
32 EmbedBounds::from_logical(
33 f32::from(bounds.origin.x),
34 f32::from(bounds.origin.y),
35 f32::from(bounds.size.width),
36 f32::from(bounds.size.height),
37 scale,
38 )
39}
40
41pub struct Native {
42 embed: MainEmbed,
43 placed: Cell<Option<(EmbedBounds, bool)>>,
44 cut: RefCell<Vec<EmbedBounds>>,
45}
46
47impl Native {
48 pub fn open(url: &str, window: &Window) -> Option<(Native, UnboundedReceiver<String>)> {
49 let parent = platform::native_handle(window)?;
50 let (embed, mut events) = host()?.open(EmbedSpec {
51 parent,
52 url: url.to_owned(),
53 background: theme::background_hex(),
54 })?;
55 let (opened, new_windows) = unbounded_channel();
56 std::thread::spawn(move || {
57 while let Some(event) = events.blocking_recv() {
58 match event {
59 EmbedEvent::NewWindow(target) => {
60 let _ = opened.send(target);
61 }
62 EmbedEvent::Closed => break,
63 EmbedEvent::Navigated(_) | EmbedEvent::Loaded => {}
64 }
65 }
66 });
67 let native = Native {
68 embed,
69 placed: Cell::new(None),
70 cut: RefCell::new(Vec::new()),
71 };
72 Some((native, new_windows))
73 }
74
75 pub fn place(
76 &self,
77 bounds: Bounds<Pixels>,
78 hidden: &[Bounds<Pixels>],
79 scale: f32,
80 visible: bool,
81 ) {
82 let cut: Vec<EmbedBounds> = hidden.iter().map(|part| physical(*part, scale)).collect();
83 if *self.cut.borrow() != cut {
84 self.embed.set_cutouts(cut.clone());
85 *self.cut.borrow_mut() = cut;
86 }
87 let placement = (physical(bounds, scale), visible);
88 if self.placed.get() != Some(placement) {
89 self.placed.set(Some(placement));
90 self.embed.place(placement.0, placement.1);
91 }
92 }
93
94 pub fn close(&self) {
95 self.embed.close();
96 }
97}