zenity/zenity.go

95 lines
2.1 KiB
Go
Raw 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
2020-01-06 07:01:51 -05:00
type constError string
func (e constError) Error() string { return string(e) }
2020-01-04 22:21:39 -05:00
type options struct {
// General options
title string
// File selection options
2020-01-24 07:52:45 -05:00
filename string
directory bool
confirmOverwrite bool
confirmCreate bool
showHidden bool
fileFilters []FileFilter
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-04 22:21:39 -05:00
// Message options
2020-01-27 10:42:43 -05:00
icon DialogIcon
2020-01-24 07:52:45 -05:00
okLabel string
cancelLabel string
extraButton string
noWrap bool
ellipsize bool
defaultCancel bool
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 {
2020-01-24 07:52:45 -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
// 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
)
// 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 })
}