-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtaskbar.go
166 lines (148 loc) · 3.86 KB
/
taskbar.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
package taskbar
import (
"errors"
"fmt"
"os"
)
type session int
type backend int
const (
waylandSession session = iota
xorgSession
)
const (
unityBackend backend = iota
xappBackend
)
// Taskbar item data
type Taskbar struct {
session session // session type (wayland or xorg)
backend backend // taskbar backend (libunity or xapp)
unityEntry *libUnityEntry // LibUnity launcher entry
xappData *xappData // Data for Xapp backend
progress int // progress value (0-100)
pulse bool // whether taskbar pulse is enabled
count int // counter value (only supported by libunity API)
}
// Creates [Taskbar] item.
// `desktopName` is a name of desktop file to be worked with using libunity
// Launcher API (".desktop" suffix can be omitted). `xid` is an xorg window ID
// used in case if taskbar item is modified using xapp window hints, set it to 0
// if not used.
func Connect(desktopName string, xid int) (*Taskbar, error) {
var t Taskbar
var session session
var backend backend
// Detect session type
xdgSession := os.Getenv("XDG_SESSION_TYPE")
switch xdgSession {
case "wayland":
session = waylandSession
case "x11":
session = xorgSession
default:
return nil, errors.New(fmt.Sprintf("Unknown session type: %v", xdgSession))
}
// Set backend
if os.Getenv("GO_TASKBAR_BACKEND") == "libunity" {
backend = unityBackend
} else if os.Getenv("GO_TASKBAR_BACKEND") == "xapp" {
backend = xappBackend
} else if session == waylandSession {
backend = unityBackend
} else if os.Getenv("XDG_CURRENT_DESKTOP") == "X-Cinnamon" {
backend = xappBackend
} else {
backend = unityBackend
}
if overrideDesktopName, res := os.LookupEnv("GO_TASKBAR_DESKTOP_NAME"); res {
desktopName = overrideDesktopName
}
// Check if current backend can be used
if backend == unityBackend && desktopName == "" {
return nil, errors.New("LibUnity backend was chosen, but desktop file name is empty.")
}
if backend == xappBackend && xid == 0 {
return nil, errors.New("Xapp backend was chosen, but XID isn't provided.")
}
if backend == unityBackend {
entry, err := libUnityConnect(desktopName)
if err != nil {
return nil, err
}
t = Taskbar{session, backend, entry, nil, 0, false, 0}
} else {
xapp, err := xappConnect(uint32(xid))
if err != nil {
return nil, err
}
t = Taskbar{session, backend, nil, xapp, 0, false, 0}
}
return &t, nil
}
// Resets all properties and gracefully disconnects from taskbar
func (t *Taskbar) Disconnect() error {
if t == nil {
return errors.New("Not connected to taskbar.")
}
if t.backend == xappBackend {
return xappDisconnect(t.xappData)
} else {
return libUnityDisconnect(t.unityEntry)
}
}
// Gets current progress value
func (t *Taskbar) Progress() int {
return t.progress
}
// Sets progress value (0-100)
func (t *Taskbar) SetProgress(p int) error {
if t.progress != p {
if p > 100 {
p = 100
} else if p < 0 {
p = 0
}
t.progress = p
return t.update()
}
return nil
}
// Gets current pulse value
func (t *Taskbar) Pulse() bool {
return t.pulse
}
// Enables or disables pulse. This property highlights the item in taskbar,
// dragging user attention. If pulse is enabled, progress is not shown.
func (t *Taskbar) SetPulse(p bool) error {
if t.pulse != p {
t.pulse = p
return t.update()
}
return nil
}
// Gets current counter value
func (t *Taskbar) Count() int {
return t.count
}
// Sets counter value (only supported by libunity Launcher API)
func (t *Taskbar) SetCount(c int) error {
if t.backend == xappBackend {
return nil
}
if t.count != c {
t.count = c
return t.update()
}
return nil
}
func (t *Taskbar) update() error {
if t.pulse {
t.progress = 0
}
if t.backend == xappBackend {
return t.xappData.update(uint64(t.progress), t.pulse)
} else {
return t.unityEntry.update(float64(t.progress)/100.0, t.pulse, int64(t.count))
}
}