Skip to content

Commit

Permalink
Add HTML only features: asHtml and tooltip.
Browse files Browse the repository at this point in the history
  • Loading branch information
treeform committed Apr 19, 2021
1 parent 557d6af commit bb6a841
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 4 deletions.
1 change: 1 addition & 0 deletions fidget.nimble
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,4 @@ requires "staticglfw >= 4.1.2"
requires "cligen >= 1.0.0"
requires "supersnappy >= 1.0.0"
requires "bumpy >= 1.0.0"
requires "jsutils >= 0.1.3"
8 changes: 8 additions & 0 deletions src/fidget.nim
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,14 @@ proc characters*(text: string) =
if current.text != text:
current.text = text

when defined(js):
proc asHtml*(asHtml: bool) =
## asHTML
current.asHtml = asHtml

template tooltip*(text: string) =
current.tooltip = text

proc image*(imageName: string) =
## Sets image fill.
current.imageName = imageName
Expand Down
2 changes: 2 additions & 0 deletions src/fidget/common.nim
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,8 @@ type
textElement*: Element
cache*: Node
zIndex*: int
asHtml*: bool
tooltip*: string
textLayoutHeight*: float32
textLayoutWidth*: float32
## Can the text be selected.
Expand Down
14 changes: 10 additions & 4 deletions src/fidget/htmlbackend.nim
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import chroma, common, dom2 as dom, html5_canvas, input, internal, math, os,
import chroma, common, dom2 as dom, jsutils/canvas, input, internal, math, os,
strformat, strutils, tables, vmath, html/ajax, json

const defaultStyle = """
Expand Down Expand Up @@ -28,7 +28,7 @@ type
var
rootDomNode*: Element
canvasNode*: Element
ctx*: CanvasRenderingContext2D
ctx*: Context2d

forceTextReLayout*: bool

Expand Down Expand Up @@ -119,7 +119,7 @@ type
ideographicBaseline*: float

proc measureText(
ctx: CanvasRenderingContext2D,
ctx: Context2d,
text: cstring
): TextMetrics {.importcpp.}

Expand Down Expand Up @@ -247,6 +247,9 @@ proc draw*(index: int, node: Node, parent: Node) =
if node.hasDifferent(uid):
node.element.setAttribute("id", node.uid)

if node.hasDifferent(tooltip):
node.element.setAttribute("title", node.tooltip)

node.zIndex =
if parent != nil:
parent.nodes.len - index
Expand Down Expand Up @@ -291,7 +294,10 @@ proc draw*(index: int, node: Node, parent: Node) =

if node.hasDifferent(text):
if keyboard.focusNode != node:
node.textElement.innerText = node.text
if node.asHTML:
node.textElement.innerHtml = node.text
else:
node.textElement.innerText = node.text
else:
# Don't mess with inner text when user is typing!
discard
Expand Down

0 comments on commit bb6a841

Please sign in to comment.