gui/window: native open and save file dialogs
ShowFileDialog (UI thread) and ChooseFile (any goroutine, waits) show a native dialog for the window -- NSOpenPanel/NSSavePanel as a sheet on macOS, GetOpenFileNameW/GetSaveFileNameW on Windows -- with a title, a start folder, a suggested name and allowed extensions. It only returns the chosen path; one dialog at a time. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
e1495430c5
commit
5d141c7cef
7 files changed
+284
-2
No files matched your search
@@ -9,6 +9,7 @@
|
||||
// marshalled with dispatch_async onto the main queue.
|
||||
#import <Cocoa/Cocoa.h>
|
||||
#import <WebKit/WebKit.h>
|
||||
#import <UniformTypeIdentifiers/UniformTypeIdentifiers.h>
|
||||
#include "darwin.h"
|
||||
|
||||
// Implemented in Go (window_darwin.go).
|
||||
@@ -19,6 +20,7 @@ extern void ytGoDispatch(uintptr_t fn);
|
||||
extern void ytGoClosed(uintptr_t h);
|
||||
extern void ytGoSnapshotDone(uintptr_t h, const char *err);
|
||||
extern void ytGoFrame(uintptr_t h, int x, int y, int w, int hgt, int zoomed);
|
||||
extern void ytGoFileDone(uintptr_t h, const char *path);
|
||||
|
||||
// YTWebView takes the first mouse click in an inactive window: without
|
||||
// it the click only activates the window, so a page (a dialog shown at
|
||||
@@ -327,3 +329,40 @@ void yt_set_app_icon(const void *png, int n) {
|
||||
if (img) NSApp.applicationIconImage = img;
|
||||
}
|
||||
}
|
||||
|
||||
// --- file dialogs ------------------------------------------------------------
|
||||
|
||||
// yt_file_dialog shows an open (save = 0) or save panel as a sheet on the
|
||||
// window. dir: the folder to start in, name: a save panel's suggested file
|
||||
// name, types: allowed file extensions, comma-separated ("" = any). The
|
||||
// chosen path, or NULL when cancelled, goes to ytGoFileDone.
|
||||
void yt_file_dialog(void *h, int save, const char *title, const char *dir, const char *name, const char *types) {
|
||||
YTHost *host = (__bridge YTHost *)h;
|
||||
uintptr_t gh = host.goHandle;
|
||||
NSSavePanel *panel;
|
||||
if (save) {
|
||||
panel = [NSSavePanel savePanel];
|
||||
if (name && name[0]) panel.nameFieldStringValue = str(name);
|
||||
} else {
|
||||
NSOpenPanel *open = [NSOpenPanel openPanel];
|
||||
open.canChooseFiles = YES;
|
||||
open.canChooseDirectories = NO;
|
||||
open.allowsMultipleSelection = NO;
|
||||
open.resolvesAliases = YES;
|
||||
panel = open;
|
||||
}
|
||||
if (title && title[0]) { panel.title = str(title); panel.message = str(title); }
|
||||
if (dir && dir[0]) panel.directoryURL = [NSURL fileURLWithPath:str(dir) isDirectory:YES];
|
||||
if (types && types[0]) {
|
||||
NSMutableArray<UTType *> *uts = [NSMutableArray array];
|
||||
for (NSString *ext in [str(types) componentsSeparatedByString:@","]) {
|
||||
UTType *t = [UTType typeWithFilenameExtension:ext];
|
||||
if (t) [uts addObject:t];
|
||||
}
|
||||
if (uts.count) panel.allowedContentTypes = uts;
|
||||
}
|
||||
[panel beginSheetModalForWindow:host.window completionHandler:^(NSModalResponse r) {
|
||||
if (r != NSModalResponseOK || !panel.URL) { ytGoFileDone(gh, NULL); return; }
|
||||
ytGoFileDone(gh, panel.URL.path.fileSystemRepresentation);
|
||||
}];
|
||||
}
|
||||
Reference in new issue
Block a user