mirror of
https://github.com/wailsapp/wails.git
synced 2026-03-16 15:45:50 +01:00
112 lines
3.1 KiB
Go
112 lines
3.1 KiB
Go
package application
|
|
|
|
import (
|
|
"github.com/wailsapp/wails/v3/pkg/errs"
|
|
)
|
|
|
|
const (
|
|
DialogInfo = 0
|
|
DialogWarning = 1
|
|
DialogError = 2
|
|
DialogQuestion = 3
|
|
DialogOpenFile = 4
|
|
DialogSaveFile = 5
|
|
)
|
|
|
|
var dialogMethodNames = map[int]string{
|
|
DialogInfo: "Info",
|
|
DialogWarning: "Warning",
|
|
DialogError: "Error",
|
|
DialogQuestion: "Question",
|
|
DialogOpenFile: "OpenFile",
|
|
DialogSaveFile: "SaveFile",
|
|
}
|
|
|
|
func (m *MessageProcessor) processDialogMethod(req *RuntimeRequest, window Window) (any, error) {
|
|
args := req.Args.AsMap()
|
|
|
|
switch req.Method {
|
|
case DialogInfo, DialogWarning, DialogError, DialogQuestion:
|
|
var options MessageDialogOptions
|
|
err := req.Args.ToStruct(&options)
|
|
if err != nil {
|
|
return nil, errs.WrapInvalidDialogCallErrorf(err, "error parsing dialog options")
|
|
}
|
|
if len(options.ButtonList) == 0 {
|
|
options.ButtonList = []*Button{{Label: "OK", IsDefault: true, IsCancel: true}}
|
|
}
|
|
var dialog *MessageDialog
|
|
switch req.Method {
|
|
case DialogInfo:
|
|
dialog = newMessageDialog(InfoDialogType)
|
|
case DialogWarning:
|
|
dialog = newMessageDialog(WarningDialogType)
|
|
case DialogError:
|
|
dialog = newMessageDialog(ErrorDialogType)
|
|
case DialogQuestion:
|
|
dialog = newMessageDialog(QuestionDialogType)
|
|
}
|
|
var detached = args.Bool("Detached")
|
|
if detached == nil || !*detached {
|
|
dialog.AttachToWindow(window)
|
|
}
|
|
|
|
dialog.SetTitle(options.Title)
|
|
dialog.SetMessage(options.Message)
|
|
dialog.ButtonList = options.ButtonList
|
|
|
|
response, err := dialog.Result()
|
|
if err != nil {
|
|
return nil, errs.WrapInvalidDialogCallErrorf(err, "Dialog.%s failed: error getting selection", dialogMethodNames[req.Method])
|
|
}
|
|
return response, nil
|
|
|
|
case DialogOpenFile:
|
|
var options OpenFileDialogOptions
|
|
err := req.Args.ToStruct(&options)
|
|
if err != nil {
|
|
return nil, errs.WrapInvalidDialogCallErrorf(err, "error parsing dialog options")
|
|
}
|
|
var detached = args.Bool("Detached")
|
|
if detached == nil || !*detached {
|
|
options.Window = window
|
|
}
|
|
dialog := globalApplication.Dialog.OpenFileWithOptions(&options)
|
|
|
|
if options.AllowsMultipleSelection {
|
|
files, err := dialog.PromptForMultipleSelection()
|
|
if err != nil {
|
|
return nil, errs.WrapInvalidDialogCallErrorf(err, "Dialog.OpenFile failed: error getting selection")
|
|
}
|
|
|
|
return files, nil
|
|
} else {
|
|
file, err := dialog.PromptForSingleSelection()
|
|
if err != nil {
|
|
return nil, errs.WrapInvalidDialogCallErrorf(err, "Dialog.OpenFile failed, error getting selection")
|
|
}
|
|
return file, nil
|
|
}
|
|
|
|
case DialogSaveFile:
|
|
var options SaveFileDialogOptions
|
|
err := req.Args.ToStruct(&options)
|
|
if err != nil {
|
|
return nil, errs.WrapInvalidDialogCallErrorf(err, "error parsing dialog options")
|
|
}
|
|
var detached = args.Bool("Detached")
|
|
if detached == nil || !*detached {
|
|
options.Window = window
|
|
}
|
|
dialog := globalApplication.Dialog.SaveFileWithOptions(&options)
|
|
|
|
file, err := dialog.PromptForSingleSelection()
|
|
if err != nil {
|
|
return nil, errs.WrapInvalidDialogCallErrorf(err, "Dialog.SaveFile failed: error getting selection")
|
|
}
|
|
return file, nil
|
|
|
|
default:
|
|
return nil, errs.NewInvalidDialogCallErrorf("unknown method: %d", req.Method)
|
|
}
|
|
}
|