zenity/zenity.go

139 lines
3.1 KiB
Go
Raw Permalink Normal View History

2020-01-23 06:44:28 -05:00
// Package zenity provides cross-platform access to simple dialogs that interact
// graphically with the user.
//
// It is inspired by, and closely follows the API of, the zenity program, which
// it uses to provide the functionality on various Unixes. See:
//
2020-01-26 11:04:49 -05:00
// https://help.gnome.org/users/zenity/stable/
2020-01-23 06:44:28 -05:00
//
// This package does not require cgo, and it does not impose any threading or
// initialization requirements.
2020-01-04 22:21:39 -05:00
package zenity
2020-01-28 07:46:43 -05:00
import (
"context"
"image/color"
)
2020-01-18 07:40:16 -05:00
2021-03-03 21:52:05 -05:00
type stringErr string
2020-01-06 07:01:51 -05:00
2021-03-03 21:52:05 -05:00
func (e stringErr) Error() string { return string(e) }
func stringPtr(s string) *string { return &s }
2020-01-06 07:01:51 -05:00
2020-01-04 22:21:39 -05:00
type options struct {
// General options
2021-03-05 10:14:30 -05:00
title *string
width uint
height uint
okLabel *string
cancelLabel *string
extraButton *string
icon DialogIcon
// Entry options
entryText string
hideText bool
2021-03-05 21:07:00 -05:00
username bool
2021-03-05 10:14:30 -05:00
// Message options
noWrap bool
ellipsize bool
defaultCancel bool
2020-01-04 22:21:39 -05:00
2021-04-07 09:16:35 -04:00
// List options
disallowEmpty bool
defaultItems []string
2020-01-04 22:21:39 -05:00
// File selection options
2020-01-24 07:52:45 -05:00
directory bool
confirmOverwrite bool
confirmCreate bool
showHidden bool
2021-03-05 10:14:30 -05:00
filename string
2021-04-11 22:59:08 -04:00
fileFilters FileFilters
2020-01-04 22:21:39 -05:00
2020-01-18 07:40:16 -05:00
// Color selection options
2020-01-24 07:52:45 -05:00
color color.Color
showPalette bool
2020-01-18 07:40:16 -05:00
2020-01-28 07:46:43 -05:00
// Context for timeout
ctx context.Context
2020-01-04 22:21:39 -05:00
}
2020-01-23 06:44:28 -05:00
// An Option is an argument passed to dialog functions to customize their
// behavior.
2020-01-24 07:52:45 -05:00
type Option interface {
apply(*options)
}
type funcOption func(*options)
func (f funcOption) apply(o *options) { f(o) }
2020-01-04 22:21:39 -05:00
2020-01-24 07:52:45 -05:00
func applyOptions(options []Option) (res options) {
2020-01-04 22:21:39 -05:00
for _, o := range options {
2020-01-24 07:52:45 -05:00
o.apply(&res)
2020-01-04 22:21:39 -05:00
}
return
}
2020-01-23 06:44:28 -05:00
// Title returns an Option to set the dialog title.
2020-01-04 22:21:39 -05:00
func Title(title string) Option {
2021-03-03 21:52:05 -05:00
return funcOption(func(o *options) { o.title = &title })
2020-01-04 22:21:39 -05:00
}
2020-01-27 10:42:43 -05:00
2021-02-12 09:24:13 -05:00
// Width returns an Option to set the dialog width (Unix only).
func Width(width uint) Option {
return funcOption(func(o *options) {
o.width = width
})
}
// Height returns an Option to set the dialog height (Unix only).
func Height(height uint) Option {
return funcOption(func(o *options) {
o.height = height
})
}
2021-03-05 10:14:30 -05:00
// OKLabel returns an Option to set the label of the OK button.
func OKLabel(ok string) Option {
return funcOption(func(o *options) { o.okLabel = &ok })
}
// CancelLabel returns an Option to set the label of the Cancel button.
func CancelLabel(cancel string) Option {
return funcOption(func(o *options) { o.cancelLabel = &cancel })
}
// ExtraButton returns an Option to add an extra button.
func ExtraButton(extra string) Option {
return funcOption(func(o *options) { o.extraButton = &extra })
}
2020-01-27 10:42:43 -05:00
// DialogIcon is the enumeration for dialog icons.
type DialogIcon int
2020-01-30 09:14:42 -05:00
// The stock dialog icons.
2020-01-27 10:42:43 -05:00
const (
ErrorIcon DialogIcon = iota + 1
WarningIcon
InfoIcon
QuestionIcon
2021-03-05 10:14:30 -05:00
PasswordIcon
2021-03-04 08:06:49 -05:00
NoIcon
2020-01-27 10:42:43 -05:00
)
// Icon returns an Option to set the dialog icon.
func Icon(icon DialogIcon) Option {
return funcOption(func(o *options) { o.icon = icon })
}
2020-01-28 07:46:43 -05:00
2020-01-29 09:15:21 -05:00
// Context returns an Option to set a Context that can dismiss the dialog.
2020-01-30 09:14:42 -05:00
//
// Dialogs dismissed by the Context return Context.Err.
2020-01-28 07:46:43 -05:00
func Context(ctx context.Context) Option {
return funcOption(func(o *options) { o.ctx = ctx })
}