oficina / slides Public

Clone
git clone https://amadan.net/oficina/slides

Plain git — no account needed to clone.

Download

Download this file

1// presenter_window.go is the presenter window (spec §5), drawn by the
2// server: GET /p/{session}/notes for a session with a position.
3//
4// The layout is Paul's mockup (docs/superpowers/specs/2026-11-17-
5// presenter-mockup.html), its ids become presenter-* attributes, and
6// static/presenter.css is its stylesheet. A header (where the talk is,
7// the step count, the mode, BLACK, the timer, the clock and the ⋯
8// menu), then three panels on a 12×12 grid: the slide on screen now,
9// what comes next, and the notes.
10//
11// Everything here works with scripts off (S20): each control is a form
12// that posts a press and redirects back (presenting_position.go's
13// pressLanding), ?slide=N&step=k draws that state, and ?pick=1 is the
14// slide picker as a page of one-button forms (P4). The slides are drawn
15// live, at their step, so the page takes the font gate.
16//
17// Sessions begun before positions keep presentation.go's presenter
18// view, whose slides are pictures.
19package slides
20
21import (
22 "fmt"
23 "html/template"
24 "net/http"
25 "net/url"
26 "strconv"
27 "strings"
28 "time"
29
30 "amadan.net/oficina/slides/internal/slides/deck"
31 slidehtml "amadan.net/oficina/slides/internal/slides/render"
32 "amadan.net/rastrillo/rastrillo"
33)
34
35// presenterPrefsCookie holds how a person likes the window: the notes
36// fitted or scrolling, their text size, and where the three panels sit.
37// It is read here; the window's script writes it (presenter-notes.mjs's
38// writePrefs, for the notes and for arranging), as
39// "fit.24.0,0,7,8;0,8,7,4;7,0,5,12" percent-encoded, because a cookie
40// value cannot hold a ";". Path /p, a year, SameSite=Lax.
41const presenterPrefsCookie = "slides-presenter"
42
43// presenterGrid is the panels' grid: 12 columns by 12 rows.
44const presenterGrid = 12
45
46// panel is one panel's place on the grid, in cells from the top left.
47type panel struct{ X, Y, W, H int }
48
49// presenterPrefs is the cookie, read.
50type presenterPrefs struct {
51 Notes string // "fit" or "scroll"
52 Size int // 16 to 48, even; default 24
53 Panels [3]panel // now, next, notes on a 12×12 grid
54}
55
56// defaultPresenterPrefs is the mockup's window: the slide on screen
57// over the one coming next on the left, the notes down the right.
58var defaultPresenterPrefs = presenterPrefs{
59 Notes: "fit",
60 Size: 24,
61 Panels: [3]panel{{0, 0, 7, 8}, {0, 8, 7, 4}, {7, 0, 5, 12}},
62}
63
64// presenterPanels names the panels in the order Panels holds them.
65var presenterPanels = [3]string{"now", "next", "notes"}
66
67// readPresenterPrefs is the request's preferences. Anything malformed,
68// off the grid, under 2×2 or overlapping gives the defaults, whole: a
69// cookie half believed would put a panel where nobody put it.
70func readPresenterPrefs(r *http.Request) presenterPrefs {
71 c, err := r.Cookie(presenterPrefsCookie)
72 if err != nil {
73 return defaultPresenterPrefs
74 }
75 raw, err := url.PathUnescape(c.Value)
76 if err != nil {
77 return defaultPresenterPrefs
78 }
79 prefs, ok := parsePresenterPrefs(raw)
80 if !ok {
81 return defaultPresenterPrefs
82 }
83 return prefs
84}
85
86// parsePresenterPrefs reads "notes.size.x,y,w,h;x,y,w,h;x,y,w,h".
87func parsePresenterPrefs(raw string) (presenterPrefs, bool) {
88 fields := strings.Split(raw, ".")
89 if len(fields) != 3 {
90 return presenterPrefs{}, false
91 }
92 var p presenterPrefs
93 switch fields[0] {
94 case "fit", "scroll":
95 p.Notes = fields[0]
96 default:
97 return presenterPrefs{}, false
98 }
99 size, ok := smallNumber(fields[1])
100 if !ok || size < 16 || size > 48 || size%2 != 0 {
101 return presenterPrefs{}, false
102 }
103 p.Size = size
104 rects := strings.Split(fields[2], ";")
105 if len(rects) != len(p.Panels) {
106 return presenterPrefs{}, false
107 }
108 for i, rect := range rects {
109 nums := strings.Split(rect, ",")
110 if len(nums) != 4 {
111 return presenterPrefs{}, false
112 }
113 var v [4]int
114 for j, n := range nums {
115 if v[j], ok = smallNumber(n); !ok {
116 return presenterPrefs{}, false
117 }
118 }
119 pl := panel{X: v[0], Y: v[1], W: v[2], H: v[3]}
120 if pl.W < 2 || pl.H < 2 || pl.X+pl.W > presenterGrid || pl.Y+pl.H > presenterGrid {
121 return presenterPrefs{}, false
122 }
123 p.Panels[i] = pl
124 }
125 for i := range p.Panels {
126 for j := i + 1; j < len(p.Panels); j++ {
127 if overlap(p.Panels[i], p.Panels[j]) {
128 return presenterPrefs{}, false
129 }
130 }
131 }
132 return p, true
133}
134
135// smallNumber is one or two ASCII digits: every number the cookie
136// holds, and nothing else (no sign, no space, no leading plus).
137func smallNumber(s string) (int, bool) {
138 if len(s) == 0 || len(s) > 2 {
139 return 0, false
140 }
141 for _, c := range s {
142 if c < '0' || c > '9' {
143 return 0, false
144 }
145 }
146 n, err := strconv.Atoi(s)
147 return n, err == nil
148}
149
150func overlap(a, b panel) bool {
151 return a.X < b.X+b.W && b.X < a.X+a.W && a.Y < b.Y+b.H && b.Y < a.Y+a.H
152}
153
154// css is the arrangement as the page's own stylesheet, so the first
155// paint is final (spec §5.4). It applies only where the panels are
156// arranged at all: below 720px wide or 560px tall they stack in a fixed
157// order (static/presenter.css).
158func (p presenterPrefs) css() template.CSS {
159 var b strings.Builder
160 b.WriteString("@media (min-width: 720px) and (min-height: 560px) {\n")
161 for i, name := range presenterPanels {
162 pl := p.Panels[i]
163 fmt.Fprintf(&b, " [presenter-panel=%q] { grid-area: %d / %d / span %d / span %d; }\n",
164 name, pl.Y+1, pl.X+1, pl.H, pl.W)
165 }
166 // The width the slide on screen is drawn at (--p-now): Now's cells
167 // of the panels' box, less the panel's padding (16px each side, 8px
168 // top and bottom), at 16:9. In the default window Next is half of it
169 // (presenter.css), so Now also leaves Next's panel, under its 24px
170 // label and 8px gap (--rst-sp-2), room for that half. In a window somebody
171 // arranged, Next fills its own panel.
172 now, next := p.Panels[0], p.Panels[1]
173 room := fmt.Sprintf("%d / %d * 100cqw - 32px, (%d / %d * 100cqh - 16px) * 16 / 9",
174 now.W, presenterGrid, now.H, presenterGrid)
175 if p.Panels == defaultPresenterPrefs.Panels {
176 room += fmt.Sprintf(", (%d / %d * 100cqh - 48px) * 2 * 16 / 9", next.H, presenterGrid)
177 }
178 fmt.Fprintf(&b, " [presenter-panel] { --p-now: min(%s); }\n", room)
179 if p.Panels != defaultPresenterPrefs.Panels {
180 b.WriteString(" [presenter-panel=\"next\"] [presenter-frame] { inline-size: min(100cqw, 100cqh * 16 / 9); }\n")
181 }
182 b.WriteString("}\n")
183 fmt.Fprintf(&b, "[presenter-notes] { font-size: %dpx; }\n", p.Size)
184 return template.CSS(b.String())
185}
186
187// presenterOp is one press a form sends: the op, a slide for a goto,
188// the remote token when the window was opened with one, and a fresh
189// press id (a Positional session requires one).
190type presenterOp struct {
191 Op, Slide, Token, Press string
192}
193
194// notePart is one part of a slide's notes (deck.NoteParts): State is
195// "now" for the part being said, "past" or "later" for the dimmed rest,
196// and empty for notes with no press line, which stay plain (§10).
197type notePart struct {
198 HTML template.HTML
199 State string
200}
201
202// presenterWindowPage is the window.
203type presenterWindowPage struct {
204 shell
205 // ArrangeCSS is the arrangement and the notes' size, from the cookie.
206 ArrangeCSS template.CSS
207 Action string
208
209 // What the window's script needs (fixtures/presenter-window.mjs):
210 // its bundle, the talk it holds in memory, the session's stream, and
211 // every sentence it draws, from the catalog.
212 Bundle string
213 TalkHref string
214 StreamHref string
215 Words presenterWords
216
217 // What the page's root says, for the window's script: the session,
218 // the slide drawn (its number in the full talk), the step, the mode
219 // and whether the room sees black, which is where it starts from;
220 // the token when this window was opened with one.
221 Session string
222 Token string
223 Slide int
224 Step int
225 Mode string
226 Blackout bool
227
228 Counter string
229 PickHref string
230 PickLabel string
231 // StepCount is "step 2 of 4", empty on a slide with no steps;
232 // Track is its segments, one per press, true for those made.
233 StepCount string
234 Track []bool
235 ModeBadge string
236 BlackLabel string
237
238 Back, Forward presenterOp
239 BackLabel, ForwardLabel string
240
241 Timer string
242 TimerSince int64
243 TimerLabel string
244 TimerReset presenterOp
245 Clock string
246 ClockLabel string
247
248 MoreLabel string
249 Menu presenterMenu
250
251 // The bar arranging shows (fixtures/presenter-arrange.mjs): what to
252 // do, the keys, and its two buttons; the eight buttons the selected
253 // panel gets; and each panel's name, which arranging gives it.
254 ArrangeHint, ArrangeHelp, ArrangeReset, ArrangeDone string
255 ArrangeTools []arrangeTool
256 NowName, NextName, NotesName string
257
258 Now template.HTML
259 NextLabel string
260 Next template.HTML
261
262 Parts []notePart
263 NoNotes string
264 NotesFit bool
265 ToolsLabel string
266 FitLabel string
267 ScrollLabel string
268 SmallerLabel string
269 LargerLabel string
270
271 Empty bool
272 EmptyTitle string
273 EmptyBody string
274}
275
276// presenterWords are the catalog's sentences the window's script draws
277// as the talk moves, unfilled: it puts in the numbers itself.
278type presenterWords struct {
279 Progress, Step string
280 NextStep, NextSlide, NextEnd string
281 ModeCore, ModeFull, Black string
282 MenuFull, MenuCore, NoNotes string
283 Loading, LoadingFailed string
284 Offline string
285 PickField, PickCount string
286 PickMatches, PickNone string
287 PickFull, PickTitle string
288 ArrangeWhere string
289}
290
291// arrangeTool is one of the selected panel's buttons while arranging:
292// the change it makes (presenter-grid.mjs's toolChange), its name and
293// its icon.
294type arrangeTool struct {
295 Op, Label, Icon string
296}
297
298// arrangeToolOps are the eight, in the order they sit: the four moves,
299// then the four resizes.
300var arrangeToolOps = []struct{ op, icon string }{
301 {"left", "arrow-left"}, {"right", "arrow-right"}, {"up", "arrow-up"}, {"down", "arrow-down"},
302 {"narrower", "fold-horizontal"}, {"wider", "unfold-horizontal"},
303 {"shorter", "fold-vertical"}, {"taller", "unfold-vertical"},
304}
305
306// presenterWordsFor is the window's sentences in the request's language.
307func presenterWordsFor(r *http.Request) presenterWords {
308 return presenterWords{
309 Progress: rastrillo.T(r, "slides.present.progress"),
310 Step: rastrillo.T(r, "slides.present.step.count"),
311 NextStep: rastrillo.T(r, "slides.present.next.step"),
312 NextSlide: rastrillo.T(r, "slides.present.next.slide"),
313 NextEnd: rastrillo.T(r, "slides.present.next.end"),
314 ModeCore: rastrillo.T(r, "slides.present.mode.core"),
315 ModeFull: rastrillo.T(r, "slides.present.mode.full"),
316 Black: rastrillo.T(r, "slides.present.black"),
317 MenuFull: rastrillo.T(r, "slides.present.menu.full"),
318 MenuCore: rastrillo.T(r, "slides.present.menu.core"),
319 NoNotes: rastrillo.T(r, "slides.notes.none"),
320 Loading: rastrillo.T(r, "slides.present.loading"),
321 LoadingFailed: rastrillo.T(r, "slides.present.loading.failed"),
322 Offline: rastrillo.T(r, "slides.present.offline"),
323 PickField: rastrillo.T(r, "slides.present.pick.field"),
324 PickCount: rastrillo.T(r, "slides.present.pick.count"),
325 PickMatches: rastrillo.T(r, "slides.present.pick.matches"),
326 PickNone: rastrillo.T(r, "slides.present.pick.none"),
327 PickFull: rastrillo.T(r, "slides.present.pick.full"),
328 PickTitle: rastrillo.T(r, "slides.present.pick.title"),
329 ArrangeWhere: rastrillo.T(r, "slides.present.arrange.where"),
330 }
331}
332
333// presenterMenu is the ⋯ menu's six rows (§5.6). Pair posts to
334// /p/{session}/pair (presentation.go's presentPair) into a window of its
335// own, so this one keeps its talk; Arrange is a button that needs the
336// window's script.
337type presenterMenu struct {
338 PairAction string
339 PairLabel string
340 WindowHref string
341 WindowLabel string
342 Mode presenterOp
343 ModeLabel string
344 ArrangeLabel string
345 DeckHref string
346 DeckLabel string
347 End presenterOp
348 EndLabel string
349}
350
351// presenterPickPage is ?pick=1: every slide in the talk, as forms.
352type presenterPickPage struct {
353 shell
354 Title string
355 Count string
356 Action string
357 WindowHref string
358 FullLabel string
359 Items []pickItem
360}
361
362// pickItem is one slide in the picker. The current one is a link back
363// to the window, which moves nothing; every other is a goto.
364type pickItem struct {
365 Number int
366 Title string
367 Thumb string
368 Full bool
369 Current bool
370 Op presenterOp
371}
372
373// presenterWindow serves the window, or with ?pick=1 its picker, for a
374// session with a position. The caller has already checked mayDrive.
375func (a *app) presenterWindow(w http.ResponseWriter, r *http.Request, p Presentation, token string) {
376 d, ok := a.deckOf(w, r, p)
377 if !ok {
378 return
379 }
380 h, view, err := a.openDeck(r.Context(), d)
381 if err != nil {
382 a.fail(w, r, "open deck", err)
383 return
384 }
385 defer h.Close(r.Context())
386 view = view.ForReaders()
387
388 // Only a token that drives is carried on: a signed-in presenter
389 // with a stale one in the address is let in by the cookie.
390 if !remoteTokenOK(p, token) {
391 token = ""
392 }
393 link := func(query url.Values) string {
394 if token != "" {
395 query.Set("t", token)
396 }
397 out := "/p/" + p.Sqid + "/notes"
398 if enc := query.Encode(); enc != "" {
399 out += "?" + enc
400 }
401 return out
402 }
403 op := func(name string) presenterOp {
404 return presenterOp{Op: name, Token: token, Press: newPressID()}
405 }
406
407 q := r.URL.Query()
408 mode := positionOf(p).Mode
409 if q.Get("pick") == "1" && len(view.Slides) > 0 {
410 a.presenterPick(w, r, view, p, link, op)
411 return
412 }
413
414 prefs := readPresenterPrefs(r)
415 now := time.Now().UTC()
416 page := presenterWindowPage{
417 shell: presentShellFor(r, rastrillo.T(r, "slides.present.notes.title"), view.Meta.Title),
418 ArrangeCSS: prefs.css(),
419 Action: "/p/" + p.Sqid + "/command",
420 Bundle: presenterBundlePath(),
421 TalkHref: "/p/" + p.Sqid + "/talk",
422 StreamHref: "/p/" + p.Sqid + "/stream",
423 Words: presenterWordsFor(r),
424 Session: p.Sqid,
425 Token: token,
426 Mode: string(mode),
427
428 BlackLabel: rastrillo.T(r, "slides.present.black"),
429 Timer: "0:00",
430 TimerLabel: rastrillo.T(r, "slides.present.timer.reset"),
431 TimerReset: op("timer-reset"),
432 Clock: clockWords(now),
433 ClockLabel: rastrillo.T(r, "slides.present.clock"),
434 MoreLabel: rastrillo.T(r, "slides.present.more"),
435
436 ArrangeHint: rastrillo.T(r, "slides.present.arrange.hint"),
437 ArrangeHelp: rastrillo.T(r, "slides.present.arrange.help"),
438 ArrangeReset: rastrillo.T(r, "slides.present.arrange.reset"),
439 ArrangeDone: rastrillo.T(r, "slides.present.arrange.done"),
440 NowName: rastrillo.T(r, "slides.present.current"),
441 NextName: rastrillo.T(r, "slides.present.next.label"),
442 NotesName: rastrillo.T(r, "slides.viewer.notes"),
443 Menu: presenterMenu{
444 PairAction: "/p/" + p.Sqid + "/pair",
445 PairLabel: rastrillo.T(r, "slides.present.menu.pair"),
446 WindowHref: "/p/" + p.Sqid,
447 WindowLabel: rastrillo.T(r, "slides.present.menu.window"),
448 Mode: op("mode"),
449 ModeLabel: rastrillo.T(r, "slides.present.menu.full"),
450 ArrangeLabel: rastrillo.T(r, "slides.present.menu.arrange"),
451 DeckHref: "/d/" + d.Sqid + "/edit",
452 DeckLabel: rastrillo.T(r, "slides.present.menu.back"),
453 End: op("end"),
454 EndLabel: rastrillo.T(r, "slides.present.menu.end"),
455 },
456
457 NoNotes: rastrillo.T(r, "slides.notes.none"),
458 NotesFit: prefs.Notes == "fit",
459 ToolsLabel: rastrillo.T(r, "slides.present.notes.tools"),
460 FitLabel: rastrillo.T(r, "slides.present.notes.fit"),
461 ScrollLabel: rastrillo.T(r, "slides.present.notes.scroll"),
462 SmallerLabel: rastrillo.T(r, "slides.present.notes.smaller"),
463 LargerLabel: rastrillo.T(r, "slides.present.notes.larger"),
464 }
465 for _, t := range arrangeToolOps {
466 page.ArrangeTools = append(page.ArrangeTools, arrangeTool{
467 Op: t.op, Label: rastrillo.T(r, "slides.present.arrange."+t.op), Icon: t.icon,
468 })
469 }
470 if token != "" {
471 page.Menu.WindowHref += "?t=" + url.QueryEscape(token)
472 page.TalkHref += "?t=" + url.QueryEscape(token)
473 }
474 if mode == deck.ModeFull {
475 page.Menu.ModeLabel = rastrillo.T(r, "slides.present.menu.core")
476 }
477 if p.TimerAt != nil {
478 page.TimerSince = p.TimerAt.Unix()
479 page.Timer = timerWords(now.Sub(*p.TimerAt))
480 }
481
482 // The empty state is on every window, hidden while there is a
483 // talk: one already open shows it when the talk empties.
484 page.EmptyTitle = rastrillo.T(r, "slides.present.empty.title")
485 page.EmptyBody = rastrillo.T(r, "slides.present.empty.body")
486 if len(view.Slides) == 0 {
487 page.Empty = true
488 render(w, "presenter", page)
489 return
490 }
491
492 at, ok := stepPlace(view, p, q)
493 if !ok {
494 a.notFound(w, r)
495 return
496 }
497 index, step := at.index, at.step
498 s := view.Slides[index]
499 presses := len(deck.Presses(s, view.Scenes))
500 page.shell.Fonts = newFontGate(r, view, "/p/"+p.Sqid)
501 page.Slide, page.Step = index+1, step
502 page.Blackout = p.Blackout && at.live
503
504 place, places := talkPlace(view, mode, index)
505 page.Counter = rastrillo.Tf(r, "slides.present.progress", "n", place, "total", places)
506 page.PickHref = link(url.Values{"pick": {"1"}})
507 page.PickLabel = rastrillo.T(r, "slides.present.pick.open")
508 if presses > 0 {
509 page.StepCount = rastrillo.Tf(r, "slides.present.step.count", "n", step, "total", presses)
510 page.Track = make([]bool, presses)
511 for i := range page.Track {
512 page.Track[i] = i < step
513 }
514 }
515 for _, sl := range view.Slides {
516 if sl.Talk == "full" {
517 page.ModeBadge = rastrillo.T(r, "slides.present.mode."+string(mode))
518 break
519 }
520 }
521 page.Back, page.Forward = op("prev"), op("next")
522 page.BackLabel, page.ForwardLabel = pressLabels(r, p)
523
524 base := assetBaseFor("/p/" + p.Sqid)
525 codes := a.followCodes(r.Context(), d, view, "/p/"+p.Sqid)
526 draw := func(i, k int) (template.HTML, error) {
527 return slidehtml.SlideWith(view, i, slidehtml.Desktop, slidehtml.Options{AssetBase: base, Reveal: &k, QRAlt: qrAlt(r), QR: codes})
528 }
529 if page.Now, err = draw(index, step); err != nil {
530 a.fail(w, r, "render the slide on screen", err)
531 return
532 }
533 switch next := nextInTalk(view, mode, index); {
534 case step < presses:
535 page.NextLabel = rastrillo.T(r, "slides.present.next.step")
536 page.Next, err = draw(index, step+1)
537 case next >= 0:
538 page.NextLabel = rastrillo.T(r, "slides.present.next.slide")
539 page.Next, err = draw(next, 0)
540 default:
541 page.NextLabel = rastrillo.T(r, "slides.present.next.end")
542 }
543 if err != nil {
544 a.fail(w, r, "render the slide coming next", err)
545 return
546 }
547 page.Parts = noteParts(s.Notes, step)
548 render(w, "presenter", page)
549}
550
551// noteParts is a slide's notes as the window shows them at step (§10):
552// split at the press lines, the part for the step bright and the rest
553// dimmed. Presses past the last part keep the last part bright. Notes
554// with no press line are one plain part.
555func noteParts(notes deck.Text, step int) []notePart {
556 if len(notes) == 0 {
557 return nil
558 }
559 parts := deck.NoteParts(notes)
560 if len(parts) == 1 {
561 return []notePart{{HTML: slidehtml.Notes(parts[0])}}
562 }
563 bright := min(max(step, 0), len(parts)-1)
564 out := make([]notePart, len(parts))
565 for i, part := range parts {
566 state := "now"
567 switch {
568 case i < bright:
569 state = "past"
570 case i > bright:
571 state = "later"
572 }
573 out[i] = notePart{HTML: slidehtml.Notes(part), State: state}
574 }
575 return out
576}
577
578// presenterPick is the scriptless picker (P4): every slide in the talk,
579// full-only ones marked, each a form that goes there. A press from it
580// lands on the window, not back here (pressLanding drops ?pick).
581func (a *app) presenterPick(w http.ResponseWriter, r *http.Request, view *deck.Deck, p Presentation,
582 link func(url.Values) string, op func(string) presenterOp) {
583 cur, _, _ := sessionPlace(view, p)
584 full := deck.TalkOrder(view, deck.ModeFull)
585 page := presenterPickPage{
586 shell: presentShellFor(r, rastrillo.T(r, "slides.present.pick.title"), view.Meta.Title),
587 Title: rastrillo.T(r, "slides.present.pick.title"),
588 Count: rastrillo.Tf(r, "slides.present.pick.count", "total", len(full)),
589 Action: "/p/" + p.Sqid + "/command",
590 WindowHref: link(url.Values{}),
591 FullLabel: rastrillo.T(r, "slides.present.pick.full"),
592 }
593 for n, i := range full {
594 s := view.Slides[i]
595 item := pickItem{
596 Number: n + 1,
597 Title: slideLabel(r, s, i+1),
598 Thumb: "/p/" + p.Sqid + "/slides/" + url.PathEscape(s.ID) + ".svg",
599 Full: s.Talk == "full",
600 Current: i == cur,
601 }
602 if !item.Current {
603 item.Op = op("goto")
604 item.Op.Slide = strconv.Itoa(n + 1)
605 }
606 page.Items = append(page.Items, item)
607 }
608 render(w, "presenter_pick", page)
609}
610
611// timerWords is the talk's timer: minutes and seconds, and hours once
612// there are any.
613func timerWords(d time.Duration) string {
614 s := max(int(d/time.Second), 0)
615 if s >= 3600 {
616 return fmt.Sprintf("%d:%02d:%02d", s/3600, s/60%60, s%60)
617 }
618 return fmt.Sprintf("%d:%02d", s/60, s%60)
619}
620