Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature suggestion: {.noredraw.} pragma #277

Merged
merged 6 commits into from
Nov 6, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 =
Araq marked this conversation as resolved.
Show resolved Hide resolved
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)
Araq marked this conversation as resolved.
Show resolved Hide resolved
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
Loading