Skip to content

Commit

Permalink
feat(unix): move new_gtk functions to *ExtUnix traits (#1070)
Browse files Browse the repository at this point in the history
* feat(unix): move `new_gtk` functions to `*ExtUnix` traits

Signed-off-by: rhysd <lin90162@yahoo.co.jp>

* Update rwh.md

---------

Signed-off-by: rhysd <lin90162@yahoo.co.jp>
  • Loading branch information
rhysd committed Nov 13, 2023
1 parent 4d6f08e commit f41a8f0
Show file tree
Hide file tree
Showing 8 changed files with 78 additions and 57 deletions.
6 changes: 4 additions & 2 deletions .changes/rwh.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
1 change: 1 addition & 0 deletions examples/async_custom_protocol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
};
Expand Down
1 change: 1 addition & 0 deletions examples/custom_protocol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
};
Expand Down
1 change: 1 addition & 0 deletions examples/custom_titlebar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
};
Expand Down
1 change: 1 addition & 0 deletions examples/multiwindow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
};
Expand Down
1 change: 1 addition & 0 deletions examples/simple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
};
Expand Down
1 change: 1 addition & 0 deletions examples/transparent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
};
Expand Down
123 changes: 68 additions & 55 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -51,6 +51,7 @@
//! )))]
//! let builder = {
//! use tao::platform::unix::WindowExtUnix;
//! use wry::WebViewBuilderExtUnix;
//! WebViewBuilder::new_gtk(&window.gtk_window())
//! };
//!
Expand Down Expand Up @@ -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`].
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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
Expand All @@ -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`].
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -1460,16 +1457,25 @@ 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",
target_os = "freebsd",
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;
}

Expand All @@ -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()
}
Expand Down

0 comments on commit f41a8f0

Please sign in to comment.