Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
choltreppe committed Nov 4, 2023
1 parent 98ae35c commit 0d4ce4b
Showing 1 changed file with 15 additions and 13 deletions.
28 changes: 15 additions & 13 deletions karax/karaxdsl.nim
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,17 @@ proc toKstring(n: NimNode): NimNode =
proc newDotAsgn(tmp: NimNode, key: string, x: NimNode): NimNode =
result = newTree(nnkAsgn, newDotExpr(tmp, newIdentNode key), x)

proc handleNoRedrawPragma(call: NimNode, tmpContext, name, anon: NimNode): NimNode =
when defined(js):
template evHandler(): untyped = bindSym"addEventHandler"
else:
template evHandler(): untyped = ident"addEventHandler"

proc genAddHandlerCall(tmpContext, n: NimNode): NimNode =
# turn it into an anon proc:
let anon = copyNimTree(n)
anon[0] = newEmptyNode()
when defined(js):
# if has noredraw pragma add handler without redraw
if anon.pragma.kind == nnkPragma and len(anon.pragma) > 0:
var hasNoRedrawPragma = false
for i in 0 ..< len(anon.pragma):
Expand All @@ -56,8 +65,10 @@ proc handleNoRedrawPragma(call: NimNode, tmpContext, name, anon: NimNode): NimNo
break
if hasNoRedrawPragma:
return newCall(ident"addEventHandlerNoRedraw", tmpContext,
newDotExpr(bindSym"EventKind", name), anon)
call
newDotExpr(bindSym"EventKind", n[0]), anon)

newCall(evHandler(), tmpContext,
newDotExpr(bindSym"EventKind", n[0]), anon, ident("kxi"))

proc tcall2(n, tmpContext: NimNode): NimNode =
# we need to distinguish statement and expression contexts:
Expand All @@ -68,10 +79,6 @@ proc tcall2(n, tmpContext: NimNode): NimNode =
# (except for the last child of the macros we consider here),
# lets, consts, types can be considered as expressions
# case is complex, calls are assumed to produce a value.
when defined(js):
template evHandler(): untyped = bindSym"addEventHandler"
else:
template evHandler(): untyped = ident"addEventHandler"

case n.kind
of nnkLiterals, nnkIdent, nnkSym, nnkDotExpr, nnkBracketExpr:
Expand Down Expand Up @@ -103,15 +110,10 @@ proc tcall2(n, tmpContext: NimNode): NimNode =
of nnkProcDef:
let name = getName n[0]
if name.startsWith"on":
# turn it into an anon proc:
let anon = copyNimTree(n)
anon[0] = newEmptyNode()
if tmpContext == nil:
error "no VNode to attach the event handler to"
else:
let call = newCall(evHandler(), tmpContext,
newDotExpr(bindSym"EventKind", n[0]), anon, ident("kxi"))
result = handleNoRedrawPragma(call, tmpContext, n[0], anon)
result = genAddHandlerCall(tmpContext, n)
else:
result = n
of nnkVarSection, nnkLetSection, nnkConstSection:
Expand Down

0 comments on commit 0d4ce4b

Please sign in to comment.