From 7d61971f86b82a2421474a7cbef021be8580fb8d Mon Sep 17 00:00:00 2001 From: stffabi Date: Thu, 15 Dec 2022 11:18:13 +0100 Subject: [PATCH] [darwin] Add support for file input selection (#2209) This will show a message on the console "+[CATransaction synchronize] called within transaction" but there's seem no way to mitigate that. Still everything works as expected. --- .../frontend/desktop/darwin/WailsContext.h | 2 +- .../frontend/desktop/darwin/WailsContext.m | 22 ++++++++++++++++++- website/src/pages/changelog.mdx | 1 + 3 files changed, 23 insertions(+), 2 deletions(-) diff --git a/v2/internal/frontend/desktop/darwin/WailsContext.h b/v2/internal/frontend/desktop/darwin/WailsContext.h index 0db6e61d9..b177e4037 100644 --- a/v2/internal/frontend/desktop/darwin/WailsContext.h +++ b/v2/internal/frontend/desktop/darwin/WailsContext.h @@ -29,7 +29,7 @@ - (void) disableWindowConstraints; @end -@interface WailsContext : NSObject +@interface WailsContext : NSObject @property (retain) WailsWindow* mainWindow; @property (retain) WKWebView* webview; diff --git a/v2/internal/frontend/desktop/darwin/WailsContext.m b/v2/internal/frontend/desktop/darwin/WailsContext.m index b36aafccc..c47fe12e5 100644 --- a/v2/internal/frontend/desktop/darwin/WailsContext.m +++ b/v2/internal/frontend/desktop/darwin/WailsContext.m @@ -250,7 +250,8 @@ } [self.webview setNavigationDelegate:self]; - + self.webview.UIDelegate = self; + NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; [defaults setBool:FALSE forKey:@"NSAutomaticQuoteSubstitutionEnabled"]; @@ -407,6 +408,25 @@ [self.webview evaluateJavaScript:script completionHandler:nil]; } +- (void)webView:(WKWebView *)webView runOpenPanelWithParameters:(WKOpenPanelParameters *)parameters + initiatedByFrame:(WKFrameInfo *)frame completionHandler:(void (^)(NSArray * URLs))completionHandler { + + NSOpenPanel *openPanel = [NSOpenPanel openPanel]; + openPanel.allowsMultipleSelection = parameters.allowsMultipleSelection; + if (@available(macOS 10.14, *)) { + openPanel.canChooseDirectories = parameters.allowsDirectories; + } + + [openPanel + beginSheetModalForWindow:webView.window + completionHandler:^(NSInteger result) { + if (result == NSModalResponseOK) + completionHandler(openPanel.URLs); + else + completionHandler(nil); + }]; +} + - (void) processURLResponse:(unsigned long long)requestId :(int)statusCode :(NSData *)headersJSON :(NSData *)data { NSNumber *key = [NSNumber numberWithUnsignedLongLong:requestId]; diff --git a/website/src/pages/changelog.mdx b/website/src/pages/changelog.mdx index dd212ae80..f213150c1 100644 --- a/website/src/pages/changelog.mdx +++ b/website/src/pages/changelog.mdx @@ -22,6 +22,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - The `WindowSetAlwaysOnTop` method is now exposed in the JS runtime. Fixed by @gotid in [PR](https://github.com/wailsapp/wails/pull/2128) - The [AssetServer](/docs/reference/options#assetserver) now supports serving the index.html file when requesting a directory. Added by @stffabi in [PR](https://github.com/wailsapp/wails/pull/2110) - Added support for WebKit2GTK 2.36+ on Linux. This brings additional features for the [AssetServer](/docs/reference/options#assetserver), like support for HTTP methods and Headers. The app must be compiled with the Go build tag `webkit2_36` to activate support for this features. This also bumps the minimum requirement of WebKit2GTK to 2.36 for your app. Fixed by @stffabi in this [PR](https://github.com/wailsapp/wails/pull/2151) +- Added support for file input selection on macOS. Added by @stffabi in [PR](https://github.com/wailsapp/wails/pull/2209) ### Fixed - The `noreload` flag in wails dev wasn't applied. Fixed by @stffabi in this [PR](https://github.com/wailsapp/wails/pull/2081)