Skip to content

Commit

Permalink
some cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
Nimaoth committed Sep 30, 2023
1 parent 2978ad1 commit 984bced
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 58 deletions.
28 changes: 1 addition & 27 deletions src/platform/widget_builder_model_document.nim
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import std/[strformat, tables, sugar, strutils]
import util, app, document_editor, model_document, text/text_document, custom_logger, widgets, platform, theme, widget_builder_text_document, config_provider
import util, app, document_editor, model_document, text/text_document, custom_logger, platform, theme, config_provider
import widget_builders_base, widget_library, ui/node
import vmath, bumpy, chroma
import ast/[types, cells]
Expand All @@ -9,17 +9,11 @@ import ast/[types, cells]

func withAlpha(color: Color, alpha: float32): Color = color(color.r, color.g, color.b, alpha)

proc updateBaseIndexAndScrollOffset(self: ModelDocumentEditor, app: App, contentPanel: WPanel) =
# let totalLineHeight = app.platform.totalLineHeight
discard

type CellLayoutContext = ref object
builder: UINodeBuilder
currentLine: int
indexInLine: int
currentIndent: int
# parentWidget: WPanel
lineWidget: WPanel
parentNode: UINode
lineNode: UINode
indentNode: UINode
Expand Down Expand Up @@ -85,30 +79,10 @@ proc newLine(self: CellLayoutContext) =
self.indentNode = currentNode

self.indexInLine = 0

self.hasIndent = false

self.indent()

proc addWidget(self: CellLayoutContext, widget: WWidget, spaceLeft: bool) =
let width = widget.right
let height = widget.bottom

self.lineWidget[self.indexInLine] = nil
if spaceLeft:
self.addSpace()

widget.left = self.lineWidget.right
widget.right = widget.left + width
widget.top = 0
widget.bottom = height

self.lineWidget.right = widget.right
self.lineWidget.bottom = max(self.lineWidget.bottom, self.lineWidget.top + height)

self.lineWidget[self.indexInLine] = widget
inc self.indexInLine

proc finish(self: CellLayoutContext) =
self.builder.finishNode(self.lineNode)
self.builder.finishNode(self.parentNode)
Expand Down
31 changes: 0 additions & 31 deletions src/platform/widget_builder_text_document.nim
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ else:
template tokenColor*(theme: Theme, part: StyledText, default: untyped): Color =
theme.tokenColor(part.scope, default)

proc updateBaseIndexAndScrollOffset*(height: float, previousBaseIndex: var int, scrollOffset: var float, lines: int, totalLineHeight: float, targetLine: Option[int])
proc shouldIgnoreAsContextLine(self: TextDocument, line: int): bool
proc clampToLine(document: TextDocument, selection: Selection, line: StyledLine): tuple[first: RuneIndex, last: RuneIndex]

Expand Down Expand Up @@ -504,36 +503,6 @@ proc clampToLine(document: TextDocument, selection: Selection, line: StyledLine)
result.first = if selection.first.line < line.index: 0.RuneIndex elif selection.first.line == line.index: document.lines[line.index].runeIndex(selection.first.column) else: line.runeLen.RuneIndex
result.last = if selection.last.line < line.index: 0.RuneIndex elif selection.last.line == line.index: document.lines[line.index].runeIndex(selection.last.column) else: line.runeLen.RuneIndex

proc updateBaseIndexAndScrollOffset*(height: float, previousBaseIndex: var int, scrollOffset: var float, lines: int, totalLineHeight: float, targetLine: Option[int]) =

if targetLine.getSome(targetLine):
let targetLineY = (targetLine - previousBaseIndex).float32 * totalLineHeight + scrollOffset

# let margin = clamp(getOption[float32](self.editor, "text.cursor-margin", 25.0), 0.0, self.lastContentBounds.h * 0.5 - totalLineHeight * 0.5)
let margin = 0.0
if targetLineY < margin:
scrollOffset = margin
previousBaseIndex = targetLine
elif targetLineY + totalLineHeight > height - margin:
scrollOffset = height - margin - totalLineHeight
previousBaseIndex = targetLine

previousBaseIndex = previousBaseIndex.clamp(0..lines)

# Adjust scroll offset and base index so that the first node on screen is the base
while scrollOffset < 0 and previousBaseIndex + 1 < lines:
if scrollOffset + totalLineHeight >= height:
break
previousBaseIndex += 1
scrollOffset += totalLineHeight

# Adjust scroll offset and base index so that the first node on screen is the base
while scrollOffset > height and previousBaseIndex > 0:
if scrollOffset - totalLineHeight <= 0:
break
previousBaseIndex -= 1
scrollOffset -= totalLineHeight

proc createLinesInPanel*(app: App, contentPanel: WPanel, previousBaseIndex: int, scrollOffset: float, lines: int, frameIndex: int, onlyRenderInBounds: bool,
renderLine: proc(lineWidget: WPanel, i: int, down: bool, frameIndex: int): bool) =

Expand Down
30 changes: 30 additions & 0 deletions src/platform/widget_library.nim
Original file line number Diff line number Diff line change
Expand Up @@ -82,3 +82,33 @@ proc createLines*(builder: UINodeBuilder, previousBaseIndex: int, scrollOffset:

if not sizeToContentY and y > 0: # fill remaining space with background color
builder.panel(&{FillX, FillBackground}, h = y, backgroundColor = backgroundColor)

proc updateBaseIndexAndScrollOffset*(height: float, previousBaseIndex: var int, scrollOffset: var float, lines: int, totalLineHeight: float, targetLine: Option[int]) =

if targetLine.getSome(targetLine):
let targetLineY = (targetLine - previousBaseIndex).float32 * totalLineHeight + scrollOffset

# let margin = clamp(getOption[float32](self.editor, "text.cursor-margin", 25.0), 0.0, self.lastContentBounds.h * 0.5 - totalLineHeight * 0.5)
let margin = 0.0
if targetLineY < margin:
scrollOffset = margin
previousBaseIndex = targetLine
elif targetLineY + totalLineHeight > height - margin:
scrollOffset = height - margin - totalLineHeight
previousBaseIndex = targetLine

previousBaseIndex = previousBaseIndex.clamp(0..lines)

# Adjust scroll offset and base index so that the first node on screen is the base
while scrollOffset < 0 and previousBaseIndex + 1 < lines:
if scrollOffset + totalLineHeight >= height:
break
previousBaseIndex += 1
scrollOffset += totalLineHeight

# Adjust scroll offset and base index so that the first node on screen is the base
while scrollOffset > height and previousBaseIndex > 0:
if scrollOffset - totalLineHeight <= 0:
break
previousBaseIndex -= 1
scrollOffset -= totalLineHeight

0 comments on commit 984bced

Please sign in to comment.