-
-
Notifications
You must be signed in to change notification settings - Fork 37
/
zenity.go
234 lines (197 loc) · 5.72 KB
/
zenity.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
// 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:
//
// https://help.gnome.org/users/zenity/stable/
//
// This package does not require cgo, and it does not impose any threading or
// initialization requirements.
package zenity
import (
"context"
"image/color"
"time"
"github.com/ncruces/zenity/internal/zenutil"
)
func ptr[T any](v T) *T { return &v }
// ErrCanceled is returned when the cancel button is pressed,
// or window functions are used to close the dialog.
const ErrCanceled = zenutil.ErrCanceled
// ErrExtraButton is returned when the extra button is pressed.
const ErrExtraButton = zenutil.ErrExtraButton
// ErrUnsupported is returned when a combination of options is not supported.
const ErrUnsupported = zenutil.ErrUnsupported
// IsAvailable reports whether dependencies of the package are installed.
// It always returns true on Windows and macOS.
func IsAvailable() bool {
return isAvailable()
}
type options struct {
// General options
title *string
width uint
height uint
okLabel *string
cancelLabel *string
extraButton *string
defaultCancel bool
icon any
windowIcon any
attach any
modal bool
display string
class string
name string
// Message options
noWrap bool
ellipsize bool
// Entry options
entryText string
hideText bool
username bool
// List options
listKind listKind
midSearch bool
disallowEmpty bool
defaultItems []string
// Calendar options
time *time.Time
// File selection options
directory bool
confirmOverwrite bool
confirmCreate bool
showHidden bool
filename string
fileFilters FileFilters
// Color selection options
color color.Color
showPalette bool
// Progress indication options
maxValue int
noCancel bool
autoClose bool
timeRemaining bool
// Context for timeout
ctx context.Context
}
// An Option is an argument passed to dialog functions to customize their
// behavior.
type Option interface {
apply(*options)
}
type funcOption func(*options)
func (f funcOption) apply(o *options) { f(o) }
func applyOptions(options []Option) (res options) {
for _, o := range options {
o.apply(&res)
}
return
}
// Title returns an Option to set the dialog title.
func Title(title string) Option {
return funcOption(func(o *options) { o.title = &title })
}
// 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
})
}
// 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 one extra button.
func ExtraButton(extra string) Option {
return funcOption(func(o *options) { o.extraButton = &extra })
}
// DefaultCancel returns an Option to give the Cancel button focus by default.
func DefaultCancel() Option {
return funcOption(func(o *options) { o.defaultCancel = true })
}
// DialogIcon is an Option that sets the dialog icon.
type DialogIcon int
func (i DialogIcon) apply(o *options) {
o.icon = i
}
// The stock dialog icons.
const (
ErrorIcon DialogIcon = iota
WarningIcon
InfoIcon
QuestionIcon
PasswordIcon
NoIcon
)
// Icon returns an Option to set the dialog icon.
//
// Icon accepts a DialogIcon, or a string.
// The string can be a GTK icon name (Unix), or a file path (Windows and macOS).
// Supported file formats depend on the plaftorm, but PNG should be cross-platform.
func Icon(icon any) Option {
switch icon.(type) {
case DialogIcon, string:
default:
panic("interface conversion: expected string or DialogIcon")
}
return funcOption(func(o *options) { o.icon = icon })
}
// WindowIcon returns an Option to set the window icon.
//
// WindowIcon accepts a DialogIcon, or a string file path.
// Supported file formats depend on the plaftorm, but PNG should be cross-platform.
func WindowIcon(icon any) Option {
switch icon.(type) {
case DialogIcon, string:
default:
panic("interface conversion: expected string or DialogIcon")
}
return funcOption(func(o *options) { o.windowIcon = icon })
}
// Attach returns an Option to set the parent window to attach to.
//
// Attach accepts:
// - a window id (int) on Unix
// - a window handle (~uintptr) on Windows
// - an application name (string) or process id (int) on macOS
func Attach(id any) Option {
return attach(id)
}
// Modal returns an Option to set the modal hint.
func Modal() Option {
return funcOption(func(o *options) { o.modal = true })
}
// Display returns an Option to set the X display to use (Unix only).
func Display(display string) Option {
return funcOption(func(o *options) { o.display = display })
}
// ClassHint returns an Option to set the program name and class
// as used by the window manager (Unix only).
func ClassHint(name, class string) Option {
return funcOption(func(o *options) {
if name != "" {
o.name = name
}
if class != "" {
o.class = class
}
})
}
// Context returns an Option to set a Context that can dismiss the dialog.
//
// Dialogs dismissed by ctx return ctx.Err().
func Context(ctx context.Context) Option {
return funcOption(func(o *options) { o.ctx = ctx })
}