From 4e896ffb7adca8039605b0b91371715b54f8cf7b Mon Sep 17 00:00:00 2001 From: Nuno Cruces Date: Tue, 6 Apr 2021 12:35:22 +0100 Subject: [PATCH] Refactor (macOS). --- README.md | 1 + entry_darwin.go | 30 +------------- internal/zenutil/run_darwin.go | 17 ++++++++ msg_darwin.go | 71 ++------------------------------ util_darwin.go | 75 ++++++++++++++++++++++++++++++++++ 5 files changed, 98 insertions(+), 96 deletions(-) create mode 100644 util_darwin.go diff --git a/README.md b/README.md index 50c5f7e..a00e70c 100644 --- a/README.md +++ b/README.md @@ -39,6 +39,7 @@ Why reinvent this particular wheel? * Explorer shell not required * works in Server Core * Unicode support + * High DPI support (no manifest required) * WSL/Cygwin/MSYS2 [support](https://github.com/ncruces/zenity/wiki/Zenity-for-WSL,-Cygwin,-MSYS2) * on macOS: * only dependency is `osascript` diff --git a/entry_darwin.go b/entry_darwin.go index 6230658..4867ca6 100644 --- a/entry_darwin.go +++ b/entry_darwin.go @@ -15,34 +15,8 @@ func entry(text string, opts options) (string, bool, error) { data.Options.Answer = &opts.entryText data.Options.Hidden = opts.hideText data.Options.Timeout = zenutil.Timeout - - switch opts.icon { - case ErrorIcon: - data.Options.Icon = "stop" - case WarningIcon: - data.Options.Icon = "caution" - case InfoIcon, QuestionIcon: - data.Options.Icon = "note" - } - - if opts.okLabel != nil || opts.cancelLabel != nil || opts.extraButton != nil { - if opts.okLabel == nil { - opts.okLabel = stringPtr("OK") - } - if opts.cancelLabel == nil { - opts.cancelLabel = stringPtr("Cancel") - } - if opts.extraButton == nil { - data.Options.Buttons = []string{*opts.cancelLabel, *opts.okLabel} - data.Options.Default = 2 - data.Options.Cancel = 1 - } else { - data.Options.Buttons = []string{*opts.extraButton, *opts.cancelLabel, *opts.okLabel} - data.Options.Default = 3 - data.Options.Cancel = 2 - } - data.Extra = opts.extraButton - } + data.Options.Icon = opts.icon.String() + data.SetButtons(getButtons(true, true, opts)) out, err := zenutil.Run(opts.ctx, "dialog", data) out = bytes.TrimSuffix(out, []byte{'\n'}) diff --git a/internal/zenutil/run_darwin.go b/internal/zenutil/run_darwin.go index e84f134..bd7f392 100644 --- a/internal/zenutil/run_darwin.go +++ b/internal/zenutil/run_darwin.go @@ -97,3 +97,20 @@ type NotifyOptions struct { Title *string `json:"withTitle,omitempty"` Subtitle string `json:"subtitle,omitempty"` } + +type Buttons struct { + Buttons []string + Default int + Cancel int + Extra int +} + +func (d *Dialog) SetButtons(btns Buttons) { + d.Options.Buttons = btns.Buttons + d.Options.Default = btns.Default + d.Options.Cancel = btns.Cancel + if btns.Extra > 0 { + name := btns.Buttons[btns.Extra-1] + d.Extra = &name + } +} diff --git a/msg_darwin.go b/msg_darwin.go index 3c40d0a..3cc4a34 100644 --- a/msg_darwin.go +++ b/msg_darwin.go @@ -23,82 +23,17 @@ func message(kind messageKind, text string, opts options) (bool, error) { if dialog { data.Operation = "displayDialog" data.Options.Title = opts.title - - switch opts.icon { - case ErrorIcon: - data.Options.Icon = "stop" - case WarningIcon: - data.Options.Icon = "caution" - case InfoIcon, QuestionIcon: - data.Options.Icon = "note" - } + data.Options.Icon = opts.icon.String() } else { data.Operation = "displayAlert" + data.Options.As = kind.String() if opts.title != nil { data.Text = *opts.title data.Options.Message = text } - - switch kind { - case infoKind: - data.Options.As = "informational" - case warningKind: - data.Options.As = "warning" - case errorKind: - data.Options.As = "critical" - } } - if kind == questionKind { - // alert defaults to a single button, we need two - if opts.cancelLabel == nil && !dialog { - opts.cancelLabel = stringPtr("Cancel") - } - } else { - // dialog defaults to two buttons, we need one - if opts.okLabel == nil && dialog { - opts.okLabel = stringPtr("OK") - } - // only questions have cancel - opts.cancelLabel = nil - } - - if opts.okLabel != nil || opts.cancelLabel != nil || opts.extraButton != nil { - if opts.okLabel == nil { - opts.okLabel = stringPtr("OK") - } - if kind == questionKind { - if opts.cancelLabel == nil { - opts.cancelLabel = stringPtr("Cancel") - } - if opts.extraButton == nil { - data.Options.Buttons = []string{*opts.cancelLabel, *opts.okLabel} - data.Options.Default = 2 - data.Options.Cancel = 1 - } else { - data.Options.Buttons = []string{*opts.extraButton, *opts.cancelLabel, *opts.okLabel} - data.Options.Default = 3 - data.Options.Cancel = 2 - } - } else { - if opts.extraButton == nil { - data.Options.Buttons = []string{*opts.okLabel} - data.Options.Default = 1 - } else { - data.Options.Buttons = []string{*opts.extraButton, *opts.okLabel} - data.Options.Default = 2 - } - } - data.Extra = opts.extraButton - } - - if kind == questionKind && opts.defaultCancel { - if data.Options.Cancel != 0 { - data.Options.Default = data.Options.Cancel - } else { - data.Options.Default = 1 - } - } + data.SetButtons(getButtons(dialog, kind == questionKind, opts)) out, err := zenutil.Run(opts.ctx, "dialog", data) if err, ok := err.(*exec.ExitError); ok && err.ExitCode() == 1 { diff --git a/util_darwin.go b/util_darwin.go new file mode 100644 index 0000000..8a79755 --- /dev/null +++ b/util_darwin.go @@ -0,0 +1,75 @@ +package zenity + +import "github.com/ncruces/zenity/internal/zenutil" + +func getButtons(dialog, okcancel bool, opts options) (btns zenutil.Buttons) { + if !okcancel { + opts.cancelLabel = nil + opts.defaultCancel = false + } + + if opts.okLabel != nil || opts.cancelLabel != nil || opts.extraButton != nil || (dialog != okcancel) { + if opts.okLabel == nil { + opts.okLabel = stringPtr("OK") + } + if okcancel { + if opts.cancelLabel == nil { + opts.cancelLabel = stringPtr("Cancel") + } + if opts.extraButton == nil { + btns.Buttons = []string{*opts.cancelLabel, *opts.okLabel} + btns.Default = 2 + btns.Cancel = 1 + } else { + btns.Buttons = []string{*opts.extraButton, *opts.cancelLabel, *opts.okLabel} + btns.Default = 3 + btns.Cancel = 2 + btns.Extra = 1 + } + } else { + if opts.extraButton == nil { + btns.Buttons = []string{*opts.okLabel} + btns.Default = 1 + } else { + btns.Buttons = []string{*opts.extraButton, *opts.okLabel} + btns.Default = 2 + btns.Extra = 1 + } + } + } + + if opts.defaultCancel { + if btns.Cancel != 0 { + btns.Default = btns.Cancel + } else { + btns.Default = 1 + } + } + return +} + +func (i DialogIcon) String() string { + switch i { + case ErrorIcon: + return "stop" + case WarningIcon: + return "caution" + case InfoIcon, QuestionIcon: + return "note" + default: + return "" + } +} + +func (k messageKind) String() string { + switch k { + case infoKind: + return "informational" + case warningKind: + return "warning" + case errorKind: + return "critical" + default: + return "" + } +}