Improve compatibility with zenity.

This commit is contained in:
Nuno Cruces 2022-12-19 19:49:05 +00:00
parent 61682a93bd
commit 9816e864ae
7 changed files with 70 additions and 4 deletions

View file

@ -54,6 +54,9 @@ var (
windowIcon string
attach string
modal bool
display string
class string
name string
multiple bool
defaultCancel bool
@ -73,6 +76,7 @@ var (
columns int
checklist bool
radiolist bool
midSearch bool
disallowEmpty bool
// Calendar options
@ -221,6 +225,9 @@ func parseFlags() []string {
fset.StringVar(&windowIcon, "window-icon", "", "Set the window `icon` (error, info, question, warning)")
fset.StringVar(&attach, "attach", "", "Set the parent `window` to attach to")
fset.BoolVar(&modal, "modal", runtime.GOOS == "darwin", "Set the modal hint")
fset.StringVar(&display, "display", "", "X `display` to use (Unix only)")
fset.StringVar(&class, "class", "", "Program `class` as used by the window manager (Unix only)")
fset.StringVar(&name, "name", "", "Program `name` as used by the window manager (Unix only)")
fset.BoolVar(&multiple, "multiple", false, "Allow multiple items to be selected")
fset.BoolVar(&defaultCancel, "default-cancel", false, "Give Cancel button focus by default")
@ -242,6 +249,7 @@ func parseFlags() []string {
fset.Bool("hide-header", true, "Hide the column headers")
fset.BoolVar(&checklist, "checklist", false, "Use check boxes for the first column (Unix only)")
fset.BoolVar(&radiolist, "radiolist", false, "Use radio buttons for the first column (Unix only)")
fset.BoolVar(&midSearch, "mid-search", false, "Change list search to find text in the middle, not on the beginning (Unix only)")
fset.BoolVar(&disallowEmpty, "disallow-empty", false, "Disallow empty selection (Windows and macOS only)")
// Calendar options
@ -501,6 +509,8 @@ func loadFlags() []zenity.Option {
if modal {
opts = append(opts, zenity.Modal())
}
opts = append(opts, zenity.Display(display))
opts = append(opts, zenity.ClassHint(name, class))
// Message options
@ -538,6 +548,9 @@ func loadFlags() []zenity.Option {
if radiolist {
opts = append(opts, zenity.RadioList())
}
if midSearch {
opts = append(opts, zenity.MidSearch())
}
if disallowEmpty {
opts = append(opts, zenity.DisallowEmpty())
}

View file

@ -52,6 +52,12 @@ const (
radioListKind
)
// MidSearch returns an Option to change list search to find text in the middle,
// not on the beginning (Unix only).
func MidSearch() Option {
return funcOption(func(o *options) { o.midSearch = true })
}
// DefaultItems returns an Option to set the items to initially select (Windows and macOS only).
func DefaultItems(items ...string) Option {
return funcOption(func(o *options) { o.defaultItems = items })

View file

@ -19,6 +19,9 @@ func list(text string, items []string, opts options) (string, error) {
args = append(args, "--column=")
args = append(args, items...)
}
if opts.midSearch {
args = append(args, "--mid-search")
}
out, err := zenutil.Run(opts.ctx, args)
return strResult(opts, out, err)

View file

@ -24,6 +24,15 @@ func appendGeneral(args []string, opts options) []string {
if opts.modal {
args = append(args, "--modal")
}
if opts.display != "" {
args = append(args, "--display", opts.display)
}
if opts.class != "" {
args = append(args, "--class", opts.class)
}
if opts.name != "" {
args = append(args, "--name", opts.name)
}
return args
}

View file

@ -34,11 +34,21 @@ func Test_quoteAccelerators(t *testing.T) {
func Test_appendGeneral(t *testing.T) {
t.Parallel()
got := appendGeneral(nil, options{
title: ptr("Title"),
attach: 12345,
modal: true,
title: ptr("Title"),
attach: 12345,
modal: true,
display: ":1",
class: "Class",
name: "Name",
})
want := []string{"--title", "Title", "--attach", "12345", "--modal"}
want := []string{
"--title", "Title",
"--attach", "12345",
"--modal",
"--display", ":1",
"--class", "Class",
"--name", "Name",
}
if !reflect.DeepEqual(got, want) {
t.Errorf("appendTitle() = %v; want %v", got, want)
}

View file

@ -49,6 +49,9 @@ type options struct {
windowIcon any
attach any
modal bool
display string
class string
name string
// Message options
noWrap bool
@ -61,6 +64,7 @@ type options struct {
// List options
listKind listKind
midSearch bool
disallowEmpty bool
defaultItems []string
@ -203,6 +207,24 @@ 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().

View file

@ -32,6 +32,8 @@ func Test_applyOptions(t *testing.T) {
{name: "Icon", args: Icon("error"), want: options{icon: "error"}},
{name: "Modal", args: Modal(), want: options{modal: true}},
{name: "Attach", args: Attach(zencmd.ParseWindowId("12345")), want: options{attach: zencmd.ParseWindowId("12345")}},
{name: "Display", args: Display(":1"), want: options{display: ":1"}},
{name: "ClassHint", args: ClassHint("Name", "Class"), want: options{name: "Name", class: "Class"}},
// Message options
{name: "NoWrap", args: NoWrap(), want: options{noWrap: true}},
@ -45,6 +47,7 @@ func Test_applyOptions(t *testing.T) {
// List options
{name: "CheckList", args: CheckList(), want: options{listKind: checkListKind}},
{name: "RadioList", args: RadioList(), want: options{listKind: radioListKind}},
{name: "MidSearch", args: MidSearch(), want: options{midSearch: true}},
{name: "DisallowEmpty", args: DisallowEmpty(), want: options{disallowEmpty: true}},
{name: "DefaultItems", args: DefaultItems("a", "b"), want: options{defaultItems: []string{"a", "b"}}},