Skip to content

Commit

Permalink
Merge branch 'dev' into refactor/migrate-to-objc2
Browse files Browse the repository at this point in the history
  • Loading branch information
pewsheen committed Jul 22, 2024
2 parents 5f55c39 + 5231a37 commit f889b7c
Show file tree
Hide file tree
Showing 9 changed files with 67 additions and 38 deletions.
6 changes: 6 additions & 0 deletions .changes/ndk-0.9.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"wry": minor
---

**Breaking change**: Upgrade `ndk` crate to `0.9` and delete unused `ndk-sys` and `ndk-context` dependencies. Types from the `ndk` crate are used in public API surface.
**Breaking change**: The public `android_setup()` function now takes `&ThreadLooper` instead of `&ForeignLooper`, signifying that the setup function must be called on the thread where the looper is attached (and the `JNIEnv` argument is already thread-local as well).
5 changes: 5 additions & 0 deletions .changes/support-webview2-before-86.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"wry": patch
---

Support WebView2 version older than 86.0.616.0 and document version requirements for `back_forward_navigation_gestures`, `with_user_agent`, `with_hotkeys_zoom`, `with_browser_accelerator_keys`
4 changes: 1 addition & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -159,9 +159,7 @@ kuchiki = { package = "kuchikiki", version = "0.8" }
sha2 = "0.10"
base64 = "0.22"
jni = "0.21"
ndk = "0.7"
ndk-sys = "0.4"
ndk-context = "0.1"
ndk = "0.9"
tao-macros = "0.1"
libc = "0.2"

Expand Down
27 changes: 10 additions & 17 deletions examples/winit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@
// SPDX-License-Identifier: Apache-2.0
// SPDX-License-Identifier: MIT

use dpi::{LogicalPosition, LogicalSize};
use winit::{
event::{Event, WindowEvent},
event_loop::{ControlFlow, EventLoop},
window::WindowBuilder,
};
use wry::WebViewBuilder;
use wry::{Rect, WebViewBuilder};

fn main() -> wry::Result<()> {
#[cfg(any(
Expand Down Expand Up @@ -39,9 +40,9 @@ fn main() -> wry::Result<()> {
.build(&event_loop)
.unwrap();

#[allow(unused_mut)]
let mut builder = WebViewBuilder::new(&window);
let _webview = builder.with_url("https://tauri.app").build()?;
let webview = WebViewBuilder::new_as_child(&window)
.with_url("https://tauri.app")
.build()?;

event_loop
.run(move |event, evl| {
Expand All @@ -59,23 +60,15 @@ fn main() -> wry::Result<()> {
}

match event {
#[cfg(any(
target_os = "linux",
target_os = "dragonfly",
target_os = "freebsd",
target_os = "netbsd",
target_os = "openbsd",
))]
Event::WindowEvent {
event: WindowEvent::Resized(size),
..
} => {
use wry::dpi::{PhysicalPosition, PhysicalSize};

_webview
.set_bounds(wry::Rect {
position: PhysicalPosition::new(0, 0).into(),
size: PhysicalSize::new(size.width, size.height).into(),
let size = size.to_logical::<u32>(window.scale_factor());
webview
.set_bounds(Rect {
position: LogicalPosition::new(0, 0).into(),
size: LogicalSize::new(size.width, size.height).into(),
})
.unwrap();
}
Expand Down
2 changes: 1 addition & 1 deletion src/android/binding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ macro_rules! android_binding {
($domain:ident, $package:ident) => {
::wry::android_binding!($domain, $package, ::wry)
};
// use import `android_setup` just to force the import path to use `wry::{}`
// use imported `android_setup` just to force the import path to use `wry::{}`
// as the macro breaks without braces
($domain:ident, $package:ident, $wry:path) => {{
use $wry::{android_setup as _, prelude::*};
Expand Down
12 changes: 9 additions & 3 deletions src/android/main_pipe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ use std::{os::unix::prelude::*, sync::atomic::Ordering};
use super::{find_class, EvalCallback, EVAL_CALLBACKS, EVAL_ID_GENERATOR, PACKAGE};

static CHANNEL: Lazy<(Sender<WebViewMessage>, Receiver<WebViewMessage>)> = Lazy::new(|| bounded(8));
pub static MAIN_PIPE: Lazy<[RawFd; 2]> = Lazy::new(|| {
pub static MAIN_PIPE: Lazy<[OwnedFd; 2]> = Lazy::new(|| {
let mut pipe: [RawFd; 2] = Default::default();
unsafe { libc::pipe(pipe.as_mut_ptr()) };
pipe
unsafe { pipe.map(|fd| OwnedFd::from_raw_fd(fd)) }
});

pub struct MainPipe<'a> {
Expand All @@ -32,7 +32,13 @@ impl<'a> MainPipe<'a> {
pub(crate) fn send(message: WebViewMessage) {
let size = std::mem::size_of::<bool>();
if let Ok(()) = CHANNEL.0.send(message) {
unsafe { libc::write(MAIN_PIPE[1], &true as *const _ as *const _, size) };
unsafe {
libc::write(
MAIN_PIPE[1].as_raw_fd(),
&true as *const _ as *const _,
size,
)
};
}
}

Expand Down
12 changes: 8 additions & 4 deletions src/android/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,14 @@ use jni::{
JNIEnv,
};
use kuchiki::NodeRef;
use ndk::looper::{FdEvent, ForeignLooper};
use ndk::looper::{FdEvent, ThreadLooper};
use once_cell::sync::OnceCell;
use raw_window_handle::HasWindowHandle;
use sha2::{Digest, Sha256};
use std::{
borrow::Cow,
collections::HashMap,
os::fd::{AsFd as _, AsRawFd as _},
sync::{atomic::AtomicI32, mpsc::channel, Mutex},
};

Expand Down Expand Up @@ -75,10 +76,13 @@ pub static EVAL_CALLBACKS: once_cell::sync::OnceCell<Mutex<HashMap<i32, EvalCall
once_cell::sync::OnceCell::new();

/// Sets up the necessary logic for wry to be able to create the webviews later.
///
/// This function must be run on the thread where the [`JNIEnv`] is registered and the looper is local,
/// hence the requirement for a [`ThreadLooper`].
pub unsafe fn android_setup(
package: &str,
mut env: JNIEnv,
looper: &ForeignLooper,
looper: &ThreadLooper,
activity: GlobalRef,
) {
PACKAGE.get_or_init(move || package.to_string());
Expand Down Expand Up @@ -108,10 +112,10 @@ pub unsafe fn android_setup(
};

looper
.add_fd_with_callback(MAIN_PIPE[0], FdEvent::INPUT, move |_| {
.add_fd_with_callback(MAIN_PIPE[0].as_fd(), FdEvent::INPUT, move |fd, _event| {
let size = std::mem::size_of::<bool>();
let mut wake = false;
if libc::read(MAIN_PIPE[0], &mut wake as *mut _ as *mut _, size) == size as libc::ssize_t {
if libc::read(fd.as_raw_fd(), &mut wake as *mut _ as *mut _, size) == size as libc::ssize_t {
main_pipe.recv().is_ok()
} else {
false
Expand Down
16 changes: 15 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -465,6 +465,9 @@ pub struct WebViewAttributes {
///
/// ## Platform-specific:
///
/// - Windows: Setting to `false` does nothing on WebView2 Runtime version before 92.0.902.0,
/// see https://learn.microsoft.com/en-us/microsoft-edge/webview2/release-notes/archive?tabs=dotnetcsharp#10902-prerelease
///
/// - **Android / iOS:** Unsupported.
pub back_forward_navigation_gestures: bool,

Expand Down Expand Up @@ -849,6 +852,11 @@ impl<'a> WebViewBuilder<'a> {
}

/// Set a custom [user-agent](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/User-Agent) for the WebView.
///
/// ## Platform-specific
///
/// - Windows: Requires WebView2 Runtime version 86.0.616.0 or higher, does nothing on older versions,
/// see https://learn.microsoft.com/en-us/microsoft-edge/webview2/release-notes/archive?tabs=dotnetcsharp#10790-prerelease
pub fn with_user_agent(mut self, user_agent: &str) -> Self {
self.attrs.user_agent = Some(user_agent.to_string());
self
Expand All @@ -874,7 +882,10 @@ impl<'a> WebViewBuilder<'a> {
///
/// ## Platform-specific
///
/// **macOS / Linux / Android / iOS**: Unsupported
/// - Windows: Setting to `false` can't disable pinch zoom on WebView2 Runtime version before 91.0.865.0,
/// see https://learn.microsoft.com/en-us/microsoft-edge/webview2/release-notes/archive?tabs=dotnetcsharp#10865-prerelease
///
/// - **macOS / Linux / Android / iOS**: Unsupported
pub fn with_hotkeys_zoom(mut self, zoom: bool) -> Self {
self.attrs.zoom_hotkeys_enabled = zoom;
self
Expand Down Expand Up @@ -1114,6 +1125,9 @@ pub trait WebViewBuilderExtWindows {
/// `false`, it disables all accelerator keys that access features specific to a web browser.
/// The default value is `true`. See the following link to know more details.
///
/// Setting to `false` does nothing on WebView2 Runtime version before 92.0.902.0,
/// see https://learn.microsoft.com/en-us/microsoft-edge/webview2/release-notes/archive?tabs=dotnetcsharp#10824-prerelease
///
/// <https://learn.microsoft.com/en-us/microsoft-edge/webview2/reference/winrt/microsoft_web_webview2_core/corewebview2settings#arebrowseracceleratorkeysenabled>
fn with_browser_accelerator_keys(self, enabled: bool) -> Self;

Expand Down
21 changes: 12 additions & 9 deletions src/webview2/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -509,21 +509,24 @@ impl InnerWebView {
settings.SetAreDevToolsEnabled(attributes.devtools)?;

if let Some(user_agent) = &attributes.user_agent {
let settings2: ICoreWebView2Settings2 = webview.Settings()?.cast()?;
let user_agent = HSTRING::from(user_agent);
settings2.SetUserAgent(&user_agent)?;
if let Ok(settings2) = settings.cast::<ICoreWebView2Settings2>() {
settings2.SetUserAgent(&HSTRING::from(user_agent))?;
}
}

if !pl_attrs.browser_accelerator_keys {
let settings3 = settings.cast::<ICoreWebView2Settings5>()?;
settings3.SetAreBrowserAcceleratorKeysEnabled(false)?;
if let Ok(settings3) = settings.cast::<ICoreWebView2Settings3>() {
settings3.SetAreBrowserAcceleratorKeysEnabled(false)?;
}
}

let settings5 = settings.cast::<ICoreWebView2Settings5>()?;
settings5.SetIsPinchZoomEnabled(attributes.zoom_hotkeys_enabled)?;
if let Ok(settings5) = settings.cast::<ICoreWebView2Settings5>() {
settings5.SetIsPinchZoomEnabled(attributes.zoom_hotkeys_enabled)?;
}

let settings6 = settings.cast::<ICoreWebView2Settings6>()?;
settings6.SetIsSwipeNavigationEnabled(attributes.back_forward_navigation_gestures)?;
if let Ok(settings6) = settings.cast::<ICoreWebView2Settings6>() {
settings6.SetIsSwipeNavigationEnabled(attributes.back_forward_navigation_gestures)?;
}

if let Ok(settings9) = settings.cast::<ICoreWebView2Settings9>() {
settings9.SetIsNonClientRegionSupportEnabled(true)?;
Expand Down

0 comments on commit f889b7c

Please sign in to comment.