mirror of
https://github.com/wailsapp/wails.git
synced 2025-05-21 19:39:29 +08:00
[windows] Improve Dialog Message API
This commit is contained in:
parent
fd40faabe8
commit
cf7d31e432
@ -10,10 +10,12 @@ import "C"
|
|||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"github.com/harry1453/go-common-file-dialog/cfd"
|
"github.com/harry1453/go-common-file-dialog/cfd"
|
||||||
|
"golang.org/x/sys/windows"
|
||||||
"log"
|
"log"
|
||||||
"runtime"
|
"runtime"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
"syscall"
|
||||||
|
|
||||||
"github.com/wailsapp/wails/v2/pkg/options/dialog"
|
"github.com/wailsapp/wails/v2/pkg/options/dialog"
|
||||||
|
|
||||||
@ -148,7 +150,7 @@ func (c *Client) OpenFileDialog(options *dialog.OpenDialog, callbackID string) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
thisdialog.SetParentWindowHandle(uintptr(C.GetWindowHandle(c.app.app)))
|
//thisdialog.SetParentWindowHandle(uintptr(C.GetWindowHandle(c.app.app)))
|
||||||
defer func(thisdialog cfd.OpenFileDialog) {
|
defer func(thisdialog cfd.OpenFileDialog) {
|
||||||
err := thisdialog.Release()
|
err := thisdialog.Release()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -180,7 +182,7 @@ func (c *Client) OpenDirectoryDialog(dialogOptions *dialog.OpenDialog, callbackI
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal()
|
log.Fatal()
|
||||||
}
|
}
|
||||||
thisDialog.SetParentWindowHandle(uintptr(C.GetWindowHandle(c.app.app)))
|
//thisDialog.SetParentWindowHandle(uintptr(C.GetWindowHandle(c.app.app)))
|
||||||
defer func(thisDialog cfd.SelectFolderDialog) {
|
defer func(thisDialog cfd.SelectFolderDialog) {
|
||||||
err := thisDialog.Release()
|
err := thisDialog.Release()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -208,8 +210,7 @@ func (c *Client) OpenMultipleFilesDialog(dialogOptions *dialog.OpenDialog, callb
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
handle := uintptr(C.GetWindowHandle(c.app.app))
|
//thisdialog.SetParentWindowHandle(uintptr(C.GetWindowHandle(c.app.app)))
|
||||||
thisdialog.SetParentWindowHandle(handle)
|
|
||||||
defer func(thisdialog cfd.OpenMultipleFilesDialog) {
|
defer func(thisdialog cfd.OpenMultipleFilesDialog) {
|
||||||
err := thisdialog.Release()
|
err := thisdialog.Release()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -248,32 +249,36 @@ func (c *Client) SaveDialog(dialogOptions *dialog.SaveDialog, callbackID string)
|
|||||||
}
|
}
|
||||||
|
|
||||||
// MessageDialog will open a message dialog with the given options
|
// MessageDialog will open a message dialog with the given options
|
||||||
func (c *Client) MessageDialog(dialogOptions *dialog.MessageDialog, callbackID string) {
|
func (c *Client) MessageDialog(options *dialog.MessageDialog, callbackID string) {
|
||||||
|
|
||||||
// Sanity check button length
|
title, err := syscall.UTF16PtrFromString(options.Title)
|
||||||
if len(dialogOptions.Buttons) > 4 {
|
if err != nil {
|
||||||
c.app.logger.Error("Given %d message dialog buttons. Maximum is 4", len(dialogOptions.Buttons))
|
log.Fatal(err)
|
||||||
return
|
}
|
||||||
|
message, err := syscall.UTF16PtrFromString(options.Message)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
var flags uint32
|
||||||
|
switch options.Type {
|
||||||
|
case dialog.InfoDialog:
|
||||||
|
flags = windows.MB_OK | windows.MB_ICONINFORMATION
|
||||||
|
case dialog.ErrorDialog:
|
||||||
|
flags = windows.MB_ICONERROR | windows.MB_OK
|
||||||
|
case dialog.QuestionDialog:
|
||||||
|
flags = windows.MB_YESNO
|
||||||
|
case dialog.WarningDialog:
|
||||||
|
flags = windows.MB_OK | windows.MB_ICONWARNING
|
||||||
}
|
}
|
||||||
|
|
||||||
// Process buttons
|
button, _ := windows.MessageBox(0, message, title, flags|windows.MB_SYSTEMMODAL)
|
||||||
buttons := []string{"", "", "", ""}
|
// This maps MessageBox return values to strings
|
||||||
for i, button := range dialogOptions.Buttons {
|
responses := []string{"", "Ok", "Cancel", "Abort", "Retry", "Ignore", "Yes", "No", "", "", "Try Again", "Continue"}
|
||||||
buttons[i] = button
|
result := "Error"
|
||||||
|
if int(button) < len(responses) {
|
||||||
|
result = responses[button]
|
||||||
}
|
}
|
||||||
|
dispatcher.DispatchMessage("DM" + callbackID + "|" + result)
|
||||||
C.MessageDialog(c.app.app,
|
|
||||||
c.app.string2CString(callbackID),
|
|
||||||
c.app.string2CString(string(dialogOptions.Type)),
|
|
||||||
c.app.string2CString(dialogOptions.Title),
|
|
||||||
c.app.string2CString(dialogOptions.Message),
|
|
||||||
c.app.string2CString(dialogOptions.Icon),
|
|
||||||
c.app.string2CString(buttons[0]),
|
|
||||||
c.app.string2CString(buttons[1]),
|
|
||||||
c.app.string2CString(buttons[2]),
|
|
||||||
c.app.string2CString(buttons[3]),
|
|
||||||
c.app.string2CString(dialogOptions.DefaultButton),
|
|
||||||
c.app.string2CString(dialogOptions.CancelButton))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Client) DarkModeEnabled(callbackID string) {
|
func (c *Client) DarkModeEnabled(callbackID string) {
|
||||||
|
Loading…
Reference in New Issue
Block a user