From 58697a6084c4836f103624c9c062f89528dc3f5d Mon Sep 17 00:00:00 2001 From: Wu Yu Wei Date: Tue, 14 Nov 2023 16:02:36 +0900 Subject: [PATCH 1/3] Add cfg_aliases for easier feature configuration --- .changes/cfg_alias.md | 5 +++ Cargo.toml | 22 +++++++------ build.rs | 12 ++++++++ src/error.rs | 40 +++--------------------- src/lib.rs | 72 ++++++------------------------------------- src/web_context.rs | 30 +++--------------- 6 files changed, 48 insertions(+), 133 deletions(-) create mode 100644 .changes/cfg_alias.md diff --git a/.changes/cfg_alias.md b/.changes/cfg_alias.md new file mode 100644 index 000000000..c3e729839 --- /dev/null +++ b/.changes/cfg_alias.md @@ -0,0 +1,5 @@ +--- +"wry": patch +--- + +- Add cfg_aliases for easier feature configuration. diff --git a/Cargo.toml b/Cargo.toml index c9db01a10..bae729e52 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -24,15 +24,19 @@ rustc-args = ["--cfg", "docsrs"] rustdoc-args = ["--cfg", "docsrs"] [features] -default = ["file-drop", "objc-exception", "protocol"] +default = ["file-drop", "objc-exception", "protocol", "gtk"] objc-exception = ["objc/exception"] file-drop = [] protocol = [] devtools = [] transparent = [] fullscreen = [] -linux-body = ["webkit2gtk/v2_40"] +linux-body = ["webkit2gtk/v2_40", "gtk"] mac-proxy = [] +gtk = ["javascriptcore-rs", "webkit2gtk", "webkit2gtk-sys", "dep:gtk", "soup3", "x11-dl", "gdkx11"] + +[build-dependencies] +cfg_aliases = "0.1" [dependencies] libc = "0.2" @@ -46,13 +50,13 @@ http = "0.2" raw-window-handle = { version = "0.5", features = ["std"] } [target."cfg(any(target_os = \"linux\", target_os = \"dragonfly\", target_os = \"freebsd\", target_os = \"openbsd\", target_os = \"netbsd\"))".dependencies] -javascriptcore-rs = { version = "=1.1", features = ["v2_28"] } -webkit2gtk = { version = "=2.0", features = ["v2_38"] } -webkit2gtk-sys = "=2.0" -gtk = "0.18" -soup3 = "0.5" -x11-dl = "2.9" -gdkx11 = "0.18" +javascriptcore-rs = { version = "=1.1", features = ["v2_28"], optional = true } +webkit2gtk = { version = "=2.0", features = ["v2_38"], optional = true } +webkit2gtk-sys = { version = "=2.0", optional = true } +gtk = { version = "0.18", optional = true } +soup3 = { version = "0.5", optional = true } +x11-dl = { version = "2.9", optional = true } +gdkx11 = { version = "0.18", optional = true } [target."cfg(target_os = \"windows\")".dependencies] webview2-com = "0.27" diff --git a/build.rs b/build.rs index dece1aa1d..676cab71c 100644 --- a/build.rs +++ b/build.rs @@ -100,4 +100,16 @@ fn main() { } } } + + cfg_aliases::cfg_aliases! { + // Platforms + android: { target_os = "android" }, + macos: { target_os = "macos" }, + ios: { target_os = "ios" }, + windows: { target_os = "windows" }, + apple: { any(target_os = "ios", target_os = "macos") }, + linux: { all(unix, not(apple), not(android)) }, + // Backends + gtk: { all(feature = "gtk", linux) }, + } } diff --git a/src/error.rs b/src/error.rs index 65abe92a5..fa1407d5d 100644 --- a/src/error.rs +++ b/src/error.rs @@ -5,49 +5,19 @@ pub type Result = std::result::Result; #[non_exhaustive] #[derive(thiserror::Error, Debug)] pub enum Error { - #[cfg(any( - target_os = "linux", - target_os = "dragonfly", - target_os = "freebsd", - target_os = "netbsd", - target_os = "openbsd" - ))] + #[cfg(gtk)] #[error(transparent)] GlibError(#[from] gtk::glib::Error), - #[cfg(any( - target_os = "linux", - target_os = "dragonfly", - target_os = "freebsd", - target_os = "netbsd", - target_os = "openbsd" - ))] + #[cfg(gtk)] #[error(transparent)] GlibBoolError(#[from] gtk::glib::BoolError), - #[cfg(any( - target_os = "linux", - target_os = "dragonfly", - target_os = "freebsd", - target_os = "netbsd", - target_os = "openbsd" - ))] + #[cfg(gtk)] #[error("Fail to fetch security manager")] MissingManager, - #[cfg(any( - target_os = "linux", - target_os = "dragonfly", - target_os = "freebsd", - target_os = "netbsd", - target_os = "openbsd" - ))] + #[cfg(gtk)] #[error("Couldn't find X11 Display")] X11DisplayNotFound, - #[cfg(any( - target_os = "linux", - target_os = "dragonfly", - target_os = "freebsd", - target_os = "netbsd", - target_os = "openbsd" - ))] + #[cfg(gtk)] #[error(transparent)] XlibError(#[from] x11_dl::error::OpenError), #[error("Failed to initialize the script")] diff --git a/src/lib.rs b/src/lib.rs index ee02835a1..f585f7b7d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -191,24 +191,12 @@ pub use android::JniHandle; #[cfg(target_os = "android")] use android::*; -#[cfg(any( - target_os = "linux", - target_os = "dragonfly", - target_os = "freebsd", - target_os = "netbsd", - target_os = "openbsd" -))] +#[cfg(gtk)] pub(crate) mod webkitgtk; /// Re-exported [raw-window-handle](https://docs.rs/raw-window-handle/latest/raw_window_handle/) crate. pub use raw_window_handle; use raw_window_handle::HasRawWindowHandle; -#[cfg(any( - target_os = "linux", - target_os = "dragonfly", - target_os = "freebsd", - target_os = "netbsd", - target_os = "openbsd" -))] +#[cfg(gtk)] use webkitgtk::*; #[cfg(any(target_os = "macos", target_os = "ios"))] @@ -521,13 +509,7 @@ pub struct WebViewBuilder<'a> { window: Option<&'a dyn HasRawWindowHandle>, platform_specific: PlatformSpecificWebViewAttributes, web_context: Option<&'a mut WebContext>, - #[cfg(any( - target_os = "linux", - target_os = "dragonfly", - target_os = "freebsd", - target_os = "netbsd", - target_os = "openbsd", - ))] + #[cfg(gtk)] gtk_widget: Option<&'a gtk::Container>, } @@ -556,13 +538,7 @@ impl<'a> WebViewBuilder<'a> { #[allow(clippy::default_constructed_unit_structs)] platform_specific: PlatformSpecificWebViewAttributes::default(), web_context: None, - #[cfg(any( - target_os = "linux", - target_os = "dragonfly", - target_os = "freebsd", - target_os = "netbsd", - target_os = "openbsd", - ))] + #[cfg(gtk)] gtk_widget: None, } } @@ -594,13 +570,7 @@ impl<'a> WebViewBuilder<'a> { #[allow(clippy::default_constructed_unit_structs)] platform_specific: PlatformSpecificWebViewAttributes::default(), web_context: None, - #[cfg(any( - target_os = "linux", - target_os = "dragonfly", - target_os = "freebsd", - target_os = "netbsd", - target_os = "openbsd", - ))] + #[cfg(gtk)] gtk_widget: None, } } @@ -983,26 +953,14 @@ impl<'a> WebViewBuilder<'a> { InnerWebView::new(window, self.attrs, self.platform_specific, self.web_context)? } } else { - #[cfg(any( - target_os = "linux", - target_os = "dragonfly", - target_os = "freebsd", - target_os = "netbsd", - target_os = "openbsd", - ))] + #[cfg(gtk)] if let Some(widget) = self.gtk_widget { InnerWebView::new_gtk(widget, self.attrs, self.platform_specific, self.web_context)? } else { unreachable!() } - #[cfg(not(any( - target_os = "linux", - target_os = "dragonfly", - target_os = "freebsd", - target_os = "netbsd", - target_os = "openbsd", - )))] + #[cfg(not(gtk))] unreachable!() }; @@ -1460,13 +1418,7 @@ impl WebViewExtWindows for WebView { } /// 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", -))] +#[cfg(gtk)] pub trait WebviewExtUnix: Sized { /// Create the webview from a GTK container widget, such as GTK window. /// @@ -1481,13 +1433,7 @@ pub trait WebviewExtUnix: Sized { fn webview(&self) -> webkit2gtk::WebView; } -#[cfg(any( - target_os = "linux", - target_os = "dragonfly", - target_os = "freebsd", - target_os = "netbsd", - target_os = "openbsd", -))] +#[cfg(gtk)] impl WebviewExtUnix for WebView { fn new_gtk(widget: &W) -> Result where diff --git a/src/web_context.rs b/src/web_context.rs index de5a4451d..271f847db 100644 --- a/src/web_context.rs +++ b/src/web_context.rs @@ -2,13 +2,7 @@ // SPDX-License-Identifier: Apache-2.0 // SPDX-License-Identifier: MIT -#[cfg(any( - target_os = "linux", - target_os = "dragonfly", - target_os = "freebsd", - target_os = "netbsd", - target_os = "openbsd" -))] +#[cfg(gtk)] use crate::webkitgtk::WebContextImpl; use std::path::{Path, PathBuf}; @@ -43,13 +37,7 @@ impl WebContext { Self { data, os } } - #[cfg(any( - target_os = "linux", - target_os = "dragonfly", - target_os = "freebsd", - target_os = "netbsd", - target_os = "openbsd" - ))] + #[cfg(gtk)] pub(crate) fn new_ephemeral() -> Self { let data = WebContextData::default(); let os = WebContextImpl::new_ephemeral(); @@ -91,21 +79,11 @@ impl WebContextData { } } -#[cfg(any( - target_os = "windows", - target_os = "android", - target_os = "macos", - target_os = "ios" -))] +#[cfg(not(gtk))] #[derive(Debug)] pub(crate) struct WebContextImpl; -#[cfg(any( - target_os = "windows", - target_os = "android", - target_os = "macos", - target_os = "ios" -))] +#[cfg(not(gtk))] impl WebContextImpl { fn new(_data: &WebContextData) -> Self { Self From 474b8898762a98a6f9bb83b51fc0138bcf30b6ed Mon Sep 17 00:00:00 2001 From: Wu Yu Wei Date: Mon, 13 Nov 2023 19:11:23 +0900 Subject: [PATCH 2/3] Add native feature flag to public --- Cargo.toml | 6 +++--- build.rs | 2 +- src/lib.rs | 2 ++ 3 files changed, 6 insertions(+), 4 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index bae729e52..e7e73bb9b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -24,16 +24,16 @@ rustc-args = ["--cfg", "docsrs"] rustdoc-args = ["--cfg", "docsrs"] [features] -default = ["file-drop", "objc-exception", "protocol", "gtk"] +default = ["file-drop", "objc-exception", "protocol", "native"] objc-exception = ["objc/exception"] file-drop = [] protocol = [] devtools = [] transparent = [] fullscreen = [] -linux-body = ["webkit2gtk/v2_40", "gtk"] +linux-body = ["webkit2gtk/v2_40", "native"] mac-proxy = [] -gtk = ["javascriptcore-rs", "webkit2gtk", "webkit2gtk-sys", "dep:gtk", "soup3", "x11-dl", "gdkx11"] +native = ["javascriptcore-rs", "webkit2gtk", "webkit2gtk-sys", "dep:gtk", "soup3", "x11-dl", "gdkx11"] [build-dependencies] cfg_aliases = "0.1" diff --git a/build.rs b/build.rs index 676cab71c..55ae85a69 100644 --- a/build.rs +++ b/build.rs @@ -110,6 +110,6 @@ fn main() { apple: { any(target_os = "ios", target_os = "macos") }, linux: { all(unix, not(apple), not(android)) }, // Backends - gtk: { all(feature = "gtk", linux) }, + gtk: { all(feature = "native", linux) }, } } diff --git a/src/lib.rs b/src/lib.rs index f585f7b7d..e82990847 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -146,6 +146,8 @@ //! //! Wry uses a set of feature flags to toggle several advanced features. //! +//! - `native` (default): Enables native WebView framework on the platform. This must be enabled +//! for the crate to work. This feature is in preparasion of other ports like cef and servo. //! - `protocol` (default): Enables [`WebViewBuilder::with_custom_protocol`] to define custom URL scheme for handling tasks like //! loading assets. //! - `file-drop` (default): Enables [`WebViewBuilder::with_file_drop_handler`] to control the behaviour when there are files From e5e923be1731d88e291450b4e805a84ca1b65388 Mon Sep 17 00:00:00 2001 From: Wu Yu Wei Date: Tue, 14 Nov 2023 15:57:38 +0900 Subject: [PATCH 3/3] Rename feature to os-webview --- .changes/cfg_alias.md | 2 +- Cargo.toml | 6 +++--- build.rs | 2 +- src/lib.rs | 4 ++-- 4 files changed, 7 insertions(+), 7 deletions(-) diff --git a/.changes/cfg_alias.md b/.changes/cfg_alias.md index c3e729839..a37846442 100644 --- a/.changes/cfg_alias.md +++ b/.changes/cfg_alias.md @@ -2,4 +2,4 @@ "wry": patch --- -- Add cfg_aliases for easier feature configuration. +- Add cfg_aliases for easier feature configuration. And add `os-webview` as default feature. diff --git a/Cargo.toml b/Cargo.toml index e7e73bb9b..27f9b67b3 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -24,16 +24,16 @@ rustc-args = ["--cfg", "docsrs"] rustdoc-args = ["--cfg", "docsrs"] [features] -default = ["file-drop", "objc-exception", "protocol", "native"] +default = ["file-drop", "objc-exception", "protocol", "os-webview"] objc-exception = ["objc/exception"] file-drop = [] protocol = [] devtools = [] transparent = [] fullscreen = [] -linux-body = ["webkit2gtk/v2_40", "native"] +linux-body = ["webkit2gtk/v2_40", "os-webview"] mac-proxy = [] -native = ["javascriptcore-rs", "webkit2gtk", "webkit2gtk-sys", "dep:gtk", "soup3", "x11-dl", "gdkx11"] +os-webview = ["javascriptcore-rs", "webkit2gtk", "webkit2gtk-sys", "dep:gtk", "soup3", "x11-dl", "gdkx11"] [build-dependencies] cfg_aliases = "0.1" diff --git a/build.rs b/build.rs index 55ae85a69..85ca9a4cc 100644 --- a/build.rs +++ b/build.rs @@ -110,6 +110,6 @@ fn main() { apple: { any(target_os = "ios", target_os = "macos") }, linux: { all(unix, not(apple), not(android)) }, // Backends - gtk: { all(feature = "native", linux) }, + gtk: { all(feature = "os-webview", linux) }, } } diff --git a/src/lib.rs b/src/lib.rs index e82990847..dee9d03af 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -146,8 +146,8 @@ //! //! Wry uses a set of feature flags to toggle several advanced features. //! -//! - `native` (default): Enables native WebView framework on the platform. This must be enabled -//! for the crate to work. This feature is in preparasion of other ports like cef and servo. +//! - `os-webview` (default): Enables the default WebView framework on the platform. This must be enabled +//! for the crate to work. This feature was added in preparation of other ports like cef and servo. //! - `protocol` (default): Enables [`WebViewBuilder::with_custom_protocol`] to define custom URL scheme for handling tasks like //! loading assets. //! - `file-drop` (default): Enables [`WebViewBuilder::with_file_drop_handler`] to control the behaviour when there are files