Skip to Content
Onevra

Dialog

Modal panels for any content, and alert dialogs that ask for a decision.

Loading the live example…

dialog.go
import (
	"github.com/egoist/mygo/ui"
	"github.com/oneo-me/onevra/source/components"
)

type renameDialog struct {
	open, discard bool
	name, draft   string
}

func (d *renameDialog) View(c *ui.Context) {
	t := c.Theme()
	ui.Column(c).Fill().Center().Gap(t.Space(3)).Children(func() {
		components.Text(c, d.name).FontSize(t.Rem(1.2)).Bold()
		if components.Button(c, "Rename…", components.ButtonSecondary).Clicked() {
			d.draft, d.open = d.name, true
		}
	})
	components.Dialog(c, &d.open, components.DialogOptions{Label: "Rename"}, func(panel ui.Element) {
		panel.Width(340)
		components.Text(c, "Rename document").Bold()
		components.TextBox(c, &d.draft, components.TextBoxOptions{}).Label("Name").FillWidth()
		ui.Row(c).FillWidth().Gap(8).Justify(ui.End).Children(func() {
			if components.Button(c, "Cancel", components.ButtonGhost).Clicked() {
				// A nested AlertDialog opens over this one.
				d.discard = d.draft != d.name
				d.open = d.discard
			}
			if components.Button(c, "Save", components.ButtonPrimary).Clicked() {
				d.name, d.open = d.draft, false
			}
		})
		if components.AlertDialog(c, &d.discard, components.AlertDialogOptions{
			Title:        "Discard the new name?",
			Message:      "The document keeps its current name.",
			ConfirmLabel: "Discard",
			CancelLabel:  "Keep editing",
		}) == components.DialogConfirmed {
			d.open = false
		}
	})
}
Source

components.Dialog builds content in a modal panel while its *bool is true. Call it every frame; MyGo traps focus inside and restores it on close. Dialogs nest: build layers back to front, and the page underneath stays open. Clicking the backdrop or pressing Escape dismisses a layer unless its options disable that.

Alert dialogs

components.AlertDialog asks a question with confirm and cancel buttons and returns the choice in the frame it is made. It never blocks the UI thread; store the result if it is needed later. HideCancel makes a one-button notice.

Loading the live example…

alert_dialog.go
import (
	"github.com/egoist/mygo/ui"
	"github.com/oneo-me/onevra/source/components"
)

type confirmDelete struct {
	open   bool
	status string
}

func (d *confirmDelete) View(c *ui.Context) {
	ui.Column(c).Fill().Center().Gap(12).Children(func() {
		components.Text(c, d.status).TextColor(c.Theme().TextMuted)
		if components.Button(c, "Delete files", components.ButtonSecondary).Clicked() {
			d.open = true
		}
	})
	// The result arrives in the frame of the choice; the call never blocks.
	switch components.AlertDialog(c, &d.open, components.AlertDialogOptions{
		Title:        "Delete 3 files?",
		Message:      "This cannot be undone.",
		ConfirmLabel: "Delete",
		CancelLabel:  "Cancel",
	}) {
	case components.DialogConfirmed:
		d.status = "Deleted"
	case components.DialogCancelled:
		d.status = "Kept the files"
	}
}
Source

API

Dialog

func Dialog(c *ui.Context, open *bool, options DialogOptions, build func(panel ui.Element)) ui.Element

Dialog builds arbitrary content in a modal panel and returns its frame-local handle (absent when closed). Applications own open and call Dialog every frame. Build layers in back-to-front order, with stable keyed parents for independent dialogs. Nested calls are supported. MyGo owns focus trapping and restoration. The panel is horizontally centered with spare vertical space biased below it. Its default maximum size leaves 20 logical pixels clear on every viewport edge; Inset overrides the clearance; WindowDragHeight can increase it. Content may customize the panel and should provide scrolling for long pages. The returned panel does not report Dismissed; use DisableBackdropClose and DisableEscapeClose to keep a layer open.

pkg.go.dev

DialogOptions

DialogOptions controls dismissal independently for each layer. The zero value allows backdrop clicks and Escape to dismiss the dialog.

Prop

Type

pkg.go.dev

AlertDialog

func AlertDialog(c *ui.Context, open *bool, options AlertDialogOptions) DialogResult

AlertDialog is a preset Dialog returning confirmation or cancellation. Programmatically closing open does not emit a result.

pkg.go.dev

AlertDialogOptions

AlertDialogOptions supplies localized text and optional cancellation. ConfirmLabel must be supplied. HideCancel produces a one-button notice; backdrop and Escape dismissal still return DialogCancelled unless disabled.

Prop

Type

pkg.go.dev

DialogResult

type DialogResult uint8

DialogResult is emitted only in the frame of a user action. Store it in application state if needed later; this UI-thread API never blocks.

const (
	DialogNone DialogResult = iota
	DialogConfirmed
	DialogCancelled
)
pkg.go.dev

WithDialogWindowDrag

func WithDialogWindowDrag(c *ui.Context, height float32, build func())

WithDialogWindowDrag supplies the default native window drag strip height in DIPs for Dialog and AlertDialog calls made synchronously by build. Shell installs this scope automatically; standalone hosts may use it too. Non-positive heights disable the default. Nested scopes replace the height and restore it on return. This does not add layout or forward pointer events. Call during the UI build; do not retain c or launch asynchronous builders.

pkg.go.dev
Edit this page on GitHub

On this page