zenity/zenity.go

118 lines
2.5 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
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-03 21:52:05 -05:00
title *string
2021-02-12 09:24:13 -05:00
width uint
height uint
2020-01-04 22:21:39 -05:00
// 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
2021-03-03 21:52:05 -05:00
okLabel *string
cancelLabel *string
extraButton *string
2020-01-24 07:52:45 -05:00
noWrap bool
ellipsize bool
defaultCancel bool
2020-01-28 07:46:43 -05:00
2021-03-04 21:01:59 -05:00
// Entry options
entryText string
hideText 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 {
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
})
}
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-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 })
}