diff --git a/.changes/cfg_alias.md b/.changes/cfg_alias.md new file mode 100644 index 000000000..a37846442 --- /dev/null +++ b/.changes/cfg_alias.md @@ -0,0 +1,5 @@ +--- +"wry": patch +--- + +- Add cfg_aliases for easier feature configuration. And add `os-webview` as default feature. diff --git a/Cargo.toml b/Cargo.toml index c9db01a10..27f9b67b3 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", "os-webview"] objc-exception = ["objc/exception"] file-drop = [] protocol = [] devtools = [] transparent = [] fullscreen = [] -linux-body = ["webkit2gtk/v2_40"] +linux-body = ["webkit2gtk/v2_40", "os-webview"] mac-proxy = [] +os-webview = ["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..85ca9a4cc 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 = "os-webview", 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 e11b0e897..a0b71f9d3 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. //! +//! - `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 @@ -191,24 +193,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 +511,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 +540,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 +572,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 +955,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 +1420,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 +1435,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