Notifications (macos).

This commit is contained in:
Nuno Cruces 2020-01-26 16:04:49 +00:00
parent d95237cefb
commit c2c09ddd32
18 changed files with 102 additions and 18 deletions

View file

@ -3,7 +3,7 @@
[![GoDoc](https://godoc.org/github.com/ncruces/zenity?status.svg)](https://godoc.org/github.com/ncruces/zenity) [![GoDoc](https://godoc.org/github.com/ncruces/zenity?status.svg)](https://godoc.org/github.com/ncruces/zenity)
This repo includes both a cross-platform Go package providing This repo includes both a cross-platform Go package providing
[Zenity](https://help.gnome.org/users/zenity/)-like dialogs [Zenity](https://help.gnome.org/users/zenity/stable/)-like dialogs
(simple dialogs that interact graphically with the user), (simple dialogs that interact graphically with the user),
as well as a *“port”* of the `zenity` command to both Windows and macOS based on that library. as well as a *“port”* of the `zenity` command to both Windows and macOS based on that library.

View file

@ -8,7 +8,7 @@ import "image/color"
// //
// Valid options: Title, Color, ShowPalette. // Valid options: Title, Color, ShowPalette.
func SelectColor(options ...Option) (color.Color, error) { func SelectColor(options ...Option) (color.Color, error) {
return selectColor(options...) return selectColor(options)
} }
// Color returns an Option to set the color. // Color returns an Option to set the color.

View file

@ -7,7 +7,7 @@ import (
"github.com/ncruces/zenity/internal/zenutil" "github.com/ncruces/zenity/internal/zenutil"
) )
func selectColor(options ...Option) (color.Color, error) { func selectColor(options []Option) (color.Color, error) {
opts := applyOptions(options) opts := applyOptions(options)
var data zenutil.Color var data zenutil.Color

View file

@ -9,7 +9,7 @@ import (
"github.com/ncruces/zenity/internal/zenutil" "github.com/ncruces/zenity/internal/zenutil"
) )
func selectColor(options ...Option) (color.Color, error) { func selectColor(options []Option) (color.Color, error) {
opts := applyOptions(options) opts := applyOptions(options)
args := []string{"--color-selection"} args := []string{"--color-selection"}

View file

@ -20,7 +20,7 @@ func init() {
} }
} }
func selectColor(options ...Option) (color.Color, error) { func selectColor(options []Option) (color.Color, error) {
opts := applyOptions(options) opts := applyOptions(options)
// load custom colors // load custom colors

View file

@ -11,7 +11,7 @@ import (
// //
// Valid options: Title, Directory, Filename, ShowHidden, FileFilter(s). // Valid options: Title, Directory, Filename, ShowHidden, FileFilter(s).
func SelectFile(options ...Option) (string, error) { func SelectFile(options ...Option) (string, error) {
return selectFile(options...) return selectFile(options)
} }
// SelectFileMutiple displays the multiple file selection dialog. // SelectFileMutiple displays the multiple file selection dialog.
@ -20,7 +20,7 @@ func SelectFile(options ...Option) (string, error) {
// //
// Valid options: Title, Directory, Filename, ShowHidden, FileFilter(s). // Valid options: Title, Directory, Filename, ShowHidden, FileFilter(s).
func SelectFileMutiple(options ...Option) ([]string, error) { func SelectFileMutiple(options ...Option) ([]string, error) {
return selectFileMutiple(options...) return selectFileMutiple(options)
} }
// SelectFileSave displays the save file selection dialog. // SelectFileSave displays the save file selection dialog.
@ -30,7 +30,7 @@ func SelectFileMutiple(options ...Option) ([]string, error) {
// Valid options: Title, Filename, ConfirmOverwrite, ConfirmCreate, ShowHidden, // Valid options: Title, Filename, ConfirmOverwrite, ConfirmCreate, ShowHidden,
// FileFilter(s). // FileFilter(s).
func SelectFileSave(options ...Option) (string, error) { func SelectFileSave(options ...Option) (string, error) {
return selectFileSave(options...) return selectFileSave(options)
} }
// Filename returns an Option to set the filename. // Filename returns an Option to set the filename.

View file

@ -7,7 +7,7 @@ import (
"github.com/ncruces/zenity/internal/zenutil" "github.com/ncruces/zenity/internal/zenutil"
) )
func selectFile(options ...Option) (string, error) { func selectFile(options []Option) (string, error) {
opts := applyOptions(options) opts := applyOptions(options)
data := zenutil.File{ data := zenutil.File{
@ -35,7 +35,7 @@ func selectFile(options ...Option) (string, error) {
return string(out), nil return string(out), nil
} }
func selectFileMutiple(options ...Option) ([]string, error) { func selectFileMutiple(options []Option) ([]string, error) {
opts := applyOptions(options) opts := applyOptions(options)
data := zenutil.File{ data := zenutil.File{
@ -68,7 +68,7 @@ func selectFileMutiple(options ...Option) ([]string, error) {
return strings.Split(string(out), zenutil.Separator), nil return strings.Split(string(out), zenutil.Separator), nil
} }
func selectFileSave(options ...Option) (string, error) { func selectFileSave(options []Option) (string, error) {
opts := applyOptions(options) opts := applyOptions(options)
data := zenutil.File{ data := zenutil.File{

View file

@ -9,7 +9,7 @@ import (
"github.com/ncruces/zenity/internal/zenutil" "github.com/ncruces/zenity/internal/zenutil"
) )
func selectFile(options ...Option) (string, error) { func selectFile(options []Option) (string, error) {
opts := applyOptions(options) opts := applyOptions(options)
args := []string{"--file-selection"} args := []string{"--file-selection"}
@ -37,7 +37,7 @@ func selectFile(options ...Option) (string, error) {
return string(out), nil return string(out), nil
} }
func selectFileMutiple(options ...Option) ([]string, error) { func selectFileMutiple(options []Option) ([]string, error) {
opts := applyOptions(options) opts := applyOptions(options)
args := []string{"--file-selection", "--multiple", "--separator", zenutil.Separator} args := []string{"--file-selection", "--multiple", "--separator", zenutil.Separator}
@ -65,7 +65,7 @@ func selectFileMutiple(options ...Option) ([]string, error) {
return strings.Split(string(out), zenutil.Separator), nil return strings.Split(string(out), zenutil.Separator), nil
} }
func selectFileSave(options ...Option) (string, error) { func selectFileSave(options []Option) (string, error) {
opts := applyOptions(options) opts := applyOptions(options)
args := []string{"--file-selection", "--save"} args := []string{"--file-selection", "--save"}

View file

@ -17,7 +17,7 @@ var (
shCreateItemFromParsingName = shell32.NewProc("SHCreateItemFromParsingName") shCreateItemFromParsingName = shell32.NewProc("SHCreateItemFromParsingName")
) )
func selectFile(options ...Option) (string, error) { func selectFile(options []Option) (string, error) {
opts := applyOptions(options) opts := applyOptions(options)
if opts.directory { if opts.directory {
res, _, err := pickFolders(opts, false) res, _, err := pickFolders(opts, false)
@ -53,7 +53,7 @@ func selectFile(options ...Option) (string, error) {
return syscall.UTF16ToString(res[:]), nil return syscall.UTF16ToString(res[:]), nil
} }
func selectFileMutiple(options ...Option) ([]string, error) { func selectFileMutiple(options []Option) ([]string, error) {
opts := applyOptions(options) opts := applyOptions(options)
if opts.directory { if opts.directory {
_, res, err := pickFolders(opts, true) _, res, err := pickFolders(opts, true)
@ -114,7 +114,7 @@ func selectFileMutiple(options ...Option) ([]string, error) {
return split, nil return split, nil
} }
func selectFileSave(options ...Option) (string, error) { func selectFileSave(options []Option) (string, error) {
opts := applyOptions(options) opts := applyOptions(options)
if opts.directory { if opts.directory {
res, _, err := pickFolders(opts, false) res, _, err := pickFolders(opts, false)

View file

@ -76,4 +76,16 @@ opts.cancelButton = {{json .Cancel}}
{{end -}} {{end -}}
var res = app[{{json .Operation}}]({{json .Text}}, opts).buttonReturned var res = app[{{json .Operation}}]({{json .Text}}, opts).buttonReturned
res === {{json .Extra}} ? res : void 0 res === {{json .Extra}} ? res : void 0
{{- end}}
{{define "notify" -}}var app = Application.currentApplication()
app.includeStandardAdditions = true
app.activate()
var opts = {}
{{if .Title -}}
opts.withTitle = {{json .Title}}
{{end -}}
{{if .Subtitle -}}
opts.subtitle = {{json .Subtitle}}
{{end -}}
void app.displayNotification({{json .Text}}, opts)
{{- end}}`)) {{- end}}`))

View file

@ -0,0 +1,14 @@
var app = Application.currentApplication()
app.includeStandardAdditions = true
app.activate()
var opts = {}
{{if .Title -}}
opts.withTitle = {{json .Title}}
{{end -}}
{{if .Subtitle -}}
opts.subtitle = {{json .Subtitle}}
{{end -}}
void app.displayNotification({{json .Text}}, opts)

View file

@ -61,3 +61,9 @@ type Msg struct {
Cancel int Cancel int
Default int Default int
} }
type Notify struct {
Text string
Title string
Subtitle string
}

6
notify.go Normal file
View file

@ -0,0 +1,6 @@
package zenity
// Notify displays a notification.
func Notify(text string, options ...Option) error {
return notify(text, options)
}

24
notify_darwin.go Normal file
View file

@ -0,0 +1,24 @@
package zenity
import (
"strings"
"github.com/ncruces/zenity/internal/zenutil"
)
func notify(text string, options []Option) error {
opts := applyOptions(options)
data := zenutil.Notify{
Text: text,
Title: opts.title,
}
if i := strings.IndexByte(text, '\n'); i >= 0 && i < len(text) {
data.Subtitle = text[:i]
data.Text = text[i+1:]
}
_, err := zenutil.Run("notify", data)
if err != nil {
return err
}
return nil
}

10
notify_test.go Normal file
View file

@ -0,0 +1,10 @@
package zenity_test
import "github.com/ncruces/zenity"
func ExampleNotify() {
zenity.Notify("An error has occurred.",
zenity.Title("Error"),
zenity.Icon(zenity.ErrorIcon))
// Output:
}

7
notify_unix.go Normal file
View file

@ -0,0 +1,7 @@
// +build !windows,!darwin
package zenity
func notify(text string, options []Option) error {
panic("not implemented")
}

5
notify_windows.go Normal file
View file

@ -0,0 +1,5 @@
package zenity
func notify(text string, options []Option) error {
panic("not implemented")
}

View file

@ -4,7 +4,7 @@
// It is inspired by, and closely follows the API of, the zenity program, which // It is inspired by, and closely follows the API of, the zenity program, which
// it uses to provide the functionality on various Unixes. See: // it uses to provide the functionality on various Unixes. See:
// //
// https://help.gnome.org/users/zenity/ // https://help.gnome.org/users/zenity/stable/
// //
// This package does not require cgo, and it does not impose any threading or // This package does not require cgo, and it does not impose any threading or
// initialization requirements. // initialization requirements.