Check and radio lists, see #34.

This commit is contained in:
Nuno Cruces 2022-07-29 00:16:15 +00:00 committed by Nuno Cruces
parent 60a828af0a
commit 44ba26c6f5
6 changed files with 50 additions and 15 deletions

22
list.go
View file

@ -3,7 +3,7 @@ package zenity
// List displays the list dialog. // List displays the list dialog.
// //
// Valid options: Title, Width, Height, OKLabel, CancelLabel, ExtraButton, // Valid options: Title, Width, Height, OKLabel, CancelLabel, ExtraButton,
// WindowIcon, Attach, Modal, DefaultItems, DisallowEmpty. // WindowIcon, Attach, Modal, RadioList, DefaultItems, DisallowEmpty.
// //
// May return: ErrCanceled, ErrExtraButton, ErrUnsupported. // May return: ErrCanceled, ErrExtraButton, ErrUnsupported.
func List(text string, items []string, options ...Option) (string, error) { func List(text string, items []string, options ...Option) (string, error) {
@ -20,7 +20,7 @@ func ListItems(text string, items ...string) (string, error) {
// ListMultiple displays the list dialog, allowing multiple items to be selected. // ListMultiple displays the list dialog, allowing multiple items to be selected.
// //
// Valid options: Title, Width, Height, OKLabel, CancelLabel, ExtraButton, // Valid options: Title, Width, Height, OKLabel, CancelLabel, ExtraButton,
// WindowIcon, Attach, Modal, DefaultItems, DisallowEmpty. // WindowIcon, Attach, Modal, CheckList, DefaultItems, DisallowEmpty.
// //
// May return: ErrCanceled, ErrExtraButton, ErrUnsupported. // May return: ErrCanceled, ErrExtraButton, ErrUnsupported.
func ListMultiple(text string, items []string, options ...Option) ([]string, error) { func ListMultiple(text string, items []string, options ...Option) ([]string, error) {
@ -34,6 +34,24 @@ func ListMultipleItems(text string, items ...string) ([]string, error) {
return ListMultiple(text, items) return ListMultiple(text, items)
} }
// CheckList returns an Option to show check boxes (Unix only).
func CheckList() Option {
return funcOption(func(o *options) { o.listKind = checkListKind })
}
// RadioList returns an Option to show radio boxes (Unix only).
func RadioList() Option {
return funcOption(func(o *options) { o.listKind = radioListKind })
}
type listKind int
const (
basicListKind listKind = iota
checkListKind
radioListKind
)
// DefaultItems returns an Option to set the items to initially select (macOS only). // DefaultItems returns an Option to set the items to initially select (macOS only).
func DefaultItems(items ...string) Option { func DefaultItems(items ...string) Option {
return funcOption(func(o *options) { o.defaultItems = items }) return funcOption(func(o *options) { o.defaultItems = items })

View file

@ -5,24 +5,40 @@ package zenity
import "github.com/ncruces/zenity/internal/zenutil" import "github.com/ncruces/zenity/internal/zenutil"
func list(text string, items []string, opts options) (string, error) { func list(text string, items []string, opts options) (string, error) {
args := []string{"--list", "--column=", "--hide-header", "--text", text} args := []string{"--list", "--hide-header", "--text", text}
args = appendGeneral(args, opts) args = appendGeneral(args, opts)
args = appendButtons(args, opts) args = appendButtons(args, opts)
args = appendWidthHeight(args, opts) args = appendWidthHeight(args, opts)
args = appendWindowIcon(args, opts) args = appendWindowIcon(args, opts)
args = append(args, items...) if opts.listKind == radioListKind {
args = append(args, "--radiolist", "--column=", "--column=")
for _, i := range items {
args = append(args, i, i)
}
} else {
args = append(args, "--column=")
args = append(args, items...)
}
out, err := zenutil.Run(opts.ctx, args) out, err := zenutil.Run(opts.ctx, args)
return strResult(opts, out, err) return strResult(opts, out, err)
} }
func listMultiple(text string, items []string, opts options) ([]string, error) { func listMultiple(text string, items []string, opts options) ([]string, error) {
args := []string{"--list", "--column=", "--hide-header", "--text", text, "--multiple", "--separator", zenutil.Separator} args := []string{"--list", "--hide-header", "--text", text, "--multiple", "--separator", zenutil.Separator}
args = appendGeneral(args, opts) args = appendGeneral(args, opts)
args = appendButtons(args, opts) args = appendButtons(args, opts)
args = appendWidthHeight(args, opts) args = appendWidthHeight(args, opts)
args = appendWindowIcon(args, opts) args = appendWindowIcon(args, opts)
args = append(args, items...) if opts.listKind == checkListKind {
args = append(args, "--checklist", "--column=", "--column=")
for _, i := range items {
args = append(args, i, i)
}
} else {
args = append(args, "--column=")
args = append(args, items...)
}
out, err := zenutil.Run(opts.ctx, args) out, err := zenutil.Run(opts.ctx, args)
return lstResult(opts, out, err) return lstResult(opts, out, err)

View file

@ -57,6 +57,7 @@ type options struct {
username bool username bool
// List options // List options
listKind listKind
disallowEmpty bool disallowEmpty bool
defaultItems []string defaultItems []string

View file

@ -3,9 +3,9 @@ package zenity
// Attach returns an Option to set the parent window to attach to. // Attach returns an Option to set the parent window to attach to.
// //
// Attach accepts: // Attach accepts:
// - a window id (int) on Unix // - a window id (int) on Unix
// - a window handle (~uintptr) on Windows // - a window handle (~uintptr) on Windows
// - an application name (string) or process id (int) on macOS // - an application name (string) or process id (int) on macOS
func Attach(id any) Option { func Attach(id any) Option {
switch id.(type) { switch id.(type) {
case int, string: case int, string:

View file

@ -5,9 +5,9 @@ package zenity
// Attach returns an Option to set the parent window to attach to. // Attach returns an Option to set the parent window to attach to.
// //
// Attach accepts: // Attach accepts:
// - a window id (int) on Unix // - a window id (int) on Unix
// - a window handle (~uintptr) on Windows // - a window handle (~uintptr) on Windows
// - an application name (string) or process id (int) on macOS // - an application name (string) or process id (int) on macOS
func Attach(id int) Option { func Attach(id int) Option {
return funcOption(func(o *options) { o.attach = id }) return funcOption(func(o *options) { o.attach = id })
} }

View file

@ -9,9 +9,9 @@ import (
// Attach returns an Option to set the parent window to attach to. // Attach returns an Option to set the parent window to attach to.
// //
// Attach accepts: // Attach accepts:
// - a window id (int) on Unix // - a window id (int) on Unix
// - a window handle (~uintptr) on Windows // - a window handle (~uintptr) on Windows
// - an application name (string) or process id (int) on macOS // - an application name (string) or process id (int) on macOS
func Attach(id any) Option { func Attach(id any) Option {
if v := reflect.ValueOf(id); v.Kind() == reflect.Uintptr { if v := reflect.ValueOf(id); v.Kind() == reflect.Uintptr {
id = win.HWND(uintptr(v.Uint())) id = win.HWND(uintptr(v.Uint()))