| 1 | package slides |
| 2 | |
| 3 | // The shared chrome (estilo chrome, round 2): every page's bar, account |
| 4 | // menu and <title> are built here, once, so no page can drift from |
| 5 | // another. Before this the bar was rastrillo's topbar layout: 45px |
| 6 | // signed out and 51px signed in (the Account summary was taller than the |
| 7 | // bar and existed only once signed in), an account menu of an address, |
| 8 | // Agents and Sign out, a separate one-item Help dropdown, and a <title> |
| 9 | // that never named the app. |
| 10 | // |
| 11 | // Spec: estilo spec/shared-chrome, docs/superpowers/specs/ |
| 12 | // 2026-09-23-shared-chrome.md, §5 (bar), §6 (switcher), §8 (account), |
| 13 | // §10 (titles). Slides is the third adopter, after Docs and Sheets. |
| 14 | // |
| 15 | // Every label is a catalog key (AGENTS.md: a hardcoded English string is |
| 16 | // a bug); estilo's templates carry no strings of their own. |
| 17 | |
| 18 | import ( |
| 19 | "net/http" |
| 20 | "net/url" |
| 21 | |
| 22 | "amadan.net/oficina/estilo.git/brand" |
| 23 | "amadan.net/oficina/estilo.git/chrome" |
| 24 | estilomenu "amadan.net/oficina/estilo.git/menu" |
| 25 | "amadan.net/oficina/home/connect" |
| 26 | "amadan.net/rastrillo/rastrillo" |
| 27 | "amadan.net/rastrillo/rastrillo/sessions" |
| 28 | ) |
| 29 | |
| 30 | // appName is the brand and the last part of every <title>. A proper |
| 31 | // noun, the same in every locale, so not a catalog key. |
| 32 | const appName = "Slides" |
| 33 | |
| 34 | // Frame is what layout.html reads from every page: the bar, the parts of |
| 35 | // the <title> before the app's name, and the Appearance preference for |
| 36 | // <html data-theme>. |
| 37 | type Frame struct { |
| 38 | Chrome chrome.Bar |
| 39 | TitleParts []string |
| 40 | // Title, when set, is the whole <title>, which then does not add the |
| 41 | // app's name: the sign-in door's title is rastrillo's, and it already |
| 42 | // names Slides ("Sign in to Slides"). |
| 43 | Title string |
| 44 | Theme string |
| 45 | // Bare pages have no bar at all: the presenting screens, which are |
| 46 | // capability-addressed and shown on a projector, a lectern or a |
| 47 | // phone in somebody's hand (spec §12.1, Slides). A signed-in |
| 48 | // presenter used to see "Account → Sign in" there. |
| 49 | Bare bool |
| 50 | } |
| 51 | |
| 52 | // shell is embedded in every page's view data, so .Frame resolves on |
| 53 | // every page the layout renders. |
| 54 | // |
| 55 | // Fonts is set on the pages that draw a slide in the deck's own faces |
| 56 | // (the viewer, the projector and follow view, the editor) and nil on |
| 57 | // every other: the layout writes the font gate's preloads, manifest and |
| 58 | // scripts into <head> only when it is there (fontgate.go). |
| 59 | type shell struct { |
| 60 | Frame Frame |
| 61 | Fonts *fontGate |
| 62 | } |
| 63 | |
| 64 | // frame describes one page's chrome. The zero value is a plain page whose |
| 65 | // <title> is just "Slides". |
| 66 | type frame struct { |
| 67 | // parts precede "Slides" in <title>, most specific first. The first |
| 68 | // is the page's h1 (spec §10). |
| 69 | parts []string |
| 70 | // title replaces the whole <title> (Frame.Title). |
| 71 | title string |
| 72 | // context is the open deck's title, shown in the bar on the deck's |
| 73 | // own pages: the viewer and every sub-page. |
| 74 | context string |
| 75 | // actions are the bar's page actions (Docs' Share… on a document): |
| 76 | // buttons at wide widths, rows in the Menu sheet below 800px. |
| 77 | actions []chrome.Action |
| 78 | // public is a read-only page a stranger reaches (the share link): |
| 79 | // the brand is plain text, and "Sign in" carries no return path, |
| 80 | // because the path is a capability and must not be copied into |
| 81 | // another URL. |
| 82 | public bool |
| 83 | // signinPage is one of the sign-in, sent and signed-out pages: the |
| 84 | // corner is empty, and the brand is plain text (every page it could |
| 85 | // link to is behind the session gate). |
| 86 | signinPage bool |
| 87 | // bare drops the bar (the presenting screens). |
| 88 | bare bool |
| 89 | // ret, when set, is where Appearance returns to instead of this |
| 90 | // request's own path and query. |
| 91 | ret string |
| 92 | } |
| 93 | |
| 94 | // titled is the frame for a page whose <title> is these parts. |
| 95 | func titled(parts ...string) frame { return frame{parts: parts} } |
| 96 | |
| 97 | // onDeck is the frame for one of a deck's sub-pages: the deck's title is |
| 98 | // the bar's context and the second title part. |
| 99 | func onDeck(page, deck string) frame { |
| 100 | return frame{parts: []string{page, deck}, context: deck} |
| 101 | } |
| 102 | |
| 103 | // viewerState is who the chrome is for: nobody, or a person and how |
| 104 | // they signed in. |
| 105 | type viewerState struct { |
| 106 | user User |
| 107 | method string |
| 108 | ok bool |
| 109 | // homeSubject is the session's Home subject when it is a Home |
| 110 | // session: what names a Home-only user in the account menu, who has |
| 111 | // no address (team mode only; homeOnlyLabel). |
| 112 | homeSubject string |
| 113 | } |
| 114 | |
| 115 | // optionalViewer resolves whoever is signed in on r without requiring |
| 116 | // anybody, and without side effects: an error page, the Open source page |
| 117 | // and the share-link viewer show the viewer's own chrome, but must never |
| 118 | // sign somebody out or create a row. An address that is no longer |
| 119 | // admitted is treated as signed out here; the next guarded page is where |
| 120 | // currentUser ends that session. |
| 121 | func (a *app) optionalViewer(r *http.Request) viewerState { |
| 122 | s, ok := sessions.Current(r) |
| 123 | if !ok && a.sessions != nil { |
| 124 | s, ok = a.sessions.From(r) |
| 125 | } |
| 126 | if !ok || s.Subject == "" { |
| 127 | return viewerState{} |
| 128 | } |
| 129 | if s.Method == connect.HomeMethod { |
| 130 | u, err := a.homeUser(r, s) |
| 131 | if err != nil { |
| 132 | return viewerState{} |
| 133 | } |
| 134 | return viewerState{user: u, method: s.Method, ok: true, homeSubject: s.Subject} |
| 135 | } |
| 136 | // In team mode the roster, not the list, decides standing, and a |
| 137 | // local session admitted at sign-in stays signed in (team_access.go). |
| 138 | if a.team == nil && !a.allowed.allows(s.Subject) { |
| 139 | return viewerState{} |
| 140 | } |
| 141 | var u User |
| 142 | if err := a.db.G.WithContext(r.Context()).Where("email = ?", normalizeAddress(s.Subject)).Take(&u).Error; err != nil { |
| 143 | return viewerState{} |
| 144 | } |
| 145 | return viewerState{user: u, method: s.Method, ok: true} |
| 146 | } |
| 147 | |
| 148 | // shellFor is the shell of a page behind the session gate: u is the |
| 149 | // person currentUser resolved. |
| 150 | func (a *app) shellFor(r *http.Request, u User, f frame) shell { |
| 151 | method, subject := "", "" |
| 152 | if s, ok := sessions.Current(r); ok { |
| 153 | method = s.Method |
| 154 | if method == connect.HomeMethod { |
| 155 | subject = s.Subject |
| 156 | } |
| 157 | } |
| 158 | return shell{Frame: a.frameFor(r, viewerState{user: u, method: method, ok: true, homeSubject: subject}, f)} |
| 159 | } |
| 160 | |
| 161 | // publicShell is the shell of a page reachable signed out: the viewer is |
| 162 | // read optionally. |
| 163 | func (a *app) publicShell(r *http.Request, f frame) shell { |
| 164 | var v viewerState |
| 165 | if !f.signinPage { |
| 166 | v = a.optionalViewer(r) |
| 167 | } |
| 168 | return shell{Frame: a.frameFor(r, v, f)} |
| 169 | } |
| 170 | |
| 171 | // frameFor builds a page's Frame for viewer v. |
| 172 | func (a *app) frameFor(r *http.Request, v viewerState, f frame) Frame { |
| 173 | bar := chrome.Bar{ |
| 174 | App: chrome.App{ |
| 175 | Name: appName, |
| 176 | Slug: "slides", |
| 177 | Home: "/", |
| 178 | Icon: brand.Path("slides"), |
| 179 | }, |
| 180 | Context: f.context, |
| 181 | Actions: f.actions, |
| 182 | Labels: chrome.BarLabels{Skip: rastrillo.T(r, "slides.chrome.skip"), Menu: rastrillo.T(r, "slides.chrome.menu")}, |
| 183 | } |
| 184 | switch { |
| 185 | case f.signinPage: |
| 186 | // The corner is empty on the pages that are themselves sign-in, |
| 187 | // and the brand is plain text: everything it could link to is |
| 188 | // behind the session gate and would bounce straight back here. |
| 189 | bar.App.Home = "" |
| 190 | case v.ok: |
| 191 | bar.Account = a.account(r, v, f.ret) |
| 192 | bar.Switcher = a.switcher(r) |
| 193 | case f.public: |
| 194 | bar.SignIn = &chrome.Link{Href: signinPath, Label: rastrillo.T(r, "slides.chrome.signin")} |
| 195 | default: |
| 196 | bar.SignIn = &chrome.Link{Href: signinPath + "?return_to=" + url.QueryEscape(returnTarget(r)), Label: rastrillo.T(r, "slides.chrome.signin")} |
| 197 | } |
| 198 | if f.public { |
| 199 | bar.App.Home = "" |
| 200 | } |
| 201 | return Frame{ |
| 202 | Chrome: bar, |
| 203 | TitleParts: f.parts, |
| 204 | Title: f.title, |
| 205 | Theme: chrome.ThemeAttr(chrome.Appearance(r)), |
| 206 | Bare: f.bare, |
| 207 | } |
| 208 | } |
| 209 | |
| 210 | // returnTarget is where a person should come back to: this page, if it |
| 211 | // is a page (a GET), otherwise the deck list. |
| 212 | func returnTarget(r *http.Request) string { |
| 213 | if r.Method != http.MethodGet && r.Method != http.MethodHead { |
| 214 | return "/" |
| 215 | } |
| 216 | return r.URL.RequestURI() |
| 217 | } |
| 218 | |
| 219 | // account is the account menu (spec §8.2): who is signed in, Agents and |
| 220 | // "Connect your Oficina account" when that means something, Appearance, |
| 221 | // Help (Open source, last), and the one Sign out in the app. |
| 222 | func (a *app) account(r *http.Request, v viewerState, ret string) *chrome.Account { |
| 223 | if ret == "" { |
| 224 | ret = returnTarget(r) |
| 225 | } |
| 226 | u := v.user |
| 227 | // who is the address; only a team-mode Home-only user has none, and |
| 228 | // is named by their person ref instead (homeOnlyLabel). |
| 229 | who := u.Email |
| 230 | if who == "" && v.homeSubject != "" { |
| 231 | who = homeOnlyLabel(v.homeSubject) |
| 232 | } |
| 233 | acc := &chrome.Account{ |
| 234 | TriggerLabel: rastrillo.T(r, "slides.chrome.account"), |
| 235 | Initials: chrome.Initials(u.Name, who), |
| 236 | Address: who, |
| 237 | Labels: chrome.AccountLabels{ |
| 238 | SignedInAs: rastrillo.T(r, "slides.chrome.signed_in_as"), |
| 239 | Appearance: rastrillo.T(r, "slides.chrome.appearance"), |
| 240 | Help: rastrillo.T(r, "slides.credits.help"), |
| 241 | }, |
| 242 | Appearance: &chrome.AppearanceMenu{ |
| 243 | Action: "/appearance", |
| 244 | Return: ret, |
| 245 | Options: chrome.AppearanceOptions(chrome.Appearance(r), |
| 246 | rastrillo.T(r, "slides.chrome.appearance.light"), |
| 247 | rastrillo.T(r, "slides.chrome.appearance.dark"), |
| 248 | rastrillo.T(r, "slides.chrome.appearance.system")), |
| 249 | }, |
| 250 | Help: []estilomenu.Item{{Href: "/help/open-source", Label: rastrillo.T(r, "slides.credits.title")}}, |
| 251 | SignOut: chrome.SignOut{Action: "/signout", Label: rastrillo.T(r, "slides.account.signout")}, |
| 252 | } |
| 253 | if u.Name != "" { |
| 254 | acc.Name = u.Name |
| 255 | } |
| 256 | if who != "" { |
| 257 | acc.TriggerLabel = rastrillo.Tf(r, "slides.chrome.account.who", "address", who) |
| 258 | } |
| 259 | // Agents (agents.go): the tokens that let a program drive this |
| 260 | // person's decks. A page, so a link and no ellipsis. |
| 261 | acc.Items = []estilomenu.Item{{Href: "/agents", Label: rastrillo.T(r, "slides.account.agents")}, |
| 262 | {Href: "/fonts", Label: rastrillo.T(r, "slides.account.fonts")}} |
| 263 | if a.offersHomeLink(r, v) { |
| 264 | acc.Items = append(acc.Items, estilomenu.Item{Href: "/account/home", Label: rastrillo.T(r, "slides.home.menu_action")}) |
| 265 | } |
| 266 | return acc |
| 267 | } |
| 268 | |
| 269 | // offersHomeLink is whether the account menu offers to connect the |
| 270 | // person's Oficina account: only when Home is configured and this |
| 271 | // person's account has no stored link yet. The stored link decides, not |
| 272 | // how this session signed in: somebody already linked who signed in with |
| 273 | // the emailed link is still linked, and used to be offered it anyway |
| 274 | // (Paul hit this). One lookup on the links table's unique (issuer, |
| 275 | // person_id) index, on each render that draws the menu; an error means |
| 276 | // unknown, and offers nothing. |
| 277 | func (a *app) offersHomeLink(r *http.Request, v viewerState) bool { |
| 278 | if a.home == nil || !v.ok || v.user.ID == 0 { |
| 279 | return false |
| 280 | } |
| 281 | linked, err := a.home.Linked(r.Context(), v.user.ID) |
| 282 | return err == nil && !linked |
| 283 | } |
| 284 | |
| 285 | // switcher is the app switcher, or nil when no hub directory is |
| 286 | // configured: the suite's six apps as tiles, each at its own host under |
| 287 | // the directory's (estilo's chrome.SuiteApps), with Slides marked |
| 288 | // current, then "All apps". |
| 289 | func (a *app) switcher(r *http.Request) *chrome.Switcher { |
| 290 | if a.directory == "" { |
| 291 | return nil |
| 292 | } |
| 293 | return &chrome.Switcher{ |
| 294 | Label: rastrillo.T(r, "slides.chrome.switcher"), |
| 295 | CurrentLabel: rastrillo.T(r, "slides.chrome.switcher.current"), |
| 296 | Apps: chrome.SuiteApps(a.directory, "slides"), |
| 297 | AllApps: estilomenu.Item{Href: a.directory + "/", Label: rastrillo.T(r, "slides.chrome.switcher.all")}, |
| 298 | } |
| 299 | } |
| 300 | |