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