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/OnBackPressedCallback.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"wry": patch
---

Use OnBackPressedCallback instead of the deprecated onKeyDown for back navigation on Android.
8 changes: 0 additions & 8 deletions .changes/remove-webkitgtk-request-workaround.md

This file was deleted.

5 changes: 5 additions & 0 deletions .changes/web-content-process-termination.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"wry": minor
---

Add handler for web content process termination.
12 changes: 12 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
# Changelog

## \[0.53.5]

- [#1622](https://github.com/tauri-apps/wry/pull/1626) Fixed an issue that caused docs.rs builds to fail. No user facing changes.

## \[0.53.4]

- [`093856a`](https://github.com/tauri-apps/wry/commit/093856a2a53a6fc1aaa759e048c7e1fe31bb09fa) ([#1622](https://github.com/tauri-apps/wry/pull/1622) by [@lucasfernog](https://github.com/tauri-apps/wry/../../lucasfernog)) Add flag to opt out of automatic back navigation handling on Android via `WryActivity#handleBackNavigation`.
- [`0f51d67`](https://github.com/tauri-apps/wry/commit/0f51d67485d84fd9c72391379a67567eea3cbbfe) ([#1605](https://github.com/tauri-apps/wry/pull/1605) by [@dgerhardt](https://github.com/tauri-apps/wry/../../dgerhardt)) On Linux, removed a workaround which forced inital requests for multiple webviews to be handled sequentially.
The workaround was intended to fix a concurrency bug with loading multiple URIs at the same time on WebKitGTK.
But it prevented parallelization and could cause a deadlock in certain situations.
It is no longer needed with newer WebKitGTK versions.

## \[0.53.3]

- [`6aa5854`](https://github.com/tauri-apps/wry/commit/6aa5854b0371a4828638cd722e18d9b1ab235a8b) ([#1609](https://github.com/tauri-apps/wry/pull/1609) by [@lucasfernog](https://github.com/tauri-apps/wry/../../lucasfernog)) Enhance error handling of the `webview_version` function on macOS.
Expand Down
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 1 addition & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ workspace = {}

[package]
name = "wry"
version = "0.53.3"
version = "0.53.5"
authors = ["Tauri Programme within The Commons Conservancy"]
edition = "2021"
license = "Apache-2.0 OR MIT"
Expand All @@ -22,8 +22,6 @@ targets = [
"x86_64-pc-windows-msvc",
"x86_64-apple-darwin",
]
rustc-args = ["--cfg", "docsrs"]
rustdoc-args = ["--cfg", "docsrs"]

[features]
default = ["drag-drop", "protocol", "os-webview", "x11"]
Expand Down
18 changes: 1 addition & 17 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -189,24 +189,8 @@ sudo dnf install gtk3-devel webkit2gtk4.1-devel

###### Nix & NixOS

```nix
# shell.nix

let
# Unstable Channel | Rolling Release
pkgs = import (fetchTarball("channel:nixpkgs-unstable")) { };
packages = with pkgs; [
pkg-config
webkitgtk_4_1
];
in
pkgs.mkShell {
buildInputs = packages;
}
```

```sh
nix-shell shell.nix
nix-shell -p pkg-config webkitgtk_4_1
```

###### GUIX
Expand Down
19 changes: 17 additions & 2 deletions examples/simple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ fn main() -> wry::Result<()> {
let window = WindowBuilder::new().build(&event_loop).unwrap();

let builder = WebViewBuilder::new()
.with_url("http://tauri.app")
.with_url("https://webrtc.github.io/samples/src/content/getusermedia/getdisplaymedia/")
.with_new_window_req_handler(|url, features| {
println!("new window req: {url} {features:?}");
wry::NewWindowResponse::Allow
Expand All @@ -39,11 +39,18 @@ fn main() -> wry::Result<()> {

#[cfg(any(
target_os = "windows",
target_os = "macos",
target_os = "ios",
target_os = "android"
))]
let _webview = builder.build(&window)?;
#[cfg(target_os = "macos")]
let _webview = {
use wry::WebViewBuilderExtMacos;
builder.with_display_capture_decision_handler(|_capture_type| {
wry::WKDisplayCapturePermissionDecision::ScreenPrompt
}).build(&window)?
};

#[cfg(not(any(
target_os = "windows",
target_os = "macos",
Expand All @@ -57,6 +64,14 @@ fn main() -> wry::Result<()> {
builder.build_gtk(vbox)?
};

#[cfg(target_os = "macos")]
{
use wry::WebViewExtMacOS;
_webview.set_display_capture_decision_handler(|_capture_type| {
wry::WKDisplayCapturePermissionDecision::ScreenPrompt
});
}

event_loop.run(move |event, _, control_flow| {
*control_flow = ControlFlow::Wait;

Expand Down
26 changes: 18 additions & 8 deletions src/android/kotlin/WryActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,33 @@ import android.os.Build
import android.os.Bundle
import android.webkit.WebView
import android.view.KeyEvent
import androidx.activity.OnBackPressedCallback
import androidx.appcompat.app.AppCompatActivity

abstract class WryActivity : AppCompatActivity() {
private lateinit var mWebView: RustWebView
open val handleBackNavigation: Boolean = true

open fun onWebViewCreate(webView: WebView) { }

fun setWebView(webView: RustWebView) {
mWebView = webView

if (handleBackNavigation) {
val callback = object : OnBackPressedCallback(true) {
override fun handleOnBackPressed() {
if (this@WryActivity.mWebView.canGoBack()) {
this@WryActivity.mWebView.goBack()
} else {
this.isEnabled = false
this@WryActivity.onBackPressed()
this.isEnabled = true
}
}
}
onBackPressedDispatcher.addCallback(this, callback)
}

onWebViewCreate(webView)
}

Expand Down Expand Up @@ -101,14 +119,6 @@ abstract class WryActivity : AppCompatActivity() {
memory()
}

override fun onKeyDown(keyCode: Int, event: KeyEvent?): Boolean {
if (keyCode == KeyEvent.KEYCODE_BACK && mWebView.canGoBack()) {
mWebView.goBack()
return true
}
return super.onKeyDown(keyCode, event)
}

fun getAppClass(name: String): Class<*> {
return Class.forName(name)
}
Expand Down
46 changes: 46 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,8 @@ use webkitgtk::*;
use objc2::rc::Retained;
#[cfg(target_os = "macos")]
use objc2_app_kit::NSWindow;
#[cfg(target_os = "macos")]
use objc2_web_kit::WKMediaCaptureType;
#[cfg(any(target_os = "macos", target_os = "ios"))]
use objc2_web_kit::WKUserContentController;
#[cfg(any(target_os = "macos", target_os = "ios"))]
Expand All @@ -387,6 +389,8 @@ pub(crate) mod wkwebview;
use wkwebview::*;
#[cfg(any(target_os = "macos", target_os = "ios"))]
pub use wkwebview::{PrintMargin, PrintOptions, WryWebView};
#[cfg(target_os = "macos")]
pub use wkwebview::WKDisplayCapturePermissionDecision;

#[cfg(target_os = "windows")]
pub(crate) mod webview2;
Expand Down Expand Up @@ -1462,12 +1466,19 @@ pub(crate) struct PlatformSpecificWebViewAttributes {
data_store_identifier: Option<[u8; 16]>,
traffic_light_inset: Option<dpi::Position>,
allow_link_preview: bool,
on_web_content_process_terminate_handler: Option<Box<dyn Fn()>>,
#[cfg(target_os = "ios")]
input_accessory_view_builder: Option<Box<InputAccessoryViewBuilder>>,
#[cfg(target_os = "ios")]
limit_navigations_to_app_bound_domains: bool,
#[cfg(target_os = "macos")]
webview_configuration: Option<Retained<objc2_web_kit::WKWebViewConfiguration>>,
#[cfg(target_os = "macos")]
/// A closure that make the permission decision for display capture (e.g. getDisplayMedia()) requests.
///
/// Only available on macOS 13+.
pub display_capture_decision_handler:
Option<Box<dyn Fn(WKMediaCaptureType) -> WKDisplayCapturePermissionDecision>>,
}

#[cfg(any(target_os = "macos", target_os = "ios"))]
Expand All @@ -1478,12 +1489,15 @@ impl Default for PlatformSpecificWebViewAttributes {
traffic_light_inset: None,
// platform default for this is true
allow_link_preview: true,
on_web_content_process_terminate_handler: None,
#[cfg(target_os = "ios")]
input_accessory_view_builder: None,
#[cfg(target_os = "ios")]
limit_navigations_to_app_bound_domains: false,
#[cfg(target_os = "macos")]
webview_configuration: None,
#[cfg(target_os = "macos")]
display_capture_decision_handler: Some(Box::new(|_| WKDisplayCapturePermissionDecision::ScreenPrompt)),
}
}
}
Expand All @@ -1509,6 +1523,8 @@ pub trait WebViewBuilderExtDarwin {
///
/// See https://developer.apple.com/documentation/webkit/wkwebview/allowslinkpreview
fn with_allow_link_preview(self, allow_link_preview: bool) -> Self;
/// Set a handler closure to respond to web content process termination. Available on macOS and iOS only.
fn with_on_web_content_process_terminate_handler(self, handler: impl Fn() + 'static) -> Self;
}

#[cfg(any(target_os = "macos", target_os = "ios"))]
Expand All @@ -1527,6 +1543,13 @@ impl WebViewBuilderExtDarwin for WebViewBuilder<'_> {
self.platform_specific.allow_link_preview = allow_link_preview;
self
}

fn with_on_web_content_process_terminate_handler(mut self, handler: impl Fn() + 'static) -> Self {
self
.platform_specific
.on_web_content_process_terminate_handler = Some(Box::new(handler));
self
}
}

#[cfg(target_os = "macos")]
Expand All @@ -1536,6 +1559,10 @@ pub trait WebViewBuilderExtMacos {
self,
configuration: Retained<objc2_web_kit::WKWebViewConfiguration>,
) -> Self;

fn with_display_capture_decision_handler<F>(self, handler: F) -> Self
where
F: Fn(WKMediaCaptureType) -> WKDisplayCapturePermissionDecision + 'static;
}

#[cfg(target_os = "macos")]
Expand All @@ -1550,6 +1577,14 @@ impl WebViewBuilderExtMacos for WebViewBuilder<'_> {
.replace(configuration);
self
}

fn with_display_capture_decision_handler<F>(mut self, handler: F) -> Self
where
F: Fn(WKMediaCaptureType) -> WKDisplayCapturePermissionDecision + 'static,
{
self.platform_specific.display_capture_decision_handler = Some(Box::new(handler));
self
}
}

#[cfg(target_os = "ios")]
Expand Down Expand Up @@ -2362,6 +2397,10 @@ pub trait WebViewExtMacOS {
/// Warning: Do not use this if your chosen window library does not support traffic light insets.
/// Warning: Only use this in **decorated** windows with a **hidden titlebar**!
fn set_traffic_light_inset<P: Into<dpi::Position>>(&self, position: P) -> Result<()>;
/// Set display capture decision handler to decide if incoming display capture request is allowed and its target.
fn set_display_capture_decision_handler<F>(&self, handler: F)
where
F: Fn(WKMediaCaptureType) -> WKDisplayCapturePermissionDecision + 'static;
}

#[cfg(target_os = "macos")]
Expand Down Expand Up @@ -2389,6 +2428,13 @@ impl WebViewExtMacOS for WebView {
fn set_traffic_light_inset<P: Into<dpi::Position>>(&self, position: P) -> Result<()> {
self.webview.set_traffic_light_inset(position.into())
}

fn set_display_capture_decision_handler<F>(&self, handler: F)
where
F: Fn(WKMediaCaptureType) -> WKDisplayCapturePermissionDecision + 'static,
{
self.webview.set_display_capture_decision_handler(handler);
}
}

/// Additional methods on `WebView` that are specific to iOS.
Expand Down
2 changes: 1 addition & 1 deletion src/webview2/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1351,7 +1351,7 @@ impl InnerWebView {

/// Public APIs
impl InnerWebView {
pub fn id(&self) -> crate::WebViewId {
pub fn id(&self) -> crate::WebViewId<'_> {
&self.id
}

Expand Down
19 changes: 19 additions & 0 deletions src/wkwebview/class/wry_navigation_delegate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ use crate::{
download::{navigation_download_action, navigation_download_response},
navigation::{
did_commit_navigation, did_finish_navigation, navigation_policy, navigation_policy_response,
web_content_process_did_terminate,
},
},
PageLoadEvent, WryWebView,
Expand All @@ -35,6 +36,7 @@ pub struct WryNavigationDelegateIvars {
pub navigation_policy_function: Box<dyn Fn(String) -> bool>,
pub download_delegate: Option<Retained<WryDownloadDelegate>>,
pub on_page_load_handler: Option<Box<dyn Fn(PageLoadEvent)>>,
pub on_web_content_process_terminate_handler: Option<Box<dyn Fn()>>,
}

define_class!(
Expand Down Expand Up @@ -96,6 +98,11 @@ define_class!(
) {
navigation_download_response(self, webview, response, download);
}

#[unsafe(method(webViewWebContentProcessDidTerminate:))]
fn web_content_process_did_terminate(&self, webview: &WKWebView) {
web_content_process_did_terminate(self, webview);
}
}
);

Expand All @@ -108,6 +115,7 @@ impl WryNavigationDelegate {
navigation_handler: Option<Box<dyn Fn(String) -> bool>>,
download_delegate: Option<Retained<WryDownloadDelegate>>,
on_page_load_handler: Option<Box<dyn Fn(PageLoadEvent, String)>>,
on_web_content_process_terminate_handler: Option<Box<dyn Fn()>>,
mtm: MainThreadMarker,
) -> Retained<Self> {
let navigation_policy_function = Box::new(move |url: String| -> bool {
Expand All @@ -125,6 +133,16 @@ impl WryNavigationDelegate {
None
};

let on_web_content_process_terminate_handler =
if let Some(handler) = on_web_content_process_terminate_handler {
let custom_handler = Box::new(move || {
handler();
}) as Box<dyn Fn()>;
Some(custom_handler)
} else {
None
};

let delegate = mtm
.alloc::<WryNavigationDelegate>()
.set_ivars(WryNavigationDelegateIvars {
Expand All @@ -133,6 +151,7 @@ impl WryNavigationDelegate {
has_download_handler,
download_delegate,
on_page_load_handler,
on_web_content_process_terminate_handler,
});

unsafe { msg_send![super(delegate), init] }
Expand Down
Loading
Loading