2020-01-04 22:21:39 -05:00
|
|
|
package zenity
|
|
|
|
|
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
|
|
|
|
filters []FileFilter
|
|
|
|
|
|
|
|
// Message options
|
|
|
|
icon MessageIcon
|
|
|
|
ok string
|
|
|
|
cancel string
|
|
|
|
extra string
|
|
|
|
nowrap bool
|
|
|
|
ellipsize bool
|
|
|
|
defcancel bool
|
|
|
|
}
|
|
|
|
|
2020-01-10 07:00:38 -05:00
|
|
|
// Options are arguments you pass 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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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, make 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-10 07:00:38 -05:00
|
|
|
// Option to activate directory-only selection.
|
2020-01-09 20:46:53 -05:00
|
|
|
func Directory(o *options) {
|
|
|
|
o.directory = true
|
|
|
|
}
|
|
|
|
|
2020-01-10 07:00:38 -05:00
|
|
|
// Option to confirm file selection if filename already exists.
|
2020-01-04 22:21:39 -05:00
|
|
|
func ConfirmOverwrite(o *options) {
|
|
|
|
o.overwrite = true
|
|
|
|
}
|
|
|
|
|
2020-01-10 07:00:38 -05:00
|
|
|
// FileFilter encapsulates a filename filter.
|
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
|
|
|
}
|
|
|
|
|
2020-01-10 07:00:38 -05:00
|
|
|
// FileFilters is a list of filename filters.
|
|
|
|
//
|
|
|
|
// macOS hides filename filters from the user,
|
|
|
|
// and only supports filtering by extension (or "type").
|
|
|
|
// We make an effort to convert any "*.EXT" like patterns.
|
2020-01-04 22:21:39 -05:00
|
|
|
type FileFilters []FileFilter
|
|
|
|
|
2020-01-10 07:00:38 -05:00
|
|
|
// Creates Option to set the filename filter list.
|
2020-01-04 22:21:39 -05:00
|
|
|
func (f FileFilters) New() Option {
|
|
|
|
return func(o *options) {
|
|
|
|
o.filters = f
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
|
InfoIcon
|
|
|
|
QuestionIcon
|
|
|
|
WarningIcon
|
|
|
|
)
|
|
|
|
|
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-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-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-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-10 07:00:38 -05:00
|
|
|
// Option to disable enable text wrapping.
|
2020-01-04 22:21:39 -05:00
|
|
|
func NoWrap(o *options) {
|
|
|
|
o.nowrap = true
|
|
|
|
}
|
|
|
|
|
2020-01-10 07:00:38 -05:00
|
|
|
// Option to enable ellipsizing in the dialog text.
|
2020-01-04 22:21:39 -05:00
|
|
|
func Ellipsize(o *options) {
|
|
|
|
o.ellipsize = true
|
|
|
|
}
|
|
|
|
|
2020-01-10 07:00:38 -05:00
|
|
|
// Option to give Cancel button focus by default.
|
2020-01-04 22:21:39 -05:00
|
|
|
func DefaultCancel(o *options) {
|
|
|
|
o.defcancel = true
|
|
|
|
}
|