[v3] Allow message dialogs to be attached to windows. JS dialogs default attached. wml confirm dialog attached.

This commit is contained in:
Lea Anthony
2023-06-23 20:19:29 +10:00
parent 76672facfe
commit 722c3a653c
14 changed files with 63 additions and 19 deletions
+8
View File
@@ -62,6 +62,14 @@ func main() {
dialog.AddButton("No")
dialog.Show()
})
questionMenu.Add("Question (Attached to Window)").OnClick(func(ctx *application.Context) {
dialog := app.QuestionDialog()
dialog.AttachToWindow(app.CurrentWindow())
dialog.SetMessage("No default button")
dialog.AddButton("Yes")
dialog.AddButton("No")
dialog.Show()
})
questionMenu.Add("Question (With Default)").OnClick(func(ctx *application.Context) {
dialog := app.QuestionDialog()
dialog.SetTitle("Quit")
+6
View File
@@ -15,6 +15,8 @@ interface MessageDialogOptions {
Message?: string;
// The buttons to use on the dialog
Buttons?: Button[];
// Whether the dialog is detached from the current window
Detached?: boolean;
}
export interface OpenFileDialogOptions {
@@ -48,6 +50,8 @@ export interface OpenFileDialogOptions {
ButtonText?: string;
// The default directory to open the dialog in
Directory?: string;
// Whether the dialog is detached from the current window
Detached?: boolean;
}
export interface FileFilter {
// The display name for the filter, e.g. "Text Files"
@@ -76,6 +80,8 @@ export interface SaveFileDialogOptions {
Filename?: string;
// The label for the select button
ButtonText?: string;
// Whether the dialog is detached from the current window
Detached?: boolean;
}
export interface Screen {
+2 -2
View File
@@ -75,7 +75,7 @@ export function Info(options) {
}
/**
* Shows an Warning dialog with the given options.
* Shows a Warning dialog with the given options.
* @param {MessageDialogOptions} options
* @returns {Promise<string>} The label of the button pressed
*/
@@ -94,7 +94,7 @@ export function Error(options) {
/**
* Shows a Question dialog with the given options.
* @param {MessageDialogOptions} options
* @param {MessageDialogOptions} options} options
* @returns {Promise<string>} The label of the button pressed
*/
export function Question(options) {
+1 -1
View File
@@ -16,7 +16,7 @@ function addWMLEventListeners() {
let callback = function () {
if (confirm) {
Question({Title: "Confirm", Message:confirm, Buttons:[{Label:"Yes"},{Label:"No", IsDefault:true}]}).then(function (result) {
Question({Title: "Confirm", Message:confirm, Detached: false, Buttons:[{Label:"Yes"},{Label:"No", IsDefault:true}]}).then(function (result) {
if (result !== "No") {
sendEvent(eventType);
}
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
+6
View File
@@ -76,6 +76,7 @@ type MessageDialogOptions struct {
Message string
Buttons []*Button
Icon []byte
window *WebviewWindow
}
type MessageDialog struct {
@@ -131,6 +132,11 @@ func (d *MessageDialog) AddButtons(buttons []*Button) *MessageDialog {
return d
}
func (d *MessageDialog) AttachToWindow(window *WebviewWindow) *MessageDialog {
d.window = window
return d
}
func (d *MessageDialog) SetDefaultButton(button *Button) *MessageDialog {
for _, b := range d.Buttons {
b.IsDefault = false
+15 -4
View File
@@ -67,9 +67,16 @@ static void* createAlert(int alertType, char* title, char *message, void *icon,
}
// Run the dialog
static int dialogRunModal(void *dialog) {
static int dialogRunModal(void *dialog, void *parent) {
NSAlert *alert = (__bridge NSAlert *)dialog;
long response = [alert runModal];
long response;
if( parent != NULL ) {
NSWindow *window = (__bridge NSWindow *)parent;
response = [alert runModalSheetForWindow:window];
} else {
response = [alert runModal];
}
int result;
if( response == NSAlertFirstButtonReturn ) {
@@ -343,13 +350,17 @@ func (m *macosDialog) show() {
iconLength = C.int(len(globalApplication.options.Icon))
}
}
if m.dialog.window != nil {
// get NSWindow from window
nsWindow = m.dialog.window.impl.(*macosWebviewWindow).nsWindow
}
alertType, ok := alertTypeMap[m.dialog.DialogType]
if !ok {
alertType = C.NSAlertStyleInformational
}
m.nsDialog = C.createAlert(alertType, title, message, iconData, iconLength)
m.nsDialog = C.createAlert(alertType, title, message, iconData, iconLength, nsWindow)
// Reverse the Buttons so that the default is on the right
reversedButtons := make([]*Button, len(m.dialog.Buttons))
@@ -361,7 +372,7 @@ func (m *macosDialog) show() {
count++
}
buttonPressed := int(C.dialogRunModal(m.nsDialog))
buttonPressed := int(C.dialogRunModal(m.nsDialog, nsWindow))
if len(m.dialog.Buttons) > buttonPressed {
button := reversedButtons[buttonPressed]
if button.Callback != nil {
+11 -2
View File
@@ -36,11 +36,20 @@ func (m *windowsDialog) show() {
flags := calculateMessageDialogFlags(m.dialog.MessageDialogOptions)
var button int32
var parentWindow uintptr
var err error
if m.dialog.window != nil {
parentWindow, err = m.dialog.window.NativeWindowHandle()
if err != nil {
w32.Fatal(err.Error())
}
}
if m.UseAppIcon {
// 3 is the application icon
button, _ = w32.MessageBoxWithIcon(0, message, title, 3, windows.MB_OK|windows.MB_USERICON)
button, _ = w32.MessageBoxWithIcon(parentWindow, message, title, 3, windows.MB_OK|windows.MB_USERICON)
} else {
button, _ = windows.MessageBox(windows.HWND(0), message, title, flags|windows.MB_SYSTEMMODAL)
button, _ = windows.MessageBox(windows.HWND(parentWindow), message, title, flags|windows.MB_SYSTEMMODAL)
}
// This maps MessageBox return values to strings
responses := []string{"", "Ok", "Cancel", "Abort", "Retry", "Ignore", "Yes", "No", "", "", "Try Again", "Continue"}
@@ -54,7 +54,11 @@ func (m *MessageProcessor) processDialogMethod(method string, rw http.ResponseWr
case "Question":
dialog = globalApplication.QuestionDialog()
}
// TODO: Add support for attaching Message dialogs to windows
var detached = params.Bool("Detached")
if detached == nil || !*detached {
dialog.AttachToWindow(window)
}
dialog.SetTitle(options.Title)
dialog.SetMessage(options.Message)
for _, button := range options.Buttons {