Refactoring.

This commit is contained in:
Nuno Cruces 2020-01-24 12:52:45 +00:00
parent 44cd5fa43b
commit 5633d0d7e6
15 changed files with 130 additions and 126 deletions

View file

@ -49,8 +49,8 @@ var (
fileFilters FileFilters fileFilters FileFilters
// Color selection options // Color selection options
initColor string defaultColor string
palette bool showPalette bool
// Windows specific options // Windows specific options
cygpath bool cygpath bool
@ -130,8 +130,8 @@ func setupFlags() {
flag.Var(&fileFilters, "file-filter", "Set a filename filter (NAME | PATTERN1 PATTERN2 ...)") flag.Var(&fileFilters, "file-filter", "Set a filename filter (NAME | PATTERN1 PATTERN2 ...)")
// Color selection options // Color selection options
flag.StringVar(&initColor, "color", "", "Set the color") flag.StringVar(&defaultColor, "color", "", "Set the color")
flag.BoolVar(&palette, "show-palette", false, "Show the palette") flag.BoolVar(&showPalette, "show-palette", false, "Show the palette")
// Windows specific options // Windows specific options
if runtime.GOOS == "windows" { if runtime.GOOS == "windows" {
@ -203,7 +203,7 @@ func loadFlags() []zenity.Option {
// File selection options // File selection options
options = append(options, fileFilters.Build()) options = append(options, fileFilters)
if filename != "" { if filename != "" {
options = append(options, zenity.Filename(ingestPath(filename))) options = append(options, zenity.Filename(ingestPath(filename)))
} }
@ -224,10 +224,10 @@ func loadFlags() []zenity.Option {
// Color selection options // Color selection options
if initColor != "" { if defaultColor != "" {
options = append(options, zenity.Color(zenutil.ParseColor(initColor))) options = append(options, zenity.Color(zenutil.ParseColor(defaultColor)))
} }
if palette { if showPalette {
options = append(options, zenity.ShowPalette()) options = append(options, zenity.ShowPalette())
} }

View file

@ -13,10 +13,10 @@ func SelectColor(options ...Option) (color.Color, error) {
// Color returns an Option to set the color. // Color returns an Option to set the color.
func Color(c color.Color) Option { func Color(c color.Color) Option {
return func(o *options) { o.color = c } return funcOption(func(o *options) { o.color = c })
} }
// ShowPalette returns an Option to show the palette. // ShowPalette returns an Option to show the palette.
func ShowPalette() Option { func ShowPalette() Option {
return func(o *options) { o.palette = true } return funcOption(func(o *options) { o.showPalette = true })
} }

View file

@ -8,7 +8,7 @@ import (
) )
func selectColor(options ...Option) (color.Color, error) { func selectColor(options ...Option) (color.Color, error) {
opts := optsParse(options) opts := applyOptions(options)
var data zenutil.Color var data zenutil.Color
if opts.color != nil { if opts.color != nil {

View file

@ -10,7 +10,7 @@ import (
) )
func selectColor(options ...Option) (color.Color, error) { func selectColor(options ...Option) (color.Color, error) {
opts := optsParse(options) opts := applyOptions(options)
args := []string{"--color-selection"} args := []string{"--color-selection"}
@ -20,7 +20,7 @@ func selectColor(options ...Option) (color.Color, error) {
if opts.color != nil { if opts.color != nil {
args = append(args, "--color", zenutil.UnparseColor(opts.color)) args = append(args, "--color", zenutil.UnparseColor(opts.color))
} }
if opts.palette { if opts.showPalette {
args = append(args, "--show-palette") args = append(args, "--show-palette")
} }

View file

@ -21,7 +21,7 @@ func init() {
} }
func selectColor(options ...Option) (color.Color, error) { func selectColor(options ...Option) (color.Color, error) {
opts := optsParse(options) opts := applyOptions(options)
// load custom colors // load custom colors
colorsMutex.Lock() colorsMutex.Lock()
@ -37,7 +37,7 @@ func selectColor(options ...Option) (color.Color, error) {
n := color.NRGBAModel.Convert(opts.color).(color.NRGBA) n := color.NRGBAModel.Convert(opts.color).(color.NRGBA)
args.RgbResult = uint32(n.R) | (uint32(n.G) << 8) | (uint32(n.B) << 16) args.RgbResult = uint32(n.R) | (uint32(n.G) << 8) | (uint32(n.B) << 16)
} }
if opts.palette { if opts.showPalette {
args.Flags |= 4 // CC_PREVENTFULLOPEN args.Flags |= 4 // CC_PREVENTFULLOPEN
} else { } else {
args.Flags |= 2 // CC_FULLOPEN args.Flags |= 2 // CC_FULLOPEN

24
file.go
View file

@ -39,32 +39,32 @@ func SelectFileSave(options ...Option) (string, error) {
// Specifying a file name, makes it the default selected file. // Specifying a file name, makes it the default selected file.
// Specifying a directory path, makes it the default dialog location. // Specifying a directory path, makes it the default dialog location.
func Filename(filename string) Option { func Filename(filename string) Option {
return func(o *options) { o.filename = filename } return funcOption(func(o *options) { o.filename = filename })
} }
// Directory returns an Option to activate directory-only selection. // Directory returns an Option to activate directory-only selection.
func Directory() Option { func Directory() Option {
return func(o *options) { o.directory = true } return funcOption(func(o *options) { o.directory = true })
} }
// ConfirmOverwrite returns an Option to confirm file selection if filename // ConfirmOverwrite returns an Option to confirm file selection if filename
// already exists. // already exists.
func ConfirmOverwrite() Option { func ConfirmOverwrite() Option {
return func(o *options) { o.overwrite = true } return funcOption(func(o *options) { o.confirmOverwrite = true })
} }
// ConfirmCreate returns an Option to confirm file selection if filename does // ConfirmCreate returns an Option to confirm file selection if filename does
// not yet exist (Windows only). // not yet exist (Windows only).
func ConfirmCreate() Option { func ConfirmCreate() Option {
return func(o *options) { o.create = true } return funcOption(func(o *options) { o.confirmCreate = true })
} }
// ShowHidden returns an Option to show hidden files (Windows and macOS only). // ShowHidden returns an Option to show hidden files (Windows and macOS only).
func ShowHidden() Option { func ShowHidden() Option {
return func(o *options) { o.hidden = true } return funcOption(func(o *options) { o.showHidden = true })
} }
// FileFilter encapsulates a filename filter. // FileFilter is an Option that sets a filename filter.
// //
// macOS hides filename filters from the user, // macOS hides filename filters from the user,
// and only supports filtering by extension (or "type"). // and only supports filtering by extension (or "type").
@ -73,17 +73,15 @@ type FileFilter struct {
Patterns []string // filter patterns for the display string Patterns []string // filter patterns for the display string
} }
// Build returns an Option to set a filename filter. func (f FileFilter) apply(o *options) {
func (f FileFilter) Build() Option { o.fileFilters = append(o.fileFilters, f)
return func(o *options) { o.filters = append(o.filters, f) }
} }
// FileFilters is a list of filename filters. // FileFilters is an Option that sets multiple filename filters.
type FileFilters []FileFilter type FileFilters []FileFilter
// Build returns an Option to set filename filters. func (f FileFilters) apply(o *options) {
func (f FileFilters) Build() Option { o.fileFilters = append(o.fileFilters, f...)
return func(o *options) { o.filters = append(o.filters, f...) }
} }
func splitDirAndName(path string) (dir, name string) { func splitDirAndName(path string) (dir, name string) {

View file

@ -8,17 +8,17 @@ import (
) )
func selectFile(options ...Option) (string, error) { func selectFile(options ...Option) (string, error) {
opts := optsParse(options) opts := applyOptions(options)
data := zenutil.File{ data := zenutil.File{
Prompt: opts.title, Prompt: opts.title,
Invisibles: opts.hidden, Invisibles: opts.showHidden,
} }
if opts.directory { if opts.directory {
data.Operation = "chooseFolder" data.Operation = "chooseFolder"
} else { } else {
data.Operation = "chooseFile" data.Operation = "chooseFile"
data.Type = initFilters(opts.filters) data.Type = initFilters(opts.fileFilters)
} }
data.Location, _ = splitDirAndName(opts.filename) data.Location, _ = splitDirAndName(opts.filename)
@ -36,11 +36,11 @@ func selectFile(options ...Option) (string, error) {
} }
func selectFileMutiple(options ...Option) ([]string, error) { func selectFileMutiple(options ...Option) ([]string, error) {
opts := optsParse(options) opts := applyOptions(options)
data := zenutil.File{ data := zenutil.File{
Prompt: opts.title, Prompt: opts.title,
Invisibles: opts.hidden, Invisibles: opts.showHidden,
Multiple: true, Multiple: true,
Separator: zenutil.Separator, Separator: zenutil.Separator,
} }
@ -48,7 +48,7 @@ func selectFileMutiple(options ...Option) ([]string, error) {
data.Operation = "chooseFolder" data.Operation = "chooseFolder"
} else { } else {
data.Operation = "chooseFile" data.Operation = "chooseFile"
data.Type = initFilters(opts.filters) data.Type = initFilters(opts.fileFilters)
} }
data.Location, _ = splitDirAndName(opts.filename) data.Location, _ = splitDirAndName(opts.filename)
@ -69,7 +69,7 @@ func selectFileMutiple(options ...Option) ([]string, error) {
} }
func selectFileSave(options ...Option) (string, error) { func selectFileSave(options ...Option) (string, error) {
opts := optsParse(options) opts := applyOptions(options)
data := zenutil.File{ data := zenutil.File{
Prompt: opts.title, Prompt: opts.title,
@ -78,7 +78,7 @@ func selectFileSave(options ...Option) (string, error) {
data.Operation = "chooseFolder" data.Operation = "chooseFolder"
} else { } else {
data.Operation = "chooseFileName" data.Operation = "chooseFileName"
data.Type = initFilters(opts.filters) data.Type = initFilters(opts.fileFilters)
} }
data.Location, data.Name = splitDirAndName(opts.filename) data.Location, data.Name = splitDirAndName(opts.filename)

View file

@ -12,7 +12,7 @@ func ExampleSelectFile() {
{"Go files", []string{"*.go"}}, {"Go files", []string{"*.go"}},
{"Web files", []string{"*.html", "*.js", "*.css"}}, {"Web files", []string{"*.html", "*.js", "*.css"}},
{"Image files", []string{"*.png", "*.gif", "*.ico", "*.jpg", "*.webp"}}, {"Image files", []string{"*.png", "*.gif", "*.ico", "*.jpg", "*.webp"}},
}.Build()) })
// Output: // Output:
} }
@ -23,7 +23,7 @@ func ExampleSelectFileMutiple() {
{"Go files", []string{"*.go"}}, {"Go files", []string{"*.go"}},
{"Web files", []string{"*.html", "*.js", "*.css"}}, {"Web files", []string{"*.html", "*.js", "*.css"}},
{"Image files", []string{"*.png", "*.gif", "*.ico", "*.jpg", "*.webp"}}, {"Image files", []string{"*.png", "*.gif", "*.ico", "*.jpg", "*.webp"}},
}.Build()) })
// Output: // Output:
} }
@ -35,7 +35,7 @@ func ExampleSelectFileSave() {
{"Go files", []string{"*.go"}}, {"Go files", []string{"*.go"}},
{"Web files", []string{"*.html", "*.js", "*.css"}}, {"Web files", []string{"*.html", "*.js", "*.css"}},
{"Image files", []string{"*.png", "*.gif", "*.ico", "*.jpg", "*.webp"}}, {"Image files", []string{"*.png", "*.gif", "*.ico", "*.jpg", "*.webp"}},
}.Build()) })
// Output: // Output:
} }

View file

@ -10,7 +10,7 @@ import (
) )
func selectFile(options ...Option) (string, error) { func selectFile(options ...Option) (string, error) {
opts := optsParse(options) opts := applyOptions(options)
args := []string{"--file-selection"} args := []string{"--file-selection"}
if opts.directory { if opts.directory {
@ -22,7 +22,7 @@ func selectFile(options ...Option) (string, error) {
if opts.filename != "" { if opts.filename != "" {
args = append(args, "--filename", opts.filename) args = append(args, "--filename", opts.filename)
} }
args = append(args, initFilters(opts.filters)...) args = append(args, initFilters(opts.fileFilters)...)
out, err := zenutil.Run(args) out, err := zenutil.Run(args)
if err, ok := err.(*exec.ExitError); ok && err.ExitCode() != 255 { if err, ok := err.(*exec.ExitError); ok && err.ExitCode() != 255 {
@ -38,7 +38,7 @@ func selectFile(options ...Option) (string, error) {
} }
func selectFileMutiple(options ...Option) ([]string, error) { func selectFileMutiple(options ...Option) ([]string, error) {
opts := optsParse(options) opts := applyOptions(options)
args := []string{"--file-selection", "--multiple", "--separator", zenutil.Separator} args := []string{"--file-selection", "--multiple", "--separator", zenutil.Separator}
if opts.directory { if opts.directory {
@ -50,7 +50,7 @@ func selectFileMutiple(options ...Option) ([]string, error) {
if opts.filename != "" { if opts.filename != "" {
args = append(args, "--filename", opts.filename) args = append(args, "--filename", opts.filename)
} }
args = append(args, initFilters(opts.filters)...) args = append(args, initFilters(opts.fileFilters)...)
out, err := zenutil.Run(args) out, err := zenutil.Run(args)
if err, ok := err.(*exec.ExitError); ok && err.ExitCode() != 255 { if err, ok := err.(*exec.ExitError); ok && err.ExitCode() != 255 {
@ -66,7 +66,7 @@ func selectFileMutiple(options ...Option) ([]string, error) {
} }
func selectFileSave(options ...Option) (string, error) { func selectFileSave(options ...Option) (string, error) {
opts := optsParse(options) opts := applyOptions(options)
args := []string{"--file-selection", "--save"} args := []string{"--file-selection", "--save"}
if opts.directory { if opts.directory {
@ -78,10 +78,10 @@ func selectFileSave(options ...Option) (string, error) {
if opts.filename != "" { if opts.filename != "" {
args = append(args, "--filename", opts.filename) args = append(args, "--filename", opts.filename)
} }
if opts.overwrite { if opts.confirmOverwrite {
args = append(args, "--confirm-overwrite") args = append(args, "--confirm-overwrite")
} }
args = append(args, initFilters(opts.filters)...) args = append(args, initFilters(opts.fileFilters)...)
out, err := zenutil.Run(args) out, err := zenutil.Run(args)
if err, ok := err.(*exec.ExitError); ok && err.ExitCode() != 255 { if err, ok := err.(*exec.ExitError); ok && err.ExitCode() != 255 {

View file

@ -18,7 +18,7 @@ var (
) )
func selectFile(options ...Option) (string, error) { func selectFile(options ...Option) (string, error) {
opts := optsParse(options) opts := applyOptions(options)
if opts.directory { if opts.directory {
res, _, err := pickFolders(opts, false) res, _, err := pickFolders(opts, false)
return res, err return res, err
@ -31,11 +31,11 @@ func selectFile(options ...Option) (string, error) {
if opts.title != "" { if opts.title != "" {
args.Title = syscall.StringToUTF16Ptr(opts.title) args.Title = syscall.StringToUTF16Ptr(opts.title)
} }
if opts.hidden { if opts.showHidden {
args.Flags |= 0x10000000 // OFN_FORCESHOWHIDDEN args.Flags |= 0x10000000 // OFN_FORCESHOWHIDDEN
} }
if opts.filters != nil { if opts.fileFilters != nil {
args.Filter = &initFilters(opts.filters)[0] args.Filter = &initFilters(opts.fileFilters)[0]
} }
res := [32768]uint16{} res := [32768]uint16{}
@ -54,7 +54,7 @@ func selectFile(options ...Option) (string, error) {
} }
func selectFileMutiple(options ...Option) ([]string, error) { func selectFileMutiple(options ...Option) ([]string, error) {
opts := optsParse(options) opts := applyOptions(options)
if opts.directory { if opts.directory {
_, res, err := pickFolders(opts, true) _, res, err := pickFolders(opts, true)
return res, err return res, err
@ -67,11 +67,11 @@ func selectFileMutiple(options ...Option) ([]string, error) {
if opts.title != "" { if opts.title != "" {
args.Title = syscall.StringToUTF16Ptr(opts.title) args.Title = syscall.StringToUTF16Ptr(opts.title)
} }
if opts.hidden { if opts.showHidden {
args.Flags |= 0x10000000 // OFN_FORCESHOWHIDDEN args.Flags |= 0x10000000 // OFN_FORCESHOWHIDDEN
} }
if opts.filters != nil { if opts.fileFilters != nil {
args.Filter = &initFilters(opts.filters)[0] args.Filter = &initFilters(opts.fileFilters)[0]
} }
res := [32768 + 1024*256]uint16{} res := [32768 + 1024*256]uint16{}
@ -115,7 +115,7 @@ func selectFileMutiple(options ...Option) ([]string, error) {
} }
func selectFileSave(options ...Option) (string, error) { func selectFileSave(options ...Option) (string, error) {
opts := optsParse(options) opts := applyOptions(options)
if opts.directory { if opts.directory {
res, _, err := pickFolders(opts, false) res, _, err := pickFolders(opts, false)
return res, err return res, err
@ -128,17 +128,17 @@ func selectFileSave(options ...Option) (string, error) {
if opts.title != "" { if opts.title != "" {
args.Title = syscall.StringToUTF16Ptr(opts.title) args.Title = syscall.StringToUTF16Ptr(opts.title)
} }
if opts.overwrite { if opts.confirmOverwrite {
args.Flags |= 0x2 // OFN_OVERWRITEPROMPT args.Flags |= 0x2 // OFN_OVERWRITEPROMPT
} }
if opts.create { if opts.confirmCreate {
args.Flags |= 0x2000 // OFN_CREATEPROMPT args.Flags |= 0x2000 // OFN_CREATEPROMPT
} }
if opts.hidden { if opts.showHidden {
args.Flags |= 0x10000000 // OFN_FORCESHOWHIDDEN args.Flags |= 0x10000000 // OFN_FORCESHOWHIDDEN
} }
if opts.filters != nil { if opts.fileFilters != nil {
args.Filter = &initFilters(opts.filters)[0] args.Filter = &initFilters(opts.fileFilters)[0]
} }
res := [32768]uint16{} res := [32768]uint16{}
@ -185,7 +185,7 @@ func pickFolders(opts options, multi bool) (str string, lst []string, err error)
if multi { if multi {
flgs |= 0x200 // FOS_ALLOWMULTISELECT flgs |= 0x200 // FOS_ALLOWMULTISELECT
} }
if opts.hidden { if opts.showHidden {
flgs |= 0x10000000 // FOS_FORCESHOWHIDDEN flgs |= 0x10000000 // FOS_FORCESHOWHIDDEN
} }
hr, _, _ = dialog.Call(dialog.vtbl.SetOptions, uintptr(flgs|0x68)) // FOS_NOCHANGEDIR|FOS_PICKFOLDERS|FOS_FORCEFILESYSTEM hr, _, _ = dialog.Call(dialog.vtbl.SetOptions, uintptr(flgs|0x68)) // FOS_NOCHANGEDIR|FOS_PICKFOLDERS|FOS_FORCEFILESYSTEM

14
msg.go
View file

@ -63,35 +63,35 @@ const (
// Icon returns an Option to set the dialog icon. // Icon returns an Option to set the dialog icon.
func Icon(icon MessageIcon) Option { func Icon(icon MessageIcon) Option {
return func(o *options) { o.icon = icon } return funcOption(func(o *options) { o.icon = icon })
} }
// OKLabel returns an Option to set the label of the OK button. // OKLabel returns an Option to set the label of the OK button.
func OKLabel(ok string) Option { func OKLabel(ok string) Option {
return func(o *options) { o.ok = ok } return funcOption(func(o *options) { o.okLabel = ok })
} }
// CancelLabel returns an Option to set the label of the Cancel button. // CancelLabel returns an Option to set the label of the Cancel button.
func CancelLabel(cancel string) Option { func CancelLabel(cancel string) Option {
return func(o *options) { o.cancel = cancel } return funcOption(func(o *options) { o.cancelLabel = cancel })
} }
// ExtraButton returns an Option to add an extra button. // ExtraButton returns an Option to add an extra button.
func ExtraButton(extra string) Option { func ExtraButton(extra string) Option {
return func(o *options) { o.extra = extra } return funcOption(func(o *options) { o.extraButton = extra })
} }
// NoWrap returns an Option to disable enable text wrapping. // NoWrap returns an Option to disable enable text wrapping.
func NoWrap() Option { func NoWrap() Option {
return func(o *options) { o.nowrap = true } return funcOption(func(o *options) { o.noWrap = true })
} }
// Ellipsize returns an Option to enable ellipsizing in the dialog text. // Ellipsize returns an Option to enable ellipsizing in the dialog text.
func Ellipsize() Option { func Ellipsize() Option {
return func(o *options) { o.ellipsize = true } return funcOption(func(o *options) { o.ellipsize = true })
} }
// DefaultCancel returns an Option to give Cancel button focus by default. // DefaultCancel returns an Option to give Cancel button focus by default.
func DefaultCancel() Option { func DefaultCancel() Option {
return func(o *options) { o.defcancel = true } return funcOption(func(o *options) { o.defaultCancel = true })
} }

View file

@ -7,7 +7,7 @@ import (
) )
func message(kind messageKind, text string, options []Option) (bool, error) { func message(kind messageKind, text string, options []Option) (bool, error) {
opts := optsParse(options) opts := applyOptions(options)
data := zenutil.Msg{Text: text} data := zenutil.Msg{Text: text}
dialog := kind == questionKind || opts.icon != 0 dialog := kind == questionKind || opts.icon != 0
@ -42,39 +42,39 @@ func message(kind messageKind, text string, options []Option) (bool, error) {
if kind != questionKind { if kind != questionKind {
if dialog { if dialog {
opts.ok = "OK" opts.okLabel = "OK"
} }
opts.cancel = "" opts.cancelLabel = ""
} }
if opts.ok != "" || opts.cancel != "" || opts.extra != "" { if opts.okLabel != "" || opts.cancelLabel != "" || opts.extraButton != "" {
if opts.ok == "" { if opts.okLabel == "" {
opts.ok = "OK" opts.okLabel = "OK"
} }
if opts.cancel == "" { if opts.cancelLabel == "" {
opts.cancel = "Cancel" opts.cancelLabel = "Cancel"
} }
if kind == questionKind { if kind == questionKind {
if opts.extra == "" { if opts.extraButton == "" {
data.Buttons = []string{opts.cancel, opts.ok} data.Buttons = []string{opts.cancelLabel, opts.okLabel}
data.Default = 2 data.Default = 2
data.Cancel = 1 data.Cancel = 1
} else { } else {
data.Buttons = []string{opts.extra, opts.cancel, opts.ok} data.Buttons = []string{opts.extraButton, opts.cancelLabel, opts.okLabel}
data.Default = 3 data.Default = 3
data.Cancel = 2 data.Cancel = 2
} }
} else { } else {
if opts.extra == "" { if opts.extraButton == "" {
data.Buttons = []string{opts.ok} data.Buttons = []string{opts.okLabel}
data.Default = 1 data.Default = 1
} else { } else {
data.Buttons = []string{opts.extra, opts.ok} data.Buttons = []string{opts.extraButton, opts.okLabel}
data.Default = 2 data.Default = 2
} }
} }
data.Extra = opts.extra data.Extra = opts.extraButton
} }
if opts.defcancel { if opts.defaultCancel {
if data.Cancel != 0 { if data.Cancel != 0 {
data.Default = data.Cancel data.Default = data.Cancel
} }
@ -90,7 +90,7 @@ func message(kind messageKind, text string, options []Option) (bool, error) {
if err != nil { if err != nil {
return false, err return false, err
} }
if len(out) > 0 && string(out[:len(out)-1]) == opts.extra { if len(out) > 0 && string(out[:len(out)-1]) == opts.extraButton {
return false, ErrExtraButton return false, ErrExtraButton
} }
return true, err return true, err

View file

@ -9,7 +9,7 @@ import (
) )
func message(kind messageKind, text string, options []Option) (bool, error) { func message(kind messageKind, text string, options []Option) (bool, error) {
opts := optsParse(options) opts := applyOptions(options)
var args []string var args []string
switch kind { switch kind {
@ -28,22 +28,22 @@ func message(kind messageKind, text string, options []Option) (bool, error) {
if opts.title != "" { if opts.title != "" {
args = append(args, "--title", opts.title) args = append(args, "--title", opts.title)
} }
if opts.ok != "" { if opts.okLabel != "" {
args = append(args, "--ok-label", opts.ok) args = append(args, "--ok-label", opts.okLabel)
} }
if opts.cancel != "" { if opts.cancelLabel != "" {
args = append(args, "--cancel-label", opts.cancel) args = append(args, "--cancel-label", opts.cancelLabel)
} }
if opts.extra != "" { if opts.extraButton != "" {
args = append(args, "--extra-button", opts.extra) args = append(args, "--extra-button", opts.extraButton)
} }
if opts.nowrap { if opts.noWrap {
args = append(args, "--no-wrap") args = append(args, "--no-wrap")
} }
if opts.ellipsize { if opts.ellipsize {
args = append(args, "--ellipsize") args = append(args, "--ellipsize")
} }
if opts.defcancel { if opts.defaultCancel {
args = append(args, "--default-cancel") args = append(args, "--default-cancel")
} }
switch opts.icon { switch opts.icon {
@ -59,7 +59,7 @@ func message(kind messageKind, text string, options []Option) (bool, error) {
out, err := zenutil.Run(args) out, err := zenutil.Run(args)
if err, ok := err.(*exec.ExitError); ok && err.ExitCode() != 255 { if err, ok := err.(*exec.ExitError); ok && err.ExitCode() != 255 {
if len(out) > 0 && string(out[:len(out)-1]) == opts.extra { if len(out) > 0 && string(out[:len(out)-1]) == opts.extraButton {
return false, ErrExtraButton return false, ErrExtraButton
} }
return false, nil return false, nil

View file

@ -11,14 +11,14 @@ var (
) )
func message(kind messageKind, text string, options []Option) (bool, error) { func message(kind messageKind, text string, options []Option) (bool, error) {
opts := optsParse(options) opts := applyOptions(options)
var flags, caption uintptr var flags, caption uintptr
switch { switch {
case kind == questionKind && opts.extra != "": case kind == questionKind && opts.extraButton != "":
flags |= 0x3 // MB_YESNOCANCEL flags |= 0x3 // MB_YESNOCANCEL
case kind == questionKind || opts.extra != "": case kind == questionKind || opts.extraButton != "":
flags |= 0x1 // MB_OKCANCEL flags |= 0x1 // MB_OKCANCEL
} }
@ -33,8 +33,8 @@ func message(kind messageKind, text string, options []Option) (bool, error) {
flags |= 0x40 // MB_ICONINFORMATION flags |= 0x40 // MB_ICONINFORMATION
} }
if kind == questionKind && opts.defcancel { if kind == questionKind && opts.defaultCancel {
if opts.extra == "" { if opts.extraButton == "" {
flags |= 0x100 // MB_DEFBUTTON2 flags |= 0x100 // MB_DEFBUTTON2
} else { } else {
flags |= 0x200 // MB_DEFBUTTON3 flags |= 0x200 // MB_DEFBUTTON3
@ -45,7 +45,7 @@ func message(kind messageKind, text string, options []Option) (bool, error) {
caption = uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(opts.title))) caption = uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(opts.title)))
} }
if opts.ok != "" || opts.cancel != "" || opts.extra != "" { if opts.okLabel != "" || opts.cancelLabel != "" || opts.extraButton != "" {
runtime.LockOSThread() runtime.LockOSThread()
defer runtime.UnlockOSThread() defer runtime.UnlockOSThread()
@ -89,17 +89,17 @@ func hookMessageLabels(kind messageKind, opts options) (hook uintptr, err error)
var text string var text string
switch ctl { switch ctl {
case 1, 6: // IDOK, IDYES case 1, 6: // IDOK, IDYES
text = opts.ok text = opts.okLabel
case 2: // IDCANCEL case 2: // IDCANCEL
if kind == questionKind { if kind == questionKind {
text = opts.cancel text = opts.cancelLabel
} else if opts.extra != "" { } else if opts.extraButton != "" {
text = opts.extra text = opts.extraButton
} else { } else {
text = opts.ok text = opts.okLabel
} }
case 7: // IDNO case 7: // IDNO
text = opts.extra text = opts.extraButton
} }
if text != "" { if text != "" {
ptr := syscall.StringToUTF16Ptr(text) ptr := syscall.StringToUTF16Ptr(text)

View file

@ -23,37 +23,43 @@ type options struct {
// File selection options // File selection options
filename string filename string
directory bool directory bool
overwrite bool confirmOverwrite bool
create bool confirmCreate bool
hidden bool showHidden bool
filters []FileFilter fileFilters []FileFilter
// Color selection options // Color selection options
color color.Color color color.Color
palette bool showPalette bool
// Message options // Message options
icon MessageIcon icon MessageIcon
ok string okLabel string
cancel string cancelLabel string
extra string extraButton string
nowrap bool noWrap bool
ellipsize bool ellipsize bool
defcancel bool defaultCancel bool
} }
// An Option is an argument passed to dialog functions to customize their // An Option is an argument passed to dialog functions to customize their
// behavior. // behavior.
type Option func(*options) type Option interface {
apply(*options)
}
func optsParse(options []Option) (res options) { type funcOption func(*options)
func (f funcOption) apply(o *options) { f(o) }
func applyOptions(options []Option) (res options) {
for _, o := range options { for _, o := range options {
o(&res) o.apply(&res)
} }
return return
} }
// Title returns an Option to set the dialog title. // Title returns an Option to set the dialog title.
func Title(title string) Option { func Title(title string) Option {
return func(o *options) { o.title = title } return funcOption(func(o *options) { o.title = title })
} }