Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changes/x11-feature.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
wry: minor
---

Added `x11` feature flag (enabled by default).
4 changes: 3 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ rustc-args = ["--cfg", "docsrs"]
rustdoc-args = ["--cfg", "docsrs"]

[features]
default = ["drag-drop", "protocol", "os-webview"]
default = ["drag-drop", "protocol", "os-webview", "x11"]
serde = ["dpi/serde"]
drag-drop = []
protocol = []
Expand All @@ -41,6 +41,8 @@ os-webview = [
"webkit2gtk-sys",
"dep:gtk",
"soup3",
]
x11 = [
"x11-dl",
"gdkx11",
]
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,7 @@ Wry uses a set of feature flags to toggle several advanced features.
loading assets.
- `drag-drop` (default): Enables [`WebViewBuilder::with_drag_drop_handler`] to control the behaviour when there are files
interacting with the window.
- `x11` (default): Enables x11 support and dependencies on Linux.
- `devtools`: Enables devtools on release builds. Devtools are always enabled in debug builds.
On **macOS**, enabling devtools, requires calling private apis so you should not enable this flag in release
build if your app needs to publish to App Store.
Expand Down
2 changes: 1 addition & 1 deletion src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ pub enum Error {
#[cfg(gtk)]
#[error("Couldn't find X11 Display")]
X11DisplayNotFound,
#[cfg(gtk)]
#[cfg(all(gtk, feature = "x11"))]
#[error(transparent)]
XlibError(#[from] x11_dl::error::OpenError),
#[error("Failed to initialize the script")]
Expand Down
67 changes: 54 additions & 13 deletions src/webkitgtk/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,32 @@
// SPDX-License-Identifier: Apache-2.0
// SPDX-License-Identifier: MIT

use dpi::{LogicalPosition, LogicalSize};
#[cfg(feature = "x11")]
use dpi::LogicalPosition;
use dpi::LogicalSize;
use ffi::CookieManageExt;
#[cfg(feature = "x11")]
use gdkx11::{
ffi::{gdk_x11_window_foreign_new_for_display, GdkX11Display},
X11Display,
};
#[cfg(feature = "x11")]
use gtk::glib::{self, translate::FromGlibPtrFull};
use gtk::{
gdk::{self},
gio::Cancellable,
glib::{self, translate::FromGlibPtrFull},
prelude::*,
};
use http::Request;
use javascriptcore::ValueExt;
use raw_window_handle::{HasWindowHandle, RawWindowHandle};
use raw_window_handle::HasWindowHandle;
#[cfg(feature = "x11")]
use raw_window_handle::RawWindowHandle;
#[cfg(feature = "x11")]
use std::ffi::c_ulong;
#[cfg(any(debug_assertions, feature = "devtools"))]
use std::sync::atomic::{AtomicBool, Ordering};
use std::{
ffi::c_ulong,
sync::{Arc, Mutex},
};
use std::sync::{Arc, Mutex};
#[cfg(any(debug_assertions, feature = "devtools"))]
use webkit2gtk::WebInspectorExt;
use webkit2gtk::{
Expand All @@ -37,6 +42,7 @@ use webkit2gtk_sys::{
webkit_get_major_version, webkit_get_micro_version, webkit_get_minor_version,
webkit_policy_decision_ignore, webkit_policy_decision_use,
};
#[cfg(feature = "x11")]
use x11_dl::xlib::*;

pub use web_context::WebContextImpl;
Expand All @@ -54,6 +60,7 @@ mod drag_drop;
mod synthetic_mouse_events;
mod web_context;

#[cfg(feature = "x11")]
struct X11Data {
is_child: bool,
xlib: Xlib,
Expand All @@ -62,6 +69,7 @@ struct X11Data {
gtk_window: gtk::Window,
}

#[cfg(feature = "x11")]
impl Drop for X11Data {
fn drop(&mut self) {
unsafe { (self.xlib.XDestroyWindow)(self.x11_display as _, self.x11_window) };
Expand All @@ -77,6 +85,7 @@ pub(crate) struct InnerWebView {
pending_scripts: Arc<Mutex<Option<Vec<String>>>>,
is_in_fixed_parent: bool,

#[cfg(feature = "x11")]
x11: Option<X11Data>,
}

Expand All @@ -92,17 +101,38 @@ impl InnerWebView {
attributes: WebViewAttributes,
pl_attrs: super::PlatformSpecificWebViewAttributes,
) -> Result<Self> {
Self::new_x11(window, attributes, pl_attrs, false)
#[cfg(feature = "x11")]
{
Self::new_x11(window, attributes, pl_attrs, false)
}
#[cfg(not(feature = "x11"))]
{
let _ = window;
let _ = attributes;
let _ = pl_attrs;
Err(Error::UnsupportedWindowHandle)
}
}

pub fn new_as_child<W: HasWindowHandle>(
parent: &W,
attributes: WebViewAttributes,
pl_attrs: super::PlatformSpecificWebViewAttributes,
) -> Result<Self> {
Self::new_x11(parent, attributes, pl_attrs, true)
#[cfg(feature = "x11")]
{
Self::new_x11(parent, attributes, pl_attrs, true)
}
#[cfg(not(feature = "x11"))]
{
let _ = parent;
let _ = attributes;
let _ = pl_attrs;
Err(Error::UnsupportedWindowHandle)
}
}

#[cfg(feature = "x11")]
fn new_x11<W: HasWindowHandle>(
window: &W,
attributes: WebViewAttributes,
Expand Down Expand Up @@ -154,6 +184,7 @@ impl InnerWebView {
})
}

#[cfg(feature = "x11")]
fn create_container_x11_window(
xlib: &Xlib,
display: *mut _XDisplay,
Expand Down Expand Up @@ -185,6 +216,7 @@ impl InnerWebView {
window
}

#[cfg(feature = "x11")]
pub fn create_gtk_window(
raw: *mut GdkX11Display,
x11_window: c_ulong,
Expand Down Expand Up @@ -294,6 +326,7 @@ impl InnerWebView {
pending_scripts: Arc::new(Mutex::new(Some(Vec::new()))),

is_in_fixed_parent,
#[cfg(feature = "x11")]
x11: None,

#[cfg(any(debug_assertions, feature = "devtools"))]
Expand Down Expand Up @@ -718,6 +751,7 @@ impl InnerWebView {
pub fn bounds(&self) -> Result<Rect> {
let mut bounds = Rect::default();

#[cfg(feature = "x11")]
if let Some(x11_data) = &self.x11 {
unsafe {
let attributes: XWindowAttributes = std::mem::zeroed();
Expand All @@ -734,11 +768,12 @@ impl InnerWebView {
bounds.size = LogicalSize::new(attributes.width, attributes.height).into();
}
}
} else {
let (size, _) = self.webview.allocated_size();
bounds.size = LogicalSize::new(size.width(), size.height()).into();
return Ok(bounds);
}

let (size, _) = self.webview.allocated_size();
bounds.size = LogicalSize::new(size.width(), size.height()).into();

Ok(bounds)
}

Expand All @@ -747,6 +782,7 @@ impl InnerWebView {
let (width, height) = bounds.size.to_logical::<i32>(scale_factor).into();
let (x, y) = bounds.position.to_logical::<i32>(scale_factor).into();

#[cfg(feature = "x11")]
if let Some(x11_data) = &self.x11 {
let window = &x11_data.gtk_window;
window.move_(x, y);
Expand All @@ -765,6 +801,7 @@ impl InnerWebView {
Ok(())
}

#[cfg(feature = "x11")]
fn set_visible_x11(&self, visible: bool) {
if let Some(x11_data) = &self.x11 {
if x11_data.is_child {
Expand All @@ -777,6 +814,7 @@ impl InnerWebView {
}
}

#[cfg(feature = "x11")]
fn set_visible_gtk(&self, visible: bool) {
if let Some(x11_data) = &self.x11 {
if x11_data.is_child {
Expand All @@ -790,6 +828,7 @@ impl InnerWebView {
}

pub fn set_visible(&self, visible: bool) -> Result<()> {
#[cfg(feature = "x11")]
self.set_visible_x11(visible);

if visible {
Expand All @@ -798,6 +837,7 @@ impl InnerWebView {
self.webview.hide();
}

#[cfg(feature = "x11")]
self.set_visible_gtk(visible);

Ok(())
Expand Down Expand Up @@ -962,8 +1002,9 @@ struct SendEnteredSpan(tracing::span::EnteredSpan);
#[cfg(feature = "tracing")]
unsafe impl Send for SendEnteredSpan {}

const BASE_DPI: f64 = 96.0;
#[cfg(feature = "x11")]
fn scale_factor_from_x11(xlib: &Xlib, display: *mut _XDisplay, parent: c_ulong) -> f64 {
const BASE_DPI: f64 = 96.0;
let mut attrs = unsafe { std::mem::zeroed() };
unsafe { (xlib.XGetWindowAttributes)(display, parent, &mut attrs) };
let scale_factor = unsafe { (*attrs.screen).width as f64 * 25.4 / (*attrs.screen).mwidth as f64 };
Expand Down