Skip to content

Commit

Permalink
Add cfg_aliases for easier feature configuration (#1077)
Browse files Browse the repository at this point in the history
* Add cfg_aliases for easier feature configuration

* Add native feature flag to public

* Rename feature to os-webview

---------

Co-authored-by: Lucas Nogueira <[email protected]>
  • Loading branch information
wusyong and lucasfernog authored Nov 17, 2023
1 parent a9ad1c5 commit 15ae3c7
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 133 deletions.
5 changes: 5 additions & 0 deletions .changes/cfg_alias.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"wry": patch
---

- Add cfg_aliases for easier feature configuration. And add `os-webview` as default feature.
22 changes: 13 additions & 9 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"
Expand Down
12 changes: 12 additions & 0 deletions build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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) },
}
}
40 changes: 5 additions & 35 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,49 +5,19 @@ pub type Result<T> = std::result::Result<T, Error>;
#[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")]
Expand Down
74 changes: 11 additions & 63 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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"))]
Expand Down Expand Up @@ -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>,
}

Expand Down Expand Up @@ -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,
}
}
Expand Down Expand Up @@ -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,
}
}
Expand Down Expand Up @@ -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!()
};

Expand Down Expand Up @@ -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.
///
Expand All @@ -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<W>(widget: &W) -> Result<Self>
where
Expand Down
30 changes: 4 additions & 26 deletions src/web_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 15ae3c7

Please sign in to comment.