Skip to content
Messages and confirmation

Messages and confirmation

Information and errors

dialog.ShowInformation shows a title, a message and an OK button. dialog.ShowError takes an error instead. Its title is “Error”, and the message is the error’s text with the first letter capitalised. Do not pass a nil error.

info := widget.NewButton("Information", func() {
	dialog.ShowInformation("Saved", "Your changes were saved.", w)
})
failure := widget.NewButton("Error", func() {
	dialog.ShowError(errors.New("the disk is full"), w)
})

A message dialog sizes itself to its text. It is never wider than 600 units, nor wider than 90% of the parent window, and longer text wraps.

Asking for confirmation

dialog.NewConfirm shows No and Yes buttons. The callback receives true when the user chose Yes, and false when they chose No or dismissed the dialog in any other way, for example with the Escape key.

confirm := widget.NewButton("Delete…", func() {
	d := dialog.NewConfirm("Delete file?", "This cannot be undone.",
		func(ok bool) {
			if ok {
				status.SetText("Deleted")
			} else {
				status.SetText("Kept")
			}
		}, w)
	d.SetConfirmText("Delete")
	d.SetDismissText("Keep")
	d.SetConfirmImportance(widget.DangerImportance)
	d.Show()
})

The returned ConfirmDialog lets you relabel both buttons with SetConfirmText and SetDismissText. Use SetConfirmImportance to change how prominent the confirm button looks; it is widget.HighImportance by default, and widget.DangerImportance suits a destructive action. You can also close the dialog from code with Confirm() (as if the user chose yes) or Dismiss() (as if they chose no).

Options every dialog shares

The Dialog interface is implemented by all the dialogs in the package. Besides Show, Hide and Resize, it has:

MethodWhat it does
SetOnClosed(func())Runs your function whenever the dialog closes, whatever the answer. Calling it twice keeps both functions.
SetDismissText(string)Relabels the dismiss button.
Dismiss()Closes the dialog without an affirmative answer.
SetWindowed(bool)Windowed (the default) gives the dialog a frame the user can drag by its title bar and resize from its corners. Pass false for a fixed, centred dialog. Call it before Show.
SetWindowHosted(bool)Opens the dialog in its own OS window instead of inside the parent window. Call it before Show.

Dialogs in their own window

By default a dialog is drawn inside the parent window, over its content. With SetWindowHosted(true) it opens in a separate window that the operating system can move and resize, and that appears in the window switcher. While it is open the parent window is covered by a scrim (a translucent layer) that blocks input; tapping the scrim brings the dialog window to the front. Your app’s other windows keep working.

hosted := widget.NewButton("In its own window", func() {
	d := dialog.NewInformation("Report ready", "The report has been generated.", w)
	d.SetWindowHosted(true) // a separate OS window on desktop
	d.SetOnClosed(func() { status.SetText("Report dialog closed") })
	d.Show()
})

Closing the dialog window, with its close button or the Escape key, counts as dismissing the dialog. On mobile and in the browser an app has only one window, so the setting is ignored there and the dialog is drawn inside the window as usual.

The complete example

// Information, error and confirmation dialogs.
package main

import (
	"errors"

	"github.com/belluikit/bellui"
	"github.com/belluikit/bellui/app"
	"github.com/belluikit/bellui/container"
	"github.com/belluikit/bellui/dialog"
	"github.com/belluikit/bellui/widget"
)

func main() {
	a := app.New()
	w := a.NewWindow("Dialogs")
	status := widget.NewLabel("Nothing chosen yet")

	info := widget.NewButton("Information", func() {
		dialog.ShowInformation("Saved", "Your changes were saved.", w)
	})
	failure := widget.NewButton("Error", func() {
		dialog.ShowError(errors.New("the disk is full"), w)
	})

	confirm := widget.NewButton("Delete…", func() {
		d := dialog.NewConfirm("Delete file?", "This cannot be undone.",
			func(ok bool) {
				if ok {
					status.SetText("Deleted")
				} else {
					status.SetText("Kept")
				}
			}, w)
		d.SetConfirmText("Delete")
		d.SetDismissText("Keep")
		d.SetConfirmImportance(widget.DangerImportance)
		d.Show()
	})

	hosted := widget.NewButton("In its own window", func() {
		d := dialog.NewInformation("Report ready", "The report has been generated.", w)
		d.SetWindowHosted(true) // a separate OS window on desktop
		d.SetOnClosed(func() { status.SetText("Report dialog closed") })
		d.Show()
	})

	w.SetContent(container.NewVBox(info, failure, confirm, hosted, status))
	bellui.ShowAndRun(w)
}