|
@@ -543,6 +543,130 @@ impl<'de, R: Runtime> CommandArg<'de, R> for Window<R> {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+/// The platform webview handle. Accessed with [`Window#method.with_webview`];
|
|
|
+#[cfg(feature = "wry")]
|
|
|
+#[cfg_attr(doc_cfg, doc(cfg(feature = "wry")))]
|
|
|
+pub struct PlatformWebview(tauri_runtime_wry::Webview);
|
|
|
+
|
|
|
+#[cfg(feature = "wry")]
|
|
|
+impl PlatformWebview {
|
|
|
+ /// Returns [`webkit2gtk::WebView`] handle.
|
|
|
+ #[cfg(any(
|
|
|
+ target_os = "linux",
|
|
|
+ target_os = "dragonfly",
|
|
|
+ target_os = "freebsd",
|
|
|
+ target_os = "netbsd",
|
|
|
+ target_os = "openbsd"
|
|
|
+ ))]
|
|
|
+ #[cfg_attr(
|
|
|
+ doc_cfg,
|
|
|
+ doc(cfg(any(
|
|
|
+ target_os = "linux",
|
|
|
+ target_os = "dragonfly",
|
|
|
+ target_os = "freebsd",
|
|
|
+ target_os = "netbsd",
|
|
|
+ target_os = "openbsd"
|
|
|
+ )))
|
|
|
+ )]
|
|
|
+ pub fn inner(&self) -> std::rc::Rc<webkit2gtk::WebView> {
|
|
|
+ self.0.clone()
|
|
|
+ }
|
|
|
+
|
|
|
+ /// Returns the WebView2 controller.
|
|
|
+ #[cfg(windows)]
|
|
|
+ #[cfg_attr(doc_cfg, doc(cfg(windows)))]
|
|
|
+ pub fn controller(
|
|
|
+ &self,
|
|
|
+ ) -> webview2_com::Microsoft::Web::WebView2::Win32::ICoreWebView2Controller {
|
|
|
+ self.0.controller.clone()
|
|
|
+ }
|
|
|
+
|
|
|
+ /// Returns the [WKWebView] handle.
|
|
|
+ ///
|
|
|
+ /// [WKWebView]: https://developer.apple.com/documentation/webkit/wkwebview
|
|
|
+ #[cfg(target_os = "macos")]
|
|
|
+ #[cfg_attr(doc_cfg, doc(cfg(target_os = "macos")))]
|
|
|
+ pub fn inner(&self) -> cocoa::base::id {
|
|
|
+ self.0.webview.clone()
|
|
|
+ }
|
|
|
+
|
|
|
+ /// Returns WKWebView [controller] handle.
|
|
|
+ ///
|
|
|
+ /// [controller]: https://developer.apple.com/documentation/webkit/wkusercontentcontroller
|
|
|
+ #[cfg(target_os = "macos")]
|
|
|
+ #[cfg_attr(doc_cfg, doc(cfg(target_os = "macos")))]
|
|
|
+ pub fn controller(&self) -> cocoa::base::id {
|
|
|
+ self.0.manager.clone()
|
|
|
+ }
|
|
|
+
|
|
|
+ /// Returns [NSWindow] associated with the WKWebView webview.
|
|
|
+ ///
|
|
|
+ /// [NSWindow]: https://developer.apple.com/documentation/appkit/nswindow
|
|
|
+ #[cfg(target_os = "macos")]
|
|
|
+ #[cfg_attr(doc_cfg, doc(cfg(target_os = "macos")))]
|
|
|
+ pub fn ns_window(&self) -> cocoa::base::id {
|
|
|
+ self.0.ns_window.clone()
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+#[cfg(feature = "wry")]
|
|
|
+impl Window<crate::Wry> {
|
|
|
+ /// Executes the closure accessing the platform's webview handle.
|
|
|
+ ///
|
|
|
+ /// The closure is executed in the main thread.
|
|
|
+ ///
|
|
|
+ /// # Examples
|
|
|
+ ///
|
|
|
+ /// ```rust,no_run
|
|
|
+ /// #[cfg(target_os = "macos")]
|
|
|
+ /// #[macro_use]
|
|
|
+ /// extern crate objc;
|
|
|
+ /// use tauri::Manager;
|
|
|
+ ///
|
|
|
+ /// fn main() {
|
|
|
+ /// tauri::Builder::default()
|
|
|
+ /// .setup(|app| {
|
|
|
+ /// let main_window = app.get_window("main").unwrap();
|
|
|
+ /// main_window.with_webview(|webview| {
|
|
|
+ /// #[cfg(target_os = "linux")]
|
|
|
+ /// {
|
|
|
+ /// // see https://docs.rs/webkit2gtk/latest/webkit2gtk/struct.WebView.html
|
|
|
+ /// // and https://docs.rs/webkit2gtk/latest/webkit2gtk/trait.WebViewExt.html
|
|
|
+ /// use webkit2gtk::traits::WebViewExt;
|
|
|
+ /// webview.inner().set_zoom_level(4.);
|
|
|
+ /// }
|
|
|
+ ///
|
|
|
+ /// #[cfg(windows)]
|
|
|
+ /// unsafe {
|
|
|
+ /// // see https://docs.rs/webview2-com/latest/webview2_com/Microsoft/Web/WebView2/Win32/struct.ICoreWebView2Controller.html
|
|
|
+ /// webview.controller().SetZoomFactor(4.).unwrap();
|
|
|
+ /// }
|
|
|
+ ///
|
|
|
+ /// #[cfg(target_os = "macos")]
|
|
|
+ /// unsafe {
|
|
|
+ /// let () = msg_send![webview.inner(), setPageZoom: 4.];
|
|
|
+ /// let () = msg_send![webview.controller(), removeAllUserScripts];
|
|
|
+ /// let bg_color: cocoa::base::id = msg_send![class!(NSColor), colorWithDeviceRed:0.5 green:0.2 blue:0.4 alpha:1.];
|
|
|
+ /// let () = msg_send![webview.ns_window(), setBackgroundColor: bg_color];
|
|
|
+ /// }
|
|
|
+ /// });
|
|
|
+ /// Ok(())
|
|
|
+ /// });
|
|
|
+ /// }
|
|
|
+ /// ```
|
|
|
+ #[cfg_attr(doc_cfg, doc(cfg(eature = "wry")))]
|
|
|
+ pub fn with_webview<F: FnOnce(PlatformWebview) + Send + 'static>(
|
|
|
+ &self,
|
|
|
+ f: F,
|
|
|
+ ) -> crate::Result<()> {
|
|
|
+ self
|
|
|
+ .window
|
|
|
+ .dispatcher
|
|
|
+ .with_webview(|w| f(PlatformWebview(w)))
|
|
|
+ .map_err(Into::into)
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
impl<R: Runtime> Window<R> {
|
|
|
/// Create a new window that is attached to the manager.
|
|
|
pub(crate) fn new(
|
|
@@ -976,15 +1100,6 @@ impl<R: Runtime> Window<R> {
|
|
|
self.window.dispatcher.hwnd().map_err(Into::into)
|
|
|
}
|
|
|
|
|
|
- /// Returns the current window theme.
|
|
|
- ///
|
|
|
- /// ## Platform-specific
|
|
|
- ///
|
|
|
- /// - **macOS / Linux**: Not implemented, always return [`Theme::Light`].
|
|
|
- pub fn theme(&self) -> crate::Result<Theme> {
|
|
|
- self.window.dispatcher.theme().map_err(Into::into)
|
|
|
- }
|
|
|
-
|
|
|
/// Returns the `ApplicatonWindow` from gtk crate that is used by this window.
|
|
|
///
|
|
|
/// Note that this can only be used on the main thread.
|
|
@@ -999,6 +1114,15 @@ impl<R: Runtime> Window<R> {
|
|
|
self.window.dispatcher.gtk_window().map_err(Into::into)
|
|
|
}
|
|
|
|
|
|
+ /// Returns the current window theme.
|
|
|
+ ///
|
|
|
+ /// ## Platform-specific
|
|
|
+ ///
|
|
|
+ /// - **macOS / Linux**: Not implemented, always return [`Theme::Light`].
|
|
|
+ pub fn theme(&self) -> crate::Result<Theme> {
|
|
|
+ self.window.dispatcher.theme().map_err(Into::into)
|
|
|
+ }
|
|
|
+
|
|
|
// Setters
|
|
|
|
|
|
/// Centers the window.
|