Skip to content

Commit

Permalink
Add the ability to navigate with swipe gesture
Browse files Browse the repository at this point in the history
This switch is for WebView on macOS,
enabling us to swipe horizontally to
trigger backward and forward page navigation.

This commit also includes a worked example for it.

For details, see <https://developer.apple.com/documentation/webkit/wkwebview/1414995-allowsbackforwardnavigationgestu>

Signed-off-by: pan93412 <pan93412@gmail.com>
  • Loading branch information
pan93412 committed Nov 16, 2022
1 parent e69ddc6 commit 3bff22e
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 0 deletions.
11 changes: 11 additions & 0 deletions .changes/allow-back-forward-gesture.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
"wry": minor
---

Add the ability to navigate with swipe gesture

This switch is for WebView on macOS, enabling us to swipe horizontally to trigger backward and forward page navigation.

This commit also includes a worked example for it.

For details, see <https://developer.apple.com/documentation/webkit/wkwebview/1414995-allowsbackforwardnavigationgestu>
36 changes: 36 additions & 0 deletions examples/with_forward_gesture.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// Copyright 2020-2022 Tauri Programme within The Commons Conservancy
// SPDX-License-Identifier: Apache-2.0
// SPDX-License-Identifier: MIT

fn main() -> wry::Result<()> {
use wry::{
application::{
event::{Event, StartCause, WindowEvent},
event_loop::{ControlFlow, EventLoop},
window::WindowBuilder,
},
webview::WebViewBuilder,
};

let event_loop = EventLoop::new();
let window = WindowBuilder::new()
.with_title("Hello World")
.build(&event_loop)?;
let _webview = WebViewBuilder::new(window)?
.with_back_forward_navigation_gestures(true)
.with_url("https://html5test.com")?
.build()?;

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

match event {
Event::NewEvents(StartCause::Init) => println!("Wry has started!"),
Event::WindowEvent {
event: WindowEvent::CloseRequested,
..
} => *control_flow = ControlFlow::Exit,
_ => (),
}
});
}
20 changes: 20 additions & 0 deletions src/webview/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,14 @@ pub struct WebViewAttributes {
///
/// This configuration only impacts macOS.
pub accept_first_mouse: bool,

/// Indicates whether horizontal swipe gestures trigger backward and forward page navigation.
///
/// ## Platform-specific
///
/// This configuration only impacts macOS.
/// [Documentation](https://developer.apple.com/documentation/webkit/wkwebview/1414995-allowsbackforwardnavigationgestu).
pub back_forward_navigation_gestures: bool,
}

impl Default for WebViewAttributes {
Expand All @@ -241,6 +249,7 @@ impl Default for WebViewAttributes {
devtools: false,
zoom_hotkeys_enabled: false,
accept_first_mouse: false,
back_forward_navigation_gestures: false,
}
}
}
Expand Down Expand Up @@ -295,6 +304,17 @@ impl<'a> WebViewBuilder<'a> {
})
}

/// Indicates whether horizontal swipe gestures trigger backward and forward page navigation.
///
/// ## Platform-specific
///
/// This configuration only impacts macOS.
/// [Documentation](https://developer.apple.com/documentation/webkit/wkwebview/1414995-allowsbackforwardnavigationgestu).
pub fn with_back_forward_navigation_gestures(mut self, gesture: bool) -> Self {
self.webview.back_forward_navigation_gestures = gesture;
self
}

/// Sets whether the WebView should be transparent.
///
/// ## Platform-specific:
Expand Down
7 changes: 7 additions & 0 deletions src/webview/wkwebview/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,13 @@ impl InnerWebView {
let _: () = msg_send![webview, initWithFrame:frame configuration:config];
}

// allowsBackForwardNavigation
#[cfg(target_os = "macos")]
{
let value = attributes.back_forward_navigation_gestures;
let _: () = msg_send![webview, setAllowsBackForwardNavigationGestures:value];
}

// Message handler
let ipc_handler_ptr = if let Some(ipc_handler) = attributes.ipc_handler {
let cls = ClassDecl::new("WebViewDelegate", class!(NSObject));
Expand Down

0 comments on commit 3bff22e

Please sign in to comment.