forked from asticode/go-astilectron
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
163 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |