Skip to content

Commit 8ba354b

Browse files
committed
Add scrollBars and scrollToTop in html mode.
1 parent aa3cc7a commit 8ba354b

File tree

4 files changed

+18
-3
lines changed

4 files changed

+18
-3
lines changed

src/fidget.nim

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -354,6 +354,10 @@ proc clipContent*(clipContent: bool) =
354354
## Causes the parent to clip the children.
355355
current.clipContent = clipContent
356356

357+
proc scrollBars*(scrollBars: bool) =
358+
## Causes the parent to clip the children and draw scroll bars.
359+
current.scrollBars = scrollBars
360+
357361
proc cursorColor*(color: Color) =
358362
## Sets the color of the text cursor.
359363
current.cursorColor = color

src/fidget/common.nim

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,7 @@ type
142142
textLayoutWidth*: float32
143143
## Can the text be selected.
144144
selectable*: bool
145+
scrollBars*: bool ## Should it have scroll bars if children are clipped.
145146

146147
KeyState* = enum
147148
Empty

src/fidget/dom2.nim

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,7 @@ type
201201
Document* = ref DocumentObj
202202
DocumentObj {.importc.} = object of NodeObj
203203
activeElement*: Element
204+
documentElement*: Element
204205
alinkColor*: cstring
205206
bgColor*: cstring
206207
body*: Element

src/fidget/htmlbackend.nim

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -270,11 +270,17 @@ proc draw*(index: int, node: Node, parent: Node) =
270270
if node.hasDifferent(transparency):
271271
node.element.style.opacity = $node.transparency
272272
273+
if node.hasDifferent(scrollBars):
274+
if node.scrollBars:
275+
node.element.style.overflow = "auto"
276+
else:
277+
node.element.style.overflow = "visible"
278+
273279
if node.hasDifferent(clipContent):
274280
if node.clipContent:
275281
node.element.style.overflow = "hidden"
276282
else:
277-
node.element.style.overflow = "visable"
283+
node.element.style.overflow = "visible"
278284
279285
if node.kind == nkText:
280286
# In a text node many params apply to the text (not the fill).
@@ -669,11 +675,14 @@ proc getUrl*(): string =
669675
$dom.window.location.search &
670676
$dom.window.location.hash
671677
672-
proc setUrl*(url: string) =
673-
## Goes to a new URL, inserts it into history so that back button works
678+
proc setUrl*(url: string, scrollToTop = true) =
679+
## Goes to a new URL, inserts it into history so that back button works.
680+
## Also scrolls to the top of the page to mimic how an HTML reload would look.
674681
if getUrl() != url:
675682
type Dummy = object
676683
dom.window.history.pushState(Dummy(), "", url)
684+
if scrollToTop:
685+
document.documentElement.scrollTop = 0
677686
refresh()
678687
679688
proc loadFont*(name: string, pathOrUrl: string) =

0 commit comments

Comments
 (0)