-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwindow_manager.go
195 lines (172 loc) · 4.03 KB
/
window_manager.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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
package squirssi
import (
"fmt"
"sync"
"code.dopame.me/veonik/squircy3/event"
"github.com/sirupsen/logrus"
"code.dopame.me/veonik/squirssi/widget"
)
type WindowManager struct {
windows []Window
activeIndex int
status *StatusWindow
events *event.Dispatcher
mu sync.RWMutex
}
func NewWindowManager(ev *event.Dispatcher) *WindowManager {
wm := &WindowManager{events: ev}
wm.status = &StatusWindow{bufferedWindow: newBufferedWindow("status", ev)}
wm.windows = []Window{wm.status}
return wm
}
func (wm *WindowManager) TabNames() ([]string, map[int]widget.ActivityType) {
wm.mu.RLock()
defer wm.mu.RUnlock()
res := make([]string, len(wm.windows))
activity := make(map[int]widget.ActivityType)
for i := 0; i < len(wm.windows); i++ {
win := wm.windows[i]
if win.HasNotice() {
activity[i] = widget.TabHasNotice
} else if win.HasActivity() {
activity[i] = widget.TabHasActivity
}
if wm.activeIndex == i {
res[i] = fmt.Sprintf(" %s ", win.Title())
} else {
res[i] = fmt.Sprintf(" %d ", i)
}
}
return res, activity
}
func (wm *WindowManager) Windows() []Window {
wm.mu.RLock()
defer wm.mu.RUnlock()
wins := make([]Window, len(wm.windows))
copy(wins, wm.windows)
return wins
}
func (wm *WindowManager) Len() int {
wm.mu.RLock()
defer wm.mu.RUnlock()
return len(wm.windows)
}
func (wm *WindowManager) ActiveIndex() int {
wm.mu.RLock()
defer wm.mu.RUnlock()
return wm.activeIndex
}
func (wm *WindowManager) Active() Window {
wm.mu.RLock()
defer wm.mu.RUnlock()
return wm.windows[wm.activeIndex]
}
func (wm *WindowManager) Append(w Window) {
wm.mu.Lock()
defer wm.mu.Unlock()
wm.windows = append(wm.windows, w)
}
func (wm *WindowManager) NamedOrActive(name string) Window {
var win Window
wm.mu.RLock()
defer wm.mu.RUnlock()
for _, w := range wm.windows {
if w.Title() == name {
win = w
break
}
}
if win == nil {
return wm.windows[wm.activeIndex]
}
return win
}
// Named returns the window with the given name, if it exists.
func (wm *WindowManager) Named(name string) Window {
var win Window
wm.mu.RLock()
defer wm.mu.RUnlock()
for _, w := range wm.windows {
if w.Title() == name {
win = w
break
}
}
return win
}
func (wm *WindowManager) Index(idx int) Window {
wm.mu.RLock()
defer wm.mu.RUnlock()
if idx >= len(wm.windows) || idx < 0 {
return nil
}
return wm.windows[idx]
}
func (wm *WindowManager) SelectIndex(idx int) {
wm.mu.Lock()
defer wm.mu.Unlock()
if idx >= len(wm.windows) || idx < 0 {
logrus.Warnf("failed to select window; no window #%d", idx)
return
}
wm.activeIndex = idx
wm.events.Emit("ui.DIRTY", nil)
}
func (wm *WindowManager) SelectNext() {
wm.mu.Lock()
defer wm.mu.Unlock()
idx := wm.activeIndex + 1
if idx >= len(wm.windows) || idx < 0 {
idx = 0
}
wm.activeIndex = idx
wm.events.Emit("ui.DIRTY", nil)
}
func (wm *WindowManager) SelectPrev() {
wm.mu.Lock()
defer wm.mu.Unlock()
idx := wm.activeIndex - 1
if idx >= len(wm.windows) || idx < 0 {
idx = len(wm.windows) - 1
}
wm.activeIndex = idx
wm.events.Emit("ui.DIRTY", nil)
}
// CloseIndex closes a window denoted by tab index.
func (wm *WindowManager) CloseIndex(ch int) {
if ch == 0 {
logrus.Warnln("cannot close status window")
return
}
wm.mu.Lock()
defer wm.mu.Unlock()
if ch >= len(wm.windows) {
logrus.Warnf("failed to close window; no window #%d", ch)
return
}
wm.windows = append(wm.windows[:ch], wm.windows[ch+1:]...)
if ch >= len(wm.windows) {
wm.activeIndex = len(wm.windows) - 1
}
wm.events.Emit("ui.DIRTY", nil)
}
// ScrollTo scrolls the currently active window up relative to the current line.
func (wm *WindowManager) ScrollOffset(offset int) {
wm.mu.RLock()
win := wm.windows[wm.activeIndex]
wm.mu.RUnlock()
wm.ScrollTo(win.CurrentLine() + offset)
}
// ScrollTo scrolls the currently active window to the given position.
func (wm *WindowManager) ScrollTo(pos int) {
wm.mu.Lock()
defer wm.mu.Unlock()
win := wm.windows[wm.activeIndex]
if pos < 0 {
pos = 0
} else if pos >= len(win.Lines()) {
pos = -1
}
win.ScrollTo(pos)
wm.events.Emit("ui.DIRTY", nil)
}