zenity/msg_darwin.go

51 lines
1.2 KiB
Go
Raw Normal View History

2020-01-04 22:21:39 -05:00
package zenity
import (
2021-03-05 21:29:00 -05:00
"bytes"
2020-01-04 22:21:39 -05:00
"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-03-04 21:01:59 -05:00
var data zenutil.Dialog
2021-02-19 12:46:47 -05:00
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
2021-04-06 07:35:22 -04:00
data.Options.Icon = opts.icon.String()
2020-01-04 22:21:39 -05:00
} else {
2020-01-09 20:46:53 -05:00
data.Operation = "displayAlert"
2021-04-06 07:35:22 -04:00
data.Options.As = kind.String()
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-04 22:21:39 -05:00
}
2021-04-06 07:35:22 -04:00
data.SetButtons(getButtons(dialog, kind == questionKind, opts))
2020-01-04 22:21:39 -05:00
2021-03-04 21:01:59 -05:00
out, err := zenutil.Run(opts.ctx, "dialog", data)
2020-01-04 22:21:39 -05:00
if err, ok := err.(*exec.ExitError); ok && err.ExitCode() == 1 {
2021-03-05 21:29:00 -05:00
if opts.extraButton != nil &&
*opts.extraButton == string(bytes.TrimSuffix(out, []byte{'\n'})) {
2021-03-04 21:01:59 -05:00
return false, ErrExtraButton
}
2020-01-04 22:21:39 -05:00
return false, nil
}
if err != nil {
return false, err
}
return true, err
}