Skip to content

Commit

Permalink
feature suggestion: {.noredraw.} pragma (karaxnim#277)
Browse files Browse the repository at this point in the history
* add {.noredraw.} pragma for event handlers

* put .noredraw. pragma handling in seperate proc

* refactor

* refactor

* Revert "refactor"

This reverts commit 0d4ce4b.

* use result for return
  • Loading branch information
choltreppe authored and daylinmorgan committed Aug 28, 2024
1 parent a11410c commit efd36b4
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 3 deletions.
8 changes: 8 additions & 0 deletions karax/karax.nim
Original file line number Diff line number Diff line change
Expand Up @@ -829,6 +829,14 @@ proc addEventHandler*(n: VNode; k: EventKind; action: proc();
if not kxi.surpressRedraws: redraw(kxi)
addEventListener(n, k, wrapper)

proc addEventHandlerNoRedraw*(n: VNode; k: EventKind; action: EventHandler) =
addEventListener(n, k, action)

proc addEventHandlerNoRedraw*(n: VNode; k: EventKind; action: proc()) =
proc wrapper(ev: Event; n: VNode) =
action()
addEventListener(n, k, wrapper)

proc setOnHashChange*(action: proc (hashPart: cstring)) {.deprecated: "use setRenderer instead".} =
## Now deprecated, instead pass a callback to ``setRenderer`` that receives
## a ``data: RouterData`` parameter.
Expand Down
22 changes: 19 additions & 3 deletions karax/karaxdsl.nim
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@

import macros, vdom, compact, kbase
from strutils import startsWith, toLowerAscii
from strutils import startsWith, toLowerAscii, cmpIgnoreStyle

when defined(js):
import karax
Expand Down Expand Up @@ -44,6 +44,21 @@ 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):
if anon.pragma.kind == nnkPragma and len(anon.pragma) > 0:
var hasNoRedrawPragma = false
for i in 0 ..< len(anon.pragma):
# using anon because anon needs to get rid of the pragma
if anon.pragma[i].kind == nnkIdent and cmpIgnoreStyle(anon.pragma[i].strVal, "noredraw") == 0:
hasNoRedrawPragma = true
anon.pragma.del(i)
break
if hasNoRedrawPragma:
return newCall(ident"addEventHandlerNoRedraw", tmpContext,
newDotExpr(bindSym"EventKind", name), anon)
result = call

proc tcall2(n, tmpContext: NimNode): NimNode =
# we need to distinguish statement and expression contexts:
# every call statement 's' needs to be transformed to 'dest.add s'.
Expand Down Expand Up @@ -94,8 +109,9 @@ proc tcall2(n, tmpContext: NimNode): NimNode =
if tmpContext == nil:
error "no VNode to attach the event handler to"
else:
result = newCall(evHandler(), tmpContext,
newDotExpr(bindSym"EventKind", n[0]), anon, ident("kxi"))
let call = newCall(evHandler(), tmpContext,
newDotExpr(bindSym"EventKind", n[0]), anon, ident("kxi"))
result = handleNoRedrawPragma(call, tmpContext, n[0], anon)
else:
result = n
of nnkVarSection, nnkLetSection, nnkConstSection:
Expand Down

0 comments on commit efd36b4

Please sign in to comment.