Repository: tauri-apps/wry (language: rust)

Issue:
`new_gtk` functions should be moved to `*ExtUnix` traits
**Is your feature request related to a problem? Please describe.**

As a convention, methods/functions for *nix platforms are defined under `*ExtUnix` trait. But `new_gtk` functions are not following the convention so users can overlook them. They would expect the functions in `ExtUnix` traits.

- `WebViewBuilder::new_gtk` → `WebViewBuilderExtUnix::new_gtk`
- `WebView::new_gtk` → `WebViewExtUnix::new_gtk`

**Describe the solution you'd like**

- Define `WebViewBuilderExtUnix` trait and move the `WebViewBuilder::new_gtk` function there.
- Move the `WebView::new_gtk` function to `WebViewExtUnix`.

**Describe alternatives you've considered**

Keep current implementation. Since currently `WebViewBuilderExtUnix` doesn't exist, I'm not confident if the trait should be defined only for the function.

**Would you want to assign yourself to implement this feature?**
- [x] Yes
- [ ] No

**Additional context**

Nothing.


Discussion / hints:

Fix patch:
diff --git a/.changes/rwh.md b/.changes/rwh.md
index 054e2af03..09d758265 100644
--- a/.changes/rwh.md
+++ b/.changes/rwh.md
@@ -9,10 +9,12 @@ Refactor new method to take raw window handle instead. Following are APIs got af
   Users should use closure to capture the types they want to use.
   - Position field in `FileDrop` event is now `Position` instead of `PhysicalPosition`. Users need to handle scale factor
   depend on the situation they have.
-  - `WebView::inner_size` is removed.
+  - `Webview::inner_size` is removed.
+  - Add `WebViewBuilderExtUnix` trait to extend `WebViewBuilder` on Unix platforms.
+  - Add `new_gtk` functions to `WebViewBuilderExtUnix` and `WebviewExtUnix`.
   - [raw-window-handle](https://docs.rs/raw-window-handle/latest/raw_window_handle/) crate is re-exported as `wry::raw_window_handle`.
 
 This also means that we removed `tao` as a dependency completely which required some changes to the Android backend:
   - We exposed the `android_setup` function that needs to be called once to setup necessary logic.
-  - Previously the `android_binding!` had internal call to `tao::android_binding` but now that `tao` has been removed,sa
+  - Previously the `android_binding!` had internal call to `tao::android_binding` but now that `tao` has been removed,
     the macro signature has changed and you now need to call `tao::android_binding` yourself, checkout the crate documentation for more information.  
diff --git a/examples/async_custom_protocol.rs b/examples/async_custom_protocol.rs
index 262561a7a..e9c01e815 100644
--- a/examples/async_custom_protocol.rs
+++ b/examples/async_custom_protocol.rs
@@ -35,6 +35,7 @@ fn main() -> wry::Result<()> {
   )))]
   let builder = {
     use tao::platform::unix::WindowExtUnix;
+    use wry::WebViewBuilderExtUnix;
     let vbox = window.default_vbox().unwrap();
     WebViewBuilder::new_gtk(vbox)
   };
diff --git a/examples/custom_protocol.rs b/examples/custom_protocol.rs
index 58e303245..fabad9c96 100644
--- a/examples/custom_protocol.rs
+++ b/examples/custom_protocol.rs
@@ -35,6 +35,7 @@ fn main() -> wry::Result<()> {
   )))]
   let builder = {
     use tao::platform::unix::WindowExtUnix;
+    use wry::WebViewBuilderExtUnix;
     let vbox = window.default_vbox().unwrap();
     WebViewBuilder::new_gtk(vbox)
   };
diff --git a/examples/custom_titlebar.rs b/examples/custom_titlebar.rs
index 67af0dbde..0033dcbe8 100644
--- a/examples/custom_titlebar.rs
+++ b/examples/custom_titlebar.rs
@@ -147,6 +147,7 @@ fn main() -> wry::Result<()> {
   )))]
   let builder = {
     use tao::platform::unix::WindowExtUnix;
+    use wry::WebViewBuilderExtUnix;
     let vbox = window.default_vbox().unwrap();
     WebViewBuilder::new_gtk(vbox)
   };
diff --git a/examples/multiwindow.rs b/examples/multiwindow.rs
index bb146d0e8..51b7c6a93 100644
--- a/examples/multiwindow.rs
+++ b/examples/multiwindow.rs
@@ -105,6 +105,7 @@ fn create_new_window(
   )))]
   let builder = {
     use tao::platform::unix::WindowExtUnix;
+    use wry::WebViewBuilderExtUnix;
     let vbox = window.default_vbox().unwrap();
     WebViewBuilder::new_gtk(vbox)
   };
diff --git a/examples/simple.rs b/examples/simple.rs
index 000b0b752..75a395e4d 100644
--- a/examples/simple.rs
+++ b/examples/simple.rs
@@ -29,6 +29,7 @@ fn main() -> wry::Result<()> {
   )))]
   let builder = {
     use tao::platform::unix::WindowExtUnix;
+    use wry::WebViewBuilderExtUnix;
     let vbox = window.default_vbox().unwrap();
     WebViewBuilder::new_gtk(vbox)
   };
diff --git a/examples/transparent.rs b/examples/transparent.rs
index 1182f314a..869cc0684 100644
--- a/examples/transparent.rs
+++ b/examples/transparent.rs
@@ -46,6 +46,7 @@ fn main() -> wry::Result<()> {
   )))]
   let builder = {
     use tao::platform::unix::WindowExtUnix;
+    use wry::WebViewBuilderExtUnix;
     let vbox = window.default_vbox().unwrap();
     WebViewBuilder::new_gtk(vbox)
   };
diff --git a/src/lib.rs b/src/lib.rs
index 8e372836c..2fa9b5256 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -31,7 +31,7 @@
 //!   .unwrap();
 //! ```
 //!
-//! If you also want to support Wayland too, then we recommend you use [`WebViewBuilder::new_gtk`] on Linux.
+//! If you also want to support Wayland too, then we recommend you use [`WebViewBuilderExtUnix::new_gtk`] on Linux.
 //!
 //! ```no_run,ignore
 //! use wry::WebViewBuilder;
@@ -51,6 +51,7 @@
 //! )))]
 //! let builder = {
 //!   use tao::platform::unix::WindowExtUnix;
+//!   use wry::WebViewBuilderExtUnix;
 //!   WebViewBuilder::new_gtk(&window.gtk_window())
 //! };
 //!
@@ -524,7 +525,7 @@ impl<'a> WebViewBuilder<'a> {
   ///
   /// # Platform-specific:
   ///
-  /// - **Linux**: Only X11 is supported, if you want to support Wayland too, use [`WebViewBuilder::new_gtk`].
+  /// - **Linux**: Only X11 is supported, if you want to support Wayland too, use [`WebViewBuilderExtUnix::new_gtk`].
   ///
   ///   Although this methods only needs an X11 window handle, we use webkit2gtk, so you still need to initialize gtk
   ///   by callling [`gtk::init`] and advance its loop alongside your event loop using [`gtk::main_iteration_do`].
@@ -593,35 +594,6 @@ impl<'a> WebViewBuilder<'a> {
     }
   }
 
-  #[cfg(any(
-    target_os = "linux",
-    target_os = "dragonfly",
-    target_os = "freebsd",
-    target_os = "netbsd",
-    target_os = "openbsd",
-  ))]
-  /// Create the webview from a GTK container widget, such as GTK window.
-  ///
-  /// # Panics:
-  ///
-  /// - Panics if [`gtk::init`] was not called in this thread.
-  pub fn new_gtk<W>(widget: &'a W) -> Self
-  where
-    W: gtk::prelude::IsA<gtk::Container>,
-  {
-    use gdkx11::glib::Cast;
-
-    Self {
-      attrs: WebViewAttributes::default(),
-      window: None,
-      as_child: false,
-      #[allow(clippy::default_constructed_unit_structs)]
-      platform_specific: PlatformSpecificWebViewAttributes::default(),
-      web_context: None,
-      gtk_widget: Some(widget.dynamic_cast_ref().unwrap()),
-    }
-  }
-
   /// Indicates whether horizontal swipe gestures trigger backward and forward page navigation.
   ///
   /// ## Platform-specific:
@@ -1180,6 +1152,50 @@ impl WebViewBuilderExtAndroid for WebViewBuilder<'_> {
   }
 }
 
+#[cfg(any(
+  target_os = "linux",
+  target_os = "dragonfly",
+  target_os = "freebsd",
+  target_os = "netbsd",
+  target_os = "openbsd",
+))]
+pub trait WebViewBuilderExtUnix<'a> {
+  /// Create the webview from a GTK container widget, such as GTK window.
+  ///
+  /// # Panics:
+  ///
+  /// - Panics if [`gtk::init`] was not called in this thread.
+  fn new_gtk<W>(widget: &'a W) -> Self
+  where
+    W: gtk::prelude::IsA<gtk::Container>;
+}
+
+#[cfg(any(
+  target_os = "linux",
+  target_os = "dragonfly",
+  target_os = "freebsd",
+  target_os = "netbsd",
+  target_os = "openbsd",
+))]
+impl<'a> WebViewBuilderExtUnix<'a> for WebViewBuilder<'a> {
+  fn new_gtk<W>(widget: &'a W) -> Self
+  where
+    W: gtk::prelude::IsA<gtk::Container>,
+  {
+    use gdkx11::glib::Cast;
+
+    Self {
+      attrs: WebViewAttributes::default(),
+      window: None,
+      as_child: false,
+      #[allow(clippy::default_constructed_unit_structs)]
+      platform_specific: PlatformSpecificWebViewAttributes::default(),
+      web_context: None,
+      gtk_widget: Some(widget.dynamic_cast_ref().unwrap()),
+    }
+  }
+}
+
 /// The fundamental type to present a [`WebView`].
 ///
 /// [`WebViewBuilder`] / [`WebView`] are the basic building blocks to construct WebView contents and
@@ -1197,7 +1213,7 @@ impl WebView {
   ///
   /// # Platform-specific:
   ///
-  /// - **Linux**: Only X11 is supported, if you want to support Wayland too, use [`WebView::new_gtk`].
+  /// - **Linux**: Only X11 is supported, if you want to support Wayland too, use [`WebViewExtUnix::new_gtk`].
   ///
   ///   Although this methods only needs an X11 window handle, you use webkit2gtk, so you still need to initialize gtk
   ///   by callling [`gtk::init`] and advance its loop alongside your event loop using [`gtk::main_iteration_do`].
@@ -1236,25 +1252,6 @@ impl WebView {
     WebViewBuilder::new_as_child(parent).build()
   }
 
-  #[cfg(any(
-    target_os = "linux",
-    target_os = "dragonfly",
-    target_os = "freebsd",
-    target_os = "netbsd",
-    target_os = "openbsd",
-  ))]
-  /// Create the webview from a GTK container widget, such as GTK window.
-  ///
-  /// # Panics:
-  ///
-  /// - Panics if [`gtk::init`] was not called in this thread.
-  pub fn new_gtk<W>(widget: &W) -> Result<Self>
-  where
-    W: gtk::prelude::IsA<gtk::Container>,
-  {
-    WebViewBuilder::new_gtk(widget).build()
-  }
-
   /// Get the current url of the webview
   pub fn url(&self) -> Url {
     self.webview.url()
@@ -1460,7 +1457,7 @@ impl WebViewExtWindows for WebView {
   }
 }
 
-/// Additional methods on `WebView` that are specific to Unix.
+/// Additional methods on `WebView` that are specific to Linux.
 #[cfg(any(
   target_os = "linux",
   target_os = "dragonfly",
@@ -1468,8 +1465,17 @@ impl WebViewExtWindows for WebView {
   target_os = "netbsd",
   target_os = "openbsd",
 ))]
-pub trait WebViewExtUnix {
-  /// Returns Webkit2gtk WebView handle
+pub trait WebviewExtUnix: Sized {
+  /// Create the webview from a GTK container widget, such as GTK window.
+  ///
+  /// # Panics:
+  ///
+  /// - Panics if [`gtk::init`] was not called in this thread.
+  fn new_gtk<W>(widget: &W) -> Result<Self>
+  where
+    W: gtk::prelude::IsA<gtk::Container>;
+
+  /// Returns Webkit2gtk Webview handle
   fn webview(&self) -> webkit2gtk::WebView;
 }
 
@@ -1480,7 +1486,14 @@ pub trait WebViewExtUnix {
   target_os = "netbsd",
   target_os = "openbsd",
 ))]
-impl WebViewExtUnix for WebView {
+impl WebviewExtUnix for WebView {
+  fn new_gtk<W>(widget: &W) -> Result<Self>
+  where
+    W: gtk::prelude::IsA<gtk::Container>,
+  {
+    WebViewBuilder::new_gtk(widget).build()
+  }
+
   fn webview(&self) -> webkit2gtk::WebView {
     self.webview.webview.clone()
   }


Test patch: