1// Package indicator is the recording HUD: a small layer surface at the top
2// centre of the recorded output (of the workspace frame, when it is smaller)
3// showing a REC timer and a Stop button.
4//
5// It is one thin surface, not a full-output overlay: it takes no keyboard
6// focus, and only the Stop button receives pointer input; the rest of the HUD
7// and everything around it pass clicks through. The border around the
8// recorded area is not drawn here: the compositor marks it.
9//
10// Capture exclusion is explicit authorization, not a property of how the HUD is
11// built. The caller supplies authorize, which NeferGUI calls with the surface
12// once it is configured and before its first buffer, to register whatever
13// exclusion the compositor contract requires on that same client; Run refuses
14// to start without it. The layer namespace is only a role hint and authorizes
15// nothing. If authorize fails, Run fails and nothing is shown.
16package indicator
17
18import (
19 "context"
20 _ "embed"
21 "errors"
22 "fmt"
23 "sync"
24 "time"
25
26 "github.com/bnema/nefergui"
27
28 "github.com/bnema/nefercap/internal/adapters/uierrors"
29 "github.com/bnema/nefercap/internal/adapters/uistyle"
30 "github.com/bnema/nefercap/internal/ports"
31)
32
33//go:embed style.css
34var styleSheet string
35
36// HUD geometry in logical pixels. The Stop button is the only input region.
37const (
38 baseFont = 16
39 Width, Height = baseFont * 24, baseFont * 5 / 2
40 stopWidth = baseFont * 45 / 8 // 5.625rem in CSS
41 topMargin = baseFont / 2
42 namespace = "nefercap-indicator"
43 tick = time.Second
44)
45
46var errOverlayClosed = errors.New("overlay closed by the compositor")
47
48// Setup errors, returned before anything is shown.
49var (
50 // ErrNoAuthorize means a HUD was built without an exclusion hook.
51 ErrNoAuthorize = errors.New("indicator: capture exclusion hook is required")
52 // ErrNoStop means a HUD was built without a stop channel.
53 ErrNoStop = errors.New("indicator: stop channel is required")
54)
55
56// Indicator shows the recording HUD.
57type Indicator struct {
58 // TargetLabel optionally names what is recorded (a workspace or output
59 // name). It is shown after the timer, cut to 12 characters, and only as
60 // text: the HUD never draws geometry for it. Set it before Run.
61 TargetLabel string
62 // Frame optionally is the part of the output that is recorded, in
63 // output-local logical pixels (a workspace smaller than its output): the
64 // HUD sits at the top centre of it. The zero value is the whole output.
65 // The position is fixed when the recording starts: if the workspace frame
66 // changes during the recording, the video follows it and the HUD stays in
67 // place. Set it before Run.
68 Frame ports.Region
69
70 authorize func(context.Context, nefergui.WaylandSurface) error
71 stop chan<- struct{}
72}
73
74// New returns an indicator. authorize is required and runs once per Run, on
75// the window's owner loop; its context ends when Run's does, so a blocking wait
76// for the compositor's acknowledgement is interrupted by cancellation. stop
77// should be buffered; a click sends one non-blocking value. The caller reacts
78// by ending the recording.
79func New(authorize func(context.Context, nefergui.WaylandSurface) error, stop chan<- struct{}) *Indicator {
80 return &Indicator{authorize: authorize, stop: stop}
81}
82
83// Run shows the HUD on output until ctx ends or the window system fails. It
84// returns nil when ctx was canceled by the caller, and an error otherwise; the
85// error names the output and its size. The elapsed time starts when Run is
86// called, before the window exists. Run blocks; call it from its own goroutine
87// and cancel ctx to remove the HUD.
88func (i *Indicator) Run(ctx context.Context, output ports.Output) error {
89 switch {
90 case i == nil || i.authorize == nil:
91 return ErrNoAuthorize
92 case i.stop == nil:
93 return ErrNoStop
94 case output.Name == "":
95 return fmt.Errorf("indicator: output %d has no name: %w", output.ID, ports.ErrOutputNotFound)
96 }
97 if err := ctx.Err(); err != nil {
98 return err
99 }
100 if err := i.run(ctx, output, time.Now()); err != nil {
101 return fmt.Errorf("indicator: output %s (%dx%d): %w", output.Name, output.Width, output.Height, err)
102 }
103 return nil
104}
105
106func (i *Indicator) run(ctx context.Context, output ports.Output, start time.Time) error {
107 sheet, remove, err := uistyle.Write(styleSheet, "nefercap-indicator-*.css")
108 if err != nil {
109 return err
110 }
111 defer remove()
112
113 child, cancel := context.WithCancel(ctx)
114 wake := make(chan struct{}, 1)
115 var wg sync.WaitGroup
116 wg.Add(1)
117 go func() { defer wg.Done(); ticker(child, wake) }()
118 defer func() { cancel(); wg.Wait() }()
119
120 runErr := nefergui.Run(child, newHUD(start, i.stop, i.TargetLabel), view,
121 nefergui.Title("NeferCap recording"),
122 nefergui.Size(Width, Height),
123 nefergui.Styles(sheet),
124 nefergui.Transparent(),
125 nefergui.Layer(layerConfig(output.Name, i.Frame)),
126 nefergui.OnSurface(i.authorize),
127 nefergui.Wake(wake),
128 )
129 // Run is only ever ended by ctx: the HUD never decides by itself. Read the
130 // parent once; a bare Canceled is then our own cancellation, and anything
131 // else, including an end with a live parent, is a failure that keeps every
132 // real error.
133 perr := ctx.Err()
134 return uierrors.Ended(runErr, perr != nil, perr, errOverlayClosed)
135}
136
137// layerConfig is a top-centre HUD that never takes keyboard focus and accepts
138// pointer input only inside the Stop button. Centre is that of the output, or
139// of frame when it is set: then the HUD is anchored to the top left of the
140// output and moved by margins.
141func layerConfig(output string, frame ports.Region) nefergui.LayerConfig {
142 c := nefergui.LayerConfig{
143 Output: output,
144 Namespace: namespace,
145 Level: nefergui.LayerOverlay,
146 Anchors: nefergui.AnchorTop,
147 Keyboard: nefergui.KeyboardNone,
148 ExclusiveZone: -1,
149 Margin: [4]int32{topMargin, 0, 0, 0},
150 InputRects: []nefergui.Rect{stopRect()},
151 }
152 if frame.Width > 0 && frame.Height > 0 {
153 c.Anchors = nefergui.AnchorTop | nefergui.AnchorLeft
154 c.Margin = [4]int32{int32(frame.Y) + topMargin, 0, 0, int32(frame.X + max((frame.Width-Width)/2, 0))}
155 }
156 return c
157}
158
159func stopRect() nefergui.Rect {
160 return nefergui.Rect{X: Width - stopWidth, Y: 0, Width: stopWidth, Height: Height}
161}
162
163// ticker sends a non-blocking wake token every second until ctx ends.
164func ticker(ctx context.Context, wake chan<- struct{}) {
165 t := time.NewTicker(tick)
166 defer t.Stop()
167 for {
168 select {
169 case <-ctx.Done():
170 return
171 case <-t.C:
172 select {
173 case wake <- struct{}{}:
174 default:
175 }
176 }
177 }
178}
179
180func view(f *nefergui.Frame, m *hud) {
181 root := f.Root(nefergui.Class("hud"))
182 row := root.Row(nefergui.Class("bar"))
183 row.Text(m.refresh(time.Now()), nefergui.Key("timer"), nefergui.Class("timer"))
184 if row.Button("Stop", nefergui.Key("stop"), nefergui.Class("stop")).Activated() {
185 m.requestStop()
186 }
187}