-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathtray.go
103 lines (93 loc) · 2.22 KB
/
tray.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
95
96
97
98
99
100
101
102
103
// Copyright 2020 Alexey Krivonogov. All rights reserved.
// Use of this source code is governed by a MIT license
// that can be found in the LICENSE file.
// +build tray
package main
import (
"eonza/lib"
es "eonza/script"
"fmt"
"os"
"github.com/gentee/systray"
"github.com/kataras/golog"
)
var isTray bool = true
func CreateSysTray() {
if storage.Settings.HideTray /*|| cfg.HTTP.Access == AccessHost*/ {
return
}
go systray.Run(TrayReady, TrayExit)
}
func HideTray() {
systray.Quit()
}
func TrayExit() {
}
func TrayReady() {
chanFav := make(chan *systray.MenuItem)
systray.SetIcon(WebAsset(`favicon.ico`))
title := storage.Settings.Title
if len(title) == 0 {
title = fmt.Sprintf("%s:%d", appInfo.Title, cfg.HTTP.Port)
}
systray.SetTitle(title)
systray.SetTooltip(appInfo.Title)
var langId int
us := RootUserSettings()
if len(us.Lang) > 0 {
langId = langsId[us.Lang]
}
glob := &langRes[langId]
scriptTitle := func(name string) string {
ret := name
if iscript := getScript(name); iscript != nil {
ret = es.ReplaceVars(iscript.Settings.Title, iscript.Langs[us.Lang], glob)
}
if len(ret) == 0 {
return name
}
return ret
}
for i, item := range us.Favs {
var m *systray.MenuItem
if (item.IsFolder && len(item.Children) == 0) || i > 15 {
continue
}
m = systray.AddMenuItemChan(scriptTitle(item.Name), item.Name, chanFav)
if item.IsFolder {
for j, sub := range item.Children {
if j > 15 {
break
}
m.AddSubMenuItemChan(scriptTitle(sub.Name), sub.Name, chanFav)
}
}
}
systray.AddSeparator()
open := Lang(langId, `openbrowser`)
mOpen := systray.AddMenuItem(open, open)
systray.AddSeparator()
shutdown := Lang(langId, `shutdown`)
mQuit := systray.AddMenuItem(shutdown, shutdown)
go func() {
var menuItem *systray.MenuItem
for {
select {
case menuItem = <-chanFav:
_, name := menuItem.Name()
if len(name) > 0 {
_, err := lib.LocalGet(cfg.HTTP.Port, fmt.Sprintf("api/run?name=%s", name))
if err != nil {
golog.Error(err)
}
}
case <-mOpen.ClickedCh:
lib.Open(fmt.Sprintf("http://%s:%d", Localhost, cfg.HTTP.Port))
case <-mQuit.ClickedCh:
golog.Info(`Tray shutdown`)
stopchan <- os.Interrupt
systray.Quit()
}
}
}()
}