winio-ui-app-kit 0.6.0

AppKit backend for winio.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
use std::{
    cell::{Cell, RefCell},
    ptr::NonNull,
    rc::Rc,
};

use cookie::Cookie;
use futures_util::FutureExt;
use inherit_methods_macro::inherit_methods;
use objc2::{
    AnyThread, DeclaredClass, MainThreadOnly, Message, define_class, msg_send,
    rc::{Allocated, Retained},
    runtime::{AnyObject, Bool, ProtocolObject},
};
use objc2_app_kit::{NSAlert, NSAlertFirstButtonReturn, NSTextField, NSWindow};
use objc2_foundation::{
    MainThreadMarker, NSArray, NSError, NSHTTPCookie, NSJSONSerialization, NSJSONWritingOptions,
    NSObject, NSObjectProtocol, NSPoint, NSRect, NSSize, NSString, NSURL, NSURLRequest,
    NSUTF8StringEncoding, ns_string,
};
use objc2_web_kit::{
    WKFrameInfo, WKNavigation, WKNavigationDelegate, WKUIDelegate, WKWebView,
    WKWebViewConfiguration,
};
use winio_callback::Callback;
use winio_handle::AsContainer;
use winio_primitive::{Point, Size};
use winio_ui_apple_common::{cookie_from_ns, cookie_to_ns};

use crate::{Error, GlobalRuntime, Result, Widget, catch, from_nsstring};

#[derive(Debug)]
pub struct WebView {
    handle: Widget,
    view: Retained<WKWebView>,
    config: Retained<WKWebViewConfiguration>,
    delegate: Retained<WebViewDelegate>,
}

#[inherit_methods(from = "self.handle")]
impl WebView {
    pub async fn new(parent: impl AsContainer) -> Result<Self> {
        let parent = parent.as_container();
        let parent_view = parent.as_app_kit();
        let mtm = parent_view.mtm();

        catch(|| unsafe {
            let frame = parent_view.frame();
            let config = WKWebViewConfiguration::new(mtm);
            let view =
                WKWebView::initWithFrame_configuration(WKWebView::alloc(mtm), frame, &config);
            let handle = Widget::from_nsview(parent, Retained::cast_unchecked(view.clone()))?;

            let delegate = WebViewDelegate::new(mtm);
            delegate.ivars().parent_window.replace(view.window());

            let del_obj = ProtocolObject::from_ref(&*delegate);
            view.setNavigationDelegate(Some(del_obj));
            let del_obj = ProtocolObject::from_ref(&*delegate);
            view.setUIDelegate(Some(del_obj));

            Ok(Self {
                handle,
                view,
                config,
                delegate,
            })
        })
        .flatten()
    }

    pub fn is_visible(&self) -> Result<bool>;

    pub fn set_visible(&mut self, v: bool) -> Result<()>;

    pub fn is_enabled(&self) -> Result<bool> {
        Ok(true)
    }

    pub fn set_enabled(&mut self, _: bool) -> Result<()> {
        Ok(())
    }

    pub fn loc(&self) -> Result<Point>;

    pub fn set_loc(&mut self, p: Point) -> Result<()>;

    pub fn size(&self) -> Result<Size>;

    pub fn set_size(&mut self, v: Size) -> Result<()>;

    pub fn source(&self) -> Result<String> {
        catch(|| unsafe {
            self.view
                .URL()
                .and_then(|url| url.absoluteString())
                .map(|s| from_nsstring(&s))
                .unwrap_or_default()
        })
    }

    pub fn set_source(&mut self, s: impl AsRef<str>) -> Result<()> {
        let s = s.as_ref();
        if s.is_empty() {
            return self.set_html("");
        }

        catch(|| {
            let url = NSURL::URLWithString(&NSString::from_str(s)).ok_or(Error::NullPointer)?;
            let req = NSURLRequest::requestWithURL(&url);
            unsafe { self.view.loadRequest(&req) };
            Ok(())
        })
        .flatten()
    }

    pub fn set_html(&mut self, html: impl AsRef<str>) -> Result<()> {
        catch(|| unsafe {
            self.view
                .loadHTMLString_baseURL(&NSString::from_str(html.as_ref()), None);
        })
    }

    pub fn can_go_forward(&self) -> Result<bool> {
        catch(|| unsafe { self.view.canGoForward() })
    }

    pub fn go_forward(&mut self) -> Result<()> {
        catch(|| unsafe {
            self.view.goForward();
        })
    }

    pub fn can_go_back(&self) -> Result<bool> {
        catch(|| unsafe { self.view.canGoBack() })
    }

    pub fn go_back(&mut self) -> Result<()> {
        catch(|| unsafe {
            self.view.goBack();
        })
    }

    pub fn reload(&mut self) -> Result<()> {
        catch(|| unsafe {
            self.view.reload();
        })
    }

    pub fn stop(&mut self) -> Result<()> {
        catch(|| unsafe {
            self.view.stopLoading();
        })
    }

    pub async fn wait_navigating(&self) {
        self.delegate.ivars().navigating.wait().await
    }

    pub async fn wait_navigated(&self) {
        self.delegate.ivars().navigated.wait().await
    }

    pub async fn cookies(&self) -> Result<Vec<Cookie<'static>>> {
        let rx = catch(|| {
            let (tx, rx) = local_sync::oneshot::channel();
            let tx = Rc::new(Cell::new(Some(tx)));
            let handler = move |cookies: NonNull<NSArray<NSHTTPCookie>>| {
                if let Some(tx) = tx.take() {
                    tx.send(unsafe { cookies.as_ref() }.retain()).ok();
                }
            };
            let block = block2::StackBlock::new(handler);
            unsafe {
                self.config
                    .websiteDataStore()
                    .httpCookieStore()
                    .getAllCookies(&block);
            }
            rx
        })?;
        let array = rx.await?;
        catch(|| {
            let mut cookies = vec![];
            for cookie in array {
                cookies.push(cookie_from_ns(&cookie)?);
            }
            Ok(cookies)
        })
        .flatten()
    }

    pub async fn set_cookie(&mut self, c: &Cookie<'_>) -> Result<()> {
        let rx = catch(|| {
            let (tx, rx) = local_sync::oneshot::channel();
            let tx = Rc::new(Cell::new(Some(tx)));
            let handler = move || {
                if let Some(tx) = tx.take() {
                    tx.send(()).ok();
                }
            };
            let block = block2::StackBlock::new(handler);
            let ns_cookie = cookie_to_ns(c)?;
            unsafe {
                self.config
                    .websiteDataStore()
                    .httpCookieStore()
                    .setCookie_completionHandler(&ns_cookie, Some(&block));
            }
            Ok(rx)
        })
        .flatten()?;
        rx.await?;
        Ok(())
    }

    pub async fn delete_cookie(&mut self, c: &Cookie<'_>) -> Result<()> {
        let rx = catch(|| {
            let (tx, rx) = local_sync::oneshot::channel();
            let tx = Rc::new(Cell::new(Some(tx)));
            let handler = move || {
                if let Some(tx) = tx.take() {
                    tx.send(()).ok();
                }
            };
            let block = block2::StackBlock::new(handler);
            let ns_cookie = cookie_to_ns(c)?;
            unsafe {
                self.config
                    .websiteDataStore()
                    .httpCookieStore()
                    .deleteCookie_completionHandler(&ns_cookie, Some(&block));
            }
            Ok(rx)
        })
        .flatten()?;
        rx.await?;
        Ok(())
    }

    pub fn run_javascript(
        &mut self,
        js: impl AsRef<str>,
    ) -> Result<impl Future<Output = Result<String>> + 'static> {
        let rx = catch(|| unsafe {
            let (tx, rx) = local_sync::oneshot::channel();
            let tx = Rc::new(Cell::new(Some(tx)));
            let handler = move |result: *mut AnyObject, error: *mut NSError| {
                let res = if error.is_null() {
                    Ok(if result.is_null() {
                        None
                    } else {
                        Some((&*result).retain())
                    })
                } else {
                    Err(Error::NS(Some((&*error).retain())))
                };
                if let Some(tx) = tx.take() {
                    tx.send(res).ok();
                }
            };
            let block = block2::StackBlock::new(handler);
            self.view.evaluateJavaScript_completionHandler(
                &NSString::from_str(js.as_ref()),
                Some(&block),
            );
            Ok(rx)
        })
        .flatten()?;
        Ok(rx.map(|res| {
            let Some(result) = res?? else {
                return Ok(String::new());
            };
            catch(|| {
                let data = unsafe {
                    NSJSONSerialization::dataWithJSONObject_options_error(
                        &result,
                        NSJSONWritingOptions(0),
                    )?
                };
                let data =
                    NSString::initWithData_encoding(NSString::alloc(), &data, NSUTF8StringEncoding);
                data.map(|s| from_nsstring(&s)).ok_or(Error::NullPointer)
            })
            .flatten()
        }))
    }
}

winio_handle::impl_as_widget!(WebView, handle);

#[derive(Debug, Default)]
struct WebViewDelegateIvars {
    navigating: Callback,
    navigated: Callback,
    parent_window: RefCell<Option<Retained<NSWindow>>>,
}

define_class! {
    #[unsafe(super(NSObject))]
    #[name = "WinioWebViewDelegate"]
    #[ivars = WebViewDelegateIvars]
    #[thread_kind = MainThreadOnly]
    #[derive(Debug)]
    struct WebViewDelegate;

    #[allow(non_snake_case)]
    impl WebViewDelegate {
        #[unsafe(method_id(init))]
        fn init(this: Allocated<Self>) -> Option<Retained<Self>> {
            let this = this.set_ivars(WebViewDelegateIvars::default());
            unsafe { msg_send![super(this), init] }
        }
    }

    unsafe impl NSObjectProtocol for WebViewDelegate {}

    #[allow(non_snake_case)]
    unsafe impl WKNavigationDelegate for WebViewDelegate {
        #[unsafe(method(webView:didCommitNavigation:))]
        unsafe fn webView_didCommitNavigation(
            &self,
            _web_view: &WKWebView,
            _navigation: Option<&WKNavigation>,
        ) {
            self.ivars().navigating.signal::<GlobalRuntime>(());
        }

        #[unsafe(method(webView:didFinishNavigation:))]
        unsafe fn webView_didFinishNavigation(
            &self,
            _web_view: &WKWebView,
            _navigation: Option<&WKNavigation>,
        ) {
            self.ivars().navigated.signal::<GlobalRuntime>(());
        }
    }

    #[allow(non_snake_case)]
    unsafe impl WKUIDelegate for WebViewDelegate {
        #[unsafe(method(webView:runJavaScriptAlertPanelWithMessage:initiatedByFrame:completionHandler:))]
        unsafe fn webView_runJavaScriptAlertPanelWithMessage_initiatedByFrame_completionHandler(
            &self,
            web_view: &WKWebView,
            message: &NSString,
            frame: &WKFrameInfo,
            completion_handler: &block2::DynBlock<dyn Fn()>,
        ) {
            let alert = NSAlert::new(self.mtm());
            alert.setMessageText(message);
            alert.addButtonWithTitle(ns_string!("OK"));
            if let Some(window) = self.ivars().parent_window.borrow().as_ref() {
                let handler = completion_handler.copy();
                let block = block2::RcBlock::new(move |_| {
                    handler.call(());
                });
                alert.beginSheetModalForWindow_completionHandler(window, Some(&block));
            } else {
                alert.runModal();
                completion_handler.call(());
            }
        }

        #[unsafe(method(webView:runJavaScriptConfirmPanelWithMessage:initiatedByFrame:completionHandler:))]
        unsafe fn webView_runJavaScriptConfirmPanelWithMessage_initiatedByFrame_completionHandler(
            &self,
            web_view: &WKWebView,
            message: &NSString,
            frame: &WKFrameInfo,
            completion_handler: &block2::DynBlock<dyn Fn(Bool)>,
        ) {
            let alert = NSAlert::new(self.mtm());
            alert.setMessageText(message);
            alert.addButtonWithTitle(ns_string!("OK"));
            alert.addButtonWithTitle(ns_string!("Cancel"));
            if let Some(window) = self.ivars().parent_window.borrow().as_ref() {
                let handler = completion_handler.copy();
                let block = block2::RcBlock::new(move |return_code| {
                    handler.call((Bool::new(return_code == NSAlertFirstButtonReturn),));
                });
                alert.beginSheetModalForWindow_completionHandler(window, Some(&block));
            } else {
                let return_code = alert.runModal();
                completion_handler.call((Bool::new(return_code == NSAlertFirstButtonReturn),));
            }
        }

        #[unsafe(method(webView:runJavaScriptTextInputPanelWithPrompt:defaultText:initiatedByFrame:completionHandler:))]
        unsafe fn webView_runJavaScriptTextInputPanelWithPrompt_defaultText_initiatedByFrame_completionHandler(
            &self,
            web_view: &WKWebView,
            prompt: &NSString,
            default_text: Option<&NSString>,
            frame: &WKFrameInfo,
            completion_handler: &block2::DynBlock<dyn Fn(*mut NSString)>,
        ) {
            let alert = NSAlert::new(self.mtm());
            alert.setMessageText(prompt);
            alert.addButtonWithTitle(ns_string!("OK"));
            alert.addButtonWithTitle(ns_string!("Cancel"));

            let input = NSTextField::initWithFrame(NSTextField::alloc(self.mtm()), NSRect::new(NSPoint::ZERO, NSSize::new(200.0, 24.0)));
            if let Some(default_text) = default_text {
                input.setStringValue(default_text);
            }
            alert.setAccessoryView(Some(&input));

            if let Some(window) = self.ivars().parent_window.borrow().as_ref() {
                let handler = completion_handler.copy();
                let block = block2::RcBlock::new(move |return_code| {
                    let result = if return_code == NSAlertFirstButtonReturn {
                        Retained::into_raw(input.stringValue())
                    } else {
                        std::ptr::null_mut()
                    };
                    handler.call((result,));
                });
                alert.beginSheetModalForWindow_completionHandler(window, Some(&block));
            } else {
                let return_code = alert.runModal();
                let result = if return_code == NSAlertFirstButtonReturn {
                    Retained::into_raw(input.stringValue())
                } else {
                    std::ptr::null_mut()
                };
                completion_handler.call((result,));
            }
        }
    }
}

impl WebViewDelegate {
    pub fn new(mtm: MainThreadMarker) -> Retained<Self> {
        unsafe { msg_send![mtm.alloc::<Self>(), init] }
    }
}