Skip to content

Commit 2a9f8e5

Browse files
committed
Add HTML only features: asHtml and tooltip.
1 parent 93bd3ff commit 2a9f8e5

File tree

4 files changed

+22
-4
lines changed

4 files changed

+22
-4
lines changed

fidget.nimble

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,5 @@ requires "html5_canvas >= 1.3"
1717
requires "staticglfw >= 4.1.2"
1818
requires "cligen >= 1.0.0"
1919
requires "supersnappy >= 1.0.0"
20+
requires "bumpy >= 1.0.0"
21+
requires "jsutils >= 0.1.3"

src/fidget.nim

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,14 @@ proc characters*(text: string) =
262262
if current.text != text:
263263
current.text = text
264264

265+
when defined(js):
266+
proc asHtml*(asHtml: bool) =
267+
## asHTML
268+
current.asHtml = asHtml
269+
270+
template tooltip*(text: string) =
271+
current.tooltip = text
272+
265273
proc image*(imageName: string) =
266274
## Sets image fill.
267275
current.imageName = imageName

src/fidget/common.nim

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,8 @@ type
138138
textElement*: Element
139139
cache*: Node
140140
zIndex*: int
141+
asHtml*: bool
142+
tooltip*: string
141143
textLayoutHeight*: float32
142144
textLayoutWidth*: float32
143145
## Can the text be selected.

src/fidget/htmlbackend.nim

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import chroma, common, dom2 as dom, html5_canvas, input, internal, math, os,
1+
import chroma, common, dom2 as dom, jsutils/canvas, input, internal, math, os,
22
strformat, strutils, tables, vmath, html/ajax, json
33

44
const defaultStyle = """
@@ -28,7 +28,7 @@ type
2828
var
2929
rootDomNode*: Element
3030
canvasNode*: Element
31-
ctx*: CanvasRenderingContext2D
31+
ctx*: Context2d
3232

3333
forceTextReLayout*: bool
3434

@@ -119,7 +119,7 @@ type
119119
ideographicBaseline*: float
120120

121121
proc measureText(
122-
ctx: CanvasRenderingContext2D,
122+
ctx: Context2d,
123123
text: cstring
124124
): TextMetrics {.importcpp.}
125125

@@ -247,6 +247,9 @@ proc draw*(index: int, node: Node, parent: Node) =
247247
if node.hasDifferent(uid):
248248
node.element.setAttribute("id", node.uid)
249249
250+
if node.hasDifferent(tooltip):
251+
node.element.setAttribute("title", node.tooltip)
252+
250253
node.zIndex =
251254
if parent != nil:
252255
parent.nodes.len - index
@@ -291,7 +294,10 @@ proc draw*(index: int, node: Node, parent: Node) =
291294
292295
if node.hasDifferent(text):
293296
if keyboard.focusNode != node:
294-
node.textElement.innerText = node.text
297+
if node.asHTML:
298+
node.textElement.innerHtml = node.text
299+
else:
300+
node.textElement.innerText = node.text
295301
else:
296302
# Don't mess with inner text when user is typing!
297303
discard

0 commit comments

Comments
 (0)