sync/utils/utils.go
2025-05-19 18:26:52 -04:00

57 lines
1.5 KiB
Go

package utils
import (
"context"
"github.com/wailsapp/wails/v2/pkg/runtime"
)
// A base type for all message box options.
// This is a functional option pattern, so you can pass in any number of
// options to customize the message box.
type MessageBoxOption func(*runtime.MessageDialogOptions)
// WithDialogType allows you to set the dialog type for the message box.
// The default is InfoDialog.
func WithDialogType(dt runtime.DialogType) MessageBoxOption {
return func(o *runtime.MessageDialogOptions) {
o.Type = dt
}
}
// Allows you to build a custom set of buttons for the dialog.
// The default is a single “OK” button.
func WithButtons(btns []string) MessageBoxOption {
return func(o *runtime.MessageDialogOptions) {
o.Buttons = btns
}
}
// BuildMessageBoxOpts builds the options struct, defaulting to
// an Info dialog and a single “OK” button. Pass WithDialogType()
// and/or WithButtons() to customize.
func BuildMessageBoxOpts(title, message string, opts ...MessageBoxOption) runtime.MessageDialogOptions {
// defaults:
o := runtime.MessageDialogOptions{
Title: title,
Message: message,
Type: runtime.InfoDialog,
Buttons: []string{"OK"},
}
// apply any overrides:
for _, opt := range opts {
opt(&o)
}
return o
}
// ShowMessageBox shows a message box with the given options.
func ShowMessageBox(ctx context.Context, title, message string, opts ...MessageBoxOption) {
// Build the options
options := BuildMessageBoxOpts(title, message, opts...)
// Show the message box
runtime.MessageDialog(ctx, options)
}