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
16 changed files
with
250 additions
and
39 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,111 @@ | ||
package astilectron | ||
|
||
import "github.com/asticode/go-astitools/context" | ||
|
||
// Dock event names | ||
const ( | ||
eventNameDockCmdBounce = "dock.cmd.bounce" | ||
eventNameDockCmdBounceDownloads = "dock.cmd.bounce.downloads" | ||
eventNameDockCmdCancelBounce = "dock.cmd.cancel.bounce" | ||
eventNameDockCmdHide = "dock.cmd.hide" | ||
eventNameDockCmdSetBadge = "dock.cmd.set.badge" | ||
eventNameDockCmdSetIcon = "dock.cmd.set.icon" | ||
eventNameDockCmdShow = "dock.cmd.show" | ||
eventNameDockEventBadgeSet = "dock.event.badge.set" | ||
eventNameDockEventBouncing = "dock.event.bouncing" | ||
eventNameDockEventBouncingCancelled = "dock.event.bouncing.cancelled" | ||
eventNameDockEventDownloadsBouncing = "dock.event.download.bouncing" | ||
eventNameDockEventHidden = "dock.event.hidden" | ||
eventNameDockEventIconSet = "dock.event.icon.set" | ||
eventNameDockEventShown = "dock.event.shown" | ||
) | ||
|
||
// Dock bounce types | ||
const ( | ||
DockBounceTypeCritical = "critical" | ||
DockBounceTypeInformational = "informational" | ||
) | ||
|
||
// Dock represents a dock | ||
// https://github.com/electron/electron/blob/v1.8.1/docs/api/app.md#appdockbouncetype-macos | ||
type Dock struct { | ||
*object | ||
} | ||
|
||
func newDock(c *asticontext.Canceller, d *dispatcher, i *identifier, wrt *writer) *Dock { | ||
return &Dock{object: newObject(nil, c, d, i, wrt, targetIDDock)} | ||
} | ||
|
||
// Bounce bounces the dock | ||
func (d *Dock) Bounce(bounceType string) (id int, err error) { | ||
if err = d.isActionable(); err != nil { | ||
return | ||
} | ||
var e Event | ||
if e, err = synchronousEvent(d.c, d, d.w, Event{Name: eventNameDockCmdBounce, TargetID: d.id, BounceType: bounceType}, eventNameDockEventBouncing); err != nil { | ||
return | ||
} | ||
if e.ID != nil { | ||
id = *e.ID | ||
} | ||
return | ||
} | ||
|
||
// BounceDownloads bounces the downloads part of the dock | ||
func (d *Dock) BounceDownloads(filePath string) (err error) { | ||
if err = d.isActionable(); err != nil { | ||
return | ||
} | ||
_, err = synchronousEvent(d.c, d, d.w, Event{Name: eventNameDockCmdBounceDownloads, TargetID: d.id, FilePath: filePath}, eventNameDockEventDownloadsBouncing) | ||
return | ||
} | ||
|
||
// CancelBounce cancels the dock bounce | ||
func (d *Dock) CancelBounce(id int) (err error) { | ||
if err = d.isActionable(); err != nil { | ||
return | ||
} | ||
_, err = synchronousEvent(d.c, d, d.w, Event{Name: eventNameDockCmdCancelBounce, TargetID: d.id, ID: PtrInt(id)}, eventNameDockEventBouncingCancelled) | ||
return | ||
} | ||
|
||
// Hide hides the dock | ||
func (d *Dock) Hide() (err error) { | ||
if err = d.isActionable(); err != nil { | ||
return | ||
} | ||
_, err = synchronousEvent(d.c, d, d.w, Event{Name: eventNameDockCmdHide, TargetID: d.id}, eventNameDockEventHidden) | ||
return | ||
} | ||
|
||
// NewMenu creates a new dock menu | ||
func (d *Dock) NewMenu(i []*MenuItemOptions) *Menu { | ||
return newMenu(d.ctx, d.id, i, d.c, d.d, d.i, d.w) | ||
} | ||
|
||
// SetBadge sets the badge of the dock | ||
func (d *Dock) SetBadge(badge string) (err error) { | ||
if err = d.isActionable(); err != nil { | ||
return | ||
} | ||
_, err = synchronousEvent(d.c, d, d.w, Event{Name: eventNameDockCmdSetBadge, TargetID: d.id, Badge: badge}, eventNameDockEventBadgeSet) | ||
return | ||
} | ||
|
||
// SetIcon sets the icon of the dock | ||
func (d *Dock) SetIcon(image string) (err error) { | ||
if err = d.isActionable(); err != nil { | ||
return | ||
} | ||
_, err = synchronousEvent(d.c, d, d.w, Event{Name: eventNameDockCmdSetIcon, TargetID: d.id, Image: image}, eventNameDockEventIconSet) | ||
return | ||
} | ||
|
||
// Show shows the dock | ||
func (d *Dock) Show() (err error) { | ||
if err = d.isActionable(); err != nil { | ||
return | ||
} | ||
_, err = synchronousEvent(d.c, d, d.w, Event{Name: eventNameDockCmdShow, TargetID: d.id}, eventNameDockEventShown) | ||
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,41 @@ | ||
package astilectron | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/asticode/go-astitools/context" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestDock_Actions(t *testing.T) { | ||
// Init | ||
var c = asticontext.NewCanceller() | ||
var d = newDispatcher() | ||
var i = newIdentifier() | ||
var wrt = &mockedWriter{} | ||
var w = newWriter(wrt) | ||
var dck = newDock(c, d, i, w) | ||
|
||
// Actions | ||
testObjectAction(t, func() error { | ||
_, err := dck.Bounce(DockBounceTypeCritical) | ||
return err | ||
}, dck.object, wrt, "{\"name\":\""+eventNameDockCmdBounce+"\",\"targetID\":\""+dck.id+"\",\"bounceType\":\"critical\"}\n", eventNameDockEventBouncing) | ||
testObjectAction(t, func() error { return dck.BounceDownloads("/path/to/file") }, dck.object, wrt, "{\"name\":\""+eventNameDockCmdBounceDownloads+"\",\"targetID\":\""+dck.id+"\",\"filePath\":\"/path/to/file\"}\n", eventNameDockEventDownloadsBouncing) | ||
testObjectAction(t, func() error { return dck.CancelBounce(1) }, dck.object, wrt, "{\"name\":\""+eventNameDockCmdCancelBounce+"\",\"targetID\":\""+dck.id+"\",\"id\":1}\n", eventNameDockEventBouncingCancelled) | ||
testObjectAction(t, func() error { return dck.Hide() }, dck.object, wrt, "{\"name\":\""+eventNameDockCmdHide+"\",\"targetID\":\""+dck.id+"\"}\n", eventNameDockEventHidden) | ||
testObjectAction(t, func() error { return dck.SetBadge("badge") }, dck.object, wrt, "{\"name\":\""+eventNameDockCmdSetBadge+"\",\"targetID\":\""+dck.id+"\",\"badge\":\"badge\"}\n", eventNameDockEventBadgeSet) | ||
testObjectAction(t, func() error { return dck.SetIcon("/path/to/icon") }, dck.object, wrt, "{\"name\":\""+eventNameDockCmdSetIcon+"\",\"targetID\":\""+dck.id+"\",\"image\":\"/path/to/icon\"}\n", eventNameDockEventIconSet) | ||
testObjectAction(t, func() error { return dck.Show() }, dck.object, wrt, "{\"name\":\""+eventNameDockCmdShow+"\",\"targetID\":\""+dck.id+"\"}\n", eventNameDockEventShown) | ||
} | ||
|
||
func TestDock_NewMenu(t *testing.T) { | ||
var c = asticontext.NewCanceller() | ||
var d = newDispatcher() | ||
var i = newIdentifier() | ||
var wrt = &mockedWriter{} | ||
var w = newWriter(wrt) | ||
var dck = newDock(c, d, i, w) | ||
m := dck.NewMenu([]*MenuItemOptions{}) | ||
assert.Equal(t, dck.id, m.rootID) | ||
} |
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
Oops, something went wrong.