oficina / estilo Public

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

Plain git — no account needed to clone.

Download

Download this file

1// Package menu is the suite's one floating panel ([estilo-menu]), its
2// rows ([estilo-menu-item]) and the document menu bar
3// ([estilo-menubar]). Docs is its first consumer.
4//
5// Scripts off, a menu is a <details>: it opens, and its links and form
6// buttons work; it closes only when its summary is clicked again. Script
7// may add Escape, outside-click close and menubar hover-switching; none
8// of it is required.
9//
10// It carries no strings. Callers supply every label in their locale.
11package menu
12
13import (
14 "embed"
15 "html/template"
16 "net/http"
17 "strings"
18)
19
20//go:embed menu.css
21var css []byte
22
23//go:embed templates/*.html
24var templateFS embed.FS
25
26// CSS returns menu.css. Link it after base.css and before chrome.css.
27func CSS() []byte { return append([]byte(nil), css...) }
28
29// Templates holds estilo-menu-item and estilo-icon-check. Parse it into
30// the app's page templates with ParseFS(Templates(), "templates/*.html").
31func Templates() embed.FS { return templateFS }
32
33// Item is one row rendered by estilo-menu-item. Set exactly one of Href,
34// Action, Dialog and Controls; each is a different element:
35//
36// - Href: a link, <a href>, for a row that navigates.
37// - Action: a one-button <form method="post"> to that path, for a row
38// that changes something ("New sheet").
39// - Dialog: a <button> that opens the <dialog> with that id through the
40// platform's invoker command (command="show-modal"), for a row that
41// asks for more before anything happens ("Keyboard shortcuts…").
42// chrome.js opens it where the engine has no invoker commands.
43// - Controls: a toggle, <button aria-expanded aria-controls>, for a row
44// or bar action that shows and hides a region of the page (Docs'
45// Format, which shows the inspector). It is rendered disabled, with
46// data-estilo-toggle, and stays so until the app's own script binds
47// it: with scripts off nothing can toggle the region, and a disabled
48// control does not claim to.
49//
50// The first three work with scripts off. Action and Dialog came with
51// Sheets, the second adopter: Docs' rows were all links, and Sheets' Help
52// moved into the account menu as a dialog. Controls came with Docs' I2
53// editor (spec 2026-11-11, revision 2.1 C1).
54type Item struct {
55 Href, Label string
56 // Key is an already-formatted shortcut, from Shortcut. Optional.
57 Key string
58 // Action is the POST target of a form row.
59 Action string
60 // Dialog is the id of the <dialog> a button row opens.
61 Dialog string
62 // Controls is the id of the region a toggle shows and hides
63 // (aria-controls); Expanded is whether that region is shown now, as
64 // the server rendered it (aria-expanded).
65 Controls string
66 Expanded bool
67 // Danger draws a destructive row in the negative tone
68 // (estilo-menu-item="danger"). Calls, the fifth adopter, was the
69 // first with one in a menu: "End meeting for everyone". The row still
70 // goes to its own confirm page; the tone only says it is the one to
71 // read twice.
72 Danger bool
73 // Icon is an optional decorative glyph drawn before the label
74 // ([estilo-menu-icon]): the app's own inline <svg aria-hidden="true">,
75 // trusted markup it rendered itself; the label still names the row.
76 // Docs' ••• menu on a phone was the first with one (2026-11-28, the
77 // phone web mock-ups). A chrome.Action has an Icon of its own for the
78 // bar; this one is the row's.
79 Icon template.HTML
80}
81
82// Apple reports whether the request comes from an Apple platform, the
83// server-side choice behind Shortcut: Sec-CH-UA-Platform "macOS" or
84// "iOS", or a User-Agent naming a Macintosh, iPhone or iPad. A response
85// whose markup depends on it must be private or no-store, or say
86// Vary: User-Agent.
87func Apple(r *http.Request) bool {
88 switch strings.Trim(r.Header.Get("Sec-CH-UA-Platform"), `"`) {
89 case "macOS", "iOS":
90 return true
91 }
92 ua := r.UserAgent()
93 return strings.Contains(ua, "Macintosh") || strings.Contains(ua, "iPhone") || strings.Contains(ua, "iPad")
94}
95
96// Shortcut formats a key chord for the request's platform, so a label
97// is right in the first paint and nothing flips once script runs.
98//
99// keys is written in one platform-neutral spelling, parts joined by
100// "+": Mod (Command on Apple, Ctrl elsewhere), Shift, Alt, Ctrl (the
101// literal Control key), then one key. Enter, Delete, Backspace, Tab and
102// Esc are named; anything else is printed as given. Apple chords print
103// their modifiers in the order ⌃⌥⇧⌘ with no separator (⇧⌘K); other
104// platforms join words with "+" in the order Ctrl, Alt, Shift
105// (Ctrl+Shift+K).
106func Shortcut(r *http.Request, keys string) string {
107 return format(Apple(r), keys)
108}
109
110var named = map[string][2]string{
111 // name: {apple, other}
112 "Enter": {"↩", "Enter"},
113 "Delete": {"⌫", "Delete"},
114 "Backspace": {"⌫", "Backspace"},
115 "Tab": {"⇥", "Tab"},
116 "Esc": {"⎋", "Esc"},
117}
118
119func format(apple bool, keys string) string {
120 var ctrl, alt, shift, cmd bool
121 key := ""
122 for _, part := range strings.Split(keys, "+") {
123 switch part {
124 case "Mod":
125 if apple {
126 cmd = true
127 } else {
128 ctrl = true
129 }
130 case "Ctrl":
131 ctrl = true
132 case "Alt":
133 alt = true
134 case "Shift":
135 shift = true
136 default:
137 key = part
138 }
139 }
140 if n, ok := named[key]; ok {
141 if apple {
142 key = n[0]
143 } else {
144 key = n[1]
145 }
146 }
147 if apple {
148 var b strings.Builder
149 for _, m := range []struct {
150 on bool
151 glyph string
152 }{{ctrl, "⌃"}, {alt, "⌥"}, {shift, "⇧"}, {cmd, "⌘"}} {
153 if m.on {
154 b.WriteString(m.glyph)
155 }
156 }
157 return b.String() + key
158 }
159 var parts []string
160 for _, m := range []struct {
161 on bool
162 word string
163 }{{ctrl, "Ctrl"}, {alt, "Alt"}, {shift, "Shift"}} {
164 if m.on {
165 parts = append(parts, m.word)
166 }
167 }
168 if key != "" {
169 parts = append(parts, key)
170 }
171 return strings.Join(parts, "+")
172}
173