mirror of
https://github.com/wailsapp/wails.git
synced 2026-03-14 14:45:49 +01:00
OpenDialog attach to window & multiple files selection
This commit is contained in:
parent
b1e8f2f887
commit
ea1f086289
4 changed files with 91 additions and 41 deletions
|
|
@ -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()
|
||||
|
|
|
|||
|
|
@ -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 {
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
|
|
|
|||
Binary file not shown.
|
Before Width: | Height: | Size: 760 B |
Loading…
Add table
Add a link
Reference in a new issue