Simplify pref macros

このコミットが含まれているのは:
Zed 2019-08-15 14:38:14 +02:00
コミット 8879612594
2個のファイルの変更16行の追加28行の削除

ファイルの表示

@ -92,17 +92,15 @@ macro genUpdatePrefs*(): untyped =
case pref.kind
of checkbox:
result.add quote do:
prefs.`ident` = `value` == "on"
result.add quote do: prefs.`ident` = `value` == "on"
of input:
result.add quote do: prefs.`ident` = `value`
of select:
let options = pref.options
let default = pref.defaultOption
result.add quote do:
if `value` in `options`: prefs.`ident` = `value`
else: prefs.`ident` = `default`
of input:
result.add quote do:
prefs.`ident` = `value`
result.add quote do:
cache(prefs)

ファイルの表示

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