Debian Codegraph @

app/quake.go

raw
1 package app
2
3 import (
4 "os/exec"
5 "sync"
6 "sync/atomic"
7 "time"
8
9 "git.sr.ht/~rjarry/aerc/config"
10 "git.sr.ht/~rjarry/aerc/lib/log"
11 "git.sr.ht/~rjarry/aerc/lib/ui"
12 "github.com/riywo/loginshell"
13 "go.rockorager.dev/vaxis"
14 )
15
16 var qt quakeTerminal
17
18 type quakeTerminal struct {
19 mu sync.Mutex
20 rolling int32
21 visible bool
22 term *Terminal
23 }
24
25 func ToggleQuake() {
26 handleErr := func(err error) {
27 log.Errorf("quake-terminal: %v", err)
28 }
29 if !qt.HasTerm() {
30 shell, err := loginshell.Shell()
31 if err != nil {
32 handleErr(err)
33 return
34 }
35 args := []string{shell}
36 cmd := exec.Command(args[0], args[1:]...)
37 term, err := NewTerminal(cmd)
38 if err != nil {
39 handleErr(err)
40 return
41 }
42 term.OnClose = func(err error) {
43 if err != nil {
44 aerc.PushError(err.Error())
45 }
46 qt.Hide()
47 qt.SetTerm(nil)
48 }
49 qt.SetTerm(term)
50 }
51
52 if qt.Rolling() {
53 return
54 }
55
56 if qt.Visible() {
57 qt.Hide()
58 } else {
59 qt.Show()
60 }
61 }
62
63 func (q *quakeTerminal) Rolling() bool {
64 return atomic.LoadInt32(&q.rolling) > 0
65 }
66
67 func (q *quakeTerminal) SetTerm(t *Terminal) {
68 q.mu.Lock()
69 defer q.mu.Unlock()
70
71 q.term = t
72 }
73
74 func (q *quakeTerminal) HasTerm() bool {
75 q.mu.Lock()
76 defer q.mu.Unlock()
77
78 return q.term != nil
79 }
80
81 func (q *quakeTerminal) Visible() bool {
82 q.mu.Lock()
83 defer q.mu.Unlock()
84
85 return q.visible
86 }
87
88 // inputReturn is helper function to create dialog boxes.
89 func inputReturn() func(int) int {
90 return func(x int) int { return x }
91 }
92
93 // fixReturn is helper function to create dialog boxes.
94 func fixReturn(x int) func(int) int {
95 return func(_ int) int { return x }
96 }
97
98 func (q *quakeTerminal) Show() {
99 q.mu.Lock()
100 defer q.mu.Unlock()
101
102 if q.term == nil {
103 return
104 }
105
106 uiConfig := SelectedAccountUiConfig()
107 h := uiConfig.QuakeHeight
108
109 termBox := NewDialog(
110 ui.NewBox(q.term, "", "", uiConfig),
111 fixReturn(0),
112 fixReturn(0),
113 inputReturn(),
114 fixReturn(h),
115 )
116
117 f := Roller{
118 span: 100 * time.Millisecond,
119 done: func() {
120 log.Tracef("restore after show")
121 atomic.StoreInt32(&q.rolling, 0)
122 ui.QueueFunc(func() {
123 CloseDialog()
124 AddDialog(termBox)
125 })
126 },
127 }
128
129 atomic.StoreInt32(&q.rolling, 1)
130 emptyBox := NewDialog(
131 ui.NewBox(&EmptyInteractive{}, "", "", uiConfig),
132 fixReturn(0),
133 fixReturn(0),
134 inputReturn(),
135 f.Roll(1, h),
136 )
137
138 q.visible = true
139 if q.term != nil {
140 q.term.Show(q.visible)
141 q.term.Focus(q.visible)
142 }
143
144 CloseDialog()
145 AddDialog(emptyBox)
146 }
147
148 func (q *quakeTerminal) Hide() {
149 uiConfig := SelectedAccountUiConfig()
150 f := Roller{
151 span: 100 * time.Millisecond,
152 done: func() {
153 atomic.StoreInt32(&q.rolling, 0)
154 ui.QueueFunc(CloseDialog)
155 log.Tracef("restore after hide")
156 },
157 }
158
159 atomic.StoreInt32(&q.rolling, 1)
160 emptyBox := NewDialog(
161 ui.NewBox(&EmptyInteractive{}, "", "", uiConfig),
162 fixReturn(0),
163 fixReturn(0),
164 inputReturn(),
165 f.Roll(uiConfig.QuakeHeight, 2),
166 )
167
168 q.mu.Lock()
169 q.visible = false
170 if q.term != nil {
171 q.term.Focus(q.visible)
172 q.term.Show(q.visible)
173 }
174 q.mu.Unlock()
175
176 ui.QueueFunc(func() {
177 CloseDialog()
178 AddDialog(emptyBox)
179 })
180 }
181
182 type EmptyInteractive struct{}
183
184 func (e *EmptyInteractive) Draw(ctx *ui.Context) {
185 w := ctx.Width()
186 h := ctx.Height()
187 if w == 0 || h == 0 {
188 return
189 }
190 style := SelectedAccountUiConfig().GetStyle(config.STYLE_DEFAULT)
191 ctx.Fill(0, 0, ctx.Width(), ctx.Height(), ' ', style)
192 }
193
194 func (e *EmptyInteractive) Invalidate() {
195 }
196
197 func (e *EmptyInteractive) MouseEvent(_ int, _ int, _ vaxis.Event) {
198 }
199
200 func (e *EmptyInteractive) Event(_ vaxis.Event) bool {
201 return true
202 }
203
204 func (e *EmptyInteractive) Focus(_ bool) {
205 }
206
207 type Roller struct {
208 span time.Duration
209 done func()
210 value int64
211 }
212
213 func (f *Roller) Roll(start, end int) func(int) int {
214 nsteps := end - start
215
216 var step int64 = 1
217 if end < start {
218 step = -1
219 nsteps = -nsteps
220 }
221
222 span := f.span.Milliseconds() / int64(nsteps)
223 refresh := time.Duration(span) * time.Millisecond
224
225 atomic.StoreInt64(&f.value, int64(start))
226
227 go func() {
228 defer log.PanicHandler()
229 for i := 0; i < int(nsteps); i++ {
230 aerc.Invalidate()
231 time.Sleep(refresh)
232 atomic.AddInt64(&f.value, step)
233 }
234 if f.done != nil {
235 ui.QueueFunc(f.done)
236 }
237 }()
238
239 return func(_ int) int {
240 log.Tracef("in roller")
241 return int(atomic.LoadInt64(&f.value))
242 }
243 }