| 1 | package docs |
| 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 template can drift |
| 5 | // from another. Before this, each page defined its own rastrillo shell |
| 6 | // blocks, and the share page fell back to rastrillo's default "Home" |
| 7 | // brand because it forgot one. |
| 8 | // |
| 9 | // Spec: estilo spec/shared-chrome, docs/superpowers/specs/ |
| 10 | // 2026-09-23-shared-chrome.md, §5 (bar), §6 (switcher), §8 (account), |
| 11 | // §10 (titles). |
| 12 | |
| 13 | import ( |
| 14 | "html/template" |
| 15 | "net/http" |
| 16 | "net/url" |
| 17 | |
| 18 | "amadan.net/oficina/estilo.git/brand" |
| 19 | "amadan.net/oficina/estilo.git/chrome" |
| 20 | estilomenu "amadan.net/oficina/estilo.git/menu" |
| 21 | "amadan.net/rastrillo/rastrillo" |
| 22 | "amadan.net/rastrillo/rastrillo/sessions" |
| 23 | ) |
| 24 | |
| 25 | // Frame is what views/layout.html reads from every page: the bar, the |
| 26 | // parts of the <title> before the app's name, and the Appearance |
| 27 | // preference for <html data-theme>. |
| 28 | type Frame struct { |
| 29 | Chrome chrome.Bar |
| 30 | TitleParts []string |
| 31 | // Title, when set, is the whole <title>, which then does not add the |
| 32 | // app's name: the sign-in door's title is rastrillo's, and it already |
| 33 | // names Docs ("Sign in to Docs"). |
| 34 | Title string |
| 35 | Theme string |
| 36 | } |
| 37 | |
| 38 | // frameOptions describes one page's chrome. The zero value is a plain |
| 39 | // signed-in-or-out page with no context. |
| 40 | type frameOptions struct { |
| 41 | // titleParts precede "Docs" in <title>, most specific first. |
| 42 | titleParts []string |
| 43 | // title replaces the whole <title> (Frame.Title). |
| 44 | title string |
| 45 | // context is the open object's title; edit replaces it with the |
| 46 | // editable title when the person may rename. |
| 47 | context string |
| 48 | edit *chrome.TitleEdit |
| 49 | actions []chrome.Action |
| 50 | // menubar and lead fill estilo's two bar slots (spec I2): the editor |
| 51 | // passes its menu bar, which makes the bar stacked, and its undo/redo |
| 52 | // pair. Every other page leaves both empty and keeps the one-row bar. |
| 53 | menubar template.HTML |
| 54 | lead template.HTML |
| 55 | // compact makes the bar the phone's one row below 704px (estilo's |
| 56 | // Bar.Compact, design/phone-web): the document passes its Aa and ••• |
| 57 | // and its inline title. |
| 58 | compact *chrome.Compact |
| 59 | // public is a read-only page a stranger reaches: the brand is plain |
| 60 | // text, and "Sign in" carries no return path (the path is a |
| 61 | // capability and must not be copied into another URL). |
| 62 | public bool |
| 63 | // signinPage is one of the sign-in, sent and signed-out pages: the |
| 64 | // corner is empty. |
| 65 | signinPage bool |
| 66 | } |
| 67 | |
| 68 | // frame builds a page's Frame. The viewer is read optionally: an error |
| 69 | // page, the public page and a sign-in page all show whatever the |
| 70 | // request's session says, without requiring one. |
| 71 | func (h *handlers) frame(r *http.Request, o frameOptions) Frame { |
| 72 | bar := chrome.Bar{ |
| 73 | App: chrome.App{ |
| 74 | Name: rastrillo.T(r, "docs.app.name"), |
| 75 | Slug: "docs", |
| 76 | Home: "/", |
| 77 | Icon: brand.Path("docs"), |
| 78 | }, |
| 79 | Context: o.context, |
| 80 | ContextEdit: o.edit, |
| 81 | Actions: o.actions, |
| 82 | Menubar: o.menubar, |
| 83 | Lead: o.lead, |
| 84 | Compact: o.compact, |
| 85 | Sticky: true, |
| 86 | Labels: chrome.BarLabels{Skip: rastrillo.T(r, "docs.chrome.skip"), Menu: rastrillo.T(r, "docs.chrome.menu")}, |
| 87 | } |
| 88 | if o.public { |
| 89 | bar.App.Home = "" |
| 90 | } |
| 91 | address, signedIn := "", false |
| 92 | if !o.signinPage { |
| 93 | address, signedIn = h.viewer(r) |
| 94 | } |
| 95 | switch { |
| 96 | case o.signinPage: |
| 97 | // The corner is empty on the pages that are themselves sign-in. |
| 98 | case signedIn: |
| 99 | bar.Account = h.account(r, address) |
| 100 | bar.Switcher = h.switcher(r) |
| 101 | case o.public: |
| 102 | bar.SignIn = &chrome.Link{Href: "/signin", Label: rastrillo.T(r, "docs.chrome.signin")} |
| 103 | default: |
| 104 | bar.SignIn = &chrome.Link{Href: "/signin?return_to=" + url.QueryEscape(returnTarget(r)), Label: rastrillo.T(r, "docs.chrome.signin")} |
| 105 | } |
| 106 | return Frame{Chrome: bar, TitleParts: o.titleParts, Title: o.title, Theme: chrome.ThemeAttr(chrome.Appearance(r))} |
| 107 | } |
| 108 | |
| 109 | // returnTarget is where a person should come back to: this page, if it |
| 110 | // is a page (a GET), otherwise the list. |
| 111 | func returnTarget(r *http.Request) string { |
| 112 | if r.Method != http.MethodGet { |
| 113 | return "/" |
| 114 | } |
| 115 | return r.URL.RequestURI() |
| 116 | } |
| 117 | |
| 118 | func (h *handlers) account(r *http.Request, address string) *chrome.Account { |
| 119 | a := &chrome.Account{ |
| 120 | TriggerLabel: rastrillo.T(r, "docs.chrome.account.anonymous"), |
| 121 | Initials: chrome.Initials("", address), |
| 122 | Address: address, |
| 123 | Labels: chrome.AccountLabels{ |
| 124 | SignedInAs: rastrillo.T(r, "docs.chrome.signedinas"), |
| 125 | Appearance: rastrillo.T(r, "docs.chrome.appearance"), |
| 126 | Help: rastrillo.T(r, "docs.chrome.help"), |
| 127 | }, |
| 128 | Appearance: &chrome.AppearanceMenu{ |
| 129 | Action: "/appearance", |
| 130 | Return: returnTarget(r), |
| 131 | Options: chrome.AppearanceOptions(chrome.Appearance(r), |
| 132 | rastrillo.T(r, "docs.chrome.appearance.light"), |
| 133 | rastrillo.T(r, "docs.chrome.appearance.dark"), |
| 134 | rastrillo.T(r, "docs.chrome.appearance.system")), |
| 135 | }, |
| 136 | // Help: how the fonts stand in for Word's, then the Colophon |
| 137 | // (estilo: "Open source" last of all, and the Colophon is that). |
| 138 | Help: []estilomenu.Item{ |
| 139 | {Href: fontsPath, Label: rastrillo.T(r, "docs.help.fonts")}, |
| 140 | {Href: colophonPath, Label: rastrillo.T(r, "docs.help.colophon")}, |
| 141 | }, |
| 142 | SignOut: chrome.SignOut{Action: "/signout", Label: rastrillo.T(r, "docs.signout.label")}, |
| 143 | } |
| 144 | if address != "" { |
| 145 | a.TriggerLabel = rastrillo.Tf(r, "docs.chrome.account", "address", address) |
| 146 | } |
| 147 | if label := h.homeLinkLabel(r); label != "" { |
| 148 | a.Items = []estilomenu.Item{{Href: "/account/home", Label: label}} |
| 149 | } |
| 150 | return a |
| 151 | } |
| 152 | |
| 153 | // switcher is the app switcher, or nil when no hub directory is |
| 154 | // configured: the suite's six apps as tiles, each at its own host under |
| 155 | // the directory's (estilo's chrome.SuiteApps), with Docs marked current, |
| 156 | // then "All apps". |
| 157 | func (h *handlers) switcher(r *http.Request) *chrome.Switcher { |
| 158 | if h.directory == "" { |
| 159 | return nil |
| 160 | } |
| 161 | return &chrome.Switcher{ |
| 162 | Label: rastrillo.T(r, "docs.chrome.apps"), |
| 163 | CurrentLabel: rastrillo.T(r, "docs.chrome.thisapp"), |
| 164 | Apps: chrome.SuiteApps(h.directory, "docs"), |
| 165 | AllApps: estilomenu.Item{Href: h.directory + "/", Label: rastrillo.T(r, "docs.chrome.allapps")}, |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | // viewer is who the request is signed in as, read optionally. Inside a |
| 170 | // signed-in route the session middleware has already resolved it; on a |
| 171 | // route no session guards (an error page, the public page) peek reads |
| 172 | // the cookie without requiring it and without writing anything. |
| 173 | // |
| 174 | // The address is empty for a session whose person has no verified |
| 175 | // address; the account menu then shows the person glyph. |
| 176 | func (h *handlers) viewer(r *http.Request) (string, bool) { |
| 177 | if id, ok := currentIdentity(r); ok { |
| 178 | return actorAddress(id.Actor), true |
| 179 | } |
| 180 | if s, ok := sessions.Current(r); ok && s.Subject != "" { |
| 181 | if s.Method == "magiclink" || s.Method == "keymail" { |
| 182 | return s.Subject, true |
| 183 | } |
| 184 | } |
| 185 | if h.peek != nil { |
| 186 | return h.peek(r) |
| 187 | } |
| 188 | return "", false |
| 189 | } |
| 190 | |
| 191 | func actorAddress(a Actor) string { |
| 192 | if a.VerifiedAddress == nil { |
| 193 | return "" |
| 194 | } |
| 195 | return *a.VerifiedAddress |
| 196 | } |
| 197 | |
| 198 | // pageHeaders marks a rendered page as depending on the platform its |
| 199 | // shortcut labels were formatted for: menu.Shortcut reads the |
| 200 | // User-Agent, so a shared cache must not hand a Mac's page to Windows. |
| 201 | func pageHeaders(w http.ResponseWriter) { |
| 202 | w.Header().Add("Vary", "User-Agent") |
| 203 | } |
| 204 | |