Skip to content

Commit

Permalink
Added notification
Browse files Browse the repository at this point in the history
  • Loading branch information
asticode committed Apr 1, 2018
1 parent e5bb0a3 commit bacb3ae
Show file tree
Hide file tree
Showing 5 changed files with 163 additions and 22 deletions.
29 changes: 28 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,33 @@ time.Sleep(time.Second)
t.SetImage(astilectron.PtrStr("/path/to/image-2.png"))
```

## Notifications

```go
// Create the notification
var n = a.NewNotification(&astilectron.NotificationOptions{
Body: "My Body",
HasReply: astilectron.PtrBool(true), // Only MacOSX
Icon: "/path/to/icon",
ReplyPlaceholder: "type your reply here", // Only MacOSX
Title: "My title",
})

// Add listeners
n.On(astilectron.EventNameNotificationEventClicked, func(e astilectron.Event) (deleteListener bool) {
astilog.Debug("the notification has been clicked!")
return
})
// Only for MacOSX
n.On(astilectron.EventNameNotificationEventReplied, func(e astilectron.Event) (deleteListener bool) {
astilog.Debugf("the user has replied to the notification: %s", e.Reply)
return
})

// Show notification
n.Show()
```

## Dock (MacOSX only)

```go
Expand Down Expand Up @@ -456,7 +483,7 @@ document.addEventListener('astilectron-ready', function() {
- [x] session
- [x] accelerators (shortcuts)
- [x] dock
- [ ] notifications (macosx)
- [x] notifications
- [ ] loader
- [ ] file methods (drag & drop, ...)
- [ ] clipboard methods
Expand Down
16 changes: 15 additions & 1 deletion astilectron.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (
// Versions
const (
DefaultAcceptTCPTimeout = 30 * time.Second
VersionAstilectron = "0.18.0"
VersionAstilectron = "0.19.0"
VersionElectron = "1.8.1"
)

Expand Down Expand Up @@ -61,6 +61,7 @@ type Astilectron struct {
reader *reader
stderrWriter *astiexec.StdWriter
stdoutWriter *astiexec.StdWriter
supported *Supported
writer *writer
}

Expand All @@ -74,6 +75,11 @@ type Options struct {
ElectronSwitches []string
}

// Supported represents Astilectron supported features
type Supported struct {
Notification *bool `json:"notification"`
}

// New creates a new Astilectron instance
func New(o Options) (a *Astilectron, err error) {
// Validate the OS
Expand Down Expand Up @@ -280,6 +286,9 @@ func (a *Astilectron) executeCmd(cmd *exec.Cmd) (err error) {

// Create dock
a.dock = newDock(a.canceller, a.dispatcher, a.identifier, a.writer)

// Update supported features
a.supported = e.Supported
return
}

Expand Down Expand Up @@ -401,3 +410,8 @@ func (a *Astilectron) NewWindowInDisplay(d *Display, url string, o *WindowOption
func (a *Astilectron) NewTray(o *TrayOptions) *Tray {
return newTray(o, a.canceller, a.dispatcher, a.identifier, a.writer)
}

// NewNotification creates a new notification
func (a *Astilectron) NewNotification(o *NotificationOptions) *Notification {
return newNotification(o, a.supported != nil && a.supported.Notification != nil && *a.supported.Notification, a.canceller, a.dispatcher, a.identifier, a.writer)
}
44 changes: 24 additions & 20 deletions event.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,26 +20,30 @@ type Event struct {
// This is a list of all possible payloads.
// A choice was made not to use interfaces since it's a pain in the ass asserting each an every payload afterwards
// We use pointers so that omitempty works
Badge string `json:"badge,omitempty"`
BounceType string `json:"bounceType,omitempty"`
CallbackID string `json:"callbackId,omitempty"`
Displays *EventDisplays `json:"displays,omitempty"`
FilePath string `json:"filePath,omitempty"`
ID *int `json:"id,omitempty"`
Image string `json:"image,omitempty"`
Menu *EventMenu `json:"menu,omitempty"`
MenuItem *EventMenuItem `json:"menuItem,omitempty"`
MenuItemOptions *MenuItemOptions `json:"menuItemOptions,omitempty"`
MenuItemPosition *int `json:"menuItemPosition,omitempty"`
MenuPopupOptions *MenuPopupOptions `json:"menuPopupOptions,omitempty"`
Message *EventMessage `json:"message,omitempty"`
SessionID string `json:"sessionId,omitempty"`
TrayOptions *TrayOptions `json:"trayOptions,omitempty"`
URL string `json:"url,omitempty"`
URLNew string `json:"newUrl,omitempty"`
URLOld string `json:"oldUrl,omitempty"`
WindowID string `json:"windowId,omitempty"`
WindowOptions *WindowOptions `json:"windowOptions,omitempty"`
Badge string `json:"badge,omitempty"`
BounceType string `json:"bounceType,omitempty"`
CallbackID string `json:"callbackId,omitempty"`
Displays *EventDisplays `json:"displays,omitempty"`
FilePath string `json:"filePath,omitempty"`
ID *int `json:"id,omitempty"`
Image string `json:"image,omitempty"`
Index *int `json:"index,omitempty"`
Menu *EventMenu `json:"menu,omitempty"`
MenuItem *EventMenuItem `json:"menuItem,omitempty"`
MenuItemOptions *MenuItemOptions `json:"menuItemOptions,omitempty"`
MenuItemPosition *int `json:"menuItemPosition,omitempty"`
MenuPopupOptions *MenuPopupOptions `json:"menuPopupOptions,omitempty"`
Message *EventMessage `json:"message,omitempty"`
NotificationOptions *NotificationOptions `json:"notificationOptions,omitempty"`
Reply string `json:"reply,omitempty"`
SessionID string `json:"sessionId,omitempty"`
Supported *Supported `json:"supported,omitempty"`
TrayOptions *TrayOptions `json:"trayOptions,omitempty"`
URL string `json:"url,omitempty"`
URLNew string `json:"newUrl,omitempty"`
URLOld string `json:"oldUrl,omitempty"`
WindowID string `json:"windowId,omitempty"`
WindowOptions *WindowOptions `json:"windowOptions,omitempty"`
}

// EventDisplays represents events displays
Expand Down
66 changes: 66 additions & 0 deletions notification.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package astilectron

import "github.com/asticode/go-astitools/context"

// Notification event names
const (
eventNameNotificationCmdCreate = "notification.cmd.create"
eventNameNotificationCmdShow = "notification.cmd.show"
EventNameNotificationEventClicked = "notification.event.clicked"
EventNameNotificationEventClosed = "notification.event.closed"
EventNameNotificationEventCreated = "notification.event.created"
EventNameNotificationEventReplied = "notification.event.replied"
EventNameNotificationEventShown = "notification.event.shown"
)

// Notification represents a notification
// https://github.com/electron/electron/blob/v1.8.1/docs/api/notification.md
type Notification struct {
isSupported bool
o *NotificationOptions
*object
}

// NotificationOptions represents notification options
type NotificationOptions struct {
Body string `json:"body,omitempty"`
HasReply *bool `json:"hasReply,omitempty"`
Icon string `json:"icon,omitempty"`
ReplyPlaceholder string `json:"replyPlaceholder,omitempty"`
Silent *bool `json:"silent,omitempty"`
Sound string `json:"sound,omitempty"`
Subtitle string `json:"subtitle,omitempty"`
Title string `json:"title,omitempty"`
}

func newNotification(o *NotificationOptions, isSupported bool, c *asticontext.Canceller, d *dispatcher, i *identifier, wrt *writer) *Notification {
return &Notification{
isSupported: isSupported,
o: o,
object: newObject(nil, c, d, i, wrt, i.new()),
}
}

// Create creates the notification
func (n *Notification) Create() (err error) {
if !n.isSupported {
return
}
if err = n.isActionable(); err != nil {
return
}
_, err = synchronousEvent(n.c, n, n.w, Event{Name: eventNameNotificationCmdCreate, TargetID: n.id, NotificationOptions: n.o}, EventNameNotificationEventCreated)
return
}

// Show shows the notification
func (n *Notification) Show() (err error) {
if !n.isSupported {
return
}
if err = n.isActionable(); err != nil {
return
}
_, err = synchronousEvent(n.c, n, n.w, Event{Name: eventNameNotificationCmdShow, TargetID: n.id}, EventNameNotificationEventShown)
return
}
30 changes: 30 additions & 0 deletions notification_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package astilectron

import (
"testing"

"github.com/asticode/go-astitools/context"
)

func TestNotification_Actions(t *testing.T) {
// Init
var c = asticontext.NewCanceller()
var d = newDispatcher()
var i = newIdentifier()
var wrt = &mockedWriter{}
var w = newWriter(wrt)
var n = newNotification(&NotificationOptions{
Body: "body",
HasReply: PtrBool(true),
Icon: "/path/to/icon",
ReplyPlaceholder: "placeholder",
Silent: PtrBool(true),
Sound: "sound",
Subtitle: "subtitle",
Title: "title",
}, true, c, d, i, w)

// Actions
testObjectAction(t, func() error { return n.Create() }, n.object, wrt, "{\"name\":\""+eventNameNotificationCmdCreate+"\",\"targetID\":\""+n.id+"\",\"notificationOptions\":{\"body\":\"body\",\"hasReply\":true,\"icon\":\"/path/to/icon\",\"replyPlaceholder\":\"placeholder\",\"silent\":true,\"sound\":\"sound\",\"subtitle\":\"subtitle\",\"title\":\"title\"}}\n", EventNameNotificationEventCreated)
testObjectAction(t, func() error { return n.Show() }, n.object, wrt, "{\"name\":\""+eventNameNotificationCmdShow+"\",\"targetID\":\""+n.id+"\"}\n", EventNameNotificationEventShown)
}

0 comments on commit bacb3ae

Please sign in to comment.