Add theme option

This commit is contained in:
Zed 2019-10-23 11:48:08 +02:00
parent 9aa4ddb30b
commit 5630a4da32
4 changed files with 21 additions and 6 deletions

View file

@ -51,6 +51,9 @@ const prefList*: OrderedTable[string, seq[Pref]] = {
], ],
"Display": @[ "Display": @[
Pref(kind: select, name: "theme", label: "Theme",
defaultOption: "Dark"),
Pref(kind: checkbox, name: "hideTweetStats", Pref(kind: checkbox, name: "hideTweetStats",
label: "Hide tweet stats (replies, retweets, likes)", label: "Hide tweet stats (replies, retweets, likes)",
defaultState: false), defaultState: false),
@ -94,10 +97,12 @@ macro genUpdatePrefs*(): untyped =
of input: of input:
result.add quote do: prefs.`ident` = xmltree.escape(strip(`value`)) result.add quote do: prefs.`ident` = xmltree.escape(strip(`value`))
of select: of select:
let name = pref.name
let options = pref.options let options = pref.options
let default = pref.defaultOption let default = pref.defaultOption
result.add quote do: 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` else: prefs.`ident` = `default`
result.add quote do: result.add quote do:

View file

@ -1,4 +1,4 @@
import strutils, uri import strutils, uri, os
import jester import jester
@ -8,13 +8,17 @@ import ../views/[general, preferences]
export 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) = proc createPrefRouter*(cfg: Config) =
router preferences: router preferences:
template savePrefs(): untyped = template savePrefs(): untyped =
setCookie("preferences", $prefs.id, daysForward(360), httpOnly=true, secure=cfg.useHttps) setCookie("preferences", $prefs.id, daysForward(360), httpOnly=true, secure=cfg.useHttps)
get "/settings": get "/settings":
let html = renderPreferences(cookiePrefs(), refPath()) let html = renderPreferences(cookiePrefs(), refPath(), findThemes(cfg.staticDir))
resp renderMain(html, request, cfg, "Preferences") resp renderMain(html, request, cfg, "Preferences")
get "/settings/@i?": get "/settings/@i?":

View file

@ -30,10 +30,12 @@ proc renderNavbar*(title, rss: string; req: Request): VNode =
proc renderMain*(body: VNode; req: Request; cfg: Config; titleText=""; desc=""; proc renderMain*(body: VNode; req: Request; cfg: Config; titleText=""; desc="";
rss=""; `type`="article"; video=""; images: seq[string] = @[]): string = rss=""; `type`="article"; video=""; images: seq[string] = @[]): string =
let prefs = getPrefs(req.cookies.getOrDefault("preferences"), cfg.hostname) let prefs = getPrefs(req.cookies.getOrDefault("preferences"), cfg.hostname)
let theme = "/css/themes/" & toLowerAscii(prefs.theme) & ".css"
let node = buildHtml(html(lang="en")): let node = buildHtml(html(lang="en")):
head: head:
link(rel="stylesheet", `type`="text/css", href="/css/style.css") 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="/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="apple-touch-icon", sizes="180x180", href="/apple-touch-icon.png")
link(rel="icon", type="image/png", sizes="32x32", href="/favicon-32x32.png") link(rel="icon", type="image/png", sizes="32x32", href="/favicon-32x32.png")

View file

@ -1,4 +1,4 @@
import tables, macros, strutils import tables, macros, strutils, os
import karax/[karaxdsl, vdom, vstyles] import karax/[karaxdsl, vdom, vstyles]
import renderutils import renderutils
@ -22,12 +22,16 @@ macro renderPrefs*(): untyped =
case pref.kind case pref.kind
of checkbox: discard of checkbox: discard
of select: stmt[0].add newLit(pref.options)
of input: stmt[0].add newLit(pref.placeholder) 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 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")): buildHtml(tdiv(class="overlay-panel")):
fieldset(class="preferences"): fieldset(class="preferences"):
form(`method`="post", action="/saveprefs"): form(`method`="post", action="/saveprefs"):