| 1 | use std::sync::{Arc, mpsc}; |
| 2 | |
| 3 | use crate::transport::UiRequest; |
| 4 | |
| 5 | #[derive(Debug, Clone)] |
| 6 | pub struct EmbedSpec { |
| 7 | pub parent: isize, |
| 8 | pub url: String, |
| 9 | pub background: u32, |
| 10 | } |
| 11 | |
| 12 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 13 | pub struct EmbedBounds { |
| 14 | pub x: i32, |
| 15 | pub y: i32, |
| 16 | pub width: i32, |
| 17 | pub height: i32, |
| 18 | } |
| 19 | |
| 20 | #[derive(Debug, Clone, PartialEq, Eq)] |
| 21 | pub enum EmbedEvent { |
| 22 | Navigated(String), |
| 23 | NewWindow(String), |
| 24 | Closed, |
| 25 | } |
| 26 | |
| 27 | #[derive(Debug)] |
| 28 | pub(crate) enum EmbedCommand { |
| 29 | Bounds(EmbedBounds), |
| 30 | Visible(bool), |
| 31 | Navigate(String), |
| 32 | Close, |
| 33 | } |
| 34 | |
| 35 | #[derive(Clone)] |
| 36 | pub struct EmbedHandle { |
| 37 | pub(crate) id: u64, |
| 38 | pub(crate) requests: mpsc::Sender<UiRequest>, |
| 39 | pub(crate) waker: Arc<dyn Fn() + Send + Sync>, |
| 40 | } |
| 41 | |
| 42 | impl EmbedHandle { |
| 43 | pub fn set_bounds(&self, bounds: EmbedBounds) { |
| 44 | self.send(EmbedCommand::Bounds(bounds)); |
| 45 | } |
| 46 | |
| 47 | pub fn set_visible(&self, visible: bool) { |
| 48 | self.send(EmbedCommand::Visible(visible)); |
| 49 | } |
| 50 | |
| 51 | pub fn navigate(&self, url: String) { |
| 52 | self.send(EmbedCommand::Navigate(url)); |
| 53 | } |
| 54 | |
| 55 | pub fn close(&self) { |
| 56 | self.send(EmbedCommand::Close); |
| 57 | } |
| 58 | |
| 59 | fn send(&self, command: EmbedCommand) { |
| 60 | let request = UiRequest::Embed { |
| 61 | id: self.id, |
| 62 | command, |
| 63 | }; |
| 64 | if self.requests.send(request).is_ok() { |
| 65 | (self.waker)(); |
| 66 | } |
| 67 | } |
| 68 | } |