Skip to content

Commit 6eb2158

Browse files
committed
still horrible
1 parent 9d26345 commit 6eb2158

File tree

3 files changed

+29
-9
lines changed

3 files changed

+29
-9
lines changed

doc.nim

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ var lineWidth = 0
1818
var textRun = false
1919
var words: seq[string] = @[]
2020
var level = 3
21+
var scrollY = 0
22+
var controlHeight = 500
23+
var firstPass = true
2124

2225
proc parse*(rst:string): PRstNode =
2326
var opts = {roSupportSmilies}
@@ -42,6 +45,12 @@ proc splitLines(canvas: Canvas) : seq[string] =
4245
lineWidth = textWidth
4346
return lines
4447

48+
proc inView():bool =
49+
let startY = scrollY
50+
let endY = scrollY + controlHeight
51+
#echo startY, ",", endY, ", ", scrollY
52+
result = (y >= startY and y <= endY)
53+
4554
proc renderLeaf(canvas: Canvas) =
4655
if x > pageWidth:
4756
x = spaceWidth
@@ -50,8 +59,9 @@ proc renderLeaf(canvas: Canvas) =
5059
var lines = splitLines(canvas)
5160
for line in lines:
5261
canvas.fontSize = size[level-1]
53-
canvas.drawText(line, x, y)
54-
drawTextCalls += 1
62+
if inView():
63+
canvas.drawText(line, x, y)
64+
drawTextCalls += 1
5565
if len(lines) != 1:
5666
y += textHeight
5767
#x = floor(textWidth.toFloat * wScale).toInt
@@ -91,6 +101,7 @@ proc renderUnknown(canvas: Canvas, node: PRstNode) =
91101
lastUnknown = true
92102

93103
proc renderNode(canvas: Canvas, node: PRstNode) =
104+
if firstPass and not inView(): return
94105
if node == nil: return
95106
#echo node.kind
96107
var dummy = 0
@@ -116,25 +127,25 @@ proc renderNode(canvas: Canvas, node: PRstNode) =
116127
for son in node.sons:
117128
renderNode(canvas, son)
118129

119-
proc render*(canvas: Canvas, root: PRstNode) =
130+
proc render*(canvas: Canvas, root: PRstNode, scrollPos:int) =
120131
x = 0
121132
y = 0
133+
scrollY = scrollPos
122134
var n = 0
123135
textRun = false
124136
words = newSeq[string]()
125137
level = 3
126138

127139
drawTextCalls = 0
128-
canvas.textColor = rgb(0, 0, 0)
129-
canvas.fontSize = 20
130-
canvas.fontFamily = "Arial"
131-
140+
132141
let time = cpuTime()
133142
for son in root.sons:
134143
n += 1
135144
renderNode(canvas, son)
136145

137-
if pageHeight != y:
146+
if pageHeight < y:
138147
pageHeight = y + 100
139148
echo "render in ",(cpuTime()-time)*1000.0
140149
echo "total drawText calls: ",drawTextCalls
150+
firstPass = false
151+

nsw.exe

1.32 KB
Binary file not shown.

nsw.nim

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,17 @@ control1.heightMode = HeightMode_Static
3232

3333
#control1.height = pageHeight
3434

35+
var initialized = false
36+
37+
3538
control1.onDraw = proc (event: DrawEvent) =
36-
render(event.control.canvas, root)
39+
if not initialized:
40+
control1.canvas.textColor = rgb(0, 0, 0)
41+
control1.canvas.fontSize = 20
42+
control1.canvas.fontFamily = "Arial"
43+
initialized = true
44+
45+
render(event.control.canvas, root, scrollContainer.yScrollPos())
3746
if control1.height != pageHeight:
3847
echo "fix height"
3948
control1.height = pageHeight

0 commit comments

Comments
 (0)