Refactor (macOS).

This commit is contained in:
Nuno Cruces 2021-04-06 12:35:22 +01:00
parent d7e87a1088
commit 4e896ffb7a
5 changed files with 98 additions and 96 deletions

View File

@ -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`

View File

@ -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'})

View File

@ -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
}
}

View File

@ -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 {

75
util_darwin.go Normal file
View File

@ -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 ""
}
}