Skip to content

Commit

Permalink
fix: support file upload panel on MacOS(fix tauri-apps#305)
Browse files Browse the repository at this point in the history
  • Loading branch information
keiya01 committed Apr 8, 2022
1 parent 36b985e commit cb2e4d3
Showing 1 changed file with 46 additions and 2 deletions.
48 changes: 46 additions & 2 deletions src/webview/wkwebview/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ pub use web_context::WebContextImpl;
#[cfg(target_os = "macos")]
use cocoa::appkit::{NSView, NSViewHeightSizable, NSViewWidthSizable};
use cocoa::{
base::{id, YES},
base::{id, nil, YES},
foundation::{NSDictionary, NSFastEnumeration, NSInteger},
};

Expand All @@ -26,7 +26,7 @@ use std::{
use core_graphics::geometry::{CGPoint, CGRect, CGSize};
use objc::{
declare::ClassDecl,
runtime::{Class, Object, Sel},
runtime::{Class, Object, Sel, BOOL},
};
use objc_id::Id;

Expand Down Expand Up @@ -357,6 +357,50 @@ impl InnerWebView {
null_mut()
};

// File upload panel handler
extern "C" fn run_file_upload_panel(
_this: &Object,
_: Sel,
_webview: id,
open_panel_params: id,
_frame: id,
handler: id,
) {
unsafe {
let handler = handler as *mut block::Block<(id,), c_void>;
let cls = class!(NSOpenPanel);
let open_panel: id = msg_send![cls, openPanel];
let _: () = msg_send![open_panel, setCanChooseFiles: YES];

let allow_multi: BOOL = msg_send![open_panel_params, allowsMultipleSelection];
let _: () = msg_send![open_panel, setAllowsMultipleSelection: allow_multi];

let allow_dir: BOOL = msg_send![open_panel_params, allowsDirectories];
let _: () = msg_send![open_panel, setCanChooseDirectories: allow_dir];

let ok: NSInteger = msg_send![open_panel, runModal];
if ok == 1 {
let url: id = msg_send![open_panel, URLs];
(*handler).call((url,));
} else {
(*handler).call((nil,));
}
}
}

let ui_delegate = match ClassDecl::new("WebViewUIDelegate", class!(NSObject)) {
Some(mut ctl) => {
ctl.add_method(
sel!(webView:runOpenPanelWithParameters:initiatedByFrame:completionHandler:),
run_file_upload_panel as extern "C" fn(&Object, Sel, id, id, id, id),
);
ctl.register()
}
None => class!(UIFileUploadPanelController),
};
let ui_delegate: id = msg_send![ui_delegate, new];
let _: () = msg_send![webview, setUIDelegate: ui_delegate];

// File drop handling
#[cfg(target_os = "macos")]
let file_drop_ptr = match attributes.file_drop_handler {
Expand Down

0 comments on commit cb2e4d3

Please sign in to comment.