zenity/zenity.go

171 lines
3.8 KiB
Go
Raw Normal View History

2020-01-04 22:21:39 -05:00
package zenity
2020-01-18 07:40:16 -05:00
import "image/color"
2020-01-06 07:01:51 -05:00
// Errors
type constError string
func (e constError) Error() string { return string(e) }
// Message errors
const ErrExtraButton = constError("Extra button pressed.")
// Options
2020-01-04 22:21:39 -05:00
type options struct {
// General options
title string
// File selection options
filename string
2020-01-09 20:46:53 -05:00
directory bool
2020-01-04 22:21:39 -05:00
overwrite bool
create bool
hidden bool
2020-01-04 22:21:39 -05:00
filters []FileFilter
2020-01-18 07:40:16 -05:00
// Color selection options
color color.Color
palette bool
2020-01-04 22:21:39 -05:00
// Message options
icon MessageIcon
ok string
cancel string
extra string
nowrap bool
ellipsize bool
defcancel bool
}
// Options are arguments passed to dialog functions to customize their behavior.
2020-01-04 22:21:39 -05:00
type Option func(*options)
func optsParse(options []Option) (res options) {
for _, o := range options {
o(&res)
}
return
}
// General options
2020-01-10 07:00:38 -05:00
// Option to set the dialog title.
2020-01-04 22:21:39 -05:00
func Title(title string) Option {
return func(o *options) { o.title = title }
2020-01-04 22:21:39 -05:00
}
// File selection options
2020-01-10 07:00:38 -05:00
// Option to set the filename.
//
// You can specify a file name, a directory path, or both.
// Specifying a file name, makes it the default selected file.
// Specifying a directory path, makes it the default dialog location.
2020-01-04 22:21:39 -05:00
func Filename(filename string) Option {
return func(o *options) { o.filename = filename }
2020-01-04 22:21:39 -05:00
}
2020-01-10 07:00:38 -05:00
// Option to activate directory-only selection.
func Directory() Option {
return func(o *options) { o.directory = true }
2020-01-09 20:46:53 -05:00
}
2020-01-10 07:00:38 -05:00
// Option to confirm file selection if filename already exists.
func ConfirmOverwrite() Option {
return func(o *options) { o.overwrite = true }
}
// Option to confirm file selection if filename does not yet exist (Windows only).
func ConfirmCreate() Option {
return func(o *options) { o.create = true }
}
// Option to show hidden files (Windows and macOS only).
func ShowHidden() Option {
return func(o *options) { o.hidden = true }
2020-01-04 22:21:39 -05:00
}
2020-01-10 07:00:38 -05:00
// FileFilter encapsulates a filename filter.
//
// macOS hides filename filters from the user,
// and only supports filtering by extension (or "type").
2020-01-04 22:21:39 -05:00
type FileFilter struct {
2020-01-10 07:00:38 -05:00
Name string // display string that describes the filter (optional)
Patterns []string // filter patterns for the display string
2020-01-04 22:21:39 -05:00
}
// Build option to set a filename filter.
func (f FileFilter) Build() Option {
return func(o *options) { o.filters = append(o.filters, f) }
}
2020-01-10 07:00:38 -05:00
// FileFilters is a list of filename filters.
2020-01-04 22:21:39 -05:00
type FileFilters []FileFilter
// Build option to set filename filters.
func (f FileFilters) Build() Option {
return func(o *options) { o.filters = append(o.filters, f...) }
2020-01-04 22:21:39 -05:00
}
2020-01-18 07:40:16 -05:00
// Color selection options
2020-01-21 11:13:45 -05:00
// Option to set the color.
2020-01-18 07:40:16 -05:00
func Color(c color.Color) Option {
return func(o *options) { o.color = c }
}
2020-01-21 11:13:45 -05:00
// Option to show the palette.
2020-01-18 07:40:16 -05:00
func ShowPalette() Option {
return func(o *options) { o.palette = true }
}
2020-01-04 22:21:39 -05:00
// Message options
2020-01-10 07:00:38 -05:00
// MessageIcon is the enumeration for message dialog icons.
2020-01-04 22:21:39 -05:00
type MessageIcon int
const (
ErrorIcon MessageIcon = iota + 1
2020-01-12 18:09:11 -05:00
WarningIcon
2020-01-04 22:21:39 -05:00
InfoIcon
QuestionIcon
)
2020-01-10 07:00:38 -05:00
// Option to set the dialog icon.
2020-01-04 22:21:39 -05:00
func Icon(icon MessageIcon) Option {
return func(o *options) { o.icon = icon }
2020-01-04 22:21:39 -05:00
}
2020-01-10 07:00:38 -05:00
// Option to set the label of the OK button.
2020-01-04 22:21:39 -05:00
func OKLabel(ok string) Option {
return func(o *options) { o.ok = ok }
2020-01-04 22:21:39 -05:00
}
2020-01-10 07:00:38 -05:00
// Option to set the label of the Cancel button.
2020-01-04 22:21:39 -05:00
func CancelLabel(cancel string) Option {
return func(o *options) { o.cancel = cancel }
2020-01-04 22:21:39 -05:00
}
2020-01-10 07:00:38 -05:00
// Option to add an extra button.
2020-01-04 22:21:39 -05:00
func ExtraButton(extra string) Option {
return func(o *options) { o.extra = extra }
2020-01-04 22:21:39 -05:00
}
2020-01-10 07:00:38 -05:00
// Option to disable enable text wrapping.
func NoWrap() Option {
return func(o *options) { o.nowrap = true }
2020-01-04 22:21:39 -05:00
}
2020-01-10 07:00:38 -05:00
// Option to enable ellipsizing in the dialog text.
func Ellipsize() Option {
return func(o *options) { o.ellipsize = true }
2020-01-04 22:21:39 -05:00
}
2020-01-10 07:00:38 -05:00
// Option to give Cancel button focus by default.
func DefaultCancel() Option {
return func(o *options) { o.defcancel = true }
2020-01-04 22:21:39 -05:00
}