Skip to content
This repository has been archived by the owner on Feb 16, 2024. It is now read-only.

Commit

Permalink
added tray popupcontextmenu(#373) (#389)
Browse files Browse the repository at this point in the history
* added tray popupcontextmenu(#373)

* update requested changes
  • Loading branch information
worlpaker authored May 4, 2023
1 parent 26d5ce8 commit 408b395
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 10 deletions.
2 changes: 1 addition & 1 deletion astilectron.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
// Versions
const (
DefaultAcceptTCPTimeout = 30 * time.Second
DefaultVersionAstilectron = "0.56.0"
DefaultVersionAstilectron = "0.57.0"
DefaultVersionElectron = "11.4.3"
)

Expand Down
47 changes: 38 additions & 9 deletions tray.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,17 @@ import (

// Tray event names
const (
EventNameTrayCmdCreate = "tray.cmd.create"
EventNameTrayCmdDestroy = "tray.cmd.destroy"
EventNameTrayCmdSetImage = "tray.cmd.set.image"
EventNameTrayEventClicked = "tray.event.clicked"
EventNameTrayEventCreated = "tray.event.created"
EventNameTrayEventDestroyed = "tray.event.destroyed"
EventNameTrayEventDoubleClicked = "tray.event.double.clicked"
EventNameTrayEventImageSet = "tray.event.image.set"
EventNameTrayEventRightClicked = "tray.event.right.clicked"
EventNameTrayCmdCreate = "tray.cmd.create"
EventNameTrayCmdDestroy = "tray.cmd.destroy"
EventNameTrayCmdSetImage = "tray.cmd.set.image"
EventNameTrayCmdPopupContextMenu = "tray.cmd.popup.context.menu"
EventNameTrayEventClicked = "tray.event.clicked"
EventNameTrayEventCreated = "tray.event.created"
EventNameTrayEventDestroyed = "tray.event.destroyed"
EventNameTrayEventDoubleClicked = "tray.event.double.clicked"
EventNameTrayEventImageSet = "tray.event.image.set"
EventNameTrayEventRightClicked = "tray.event.right.clicked"
EventNameTrayEventContextMenuPoppedUp = "tray.event.context.menu.popped.up"
)

// Tray represents a tray
Expand All @@ -34,6 +36,12 @@ type TrayOptions struct {
Tooltip *string `json:"tooltip,omitempty"`
}

// TrayPopUpOptions represents Tray PopUpContextMenu options
type TrayPopUpOptions struct {
Menu *Menu
Position *PositionOptions
}

// newTray creates a new tray
func newTray(ctx context.Context, o *TrayOptions, d *dispatcher, i *identifier, wrt *writer) (t *Tray) {
// Init
Expand Down Expand Up @@ -83,3 +91,24 @@ func (t *Tray) SetImage(image string) (err error) {
_, err = synchronousEvent(t.ctx, t, t.w, Event{Name: EventNameTrayCmdSetImage, Image: image, TargetID: t.id}, EventNameTrayEventImageSet)
return
}

// PopUpContextMenu pops up the context menu of the tray icon.
// When menu is passed, the menu will be shown instead of the tray icon's context menu.
// The position is only available on Windows, and it is (0, 0) by default.
// https://www.electronjs.org/docs/latest/api/tray#traypopupcontextmenumenu-position-macos-windows
func (t *Tray) PopUpContextMenu(p *TrayPopUpOptions) (err error) {
var em *EventMenu
var mp *MenuPopupOptions
if err = t.ctx.Err(); err != nil {
return
}
if p.Menu != nil {
em = p.Menu.toEvent()
}
if p.Position != nil {
mp = &MenuPopupOptions{PositionOptions: *p.Position}
}
var e = Event{Name: EventNameTrayCmdPopupContextMenu, TargetID: t.id, Menu: em, MenuPopupOptions: mp}
_, err = synchronousEvent(t.ctx, t, t.w, e, EventNameTrayEventContextMenuPoppedUp)
return
}
18 changes: 18 additions & 0 deletions tray_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,28 @@ func TestTray_Actions(t *testing.T) {
Image: astikit.StrPtr("/path/to/image"),
Tooltip: astikit.StrPtr("tooltip"),
}, d, i, w)
var m = tr.NewMenu([]*MenuItemOptions{
{
Label: astikit.StrPtr("Root 1"),
SubMenu: []*MenuItemOptions{
{Label: astikit.StrPtr("Item 1")},
{Label: astikit.StrPtr("Item 2")},
{Type: MenuItemTypeSeparator},
{Label: astikit.StrPtr("Item 3")},
},
}})
var p = &PositionOptions{
X: astikit.IntPtr(250),
Y: astikit.IntPtr(250),
}

// Actions
testObjectAction(t, func() error { return tr.Create() }, tr.object, wrt, "{\"name\":\""+EventNameTrayCmdCreate+"\",\"targetID\":\""+tr.id+"\",\"trayOptions\":{\"image\":\"/path/to/image\",\"tooltip\":\"tooltip\"}}\n", EventNameTrayEventCreated, nil)
testObjectAction(t, func() error { return tr.SetImage("test") }, tr.object, wrt, "{\"name\":\""+EventNameTrayCmdSetImage+"\",\"targetID\":\""+tr.id+"\",\"image\":\"test\"}\n", EventNameTrayEventImageSet, nil)
testObjectAction(t, func() error { return tr.PopUpContextMenu(&TrayPopUpOptions{}) }, tr.object, wrt, "{\"name\":\""+EventNameTrayCmdPopupContextMenu+"\",\"targetID\":\""+tr.id+"\"}\n", EventNameTrayEventContextMenuPoppedUp, nil)
testObjectAction(t, func() error { return tr.PopUpContextMenu(&TrayPopUpOptions{Position: p}) }, tr.object, wrt, "{\"name\":\""+EventNameTrayCmdPopupContextMenu+"\",\"targetID\":\""+tr.id+"\",\"menuPopupOptions\":{\"x\":250,\"y\":250}}\n", EventNameTrayEventContextMenuPoppedUp, nil)
testObjectAction(t, func() error { return tr.PopUpContextMenu(&TrayPopUpOptions{Menu: m}) }, tr.object, wrt, "{\"name\":\""+EventNameTrayCmdPopupContextMenu+"\",\"targetID\":\""+tr.id+"\",\"menu\":{\"id\":\""+m.id+"\",\"items\":[{\"id\":\"3\",\"options\":{\"label\":\"Root 1\"},\"rootId\":\""+m.rootID+"\",\"submenu\":{\"id\":\"4\",\"items\":[{\"id\":\"5\",\"options\":{\"label\":\"Item 1\"},\"rootId\":\""+m.rootID+"\"},{\"id\":\"6\",\"options\":{\"label\":\"Item 2\"},\"rootId\":\""+m.rootID+"\"},{\"id\":\"7\",\"options\":{\"type\":\"separator\"},\"rootId\":\""+m.rootID+"\"},{\"id\":\"8\",\"options\":{\"label\":\"Item 3\"},\"rootId\":\""+m.rootID+"\"}],\"rootId\":\""+m.rootID+"\"}}],\"rootId\":\""+m.rootID+"\"}}\n", EventNameTrayEventContextMenuPoppedUp, nil)
testObjectAction(t, func() error { return tr.PopUpContextMenu(&TrayPopUpOptions{Menu: m, Position: p}) }, tr.object, wrt, "{\"name\":\""+EventNameTrayCmdPopupContextMenu+"\",\"targetID\":\""+tr.id+"\",\"menu\":{\"id\":\""+m.id+"\",\"items\":[{\"id\":\"3\",\"options\":{\"label\":\"Root 1\"},\"rootId\":\""+m.rootID+"\",\"submenu\":{\"id\":\"4\",\"items\":[{\"id\":\"5\",\"options\":{\"label\":\"Item 1\"},\"rootId\":\""+m.rootID+"\"},{\"id\":\"6\",\"options\":{\"label\":\"Item 2\"},\"rootId\":\""+m.rootID+"\"},{\"id\":\"7\",\"options\":{\"type\":\"separator\"},\"rootId\":\""+m.rootID+"\"},{\"id\":\"8\",\"options\":{\"label\":\"Item 3\"},\"rootId\":\""+m.rootID+"\"}],\"rootId\":\""+m.rootID+"\"}}],\"rootId\":\""+m.rootID+"\"},\"menuPopupOptions\":{\"x\":250,\"y\":250}}\n", EventNameTrayEventContextMenuPoppedUp, nil)
testObjectAction(t, func() error { return tr.Destroy() }, tr.object, wrt, "{\"name\":\""+EventNameTrayCmdDestroy+"\",\"targetID\":\""+tr.id+"\"}\n", EventNameTrayEventDestroyed, nil)
assert.True(t, tr.ctx.Err() != nil)
}
Expand Down

0 comments on commit 408b395

Please sign in to comment.