sunday_slate/chrome.rs
1//! Typed page chrome for logged-in pages.
2//!
3//! Every template that extends `app.html` carries a [`Chrome`] field; the
4//! shared shell renders the top bar and bottom tab bar from it, so a page
5//! that forgets its chrome fails to compile.
6
7/// One bottom-bar tab.
8#[derive(Debug, Clone, Copy, PartialEq, Eq)]
9pub enum Tab {
10 Current,
11 Standings,
12 Contests,
13 MyTeam,
14}
15
16/// Top-bar and tab-bar state for one page.
17pub struct Chrome {
18 /// Bar text and the `<title>` tag.
19 pub title: String,
20 /// `Some(href)` renders a focused flow: ✕ to `href`, centered title,
21 /// no tab bar. `None` renders a left-aligned title and the tab bar,
22 /// whose More item carries the overflow menu.
23 pub close: Option<String>,
24 /// The lit tab. `None` with `close: None` shows the bar with nothing
25 /// lit (admin, invites).
26 pub tab: Option<Tab>,
27}
28
29impl Chrome {
30 /// A browsing page: tab bar with `tab` lit.
31 pub fn tabbed(title: impl Into<String>, tab: Tab) -> Self {
32 Self {
33 title: title.into(),
34 close: None,
35 tab: Some(tab),
36 }
37 }
38 /// A contest page: the Current tab when it is the request's current
39 /// contest, otherwise the Contests tab.
40 pub(crate) fn contest(title: impl Into<String>, contest_id: i64) -> Self {
41 let tab = if crate::context::current_contest() == Some(contest_id) {
42 Tab::Current
43 } else {
44 Tab::Contests
45 };
46 Self::tabbed(title, tab)
47 }
48
49 /// A browsing page outside the four tabs: tab bar, nothing lit.
50 pub fn unlit(title: impl Into<String>) -> Self {
51 Self {
52 title: title.into(),
53 close: None,
54 tab: None,
55 }
56 }
57
58 /// A focused flow: ✕ to `close`, centered title, no tab bar.
59 pub fn focused(title: impl Into<String>, close: impl Into<String>) -> Self {
60 Self {
61 title: title.into(),
62 close: Some(close.into()),
63 tab: None,
64 }
65 }
66
67 // Lit-tab tests, one per tab, callable from Askama.
68 pub fn is_current(&self) -> bool {
69 self.tab == Some(Tab::Current)
70 }
71 pub fn is_standings(&self) -> bool {
72 self.tab == Some(Tab::Standings)
73 }
74 pub fn is_contests(&self) -> bool {
75 self.tab == Some(Tab::Contests)
76 }
77 pub fn is_my_team(&self) -> bool {
78 self.tab == Some(Tab::MyTeam)
79 }
80}