@@ -543,6 +543,130 @@ impl<'de, R: Runtime> CommandArg<'de, R> for Window<R> {
543
543
}
544
544
}
545
545
546
+ /// The platform webview handle. Accessed with [`Window#method.with_webview`];
547
+ #[ cfg( feature = "wry" ) ]
548
+ #[ cfg_attr( doc_cfg, doc( cfg( feature = "wry" ) ) ) ]
549
+ pub struct PlatformWebview ( tauri_runtime_wry:: Webview ) ;
550
+
551
+ #[ cfg( feature = "wry" ) ]
552
+ impl PlatformWebview {
553
+ /// Returns [`webkit2gtk::WebView`] handle.
554
+ #[ cfg( any(
555
+ target_os = "linux" ,
556
+ target_os = "dragonfly" ,
557
+ target_os = "freebsd" ,
558
+ target_os = "netbsd" ,
559
+ target_os = "openbsd"
560
+ ) ) ]
561
+ #[ cfg_attr(
562
+ doc_cfg,
563
+ doc( cfg( any(
564
+ target_os = "linux" ,
565
+ target_os = "dragonfly" ,
566
+ target_os = "freebsd" ,
567
+ target_os = "netbsd" ,
568
+ target_os = "openbsd"
569
+ ) ) )
570
+ ) ]
571
+ pub fn inner ( & self ) -> std:: rc:: Rc < webkit2gtk:: WebView > {
572
+ self . 0 . clone ( )
573
+ }
574
+
575
+ /// Returns the WebView2 controller.
576
+ #[ cfg( windows) ]
577
+ #[ cfg_attr( doc_cfg, doc( cfg( windows) ) ) ]
578
+ pub fn controller (
579
+ & self ,
580
+ ) -> webview2_com:: Microsoft :: Web :: WebView2 :: Win32 :: ICoreWebView2Controller {
581
+ self . 0 . controller . clone ( )
582
+ }
583
+
584
+ /// Returns the [WKWebView] handle.
585
+ ///
586
+ /// [WKWebView]: https://developer.apple.com/documentation/webkit/wkwebview
587
+ #[ cfg( target_os = "macos" ) ]
588
+ #[ cfg_attr( doc_cfg, doc( cfg( target_os = "macos" ) ) ) ]
589
+ pub fn inner ( & self ) -> cocoa:: base:: id {
590
+ self . 0 . webview . clone ( )
591
+ }
592
+
593
+ /// Returns WKWebView [controller] handle.
594
+ ///
595
+ /// [controller]: https://developer.apple.com/documentation/webkit/wkusercontentcontroller
596
+ #[ cfg( target_os = "macos" ) ]
597
+ #[ cfg_attr( doc_cfg, doc( cfg( target_os = "macos" ) ) ) ]
598
+ pub fn controller ( & self ) -> cocoa:: base:: id {
599
+ self . 0 . manager . clone ( )
600
+ }
601
+
602
+ /// Returns [NSWindow] associated with the WKWebView webview.
603
+ ///
604
+ /// [NSWindow]: https://developer.apple.com/documentation/appkit/nswindow
605
+ #[ cfg( target_os = "macos" ) ]
606
+ #[ cfg_attr( doc_cfg, doc( cfg( target_os = "macos" ) ) ) ]
607
+ pub fn ns_window ( & self ) -> cocoa:: base:: id {
608
+ self . 0 . ns_window . clone ( )
609
+ }
610
+ }
611
+
612
+ #[ cfg( feature = "wry" ) ]
613
+ impl Window < crate :: Wry > {
614
+ /// Executes the closure accessing the platform's webview handle.
615
+ ///
616
+ /// The closure is executed in the main thread.
617
+ ///
618
+ /// # Examples
619
+ ///
620
+ /// ```rust,no_run
621
+ /// #[cfg(target_os = "macos")]
622
+ /// #[macro_use]
623
+ /// extern crate objc;
624
+ /// use tauri::Manager;
625
+ ///
626
+ /// fn main() {
627
+ /// tauri::Builder::default()
628
+ /// .setup(|app| {
629
+ /// let main_window = app.get_window("main").unwrap();
630
+ /// main_window.with_webview(|webview| {
631
+ /// #[cfg(target_os = "linux")]
632
+ /// {
633
+ /// // see https://docs.rs/webkit2gtk/latest/webkit2gtk/struct.WebView.html
634
+ /// // and https://docs.rs/webkit2gtk/latest/webkit2gtk/trait.WebViewExt.html
635
+ /// use webkit2gtk::traits::WebViewExt;
636
+ /// webview.inner().set_zoom_level(4.);
637
+ /// }
638
+ ///
639
+ /// #[cfg(windows)]
640
+ /// unsafe {
641
+ /// // see https://docs.rs/webview2-com/latest/webview2_com/Microsoft/Web/WebView2/Win32/struct.ICoreWebView2Controller.html
642
+ /// webview.controller().SetZoomFactor(4.).unwrap();
643
+ /// }
644
+ ///
645
+ /// #[cfg(target_os = "macos")]
646
+ /// unsafe {
647
+ /// let () = msg_send![webview.inner(), setPageZoom: 4.];
648
+ /// let () = msg_send![webview.controller(), removeAllUserScripts];
649
+ /// let bg_color: cocoa::base::id = msg_send![class!(NSColor), colorWithDeviceRed:0.5 green:0.2 blue:0.4 alpha:1.];
650
+ /// let () = msg_send![webview.ns_window(), setBackgroundColor: bg_color];
651
+ /// }
652
+ /// });
653
+ /// Ok(())
654
+ /// });
655
+ /// }
656
+ /// ```
657
+ #[ cfg_attr( doc_cfg, doc( cfg( eature = "wry" ) ) ) ]
658
+ pub fn with_webview < F : FnOnce ( PlatformWebview ) + Send + ' static > (
659
+ & self ,
660
+ f : F ,
661
+ ) -> crate :: Result < ( ) > {
662
+ self
663
+ . window
664
+ . dispatcher
665
+ . with_webview ( |w| f ( PlatformWebview ( w) ) )
666
+ . map_err ( Into :: into)
667
+ }
668
+ }
669
+
546
670
impl < R : Runtime > Window < R > {
547
671
/// Create a new window that is attached to the manager.
548
672
pub ( crate ) fn new (
@@ -976,15 +1100,6 @@ impl<R: Runtime> Window<R> {
976
1100
self . window . dispatcher . hwnd ( ) . map_err ( Into :: into)
977
1101
}
978
1102
979
- /// Returns the current window theme.
980
- ///
981
- /// ## Platform-specific
982
- ///
983
- /// - **macOS / Linux**: Not implemented, always return [`Theme::Light`].
984
- pub fn theme ( & self ) -> crate :: Result < Theme > {
985
- self . window . dispatcher . theme ( ) . map_err ( Into :: into)
986
- }
987
-
988
1103
/// Returns the `ApplicatonWindow` from gtk crate that is used by this window.
989
1104
///
990
1105
/// Note that this can only be used on the main thread.
@@ -999,6 +1114,15 @@ impl<R: Runtime> Window<R> {
999
1114
self . window . dispatcher . gtk_window ( ) . map_err ( Into :: into)
1000
1115
}
1001
1116
1117
+ /// Returns the current window theme.
1118
+ ///
1119
+ /// ## Platform-specific
1120
+ ///
1121
+ /// - **macOS / Linux**: Not implemented, always return [`Theme::Light`].
1122
+ pub fn theme ( & self ) -> crate :: Result < Theme > {
1123
+ self . window . dispatcher . theme ( ) . map_err ( Into :: into)
1124
+ }
1125
+
1002
1126
// Setters
1003
1127
1004
1128
/// Centers the window.
0 commit comments