| 1 | use std::rc::Rc; |
| 2 | |
| 3 | use tokio::sync::mpsc::UnboundedSender; |
| 4 | use webview2_com::Microsoft::Web::WebView2::Win32::*; |
| 5 | use webview2_com::{ |
| 6 | CreateCoreWebView2ControllerCompletedHandler, NewWindowRequestedEventHandler, |
| 7 | SourceChangedEventHandler, take_pwstr, |
| 8 | }; |
| 9 | use windows::Win32::Foundation::{HWND, RECT}; |
| 10 | use windows::core::{HSTRING, Interface, PWSTR}; |
| 11 | |
| 12 | use crate::embed::{EmbedBounds, EmbedCommand, EmbedEvent, EmbedSpec}; |
| 13 | use crate::host::{Shared, shared as running_host}; |
| 14 | |
| 15 | struct EmbedView { |
| 16 | controller: ICoreWebView2Controller, |
| 17 | webview: ICoreWebView2, |
| 18 | } |
| 19 | |
| 20 | pub(crate) struct Embed { |
| 21 | view: Option<EmbedView>, |
| 22 | bounds: Option<EmbedBounds>, |
| 23 | visible: bool, |
| 24 | events: UnboundedSender<EmbedEvent>, |
| 25 | } |
| 26 | |
| 27 | pub(crate) fn open( |
| 28 | shared: &Rc<Shared>, |
| 29 | id: u64, |
| 30 | spec: EmbedSpec, |
| 31 | events: UnboundedSender<EmbedEvent>, |
| 32 | ) { |
| 33 | let Some(environment) = shared.environment.borrow().clone() else { |
| 34 | let _ = events.send(EmbedEvent::Closed); |
| 35 | return; |
| 36 | }; |
| 37 | shared.embeds.borrow_mut().insert( |
| 38 | id, |
| 39 | Embed { |
| 40 | view: None, |
| 41 | bounds: None, |
| 42 | visible: false, |
| 43 | events, |
| 44 | }, |
| 45 | ); |
| 46 | let parent = HWND(spec.parent as *mut _); |
| 47 | let handler = |
| 48 | CreateCoreWebView2ControllerCompletedHandler::create(Box::new(move |code, controller| { |
| 49 | let Some(shared) = running_host() else { |
| 50 | return Ok(()); |
| 51 | }; |
| 52 | let attached = code |
| 53 | .and_then(|()| { |
| 54 | controller.ok_or_else(|| windows::core::Error::from(windows::core::HRESULT(-1))) |
| 55 | }) |
| 56 | .and_then(|controller| attach(&shared, id, controller, &spec)); |
| 57 | if attached.is_err() { |
| 58 | close(&shared, id); |
| 59 | } |
| 60 | Ok(()) |
| 61 | })); |
| 62 | if unsafe { environment.CreateCoreWebView2Controller(parent, &handler) }.is_err() { |
| 63 | close(shared, id); |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | pub(crate) fn run(shared: &Rc<Shared>, id: u64, command: EmbedCommand) { |
| 68 | match command { |
| 69 | EmbedCommand::Bounds(bounds) => { |
| 70 | let controller = update(shared, id, |embed| embed.bounds = Some(bounds)); |
| 71 | if let Some(controller) = controller { |
| 72 | apply_bounds(&controller, bounds); |
| 73 | } |
| 74 | } |
| 75 | EmbedCommand::Visible(visible) => { |
| 76 | let controller = update(shared, id, |embed| embed.visible = visible); |
| 77 | if let Some(controller) = controller { |
| 78 | unsafe { |
| 79 | let _ = controller.SetIsVisible(visible); |
| 80 | } |
| 81 | } |
| 82 | } |
| 83 | EmbedCommand::Navigate(url) => { |
| 84 | let webview = shared |
| 85 | .embeds |
| 86 | .borrow() |
| 87 | .get(&id) |
| 88 | .and_then(|embed| embed.view.as_ref().map(|view| view.webview.clone())); |
| 89 | if let Some(webview) = webview { |
| 90 | unsafe { |
| 91 | let _ = webview.Navigate(&HSTRING::from(url)); |
| 92 | } |
| 93 | } |
| 94 | } |
| 95 | EmbedCommand::Close => close(shared, id), |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | pub(crate) fn close(shared: &Rc<Shared>, id: u64) { |
| 100 | let Some(embed) = shared.embeds.borrow_mut().remove(&id) else { |
| 101 | return; |
| 102 | }; |
| 103 | if let Some(view) = &embed.view { |
| 104 | unsafe { |
| 105 | let _ = view.controller.Close(); |
| 106 | } |
| 107 | } |
| 108 | let _ = embed.events.send(EmbedEvent::Closed); |
| 109 | } |
| 110 | |
| 111 | pub(crate) fn close_all(shared: &Rc<Shared>) { |
| 112 | let ids: Vec<u64> = shared.embeds.borrow().keys().copied().collect(); |
| 113 | for id in ids { |
| 114 | close(shared, id); |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | fn update( |
| 119 | shared: &Shared, |
| 120 | id: u64, |
| 121 | change: impl FnOnce(&mut Embed), |
| 122 | ) -> Option<ICoreWebView2Controller> { |
| 123 | let mut embeds = shared.embeds.borrow_mut(); |
| 124 | let embed = embeds.get_mut(&id)?; |
| 125 | change(embed); |
| 126 | embed.view.as_ref().map(|view| view.controller.clone()) |
| 127 | } |
| 128 | |
| 129 | fn emit(id: u64, event: EmbedEvent) { |
| 130 | if let Some(shared) = running_host() |
| 131 | && let Some(embed) = shared.embeds.borrow().get(&id) |
| 132 | { |
| 133 | let _ = embed.events.send(event); |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | fn apply_bounds(controller: &ICoreWebView2Controller, bounds: EmbedBounds) { |
| 138 | unsafe { |
| 139 | let _ = controller.SetBounds(RECT { |
| 140 | left: bounds.x, |
| 141 | top: bounds.y, |
| 142 | right: bounds.x + bounds.width, |
| 143 | bottom: bounds.y + bounds.height, |
| 144 | }); |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | fn attach( |
| 149 | shared: &Rc<Shared>, |
| 150 | id: u64, |
| 151 | controller: ICoreWebView2Controller, |
| 152 | spec: &EmbedSpec, |
| 153 | ) -> windows::core::Result<()> { |
| 154 | let Some((bounds, visible)) = shared |
| 155 | .embeds |
| 156 | .borrow() |
| 157 | .get(&id) |
| 158 | .map(|embed| (embed.bounds, embed.visible)) |
| 159 | else { |
| 160 | unsafe { controller.Close() }?; |
| 161 | return Ok(()); |
| 162 | }; |
| 163 | let webview = unsafe { controller.CoreWebView2() }?; |
| 164 | unsafe { |
| 165 | controller |
| 166 | .cast::<ICoreWebView2Controller2>()? |
| 167 | .SetDefaultBackgroundColor(COREWEBVIEW2_COLOR { |
| 168 | A: 255, |
| 169 | R: (spec.background >> 16) as u8, |
| 170 | G: (spec.background >> 8) as u8, |
| 171 | B: spec.background as u8, |
| 172 | })?; |
| 173 | webview.Settings()?.SetIsStatusBarEnabled(false)?; |
| 174 | } |
| 175 | if let Some(bounds) = bounds { |
| 176 | apply_bounds(&controller, bounds); |
| 177 | } |
| 178 | watch_source(&webview, id)?; |
| 179 | forward_new_windows(&webview, id)?; |
| 180 | unsafe { |
| 181 | controller.SetIsVisible(visible)?; |
| 182 | webview.Navigate(&HSTRING::from(spec.url.as_str()))?; |
| 183 | } |
| 184 | if let Some(embed) = shared.embeds.borrow_mut().get_mut(&id) { |
| 185 | embed.view = Some(EmbedView { |
| 186 | controller, |
| 187 | webview, |
| 188 | }); |
| 189 | } |
| 190 | Ok(()) |
| 191 | } |
| 192 | |
| 193 | fn watch_source(webview: &ICoreWebView2, id: u64) -> windows::core::Result<()> { |
| 194 | let handler = SourceChangedEventHandler::create(Box::new(move |sender, _| { |
| 195 | if let Some(sender) = sender { |
| 196 | let mut source = PWSTR::null(); |
| 197 | if unsafe { sender.Source(&mut source) }.is_ok() { |
| 198 | emit(id, EmbedEvent::Navigated(take_pwstr(source))); |
| 199 | } |
| 200 | } |
| 201 | Ok(()) |
| 202 | })); |
| 203 | let mut token = 0; |
| 204 | unsafe { webview.add_SourceChanged(&handler, &mut token) } |
| 205 | } |
| 206 | |
| 207 | fn forward_new_windows(webview: &ICoreWebView2, id: u64) -> windows::core::Result<()> { |
| 208 | let handler = NewWindowRequestedEventHandler::create(Box::new(move |_, arguments| { |
| 209 | let Some(arguments) = arguments else { |
| 210 | return Ok(()); |
| 211 | }; |
| 212 | let mut target = PWSTR::null(); |
| 213 | unsafe { |
| 214 | arguments.Uri(&mut target)?; |
| 215 | arguments.SetHandled(true)?; |
| 216 | } |
| 217 | emit(id, EmbedEvent::NewWindow(take_pwstr(target))); |
| 218 | Ok(()) |
| 219 | })); |
| 220 | let mut token = 0; |
| 221 | unsafe { webview.add_NewWindowRequested(&handler, &mut token) } |
| 222 | } |