Skip to content

Commit

Permalink
Fixed some visual bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
Nimaoth committed Sep 29, 2023
1 parent 5fe8563 commit ea7e01d
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 30 deletions.
2 changes: 2 additions & 0 deletions src/platform/gui_platform.nim
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,8 @@ proc drawNode(builder: UINodeBuilder, platform: GuiPlatform, node: UINode, offse
if builder.useInvalidation and not force and node.lastChange < builder.frameIndex:
return

node.lastRenderTime = builder.frameIndex

if node.flags.any &{UINodeFlag.FillBackground, DrawBorder, DrawText}:
platform.drawnNodes.add node

Expand Down
2 changes: 2 additions & 0 deletions src/platform/terminal_platform.nim
Original file line number Diff line number Diff line change
Expand Up @@ -418,6 +418,8 @@ proc drawNode(builder: UINodeBuilder, platform: TerminalPlatform, node: UINode,
if builder.useInvalidation and not force and node.lastChange < builder.frameIndex:
return

node.lastRenderTime = builder.frameIndex

if node.flags.any &{UINodeFlag.FillBackground, DrawText}:
force = true

Expand Down
13 changes: 8 additions & 5 deletions src/platform/widget_builder_text_document.nim
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ proc renderLine*(
builder.panel(flagsInner + LayoutVertical, y = y, pivot = pivot, userId = newSecondaryId(parentId, lineId)):
let lineWidth = currentNode.bounds.w

var subLine: UINode = nil

var start = 0
var startRune = 0.RuneCount
var lastPartXW: float32 = 0
Expand All @@ -73,6 +75,7 @@ proc renderLine*(
while partIndex < line.parts.len:

builder.panel(flagsInner + LayoutHorizontal):
subLine = currentNode

if lineNumberText.len > 0:
builder.panel(&{UINodeFlag.FillBackground, FillY}, w = lineNumberTotalWidth, backgroundColor = backgroundColor):
Expand Down Expand Up @@ -174,11 +177,6 @@ proc renderLine*(
partIndex += 1
subLinePartIndex += 1

# cursor after latest char
for curs in cursors:
if curs == lineOriginal.len:
result.add (currentNode, "", rect(lastPartXW, 0, builder.charWidth, builder.textHeight), (line.index, curs))

# Fill rest of line with background
builder.panel(&{FillX, FillY, FillBackground}, backgroundColor = backgroundColor):
capture line, currentNode:
Expand All @@ -195,6 +193,11 @@ proc renderLine*(
self.selection = (currentSelection.first, (line.index, self.document.lineLength(line.index)))
self.markDirty()

# cursor after latest char
for curs in cursors:
if curs == lineOriginal.len:
result.add (subLine, "", rect(lastPartXW, 0, builder.charWidth, builder.textHeight), (line.index, curs))

proc createHeader(self: TextDocumentEditor, builder: UINodeBuilder, app: App, headerColor: Color, textColor: Color): UINode =
if self.renderHeader:
builder.panel(&{FillX, SizeToContentY, FillBackground, LayoutHorizontal}, backgroundColor = headerColor):
Expand Down
7 changes: 7 additions & 0 deletions src/text/text_editor.nim
Original file line number Diff line number Diff line change
Expand Up @@ -468,17 +468,24 @@ proc selectPrev(self: TextDocumentEditor) {.expose("editor.text").} =
let selection = self.selectionHistory.popLast
self.selectionHistory.addFirst self.selections
self.selectionsInternal = selection
self.cursorVisible = true
if self.blinkCursorTask.isNotNil and self.active:
self.blinkCursorTask.reschedule()
self.scrollToCursor(self.selection.last)

proc selectNext(self: TextDocumentEditor) {.expose("editor.text").} =
if self.selectionHistory.len > 0:
let selection = self.selectionHistory.popFirst
self.selectionHistory.addLast self.selections
self.selectionsInternal = selection
self.cursorVisible = true
if self.blinkCursorTask.isNotNil and self.active:
self.blinkCursorTask.reschedule()
self.scrollToCursor(self.selection.last)

proc selectInside(self: TextDocumentEditor, cursor: Cursor) {.expose("editor.text").} =
self.selection = self.getSelectionForMove(cursor, "word")
# todo
# let regex = re("[a-zA-Z0-9_]")
# var first = cursor.column
# # echo self.document.lines[cursor.line], ", ", first, ", ", self.document.lines[cursor.line].matchLen(regex, start = first - 1)
Expand Down
53 changes: 28 additions & 25 deletions src/ui/node.nim
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ defineBitFlag:
AnimateBounds
AnimatePosition
AnimateSize
SnapInitialBounds

type
UIUserIdKind* = enum None, Primary, Secondary
Expand Down Expand Up @@ -786,6 +787,25 @@ proc removeFromParent*(node: UINode) =
proc getNextOrNewNode(builder: UINodeBuilder, node: UINode, last: UINode, userId: var UIUserId): UINode =
let insert = true

let firstOrLast = if last.isNotNil: last else: node.first

# search ahead to see if we find a matching node
var matchingNode = UINode.none
if userId.kind != None and node.first.isNotNil:
for _, c in firstOrLast.nextSiblings:
if c.userId == userId:
matchingNode = c.some
break

if matchingNode.isSome:
matchingNode.get.removeFromParent()
node.insert(matchingNode.get, firstOrLast)

assert firstOrLast.next == matchingNode.get
assert firstOrLast.next.userId == userId

return firstOrLast.next

if last.isNil: # Creating/Updating first child
if node.first.isNotNil: # First child already exists
if node.first.userId == userId: # User id matches, reuse existing
Expand All @@ -809,30 +829,6 @@ proc getNextOrNewNode(builder: UINodeBuilder, node: UINode, last: UINode, userId
if last.next.userId == userId:
return last.next
elif userId.kind != None: # Has user id, doesn't match

# search ahead to see if we find a matching node
var matchingNode = UINode.none
for _, c in last.nextSiblings:
if c.userId == userId:
matchingNode = c.some
break

if matchingNode.isSome:
# echo "found matching node later, delete inbetween"
# remove all nodes in between
for _, c in last.nextSiblings:
if c == matchingNode.get:
break
# echo "delete old node ", c.dump(), ", ", node.clearRect
if builder.useInvalidation:
node.clearedChildrenBounds = node.clearedChildrenBounds or c.boundsActual.some
c.removeFromParent()
builder.returnNode(c)
assert last.next == matchingNode.get
assert last.next.userId == userId

return last.next

let newNode = builder.unpoolNode(userId)

if newNode.parent.isNotNil:
Expand Down Expand Up @@ -928,7 +924,14 @@ proc postProcessNodeBackwards(builder: UINodeBuilder, node: UINode, offsetX: flo
let wasAnimating = node.id in builder.animatingNodes

let animationProgress = clamp(node.boundsLerpSpeed * builder.animationSpeedModifier * builder.frameTime, 0, 1)
if AnimateBounds in node.flags or node.flags.all &{AnimatePosition, AnimateSize}:
if node.lastRenderTime == 0 and SnapInitialBounds in node.flags:
node.boundsActual.x = node.x
node.boundsActual.y = node.y
node.boundsActual.w = node.w
node.boundsActual.h = node.h
builder.animatingNodes.excl node.id

elif AnimateBounds in node.flags or node.flags.all &{AnimatePosition, AnimateSize}:
if node.boundsActual.x == node.x and
node.boundsActual.y == node.y and
node.boundsActual.w == node.w and
Expand Down

0 comments on commit ea7e01d

Please sign in to comment.