Skip to content

Commit

Permalink
WIP: refactor(ios): add wkwebview for ios
Browse files Browse the repository at this point in the history
  • Loading branch information
pewsheen committed Sep 8, 2024
1 parent a7919ca commit 320b26d
Show file tree
Hide file tree
Showing 12 changed files with 777 additions and 78 deletions.
49 changes: 30 additions & 19 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ workspace = {}
[package]
name = "wry"
version = "0.43.1"
authors = [ "Tauri Programme within The Commons Conservancy" ]
authors = ["Tauri Programme within The Commons Conservancy"]
edition = "2021"
license = "Apache-2.0 OR MIT"
description = "Cross-platform WebView rendering library"
Expand All @@ -25,16 +25,16 @@ rustc-args = ["--cfg", "docsrs"]
rustdoc-args = ["--cfg", "docsrs"]

[features]
default = [ "drag-drop", "objc-exception", "protocol", "os-webview" ]
serde = [ "dpi/serde" ]
default = ["drag-drop", "objc-exception", "protocol", "os-webview"]
serde = ["dpi/serde"]
objc-exception = ["objc2/exception"]
drag-drop = [ ]
protocol = [ ]
devtools = [ ]
transparent = [ ]
fullscreen = [ ]
linux-body = [ "webkit2gtk/v2_40", "os-webview" ]
mac-proxy = [ ]
drag-drop = []
protocol = []
devtools = []
transparent = []
fullscreen = []
linux-body = ["webkit2gtk/v2_40", "os-webview"]
mac-proxy = []
os-webview = [
"javascriptcore-rs",
"webkit2gtk",
Expand All @@ -51,12 +51,14 @@ tracing = { version = "0.1", optional = true }
once_cell = "1"
thiserror = "1.0"
http = "1.1"
raw-window-handle = { version = "0.6", features = [ "std" ] }
raw-window-handle = { version = "0.6", features = ["std"] }
dpi = "0.1"

[target."cfg(any(target_os = \"linux\", target_os = \"dragonfly\", target_os = \"freebsd\", target_os = \"openbsd\", target_os = \"netbsd\"))".dependencies]
javascriptcore-rs = { version = "=1.1.2", features = [ "v2_28" ], optional = true }
webkit2gtk = { version = "=2.0.1", features = [ "v2_38" ], optional = true }
javascriptcore-rs = { version = "=1.1.2", features = [
"v2_28",
], optional = true }
webkit2gtk = { version = "=2.0.1", features = ["v2_38"], optional = true }
webkit2gtk-sys = { version = "=2.0.1", optional = true }
gtk = { version = "0.18", optional = true }
soup3 = { version = "0.5", optional = true }
Expand Down Expand Up @@ -133,7 +135,21 @@ objc2-foundation = { version = "0.2.0", features = [
"NSDate",
"NSBundle",
"NSProcessInfo",
"NSValue",
"NSRange",
] }

[target."cfg(target_os = \"ios\")".dependencies]
objc2-ui-kit = { version = "0.2.2", features = [
"UIResponder",
"UIScrollView",
"UIView",
"UIWindow",
"UIApplication",
"UIEvent",
] }

[target."cfg(target_os = \"macos\")".dependencies]
objc2-app-kit = { version = "0.2.0", features = [
"NSApplication",
"NSEvent",
Expand All @@ -146,11 +162,6 @@ objc2-app-kit = { version = "0.2.0", features = [
"NSSavePanel",
"NSMenu",
] }
objc2-ui-kit = { version = "0.2.2", features = [
"UIResponder",
"UIScrollView",
"UIView",
] }

[target."cfg(target_os = \"android\")".dependencies]
crossbeam-channel = "0.5"
Expand All @@ -174,4 +185,4 @@ percent-encoding = "2.3"

[lints.rust.unexpected_cfgs]
level = "warn"
check-cfg = [ "cfg(linux)", "cfg(gtk)" ]
check-cfg = ["cfg(linux)", "cfg(gtk)"]
14 changes: 7 additions & 7 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ use webkitgtk::*;

#[cfg(any(target_os = "macos", target_os = "ios"))]
use objc2::rc::Retained;
#[cfg(any(target_os = "macos", target_os = "ios"))]
#[cfg(any(target_os = "macos"))]
use objc2_app_kit::NSWindow;
#[cfg(any(target_os = "macos", target_os = "ios"))]
use objc2_web_kit::WKUserContentController;
Expand Down Expand Up @@ -1685,19 +1685,19 @@ impl WebViewExtMacOS for WebView {
#[cfg(target_os = "ios")]
pub trait WebViewExtIOS {
/// Returns WKWebView handle
fn webview(&self) -> cocoa::base::id;
fn webview(&self) -> Retained<WryWebView>;
/// Returns WKWebView manager [(userContentController)](https://developer.apple.com/documentation/webkit/wkscriptmessagehandler/1396222-usercontentcontroller) handle
fn manager(&self) -> cocoa::base::id;
fn manager(&self) -> Retained<WKUserContentController>;
}

#[cfg(target_os = "ios")]
impl WebViewExtIOS for WebView {
fn webview(&self) -> cocoa::base::id {
self.webview.webview
fn webview(&self) -> Retained<WryWebView> {
self.webview.webview.clone()
}

fn manager(&self) -> cocoa::base::id {
self.webview.manager
fn manager(&self) -> Retained<WKUserContentController> {
self.webview.manager.clone()
}
}

Expand Down
29 changes: 14 additions & 15 deletions src/wkwebview/class/url_scheme_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,24 @@
// SPDX-License-Identifier: Apache-2.0
// SPDX-License-Identifier: MIT

use std::borrow::Cow;
use std::ffi::c_void;
use std::panic::AssertUnwindSafe;
use std::ptr::NonNull;
use std::slice;

use http::header::{CONTENT_LENGTH, CONTENT_TYPE};
use http::{Request, Response as HttpResponse, StatusCode, Version};
use objc2::rc::Retained;
use objc2::runtime::{AnyClass, AnyObject, ClassBuilder, ProtocolObject};
use objc2::ClassType;
use objc2_foundation::NSObjectProtocol;
use std::{borrow::Cow, ffi::c_void, panic::AssertUnwindSafe, ptr::NonNull, slice};

use http::{
header::{CONTENT_LENGTH, CONTENT_TYPE},
Request, Response as HttpResponse, StatusCode, Version,
};
use objc2::{
rc::Retained,
runtime::{AnyClass, AnyObject, ClassBuilder, ProtocolObject},
ClassType,
};
use objc2_foundation::{
NSData, NSHTTPURLResponse, NSMutableDictionary, NSObject, NSString, NSURL, NSUUID,
NSData, NSHTTPURLResponse, NSMutableDictionary, NSObject, NSObjectProtocol, NSString, NSURL,
NSUUID,
};
use objc2_web_kit::{WKURLSchemeHandler, WKURLSchemeTask};

use crate::wkwebview::WEBVIEW_IDS;
use crate::{RequestAsyncResponder, WryWebView};
use crate::{wkwebview::WEBVIEW_IDS, RequestAsyncResponder, WryWebView};

pub fn create(name: &str) -> &AnyClass {
unsafe {
Expand Down
7 changes: 6 additions & 1 deletion src/wkwebview/class/wry_navigation_delegate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,14 @@ use objc2::{
use objc2_foundation::{MainThreadMarker, NSObjectProtocol};
use objc2_web_kit::{
WKDownload, WKNavigation, WKNavigationAction, WKNavigationActionPolicy, WKNavigationDelegate,
WKNavigationResponse, WKNavigationResponsePolicy, WKWebView,
WKNavigationResponse, WKNavigationResponsePolicy,
};

#[cfg(target_os = "ios")]
use crate::wkwebview::ios::WKWebView::WKWebView;
#[cfg(target_os = "macos")]
use objc2_web_kit::WKWebView;

use crate::{
url_from_webview,
wkwebview::{
Expand Down
18 changes: 12 additions & 6 deletions src/wkwebview/class/wry_web_view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,26 @@

use std::collections::HashMap;

#[cfg(target_os = "macos")]
use objc2::runtime::ProtocolObject;
use objc2::{
declare_class,
mutability::MainThreadOnly,
rc::Retained,
runtime::{Bool, ProtocolObject},
ClassType, DeclaredClass,
declare_class, mutability::MainThreadOnly, rc::Retained, runtime::Bool, ClassType, DeclaredClass,
};
#[cfg(target_os = "macos")]
use objc2_app_kit::{NSDraggingDestination, NSEvent};
use objc2_foundation::{NSObjectProtocol, NSUUID};
use objc2_web_kit::WKWebView;

#[cfg(target_os = "ios")]
use crate::wkwebview::ios::WKWebView::WKWebView;
#[cfg(target_os = "macos")]
use crate::{
wkwebview::{drag_drop, synthetic_mouse_events},
DragDropEvent,
};
#[cfg(target_os = "ios")]
use objc2_ui_kit::UIEvent as NSEvent;
#[cfg(target_os = "macos")]
use objc2_web_kit::WKWebView;

pub struct WryWebViewIvars {
pub(crate) is_child: bool,
Expand Down Expand Up @@ -62,6 +67,7 @@ declare_class!(
}
}

#[cfg(target_os = "macos")]
#[method(acceptsFirstMouse:)]
fn accept_first_mouse(
&self,
Expand Down
9 changes: 7 additions & 2 deletions src/wkwebview/class/wry_web_view_parent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,11 @@
use objc2::{
declare_class, msg_send_id, mutability::MainThreadOnly, rc::Retained, ClassType, DeclaredClass,
};
use objc2_app_kit::{NSApp, NSEvent, NSView};
#[cfg(target_os = "macos")]
use objc2_app_kit::{NSApplication, NSEvent, NSView};
use objc2_foundation::MainThreadMarker;
#[cfg(target_os = "ios")]
use objc2_ui_kit::UIView as NSView;

pub struct WryWebViewParentIvars {}

Expand All @@ -24,13 +27,14 @@ declare_class!(
}

unsafe impl WryWebViewParent {
#[cfg(target_os = "macos")]
#[method(keyDown:)]
fn key_down(
&self,
event: &NSEvent,
) {
let mtm = MainThreadMarker::new().unwrap();
let app = NSApp(mtm);
let app = NSApplication::sharedApplication(mtm);
unsafe {
if let Some(menu) = app.mainMenu() {
menu.performKeyEquivalent(event);
Expand All @@ -41,6 +45,7 @@ declare_class!(
);

impl WryWebViewParent {
#[allow(dead_code)]
pub fn new(mtm: MainThreadMarker) -> Retained<Self> {
let delegate = mtm
.alloc::<WryWebViewParent>()
Expand Down
13 changes: 10 additions & 3 deletions src/wkwebview/class/wry_web_view_ui_delegate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,24 @@
// SPDX-License-Identifier: Apache-2.0
// SPDX-License-Identifier: MIT

#[cfg(target_os = "macos")]
use std::ptr::null_mut;

use block2::Block;
use objc2::{
declare_class, msg_send_id, mutability::MainThreadOnly, rc::Retained, runtime::NSObject,
ClassType, DeclaredClass,
};
#[cfg(target_os = "macos")]
use objc2_app_kit::{NSModalResponse, NSModalResponseOK, NSOpenPanel};
use objc2_foundation::{MainThreadMarker, NSArray, NSObjectProtocol, NSURL};
use objc2_foundation::{MainThreadMarker, NSObjectProtocol};
#[cfg(target_os = "macos")]
use objc2_foundation::{NSArray, NSURL};

#[cfg(target_os = "macos")]
use objc2_web_kit::WKOpenPanelParameters;
use objc2_web_kit::{
WKFrameInfo, WKMediaCaptureType, WKOpenPanelParameters, WKPermissionDecision, WKSecurityOrigin,
WKUIDelegate,
WKFrameInfo, WKMediaCaptureType, WKPermissionDecision, WKSecurityOrigin, WKUIDelegate,
};

use crate::WryWebView;
Expand All @@ -36,6 +42,7 @@ declare_class!(
unsafe impl NSObjectProtocol for WryWebViewUIDelegate {}

unsafe impl WKUIDelegate for WryWebViewUIDelegate {
#[cfg(target_os = "macos")]
#[method(webView:runOpenPanelWithParameters:initiatedByFrame:completionHandler:)]
fn run_file_upload_panel(
&self,
Expand Down
7 changes: 6 additions & 1 deletion src/wkwebview/download.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@ use std::{path::PathBuf, ptr::null_mut};

use objc2::{rc::Retained, runtime::ProtocolObject, DeclaredClass};
use objc2_foundation::{NSData, NSError, NSString, NSURLResponse, NSURL};
use objc2_web_kit::{WKDownload, WKNavigationAction, WKNavigationResponse, WKWebView};
use objc2_web_kit::{WKDownload, WKNavigationAction, WKNavigationResponse};

#[cfg(target_os = "ios")]
use crate::wkwebview::ios::WKWebView::WKWebView;
#[cfg(target_os = "macos")]
use objc2_web_kit::WKWebView;

use super::class::{
wry_download_delegate::WryDownloadDelegate, wry_navigation_delegate::WryNavigationDelegate,
Expand Down
Loading

0 comments on commit 320b26d

Please sign in to comment.