diff --git a/exp/examples/dialogs/main.go b/exp/examples/dialogs/main.go index 1f1a8361b..d6c40a3a1 100644 --- a/exp/examples/dialogs/main.go +++ b/exp/examples/dialogs/main.go @@ -3,6 +3,7 @@ package main import ( _ "embed" "log" + "strings" "github.com/wailsapp/wails/exp/pkg/application" ) @@ -139,7 +140,7 @@ func main() { openMenu.Add("Open File").OnClick(func(ctx *application.Context) { result, _ := app.NewOpenFileDialog(). CanChooseFiles(true). - Show() + PromptForSingleFile() if result != "" { app.NewInfoDialog().SetMessage(result).Show() } else { @@ -151,29 +152,42 @@ func main() { CanChooseFiles(true). CanCreateDirectories(true). ShowHiddenFiles(true). - Show() + PromptForSingleFile() if result != "" { app.NewInfoDialog().SetMessage(result).Show() } else { app.NewInfoDialog().SetMessage("No file selected").Show() } }) - //openMenu.Add("Open Multiple Files (Show Hidden Files)").OnClick(func(ctx *application.Context) { - // result, _ := app.NewOpenMultipleFilesDialog(). - // CanChooseFiles(true). - // CanCreateDirectories(true). - // ShowHiddenFiles(true). - // Show() - // if len(result) > 0 { - // app.NewInfoDialog().SetMessage(strings.Join(result, ",")).Show() - // } else { - // app.NewInfoDialog().SetMessage("No file selected").Show() - // } - //}) + openMenu.Add("Open File (Attach to window)").OnClick(func(ctx *application.Context) { + result, _ := app.NewOpenFileDialog(). + CanChooseFiles(true). + CanCreateDirectories(true). + ShowHiddenFiles(true). + AttachToWindow(app.GetCurrentWindow()). + PromptForSingleFile() + if result != "" { + app.NewInfoDialog().SetMessage(result).Show() + } else { + app.NewInfoDialog().SetMessage("No file selected").Show() + } + }) + openMenu.Add("Open Multiple Files (Show Hidden Files)").OnClick(func(ctx *application.Context) { + result, _ := app.NewOpenFileDialog(). + CanChooseFiles(true). + CanCreateDirectories(true). + ShowHiddenFiles(true). + PromptForMultipleFiles() + if len(result) > 0 { + app.NewInfoDialog().SetMessage(strings.Join(result, ",")).Show() + } else { + app.NewInfoDialog().SetMessage("No file selected").Show() + } + }) openMenu.Add("Open Directory").OnClick(func(ctx *application.Context) { result, _ := app.NewOpenFileDialog(). CanChooseDirectories(true). - Show() + PromptForSingleFile() if result != "" { app.NewInfoDialog().SetMessage(result).Show() } else { @@ -184,7 +198,7 @@ func main() { result, _ := app.NewOpenFileDialog(). CanChooseDirectories(true). CanCreateDirectories(true). - Show() + PromptForSingleFile() if result != "" { app.NewInfoDialog().SetMessage(result).Show() } else { @@ -194,6 +208,7 @@ func main() { app.SetMenu(menu) + app.NewWindow() app.NewWindow() err := app.Run() diff --git a/exp/pkg/application/dialogs.go b/exp/pkg/application/dialogs.go index 680cf7898..6309f2f6c 100644 --- a/exp/pkg/application/dialogs.go +++ b/exp/pkg/application/dialogs.go @@ -124,6 +124,7 @@ type OpenFileDialog struct { canCreateDirectories bool showHiddenFiles bool allowsMultipleSelection bool + window *Window impl openFileDialogImpl } @@ -148,12 +149,31 @@ func (d *OpenFileDialog) ShowHiddenFiles(showHiddenFiles bool) *OpenFileDialog { return d } -func (d *OpenFileDialog) Show() (string, error) { +func (d *OpenFileDialog) AttachToWindow(window *Window) *OpenFileDialog { + d.window = window + return d +} + +func (d *OpenFileDialog) PromptForSingleFile() (string, error) { + d.allowsMultipleSelection = false if d.impl == nil { d.impl = newOpenFileDialogImpl(d) } - result, err := d.impl.show() - return result[0], err + selection, err := d.impl.show() + var result string + if len(selection) > 0 { + result = selection[0] + } + + return result, err +} + +func (d *OpenFileDialog) PromptForMultipleFiles() ([]string, error) { + d.allowsMultipleSelection = true + if d.impl == nil { + d.impl = newOpenFileDialogImpl(d) + } + return d.impl.show() } func newOpenFileDialog() *OpenFileDialog { diff --git a/exp/pkg/application/dialogs_darwin.go b/exp/pkg/application/dialogs_darwin.go index 0c619231a..67eb8406c 100644 --- a/exp/pkg/application/dialogs_darwin.go +++ b/exp/pkg/application/dialogs_darwin.go @@ -102,7 +102,27 @@ static void alertAddButton(void *dialog, char *label, bool isDefault, bool isCan } } -static void showOpenFileDialog(unsigned int dialogID, bool canChooseFiles, bool canChooseDirectories, bool canCreateDirectories, bool showHiddenFiles, bool allowsMultipleSelection) { +static void processOpenFileDialogResults(NSOpenPanel *panel, NSInteger result, bool allowsMultipleSelection, uint dialogID) { + const char *path = NULL; + if (result == NSModalResponseOK) { + if (allowsMultipleSelection) { + NSArray *urls = [panel URLs]; + for (NSURL *url in urls) { + path = [[url path] UTF8String]; + openFileDialogCallback(dialogID, (char *)path); + } + + } else { + NSURL *url = [panel URL]; + path = [[url path] UTF8String]; + openFileDialogCallback(dialogID, (char *)path); + } + } + openFileDialogCallbackEnd(dialogID); +} + + +static void showOpenFileDialog(unsigned int dialogID, bool canChooseFiles, bool canChooseDirectories, bool canCreateDirectories, bool showHiddenFiles, bool allowsMultipleSelection, void *window) { // run on main thread dispatch_async(dispatch_get_main_queue(), ^{ @@ -114,26 +134,15 @@ static void showOpenFileDialog(unsigned int dialogID, bool canChooseFiles, bool [panel setShowsHiddenFiles:showHiddenFiles]; [panel setAllowsMultipleSelection:allowsMultipleSelection]; - - // Show panel - [panel beginWithCompletionHandler:^(NSInteger result) { - const char *path = NULL; - if (result == NSModalResponseOK) { - if (allowsMultipleSelection) { - NSArray *urls = [panel URLs]; - for (NSURL *url in urls) { - path = [[url path] UTF8String]; - openFileDialogCallback(dialogID, (char *)path); - } - - } else { - NSURL *url = [panel URL]; - path = [[url path] UTF8String]; - openFileDialogCallback(dialogID, (char *)path); - } - } - openFileDialogCallbackEnd(dialogID); - }]; + if (window != NULL) { + [panel beginSheetModalForWindow:(__bridge NSWindow *)window completionHandler:^(NSInteger result) { + processOpenFileDialogResults(panel, result, allowsMultipleSelection, dialogID); + }]; + } else { + [panel beginWithCompletionHandler:^(NSInteger result) { + processOpenFileDialogResults(panel, result, allowsMultipleSelection, dialogID); + }]; + } }); } @@ -246,12 +255,18 @@ func newOpenFileDialogImpl(d *OpenFileDialog) *macosOpenFileDialog { func (m *macosOpenFileDialog) show() ([]string, error) { openFileResponses[dialogID] = make(chan string) + nsWindow := unsafe.Pointer(nil) + if m.dialog.window != nil { + // get NSWindow from window + nsWindow = m.dialog.window.impl.(*macosWindow).nsWindow + } C.showOpenFileDialog(C.uint(m.dialog.id), C.bool(m.dialog.canChooseFiles), C.bool(m.dialog.canChooseDirectories), C.bool(m.dialog.canCreateDirectories), C.bool(m.dialog.showHiddenFiles), - C.bool(m.dialog.allowsMultipleSelection)) + C.bool(m.dialog.allowsMultipleSelection), + nsWindow) var result []string for filename := range openFileResponses[m.dialog.id] { result = append(result, filename) diff --git a/exp/tasks/png2bytes/macos_template_icon.png b/exp/tasks/png2bytes/macos_template_icon.png deleted file mode 100644 index f30e111d4..000000000 Binary files a/exp/tasks/png2bytes/macos_template_icon.png and /dev/null differ