forked from overlordtm/trayhost
-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathtrayhost_test.go
94 lines (84 loc) · 2.24 KB
/
trayhost_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package trayhost_test
import (
"fmt"
"io/ioutil"
"log"
"os"
"path/filepath"
"time"
"github.com/shurcooL/trayhost"
)
func Example() {
menuItems := []trayhost.MenuItem{
{
Title: "Example Item",
Handler: func() {
fmt.Println("do stuff")
},
},
{
Title: "Get Clipboard Content",
Handler: func() {
cc, err := trayhost.GetClipboardContent()
if err != nil {
fmt.Printf("GetClipboardContent() error: %v\n", err)
return
}
fmt.Printf("Text: %q\n", cc.Text)
fmt.Printf("Image: %v len(%v)\n", cc.Image.Kind, len(cc.Image.Bytes))
fmt.Printf("Files: len(%v) %v\n", len(cc.Files), cc.Files)
},
},
{
Title: "Set Clipboard Text",
Handler: func() {
const text = "this text gets copied"
trayhost.SetClipboardText(text)
fmt.Printf("Text %q got copied into your clipboard.\n", text)
},
},
{
// Displaying notifications requires a proper app bundle and won't work without one.
// See https://godoc.org/github.com/shurcooL/trayhost#hdr-Notes.
Title: "Display Notification",
Handler: func() {
notification := trayhost.Notification{
Title: "Example Notification",
Body: "Notification body text is here.",
Timeout: 3 * time.Second,
Handler: func() {
fmt.Println("do stuff when notification is clicked")
},
}
if cc, err := trayhost.GetClipboardContent(); err == nil && cc.Image.Kind != "" {
// Use image from clipboard as notification image.
notification.Image = cc.Image
}
notification.Display()
},
},
trayhost.SeparatorMenuItem(),
{
Title: "Quit",
Handler: trayhost.Exit,
},
}
// On macOS, when you run an app bundle, the working directory of the executed process
// is the root directory (/), not the app bundle's Contents/Resources directory.
// Change directory to Resources so that we can load resources from there.
ep, err := os.Executable()
if err != nil {
log.Fatalln("os.Executable:", err)
}
err = os.Chdir(filepath.Join(filepath.Dir(ep), "..", "Resources"))
if err != nil {
log.Fatalln("os.Chdir:", err)
}
// Load tray icon.
iconData, err := ioutil.ReadFile("icon@2x.png")
if err != nil {
log.Fatalln(err)
}
trayhost.Initialize("Example App", iconData, menuItems)
trayhost.EnterLoop()
}