zenity/msg_darwin.go

115 lines
2.7 KiB
Go
Raw Normal View History

2020-01-04 22:21:39 -05:00
package zenity
import (
"os/exec"
2020-01-05 07:37:45 -05:00
2020-01-19 06:57:05 -05:00
"github.com/ncruces/zenity/internal/zenutil"
2020-01-04 22:21:39 -05:00
)
2021-03-04 07:42:30 -05:00
func message(kind messageKind, text string, opts options) (bool, error) {
2021-02-19 12:46:47 -05:00
var data zenutil.Msg
data.Text = text
data.Options.Timeout = zenutil.Timeout
2021-03-03 21:52:05 -05:00
// dialog is more flexible, alert prettier
var dialog bool
if opts.icon != 0 { // use if we want to show a specific icon
dialog = true
} else if kind == questionKind && opts.cancelLabel == nil { // use for questions with default buttons
dialog = true
}
2020-01-04 22:21:39 -05:00
2020-01-06 07:01:51 -05:00
if dialog {
2020-01-09 20:46:53 -05:00
data.Operation = "displayDialog"
2021-02-19 12:46:47 -05:00
data.Options.Title = opts.title
2020-01-04 22:21:39 -05:00
switch opts.icon {
case ErrorIcon:
2021-02-19 12:46:47 -05:00
data.Options.Icon = "stop"
2020-01-04 22:21:39 -05:00
case WarningIcon:
2021-02-19 12:46:47 -05:00
data.Options.Icon = "caution"
2020-01-12 18:09:11 -05:00
case InfoIcon, QuestionIcon:
2021-02-19 12:46:47 -05:00
data.Options.Icon = "note"
2020-01-04 22:21:39 -05:00
}
} else {
2020-01-09 20:46:53 -05:00
data.Operation = "displayAlert"
2021-03-03 21:52:05 -05:00
if opts.title != nil {
data.Text = *opts.title
2021-02-19 12:46:47 -05:00
data.Options.Message = text
2020-01-09 20:46:53 -05:00
}
2020-01-23 06:44:28 -05:00
switch kind {
case infoKind:
2021-02-19 12:46:47 -05:00
data.Options.As = "informational"
2020-01-23 06:44:28 -05:00
case warningKind:
2021-02-19 12:46:47 -05:00
data.Options.As = "warning"
2020-01-23 06:44:28 -05:00
case errorKind:
2021-02-19 12:46:47 -05:00
data.Options.As = "critical"
2020-01-04 22:21:39 -05:00
}
}
2021-03-03 21:52:05 -05:00
if kind == questionKind {
// alert defaults to a single button, we need two
if opts.cancelLabel == nil && !dialog {
opts.cancelLabel = stringPtr("Cancel")
2020-01-04 22:21:39 -05:00
}
2021-03-03 21:52:05 -05:00
} else {
// dialog defaults to two buttons, we need one
if opts.okLabel == nil && dialog {
opts.okLabel = stringPtr("OK")
2020-01-04 22:21:39 -05:00
}
2021-03-03 21:52:05 -05:00
// 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")
2020-01-04 22:21:39 -05:00
}
2020-01-23 06:44:28 -05:00
if kind == questionKind {
2021-03-03 21:52:05 -05:00
if opts.cancelLabel == nil {
opts.cancelLabel = stringPtr("Cancel")
}
if opts.extraButton == nil {
data.Options.Buttons = []string{*opts.cancelLabel, *opts.okLabel}
2021-02-19 12:46:47 -05:00
data.Options.Default = 2
data.Options.Cancel = 1
2020-01-04 22:21:39 -05:00
} else {
2021-03-03 21:52:05 -05:00
data.Options.Buttons = []string{*opts.extraButton, *opts.cancelLabel, *opts.okLabel}
2021-02-19 12:46:47 -05:00
data.Options.Default = 3
data.Options.Cancel = 2
2020-01-04 22:21:39 -05:00
}
} else {
2021-03-03 21:52:05 -05:00
if opts.extraButton == nil {
data.Options.Buttons = []string{*opts.okLabel}
2021-02-19 12:46:47 -05:00
data.Options.Default = 1
2020-01-04 22:21:39 -05:00
} else {
2021-03-03 21:52:05 -05:00
data.Options.Buttons = []string{*opts.extraButton, *opts.okLabel}
2021-02-19 12:46:47 -05:00
data.Options.Default = 2
2020-01-04 22:21:39 -05:00
}
}
2020-01-24 07:52:45 -05:00
data.Extra = opts.extraButton
2020-01-04 22:21:39 -05:00
}
2021-03-03 21:52:05 -05:00
if kind == questionKind && opts.defaultCancel {
2021-02-19 12:46:47 -05:00
if data.Options.Cancel != 0 {
data.Options.Default = data.Options.Cancel
2021-03-03 21:52:05 -05:00
} else {
2021-02-19 12:46:47 -05:00
data.Options.Default = 1
2020-01-04 22:21:39 -05:00
}
}
2020-01-28 07:46:43 -05:00
out, err := zenutil.Run(opts.ctx, "msg", data)
2021-03-03 22:32:55 -05:00
if len(out) > 0 && opts.extraButton != nil &&
string(out[:len(out)-1]) == *opts.extraButton {
return false, ErrExtraButton
}
2020-01-04 22:21:39 -05:00
if err, ok := err.(*exec.ExitError); ok && err.ExitCode() == 1 {
return false, nil
}
if err != nil {
return false, err
}
return true, err
}