Skip to content

Commit

Permalink
fixed some bugs, set cursor when clicking
Browse files Browse the repository at this point in the history
  • Loading branch information
Nimaoth committed Oct 3, 2023
1 parent 42e55da commit 360a811
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 42 deletions.
1 change: 1 addition & 0 deletions src/id.nim
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ proc hashInt32(x: uint32): uint32 {.inline.} =
result = ((result shr 16) xor result) * 0x45d9f3b
result = ((result shr 16) xor result) * 0x45d9f3b
result = (result shr 16) xor result
result = result and 0x7FFFFFFF.uint32

proc hash*(oid: Oid): Hash =
## Generates the hash of an OID for use in hashtables.
Expand Down
2 changes: 1 addition & 1 deletion src/model_document.nim
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ type
targetNodeOld*: UINode
targetNode*: UINode
targetCell*: Cell
handleClick*: proc(node: UINode, cell: Cell, path: seq[int])
handleClick*: proc(node: UINode, cell: Cell, path: seq[int], cursor: CellCursor)

proc `$`(op: ModelOperation): string =
result = fmt"{op.kind}, '{op.value}'"
Expand Down
118 changes: 77 additions & 41 deletions src/platform/widget_builder_model_document.nim
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import std/[strformat, tables, sugar, strutils, json]
import util, app, document_editor, model_document, text/text_document, custom_logger, platform, theme, config_provider, input
import widget_builders_base, widget_library, ui/node
import widget_builders_base, widget_library, ui/node, custom_unicode
import vmath, bumpy, chroma
import ast/[types, cells]

Expand Down Expand Up @@ -260,6 +260,17 @@ proc createCompletions(self: ModelDocumentEditor, builder: UINodeBuilder, app: A
completionsPanel.rawY = cursorBounds.y
completionsPanel.pivot = vec2(0, 1)

proc getCursorPos(builder: UINodeBuilder, line: openArray[char], startOffset: RuneIndex, pos: Vec2): int =
var offsetFromLeft = pos.x / builder.charWidth
if false: # self.isThickCursor(): # todo
offsetFromLeft -= 0.0
else:
offsetFromLeft += 0.5

let index = clamp(offsetFromLeft.int, 0, line.runeLen.int)
let byteIndex = line.runeOffset(startOffset + index.RuneCount)
return byteIndex

method createCellUI*(cell: Cell, builder: UINodeBuilder, app: App, ctx: CellLayoutContext, updateContext: UpdateContext, spaceLeft: bool, path: openArray[int]) {.base.} = discard

method createCellUI*(cell: ConstantCell, builder: UINodeBuilder, app: App, ctx: CellLayoutContext, updateContext: UpdateContext, spaceLeft: bool, path: openArray[int]) =
Expand All @@ -281,9 +292,14 @@ method createCellUI*(cell: ConstantCell, builder: UINodeBuilder, app: App, ctx:
# builder.panel(&{SizeToContentX, SizeToContentY, FillY, DrawText}, text = text, textColor = color, userId = cell.id.newPrimaryId):
builder.panel(&{SizeToContentX, SizeToContentY, FillY, DrawText}, text = text, textColor = color):
updateContext.cellToWidget[cell.id] = currentNode
onClick MouseButton.Left:
capture cellPath:
updateContext.handleClick(currentNode, cell, cellPath)
onClickAny btn:
if btn == MouseButton.Left:
if cell.canSelect:
let offset = builder.getCursorPos(text, 0.RuneIndex, pos)
let cursor = cell.toCursor(offset)
capture cellPath:
updateContext.handleClick(currentNode, cell, cellPath, cursor)


# widget.fontSizeIncreasePercent = cell.fontSizeIncreasePercent
# setBackgroundColor(app, cell, widget)
Expand All @@ -306,9 +322,13 @@ method createCellUI*(cell: PlaceholderCell, builder: UINodeBuilder, app: App, ct
# builder.panel(&{SizeToContentX, SizeToContentY, FillY, DrawText}, text = text, textColor = color, userId = cell.id.newPrimaryId):
builder.panel(&{SizeToContentX, SizeToContentY, FillY, DrawText}, text = text, textColor = color):
updateContext.cellToWidget[cell.id] = currentNode
onClick MouseButton.Left:
capture cellPath:
updateContext.handleClick(currentNode, cell, cellPath)
onClickAny btn:
if btn == MouseButton.Left:
if cell.canSelect:
let offset = builder.getCursorPos(text, 0.RuneIndex, pos)
let cursor = cell.toCursor(offset)
capture cellPath:
updateContext.handleClick(currentNode, cell, cellPath, cursor)

# widget.fontSizeIncreasePercent = cell.fontSizeIncreasePercent
# setBackgroundColor(app, cell, widget)
Expand All @@ -331,14 +351,18 @@ method createCellUI*(cell: AliasCell, builder: UINodeBuilder, app: App, ctx: Cel
# builder.panel(&{SizeToContentX, SizeToContentY, FillY, DrawText}, text = text, textColor = color, userId = cell.id.newPrimaryId):
builder.panel(&{SizeToContentX, SizeToContentY, FillY, DrawText}, text = text, textColor = color):
updateContext.cellToWidget[cell.id] = currentNode
onClick MouseButton.Left:
capture cellPath:
updateContext.handleClick(currentNode, cell, cellPath)
onClickAny btn:
if btn == MouseButton.Left:
if cell.canSelect:
let offset = builder.getCursorPos(text, 0.RuneIndex, pos)
let cursor = cell.toCursor(offset)
capture cellPath:
updateContext.handleClick(currentNode, cell, cellPath, cursor)

# widget.fontSizeIncreasePercent = cell.fontSizeIncreasePercent
# setBackgroundColor(app, cell, widget)

method createCellUI*(cell: NodeReferenceCell, builder: UINodeBuilder, app: App, ctx: CellLayoutContext, updateContext: UpdateContext, spaceLeft: bool, path: openArray[int]) =
method createCellUI*(cell: PropertyCell, builder: UINodeBuilder, app: App, ctx: CellLayoutContext, updateContext: UpdateContext, spaceLeft: bool, path: openArray[int]) =
if cell.isVisible.isNotNil and not cell.isVisible(cell.node):
return

Expand All @@ -349,28 +373,25 @@ method createCellUI*(cell: NodeReferenceCell, builder: UINodeBuilder, app: App,
stackSize.dec
cell.logc fmt"createCellUI (ConstantCell) {path}"

if cell.child.isNil:
let reference = cell.node.reference(cell.reference)
let defaultColor = if cell.foregroundColor.a != 0: cell.foregroundColor else: color(1, 1, 1)
let textColor = if cell.themeForegroundColors.len == 0: defaultColor else: app.theme.anyColor(cell.themeForegroundColors, defaultColor)

if spaceLeft:
ctx.addSpace()

# builder.panel(&{SizeToContentX, SizeToContentY, FillY, DrawText}, text = $reference, textColor = textColor, userId = cell.id.newPrimaryId):
builder.panel(&{SizeToContentX, SizeToContentY, FillY, DrawText}, text = $reference, textColor = textColor):
updateContext.cellToWidget[cell.id] = currentNode
onClick MouseButton.Left:
capture cellPath:
updateContext.handleClick(currentNode, cell, cellPath)
if spaceLeft:
ctx.addSpace()

else:
cell.child.createCellUI(builder, app, ctx, updateContext, spaceLeft, path)
let (text, color) = app.getTextAndColor(cell)
# builder.panel(&{SizeToContentX, SizeToContentY, FillY, DrawText}, text = text, textColor = color, userId = cell.id.newPrimaryId):
builder.panel(&{SizeToContentX, SizeToContentY, FillY, DrawText}, text = text, textColor = color):
updateContext.cellToWidget[cell.id] = currentNode
onClickAny btn:
if btn == MouseButton.Left:
if cell.canSelect:
let offset = builder.getCursorPos(text, 0.RuneIndex, pos)
let cursor = cell.toCursor(offset)
capture cellPath:
updateContext.handleClick(currentNode, cell, cellPath, cursor)

# widget.fontSizeIncreasePercent = cell.fontSizeIncreasePercent
# setBackgroundColor(app, cell, widget)

method createCellUI*(cell: PropertyCell, builder: UINodeBuilder, app: App, ctx: CellLayoutContext, updateContext: UpdateContext, spaceLeft: bool, path: openArray[int]) =
method createCellUI*(cell: NodeReferenceCell, builder: UINodeBuilder, app: App, ctx: CellLayoutContext, updateContext: UpdateContext, spaceLeft: bool, path: openArray[int]) =
if cell.isVisible.isNotNil and not cell.isVisible(cell.node):
return

Expand All @@ -381,16 +402,28 @@ method createCellUI*(cell: PropertyCell, builder: UINodeBuilder, app: App, ctx:
stackSize.dec
cell.logc fmt"createCellUI (ConstantCell) {path}"

if spaceLeft:
ctx.addSpace()
if cell.child.isNil:
let reference = cell.node.reference(cell.reference)
let defaultColor = if cell.foregroundColor.a != 0: cell.foregroundColor else: color(1, 1, 1)
let textColor = if cell.themeForegroundColors.len == 0: defaultColor else: app.theme.anyColor(cell.themeForegroundColors, defaultColor)

let (text, color) = app.getTextAndColor(cell)
# builder.panel(&{SizeToContentX, SizeToContentY, FillY, DrawText}, text = text, textColor = color, userId = cell.id.newPrimaryId):
builder.panel(&{SizeToContentX, SizeToContentY, FillY, DrawText}, text = text, textColor = color):
updateContext.cellToWidget[cell.id] = currentNode
onClick MouseButton.Left:
capture cellPath:
updateContext.handleClick(currentNode, cell, cellPath)
if spaceLeft:
ctx.addSpace()

# builder.panel(&{SizeToContentX, SizeToContentY, FillY, DrawText}, text = $reference, textColor = textColor, userId = cell.id.newPrimaryId):
let text = $reference
builder.panel(&{SizeToContentX, SizeToContentY, FillY, DrawText}, text = text, textColor = textColor):
updateContext.cellToWidget[cell.id] = currentNode
onClickAny btn:
if btn == MouseButton.Left:
if cell.canSelect:
let offset = builder.getCursorPos(text, 0.RuneIndex, pos)
let cursor = cell.toCursor(offset)
capture cellPath:
updateContext.handleClick(currentNode, cell, cellPath, cursor)

else:
cell.child.createCellUI(builder, app, ctx, updateContext, spaceLeft, path)

# widget.fontSizeIncreasePercent = cell.fontSizeIncreasePercent
# setBackgroundColor(app, cell, widget)
Expand Down Expand Up @@ -662,11 +695,12 @@ method createUI*(self: ModelDocumentEditor, builder: UINodeBuilder, app: App): s
continue

self.cellWidgetContext.targetNode = nil
self.cellWidgetContext.handleClick = proc(node: UINode, cell: Cell, cellPath: seq[int]) =
self.cellWidgetContext.handleClick = proc(node: UINode, cell: Cell, cellPath: seq[int], cursor: CellCursor) =
let bounds = node.bounds.transformRect(node.parent, uiae.parent)
targetCellPath = cellPath
# debugf"click: {self.scrollOffset} -> {bounds.y} | {cell.dump} | {node.dump}"
self.scrollOffset = bounds.y
self.cursor = cursor
self.markDirty()

# echo self.scrollOffset
Expand All @@ -676,9 +710,11 @@ method createUI*(self: ModelDocumentEditor, builder: UINodeBuilder, app: App): s
cell.createCellUI(builder, app, myCtx, self.cellWidgetContext, false, targetCellPath)
myCtx.finish()

if self.cellWidgetContext.targetNodeOld.isNotNil and self.cellWidgetContext.targetNodeOld != self.cellWidgetContext.targetNode:
self.cellWidgetContext.targetNodeOld.contentDirty = true
self.cellWidgetContext.targetNode.contentDirty = true
if self.cellWidgetContext.targetNodeOld != self.cellWidgetContext.targetNode:
if self.cellWidgetContext.targetNodeOld.isNotNil:
self.cellWidgetContext.targetNodeOld.contentDirty = true
if self.cellWidgetContext.targetNode.isNotNil:
self.cellWidgetContext.targetNode.contentDirty = true

self.cellWidgetContext.targetNodeOld = self.cellWidgetContext.targetNode

Expand Down

0 comments on commit 360a811

Please sign in to comment.