| 1 | use std::cell::Cell; |
| 2 | use std::rc::Rc; |
| 3 | |
| 4 | use gpui_kit::*; |
| 5 | use tokio::sync::mpsc::UnboundedReceiver; |
| 6 | use webview::{EmbedBounds, EmbedHandle, EmbedSpec}; |
| 7 | |
| 8 | use crate::{notify, task_dialog, theme}; |
| 9 | |
| 10 | pub use webview::EmbedEvent as NativeEvent; |
| 11 | |
| 12 | pub fn available() -> bool { |
| 13 | task_dialog::host().is_some() |
| 14 | } |
| 15 | |
| 16 | #[derive(Clone)] |
| 17 | pub struct Native { |
| 18 | handle: EmbedHandle, |
| 19 | placed: Rc<Cell<Option<(EmbedBounds, bool)>>>, |
| 20 | } |
| 21 | |
| 22 | impl Native { |
| 23 | pub fn open(url: &str, window: &Window) -> Option<(Native, UnboundedReceiver<NativeEvent>)> { |
| 24 | let host = task_dialog::host()?; |
| 25 | let parent = notify::native_handle(window)?; |
| 26 | let (handle, events) = host.open_embed(EmbedSpec { |
| 27 | parent, |
| 28 | url: url.to_owned(), |
| 29 | background: theme::BACKGROUND, |
| 30 | }); |
| 31 | let native = Native { |
| 32 | handle, |
| 33 | placed: Rc::new(Cell::new(None)), |
| 34 | }; |
| 35 | Some((native, events)) |
| 36 | } |
| 37 | |
| 38 | pub fn place(&self, bounds: Bounds<Pixels>, scale: f32, visible: bool) { |
| 39 | let physical = |value: Pixels| (f32::from(value) * scale).round() as i32; |
| 40 | let placement = ( |
| 41 | EmbedBounds { |
| 42 | x: physical(bounds.origin.x), |
| 43 | y: physical(bounds.origin.y), |
| 44 | width: physical(bounds.size.width), |
| 45 | height: physical(bounds.size.height), |
| 46 | }, |
| 47 | visible, |
| 48 | ); |
| 49 | if self.placed.get() == Some(placement) { |
| 50 | return; |
| 51 | } |
| 52 | self.placed.set(Some(placement)); |
| 53 | self.handle.set_bounds(placement.0); |
| 54 | self.handle.set_visible(placement.1); |
| 55 | } |
| 56 | |
| 57 | pub fn close(&self) { |
| 58 | self.handle.close(); |
| 59 | } |
| 60 | } |