From 5630a4da32e81575daafda24d6787acccc5cc125 Mon Sep 17 00:00:00 2001 From: Zed Date: Wed, 23 Oct 2019 11:48:08 +0200 Subject: [PATCH] Add theme option --- src/prefs_impl.nim | 7 ++++++- src/routes/preferences.nim | 8 ++++++-- src/views/general.nim | 2 ++ src/views/preferences.nim | 10 +++++++--- 4 files changed, 21 insertions(+), 6 deletions(-) diff --git a/src/prefs_impl.nim b/src/prefs_impl.nim index 96f8479..4978b70 100644 --- a/src/prefs_impl.nim +++ b/src/prefs_impl.nim @@ -51,6 +51,9 @@ const prefList*: OrderedTable[string, seq[Pref]] = { ], "Display": @[ + Pref(kind: select, name: "theme", label: "Theme", + defaultOption: "Dark"), + Pref(kind: checkbox, name: "hideTweetStats", label: "Hide tweet stats (replies, retweets, likes)", defaultState: false), @@ -94,10 +97,12 @@ macro genUpdatePrefs*(): untyped = of input: result.add quote do: prefs.`ident` = xmltree.escape(strip(`value`)) of select: + let name = pref.name let options = pref.options let default = pref.defaultOption result.add quote do: - if `value` in `options`: prefs.`ident` = `value` + if `name` == "theme": prefs.`ident` = `value` + elif `value` in `options`: prefs.`ident` = `value` else: prefs.`ident` = `default` result.add quote do: diff --git a/src/routes/preferences.nim b/src/routes/preferences.nim index 47bd5c3..6bba29d 100644 --- a/src/routes/preferences.nim +++ b/src/routes/preferences.nim @@ -1,4 +1,4 @@ -import strutils, uri +import strutils, uri, os import jester @@ -8,13 +8,17 @@ import ../views/[general, preferences] export preferences +proc findThemes*(dir: string): seq[string] = + for kind, path in walkDir(dir / "css" / "themes"): + result.add path.splitFile.name.capitalizeAscii + proc createPrefRouter*(cfg: Config) = router preferences: template savePrefs(): untyped = setCookie("preferences", $prefs.id, daysForward(360), httpOnly=true, secure=cfg.useHttps) get "/settings": - let html = renderPreferences(cookiePrefs(), refPath()) + let html = renderPreferences(cookiePrefs(), refPath(), findThemes(cfg.staticDir)) resp renderMain(html, request, cfg, "Preferences") get "/settings/@i?": diff --git a/src/views/general.nim b/src/views/general.nim index af650e7..e0433aa 100644 --- a/src/views/general.nim +++ b/src/views/general.nim @@ -30,10 +30,12 @@ proc renderNavbar*(title, rss: string; req: Request): VNode = proc renderMain*(body: VNode; req: Request; cfg: Config; titleText=""; desc=""; rss=""; `type`="article"; video=""; images: seq[string] = @[]): string = let prefs = getPrefs(req.cookies.getOrDefault("preferences"), cfg.hostname) + let theme = "/css/themes/" & toLowerAscii(prefs.theme) & ".css" let node = buildHtml(html(lang="en")): head: link(rel="stylesheet", `type`="text/css", href="/css/style.css") link(rel="stylesheet", `type`="text/css", href="/css/fontello.css") + link(rel="stylesheet", `type`="text/css", href=theme) link(rel="apple-touch-icon", sizes="180x180", href="/apple-touch-icon.png") link(rel="icon", type="image/png", sizes="32x32", href="/favicon-32x32.png") diff --git a/src/views/preferences.nim b/src/views/preferences.nim index c503cbc..7a415f6 100644 --- a/src/views/preferences.nim +++ b/src/views/preferences.nim @@ -1,4 +1,4 @@ -import tables, macros, strutils +import tables, macros, strutils, os import karax/[karaxdsl, vdom, vstyles] import renderutils @@ -22,12 +22,16 @@ macro renderPrefs*(): untyped = case pref.kind of checkbox: discard - of select: stmt[0].add newLit(pref.options) of input: stmt[0].add newLit(pref.placeholder) + of select: + if pref.name == "theme": + stmt[0].add ident("themes") + else: + stmt[0].add newLit(pref.options) result[2].add stmt -proc renderPreferences*(prefs: Prefs; path: string): VNode = +proc renderPreferences*(prefs: Prefs; path: string; themes: seq[string]): VNode = buildHtml(tdiv(class="overlay-panel")): fieldset(class="preferences"): form(`method`="post", action="/saveprefs"):