Simplify pref macros

This commit is contained in:
Zed 2019-08-15 14:38:14 +02:00
parent 84dfcd089a
commit 8879612594
2 changed files with 16 additions and 28 deletions

View File

@ -92,17 +92,15 @@ macro genUpdatePrefs*(): untyped =
case pref.kind case pref.kind
of checkbox: of checkbox:
result.add quote do: result.add quote do: prefs.`ident` = `value` == "on"
prefs.`ident` = `value` == "on" of input:
result.add quote do: prefs.`ident` = `value`
of select: of select:
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 `value` in `options`: prefs.`ident` = `value`
else: prefs.`ident` = `default` else: prefs.`ident` = `default`
of input:
result.add quote do:
prefs.`ident` = `value`
result.add quote do: result.add quote do:
cache(prefs) cache(prefs)

View File

@ -3,7 +3,7 @@ import karax/[karaxdsl, vdom, vstyles]
import ../types, ../prefs import ../types, ../prefs
proc genCheckbox(pref: string; label: string; state: bool): VNode = proc genCheckbox(pref, label: string; state: bool): VNode =
buildHtml(tdiv(class="pref-group")): buildHtml(tdiv(class="pref-group")):
if state: if state:
input(name=pref, `type`="checkbox", checked="") input(name=pref, `type`="checkbox", checked="")
@ -11,7 +11,7 @@ proc genCheckbox(pref: string; label: string; state: bool): VNode =
input(name=pref, `type`="checkbox") input(name=pref, `type`="checkbox")
label(`for`=pref): text label label(`for`=pref): text label
proc genSelect(pref: string; label: string; options: seq[string]; state: string): VNode = proc genSelect(pref, label, state: string; options: seq[string]): VNode =
buildHtml(tdiv(class="pref-group")): buildHtml(tdiv(class="pref-group")):
select(name=pref): select(name=pref):
for opt in options: for opt in options:
@ -21,7 +21,7 @@ proc genSelect(pref: string; label: string; options: seq[string]; state: string)
option(value=opt): text opt option(value=opt): text opt
label(`for`=pref): text label label(`for`=pref): text label
proc genInput(pref: string; label: string; placeholder, state: string): VNode = proc genInput(pref, label, state, placeholder: string): VNode =
buildHtml(tdiv(class="pref-group")): buildHtml(tdiv(class="pref-group")):
input(name=pref, `type`="text", placeholder=placeholder, value=state) input(name=pref, `type`="text", placeholder=placeholder, value=state)
label(`for`=pref): text label label(`for`=pref): text label
@ -37,27 +37,17 @@ macro renderPrefs*(): untyped =
nnkCommand.newTree(ident("text"), newLit(header)))) nnkCommand.newTree(ident("text"), newLit(header))))
for pref in options: for pref in options:
let name = newLit(pref.name) let procName = ident("gen" & capitalizeAscii($pref.kind))
let label = newLit(pref.label) let state = nnkDotExpr.newTree(ident("prefs"), ident(pref.name))
let field = ident(pref.name) var stmt = nnkStmtList.newTree(
nnkCall.newTree(procName, newLit(pref.name), newLit(pref.label), state))
case pref.kind case pref.kind
of checkbox: of checkbox: discard
result[2].add nnkStmtList.newTree( of select: stmt[0].add newLit(pref.options)
nnkCall.newTree( of input: stmt[0].add newLit(pref.placeholder)
ident("genCheckbox"), name, label,
nnkDotExpr.newTree(ident("prefs"), field))) result[2].add stmt
of select:
let options = newLit(pref.options)
result[2].add nnkStmtList.newTree(
nnkCall.newTree(
ident("genSelect"), name, label, options,
nnkDotExpr.newTree(ident("prefs"), field)))
of input:
let placeholder = newLit(pref.placeholder)
result[2].add nnkStmtList.newTree(
nnkCall.newTree(
ident("genInput"), name, label, placeholder,
nnkDotExpr.newTree(ident("prefs"), field)))
proc renderPreferences*(prefs: Prefs): VNode = proc renderPreferences*(prefs: Prefs): VNode =
buildHtml(tdiv(class="preferences-container")): buildHtml(tdiv(class="preferences-container")):