-
-
Notifications
You must be signed in to change notification settings - Fork 46
/
widget_recent_window.go
91 lines (74 loc) · 1.94 KB
/
widget_recent_window.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
package main
import (
"fmt"
"image"
"os"
)
// RecentWindowWidget is a widget displaying a recently activated window.
type RecentWindowWidget struct {
*ButtonWidget
window uint8
showTitle bool
lastID uint32
}
// NewRecentWindowWidget returns a new RecentWindowWidget.
func NewRecentWindowWidget(bw *BaseWidget, opts WidgetConfig) (*RecentWindowWidget, error) {
var window int64
if err := ConfigValue(opts.Config["window"], &window); err != nil {
return nil, err
}
var showTitle bool
_ = ConfigValue(opts.Config["showTitle"], &showTitle)
widget, err := NewButtonWidget(bw, opts)
if err != nil {
return nil, err
}
return &RecentWindowWidget{
ButtonWidget: widget,
window: uint8(window),
showTitle: showTitle,
}, nil
}
// RequiresUpdate returns true when the widget wants to be repainted.
func (w *RecentWindowWidget) RequiresUpdate() bool {
if int(w.window) < len(recentWindows) {
return w.lastID != recentWindows[w.window].ID
}
return w.BaseWidget.RequiresUpdate()
}
// Update renders the widget.
func (w *RecentWindowWidget) Update() error {
img := image.NewRGBA(image.Rect(0, 0, int(w.dev.Pixels), int(w.dev.Pixels)))
if int(w.window) < len(recentWindows) {
if w.lastID == recentWindows[w.window].ID {
return nil
}
w.lastID = recentWindows[w.window].ID
var name string
if w.showTitle {
name = recentWindows[w.window].Name
runes := []rune(name)
if len(runes) > 10 {
name = string(runes[:10])
}
}
w.label = name
w.SetImage(recentWindows[w.window].Icon)
return w.ButtonWidget.Update()
}
return w.render(w.dev, img)
}
// TriggerAction gets called when a button is pressed.
func (w *RecentWindowWidget) TriggerAction(hold bool) {
if xorg == nil {
fmt.Fprintln(os.Stderr, "xorg support is disabled!")
return
}
if int(w.window) < len(recentWindows) {
if hold {
_ = xorg.CloseWindow(recentWindows[w.window])
return
}
_ = xorg.RequestActivation(recentWindows[w.window])
}
}