Skip to content

Commit 5e39aed

Browse files
leaanthonyclaude
andcommitted
feat(v3): MessageDialog.Show() returns clicked button label
Make MessageDialog.Show() return the clicked button's label as a string, enabling synchronous dialog workflows. This allows developers to use switch statements on the result instead of callbacks for decision-making flows. All platforms now consistently block until the user clicks a button and return the label. Button callbacks are still called for backwards compatibility. Closes #4792 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
1 parent a13a426 commit 5e39aed

File tree

7 files changed

+33
-9
lines changed

7 files changed

+33
-9
lines changed

v3/UNRELEASED_CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ After processing, the content will be moved to the main changelog and this file
2020

2121
## Changed
2222
<!-- Changes in existing functionality -->
23+
- `MessageDialog.Show()` now returns the clicked button's label as a string, enabling synchronous dialog workflows (#4792)
2324

2425
## Fixed
2526
<!-- Bug fixes -->

v3/pkg/application/dialogs.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ func (b *Button) SetAsCancel() *Button {
6666
}
6767

6868
type messageDialogImpl interface {
69-
show()
69+
show() string
7070
}
7171

7272
type MessageDialogOptions struct {
@@ -106,11 +106,14 @@ func (d *MessageDialog) SetTitle(title string) *MessageDialog {
106106
return d
107107
}
108108

109-
func (d *MessageDialog) Show() {
109+
// Show displays the dialog and returns the label of the clicked button.
110+
// This method blocks until the user clicks a button.
111+
// If the dialog has button callbacks defined, they will still be called.
112+
func (d *MessageDialog) Show() string {
110113
if d.impl == nil {
111114
d.impl = newDialogImpl(d)
112115
}
113-
InvokeSync(d.impl.show)
116+
return InvokeSyncWithResult(d.impl.show)
114117
}
115118

116119
func (d *MessageDialog) SetIcon(icon []byte) *MessageDialog {

v3/pkg/application/dialogs_android.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,9 @@ type androidDialog struct {
6565
dialog *MessageDialog
6666
}
6767

68-
func (d *androidDialog) show() {
68+
func (d *androidDialog) show() string {
6969
// TODO: Implement using AlertDialog
70+
return ""
7071
}
7172

7273
func newDialogImpl(d *MessageDialog) *androidDialog {

v3/pkg/application/dialogs_darwin.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -371,7 +371,10 @@ type macosDialog struct {
371371
nsDialog unsafe.Pointer
372372
}
373373

374-
func (m *macosDialog) show() {
374+
func (m *macosDialog) show() string {
375+
// Channel to receive the result
376+
resultChan := make(chan string, 1)
377+
375378
InvokeAsync(func() {
376379

377380
// Mac can only have 4 Buttons on a dialog
@@ -429,19 +432,24 @@ func (m *macosDialog) show() {
429432

430433
var callBackID int
431434
callBackID = addDialogCallback(func(buttonPressed int) {
435+
var buttonLabel string
432436
if len(m.dialog.Buttons) > buttonPressed {
433437
button := reversedButtons[buttonPressed]
438+
buttonLabel = button.Label
434439
if button.Callback != nil {
435440
button.Callback()
436441
}
437442
}
438443
removeDialogCallback(callBackID)
444+
resultChan <- buttonLabel
439445
})
440446

441447
C.dialogRunModal(m.nsDialog, parent, C.int(callBackID))
442448

443449
})
444450

451+
// Wait for and return the result
452+
return <-resultChan
445453
}
446454

447455
func newDialogImpl(d *MessageDialog) *macosDialog {

v3/pkg/application/dialogs_ios.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,9 @@ type iosDialog struct {
6565
dialog *MessageDialog
6666
}
6767

68-
func (d *iosDialog) show() {
68+
func (d *iosDialog) show() string {
6969
// TODO: Implement using UIAlertController
70+
return ""
7071
}
7172

7273
func newDialogImpl(d *MessageDialog) *iosDialog {
@@ -87,4 +88,4 @@ func newOpenFileDialogImpl(_ *OpenFileDialogStruct) openFileDialogImpl {
8788

8889
func newSaveFileDialogImpl(_ *SaveFileDialogStruct) saveFileDialogImpl {
8990
return &dialogsImpl{}
90-
}
91+
}

v3/pkg/application/dialogs_linux.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ type linuxDialog struct {
2727
dialog *MessageDialog
2828
}
2929

30-
func (m *linuxDialog) show() {
30+
func (m *linuxDialog) show() string {
3131
windowId := getNativeApplication().getCurrentWindowID()
3232
window, _ := globalApplication.Window.GetByID(windowId)
3333
var parent uintptr
@@ -38,18 +38,27 @@ func (m *linuxDialog) show() {
3838
}
3939
}
4040

41+
// Channel to receive the result
42+
resultChan := make(chan string, 1)
43+
4144
InvokeAsync(func() {
4245
response := runQuestionDialog(pointer(parent), m.dialog)
46+
var buttonLabel string
4347
if response >= 0 && response < len(m.dialog.Buttons) {
4448
button := m.dialog.Buttons[response]
49+
buttonLabel = button.Label
4550
if button.Callback != nil {
4651
go func() {
4752
defer handlePanic()
4853
button.Callback()
4954
}()
5055
}
5156
}
57+
resultChan <- buttonLabel
5258
})
59+
60+
// Wait for and return the result
61+
return <-resultChan
5362
}
5463

5564
func newDialogImpl(d *MessageDialog) *linuxDialog {

v3/pkg/application/dialogs_windows.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ type windowsDialog struct {
3030
UseAppIcon bool
3131
}
3232

33-
func (m *windowsDialog) show() {
33+
func (m *windowsDialog) show() string {
3434

3535
title := w32.MustStringToUTF16Ptr(m.dialog.Title)
3636
message := w32.MustStringToUTF16Ptr(m.dialog.Message)
@@ -72,6 +72,7 @@ func (m *windowsDialog) show() {
7272
}
7373
}
7474
}
75+
return result
7576
}
7677

7778
func newDialogImpl(d *MessageDialog) *windowsDialog {

0 commit comments

Comments
 (0)