5
0
mirror of https://github.com/wailsapp/wails.git synced 2025-05-03 04:29:35 +08:00

feat: add dialog filter for Cocoa

This commit is contained in:
Florian Didron 2020-06-17 19:09:09 +09:00 committed by Lea Anthony
parent bc570999e8
commit 7522428d5c
5 changed files with 35 additions and 15 deletions

View File

@ -17,9 +17,9 @@ type Renderer interface {
NotifyEvent(eventData *messages.EventData) error NotifyEvent(eventData *messages.EventData) error
// Dialog Runtime // Dialog Runtime
SelectFile(filter string) string SelectFile(title string, filter string) string
SelectDirectory() string SelectDirectory() string
SelectSaveFile(filter string) string SelectSaveFile(title string, filter string) string
// Window Runtime // Window Runtime
SetColour(string) error SetColour(string) error

View File

@ -114,7 +114,7 @@ func (h *Bridge) NewBinding(methodName string) error {
// SelectFile is unsupported for Bridge but required // SelectFile is unsupported for Bridge but required
// for the Renderer interface // for the Renderer interface
func (h *Bridge) SelectFile(filter string) string { func (h *Bridge) SelectFile(title string, filter string) string {
h.log.Warn("SelectFile() unsupported in bridge mode") h.log.Warn("SelectFile() unsupported in bridge mode")
return "" return ""
} }
@ -128,7 +128,7 @@ func (h *Bridge) SelectDirectory() string {
// SelectSaveFile is unsupported for Bridge but required // SelectSaveFile is unsupported for Bridge but required
// for the Renderer interface // for the Renderer interface
func (h *Bridge) SelectSaveFile(filter string) string { func (h *Bridge) SelectSaveFile(title string, filter string) string {
h.log.Warn("SelectSaveFile() unsupported in bridge mode") h.log.Warn("SelectSaveFile() unsupported in bridge mode")
return "" return ""
} }

View File

@ -245,7 +245,7 @@ func (w *WebView) NewBinding(methodName string) error {
} }
// SelectFile opens a dialog that allows the user to select a file // SelectFile opens a dialog that allows the user to select a file
func (w *WebView) SelectFile(filter string) string { func (w *WebView) SelectFile(title string, filter string) string {
var result string var result string
// We need to run this on the main thread, however Dispatch is // We need to run this on the main thread, however Dispatch is
@ -255,7 +255,7 @@ func (w *WebView) SelectFile(filter string) string {
wg.Add(1) wg.Add(1)
go func() { go func() {
w.window.Dispatch(func() { w.window.Dispatch(func() {
result = w.window.Dialog(wv.DialogTypeOpen, 0, "Select File", "", filter) result = w.window.Dialog(wv.DialogTypeOpen, 0, title, "", filter)
wg.Done() wg.Done()
}) })
}() }()
@ -282,7 +282,7 @@ func (w *WebView) SelectDirectory() string {
} }
// SelectSaveFile opens a dialog that allows the user to select a file to save // SelectSaveFile opens a dialog that allows the user to select a file to save
func (w *WebView) SelectSaveFile(filter string) string { func (w *WebView) SelectSaveFile(title string, filter string) string {
var result string var result string
// We need to run this on the main thread, however Dispatch is // We need to run this on the main thread, however Dispatch is
// non-blocking so we launch this in a goroutine and wait for // non-blocking so we launch this in a goroutine and wait for
@ -291,7 +291,7 @@ func (w *WebView) SelectSaveFile(filter string) string {
wg.Add(1) wg.Add(1)
go func() { go func() {
w.window.Dispatch(func() { w.window.Dispatch(func() {
result = w.window.Dialog(wv.DialogTypeSave, 0, "Save file", "", filter) result = w.window.Dialog(wv.DialogTypeSave, 0, title, "", filter)
wg.Done() wg.Done()
}) })
}() }()

View File

@ -1838,7 +1838,7 @@ struct webview_priv
int i=0; int i=0;
char* token; char* token;
char* filter_dup = strdup(filter); char* filter_dup = strdup(filter);
for (count=1; filter[count]; filter[count]==',' ? count++ : *filter++); for (count=1; filter[count]; filter[count]==',' ? count++ : *filter++);
COMDLG_FILTERSPEC rgSpec[count]; COMDLG_FILTERSPEC rgSpec[count];
char* filters[count]; char* filters[count];
token = strtok(filter_dup, ","); token = strtok(filter_dup, ",");
@ -1976,7 +1976,7 @@ struct webview_priv
(struct webview *)objc_getAssociatedObject(self, "webview"); (struct webview *)objc_getAssociatedObject(self, "webview");
webview_dialog(w, WEBVIEW_DIALOG_TYPE_OPEN, WEBVIEW_DIALOG_FLAG_FILE, "", "", webview_dialog(w, WEBVIEW_DIALOG_TYPE_OPEN, WEBVIEW_DIALOG_FLAG_FILE, "", "",
filename, 255); filename, 255, "");
filename[255] = '\0'; filename[255] = '\0';
if (strlen(filename) > 0) if (strlen(filename) > 0)
{ {
@ -2239,12 +2239,16 @@ struct webview_priv
WEBVIEW_API void webview_dialog(struct webview *w, WEBVIEW_API void webview_dialog(struct webview *w,
enum webview_dialog_type dlgtype, int flags, enum webview_dialog_type dlgtype, int flags,
const char *title, const char *arg, const char *title, const char *arg,
char *result, size_t resultsz) char *result, size_t resultsz, char *filter)
{ {
if (dlgtype == WEBVIEW_DIALOG_TYPE_OPEN || if (dlgtype == WEBVIEW_DIALOG_TYPE_OPEN ||
dlgtype == WEBVIEW_DIALOG_TYPE_SAVE) dlgtype == WEBVIEW_DIALOG_TYPE_SAVE)
{ {
NSSavePanel *panel; NSSavePanel *panel;
NSString *filter_str = [NSString stringWithUTF8String:filter];
filter_str = [filter_str stringByReplacingOccurrencesOfString:@"*."
withString:@""];
NSArray *fileTypes = [filter_str componentsSeparatedByString:@","];
if (dlgtype == WEBVIEW_DIALOG_TYPE_OPEN) if (dlgtype == WEBVIEW_DIALOG_TYPE_OPEN)
{ {
NSOpenPanel *openPanel = [NSOpenPanel openPanel]; NSOpenPanel *openPanel = [NSOpenPanel openPanel];
@ -2257,6 +2261,10 @@ struct webview_priv
{ {
[openPanel setCanChooseFiles:YES]; [openPanel setCanChooseFiles:YES];
[openPanel setCanChooseDirectories:NO]; [openPanel setCanChooseDirectories:NO];
if(filter[0] != NULL)
{
[openPanel setAllowedFileTypes:fileTypes];
}
} }
[openPanel setResolvesAliases:NO]; [openPanel setResolvesAliases:NO];
[openPanel setAllowsMultipleSelection:NO]; [openPanel setAllowsMultipleSelection:NO];
@ -2270,6 +2278,10 @@ struct webview_priv
[panel setShowsHiddenFiles:YES]; [panel setShowsHiddenFiles:YES];
[panel setExtensionHidden:NO]; [panel setExtensionHidden:NO];
[panel setCanSelectHiddenExtension:NO]; [panel setCanSelectHiddenExtension:NO];
if(filter[0] != NULL)
{
[panel setAllowedFileTypes:fileTypes];
}
[panel setTreatsFilePackagesAsDirectories:YES]; [panel setTreatsFilePackagesAsDirectories:YES];
[panel beginSheetModalForWindow:w->priv.window [panel beginSheetModalForWindow:w->priv.window
completionHandler:^(NSInteger result) { completionHandler:^(NSInteger result) {

View File

@ -16,11 +16,15 @@ func NewDialog(renderer interfaces.Renderer) *Dialog {
// SelectFile prompts the user to select a file // SelectFile prompts the user to select a file
func (r *Dialog) SelectFile(params ...string) string { func (r *Dialog) SelectFile(params ...string) string {
title := "Select File"
filter := "" filter := ""
if len(params) > 0 { if len(params) > 0 {
filter = params[0] title = params[0]
} }
return r.renderer.SelectFile(filter) if len(params) > 1 {
filter = params[1]
}
return r.renderer.SelectFile(title, filter)
} }
// SelectDirectory prompts the user to select a directory // SelectDirectory prompts the user to select a directory
@ -30,9 +34,13 @@ func (r *Dialog) SelectDirectory() string {
// SelectSaveFile prompts the user to select a file for saving // SelectSaveFile prompts the user to select a file for saving
func (r *Dialog) SelectSaveFile(params ...string) string { func (r *Dialog) SelectSaveFile(params ...string) string {
title := "Select Save"
filter := "" filter := ""
if len(params) > 0 { if len(params) > 0 {
filter = params[0] title = params[0]
} }
return r.renderer.SelectSaveFile(filter) if len(params) > 1 {
filter = params[1]
}
return r.renderer.SelectSaveFile(title, filter)
} }