Files

167 lines
4.3 KiB
Go
Raw Permalink Normal View History

2023-02-06 20:50:11 +11:00
package application
import (
"encoding/json"
"fmt"
"net/http"
"runtime"
)
2023-08-24 20:24:08 +10:00
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",
}
2023-09-28 11:38:59 -05:00
func (m *MessageProcessor) dialogErrorCallback(window Window, message string, dialogID *string, err error) {
2023-02-06 20:50:11 +11:00
errorMsg := fmt.Sprintf(message, err)
m.Error(errorMsg)
2023-09-28 11:38:59 -05:00
window.DialogError(*dialogID, errorMsg)
2023-02-06 20:50:11 +11:00
}
2023-09-28 11:38:59 -05:00
func (m *MessageProcessor) dialogCallback(window Window, dialogID *string, result string, isJSON bool) {
window.DialogResponse(*dialogID, result, isJSON)
2023-02-06 20:50:11 +11:00
}
2023-09-28 11:38:59 -05:00
func (m *MessageProcessor) processDialogMethod(method int, rw http.ResponseWriter, r *http.Request, window Window, params QueryParams) {
2023-02-06 20:50:11 +11:00
args, err := params.Args()
if err != nil {
2023-09-08 12:03:55 +10:00
m.httpError(rw, "Unable to parse arguments: %s", err.Error())
2023-02-06 20:50:11 +11:00
return
}
dialogID := args.String("dialog-id")
if dialogID == nil {
m.Error("dialog-id is required")
return
}
2023-08-24 20:24:08 +10:00
var methodName = "Dialog." + dialogMethodNames[method]
2023-02-06 20:50:11 +11:00
switch method {
2023-08-24 20:24:08 +10:00
case DialogInfo, DialogWarning, DialogError, DialogQuestion:
2023-02-06 20:50:11 +11:00
var options MessageDialogOptions
err := params.ToStruct(&options)
if err != nil {
m.dialogErrorCallback(window, "Error parsing dialog options: %s", dialogID, err)
2023-02-06 20:50:11 +11:00
return
}
if len(options.Buttons) == 0 {
switch runtime.GOOS {
case "darwin":
options.Buttons = []*Button{{Label: "OK", IsDefault: true}}
}
}
var dialog *MessageDialog
switch method {
2023-08-24 20:24:08 +10:00
case DialogInfo:
2023-06-24 13:57:51 +10:00
dialog = InfoDialog()
2023-08-24 20:24:08 +10:00
case DialogWarning:
2023-06-24 13:57:51 +10:00
dialog = WarningDialog()
2023-08-24 20:24:08 +10:00
case DialogError:
2023-06-24 13:57:51 +10:00
dialog = ErrorDialog()
2023-08-24 20:24:08 +10:00
case DialogQuestion:
2023-06-24 13:57:51 +10:00
dialog = QuestionDialog()
2023-02-06 20:50:11 +11:00
}
var detached = args.Bool("Detached")
if detached == nil || !*detached {
dialog.AttachToWindow(window)
}
2023-02-06 20:50:11 +11:00
dialog.SetTitle(options.Title)
dialog.SetMessage(options.Message)
for _, button := range options.Buttons {
label := button.Label
button.OnClick(func() {
m.dialogCallback(window, dialogID, label, false)
2023-02-06 20:50:11 +11:00
})
}
dialog.AddButtons(options.Buttons)
dialog.Show()
m.ok(rw)
m.Info("Runtime Call:", "method", methodName, "options", options)
2023-08-18 09:35:03 +10:00
2023-08-24 20:24:08 +10:00
case DialogOpenFile:
2023-02-06 20:50:11 +11:00
var options OpenFileDialogOptions
err := params.ToStruct(&options)
if err != nil {
m.httpError(rw, "Error parsing dialog options: %s", err.Error())
return
}
var detached = args.Bool("Detached")
if detached == nil || !*detached {
2023-09-29 11:29:31 -05:00
options.Window = window.(*WebviewWindow)
}
2023-06-24 13:57:51 +10:00
dialog := OpenFileDialogWithOptions(&options)
2023-02-06 20:50:11 +11:00
go func() {
if options.AllowsMultipleSelection {
files, err := dialog.PromptForMultipleSelection()
if err != nil {
m.dialogErrorCallback(window, "Error getting selection: %s", dialogID, err)
2023-02-06 20:50:11 +11:00
return
} else {
result, err := json.Marshal(files)
if err != nil {
m.dialogErrorCallback(window, "Error marshalling files: %s", dialogID, err)
2023-02-06 20:50:11 +11:00
return
}
m.dialogCallback(window, dialogID, string(result), true)
m.Info("Runtime Call:", "method", methodName, "result", result)
2023-02-06 20:50:11 +11:00
}
} else {
file, err := dialog.PromptForSingleSelection()
if err != nil {
m.dialogErrorCallback(window, "Error getting selection: %s", dialogID, err)
2023-02-06 20:50:11 +11:00
return
}
m.dialogCallback(window, dialogID, file, false)
m.Info("Runtime Call:", "method", methodName, "result", file)
2023-02-06 20:50:11 +11:00
}
}()
m.ok(rw)
m.Info("Runtime Call:", "method", methodName, "options", options)
2023-08-18 09:35:03 +10:00
2023-08-24 20:24:08 +10:00
case DialogSaveFile:
2023-02-06 20:50:11 +11:00
var options SaveFileDialogOptions
err := params.ToStruct(&options)
if err != nil {
m.httpError(rw, "Error parsing dialog options: %s", err.Error())
return
}
var detached = args.Bool("Detached")
if detached == nil || !*detached {
2023-09-29 12:13:51 -05:00
options.Window = window.(*WebviewWindow)
}
2023-06-24 13:57:51 +10:00
dialog := SaveFileDialogWithOptions(&options)
2023-02-06 20:50:11 +11:00
go func() {
file, err := dialog.PromptForSingleSelection()
if err != nil {
m.dialogErrorCallback(window, "Error getting selection: %s", dialogID, err)
2023-02-06 20:50:11 +11:00
return
}
m.dialogCallback(window, dialogID, file, false)
m.Info("Runtime Call:", "method", methodName, "result", file)
2023-02-06 20:50:11 +11:00
}()
m.ok(rw)
m.Info("Runtime Call:", "method", methodName, "options", options)
2023-02-06 20:50:11 +11:00
default:
2023-09-08 12:03:55 +10:00
m.httpError(rw, "Unknown dialog method: %d", method)
2023-02-06 20:50:11 +11:00
}
}