terminal_focus.go RawHistoryFolder
1package daemon
2
3import (
4 "github.com/bnema/vev/internal/domain"
5 "github.com/bnema/vev/internal/protocol"
6)
7
8// Terminal focus.
9//
10// Each attachment carries the focus its client's terminal window last
11// reported. It is a lock-free read, so any daemon feature can ask whether a
12// user may be looking at an attachment. An attachment that never reports keeps
13// unknown focus, which counts as possibly seen, so terminals without focus
14// reporting behave exactly as before.
15//
16// Attention uses it today: only an attachment that may be seen acknowledges a
17// bell, so a vev window left open on another workspace no longer clears it for
18// every other client. When that window gains focus again, the tab it shows is
19// acknowledged by its next paint.
20//
21// Geometry uses it too: a reported focus gain makes the attachment the latest
22// shared PTY geometry claimant, so session content follows the focused window.
23
24// terminalFocus returns the focus the client last reported.
25func (ac *attachedClient) terminalFocus() domain.TerminalFocus {
26 if ac == nil {
27 return domain.TerminalFocusUnknown
28 }
29 return domain.TerminalFocus(ac.focus.Load())
30}
31
32// setTerminalFocus records a reported focus and reports whether it changed.
33func (ac *attachedClient) setTerminalFocus(focus domain.TerminalFocus) bool {
34 return domain.TerminalFocus(ac.focus.Swap(uint32(focus))) != focus
35}
36
37// applyTerminalFocusForAttachment records a focus report. Gaining focus
38// claims shared session geometry and repaints the attachment, whose paint
39// acknowledges the visible tab's bell, and the bars of every client, whose
40// bells may clear with it.
41func (d *Daemon) applyTerminalFocusForAttachment(effect *attachmentEffect, message protocol.TerminalFocus) {
42 if message.Validate() != nil || effect == nil || effect.sess == nil || effect.ac == nil || effect.ac.terminalFocus() == message.Focus {
43 return
44 }
45 if !message.Focus.MaySee() {
46 effect.ac.setTerminalFocus(message.Focus)
47 return
48 }
49 // Like a bell rung on a seen tab, the bell a focused window finds on its
50 // tab is drawn for one pulse before a paint acknowledges it. Mark it before
51 // the focus lets any paint acknowledge.
52 effect.sess.markVisibleAttention(effect.ac)
53 effect.ac.setTerminalFocus(message.Focus)
54 // The caller admitted this effect, and only Focused reaches here.
55 effect.sess.geometry.reconcileAndInvalidate(d, effect.sess, effect.ac, "terminal_focus.go")
56 d.invalidateRender(effect.sess, effect.ac, false, "terminal_focus.go")
57}
58
59// markVisibleAttention asks for one visible bell pulse on the tab ac shows,
60// when that tab is ringing.
61func (s *session) markVisibleAttention(ac *attachedClient) {
62 view := ac.viewSnapshot()
63 s.mu.Lock()
64 defer s.mu.Unlock()
65 for _, tb := range s.tabs {
66 if tb != nil && tb.attention && domain.TabStableID(tb.stableID) == view.tabID {
67 tb.attentionVisiblePaint = true
68 return
69 }
70 }
71}