Skip to content

Commit

Permalink
fixed some bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
Nimaoth committed Sep 26, 2023
1 parent 707bcb7 commit 182d8ef
Show file tree
Hide file tree
Showing 11 changed files with 351 additions and 118 deletions.
11 changes: 11 additions & 0 deletions src/array_set.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import std/[sequtils]

func `in`*[T](x: T, xs: openArray[T]): bool = xs.find(x) != -1

func incl*[T](xs: var seq[T], x: T) =
if xs.find(x) == -1: xs.add(x)

func excl*[T](xs: var seq[T], x: T) =
let i = xs.find(x)
if i != -1:
xs.del(i)
6 changes: 6 additions & 0 deletions src/platform/browser_platform.nim
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ type
mLineHeight: float
mLineDistance: float
mCharWidth: float
mCharGap: float

doubleClickTimer: Timer
doubleClickCounter: int
Expand Down Expand Up @@ -77,6 +78,7 @@ method init*(self: BrowserPlatform) =
self.mLineHeight = 20
self.mLineDistance = 2
self.mCharWidth = 18
self.mCharGap = 1
self.supportsThinCursor = true
self.doubleClickTime = 0.35

Expand Down Expand Up @@ -254,6 +256,7 @@ proc updateFontSettings*(self: BrowserPlatform) =
self.content.appendChild(d)
self.mLineHeight = d.clientHeight.float
self.mCharWidth = d.clientWidth.float / 100
self.mCharGap = 1
self.content.removeChild(d)

self.builder.charWidth = self.mCharWidth
Expand All @@ -279,6 +282,9 @@ method lineHeight*(self: BrowserPlatform): float =
method charWidth*(self: BrowserPlatform): float =
self.mCharWidth

method charGap*(self: BrowserPlatform): float =
self.mCharGap

method measureText*(self: BrowserPlatform, text: string): Vec2 =
return vec2(text.len.float * self.mCharWidth, self.totalLineHeight)

Expand Down
4 changes: 4 additions & 0 deletions src/platform/gui_platform.nim
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ type
mLineHeight: float
mLineDistance: float
mCharWidth: float
mCharGap: float

framebufferId: GLuint
framebuffer: Texture
Expand Down Expand Up @@ -231,7 +232,9 @@ method sizeChanged*(self: GuiPlatform): bool =
proc updateCharWidth*(self: GuiPlatform) =
let font = self.getFont(self.ctx.font, self.ctx.fontSize)
let bounds = font.typeset(repeat("#", 100)).layoutBounds()
let boundsSingle = font.typeset("#").layoutBounds()
self.mCharWidth = bounds.x / 100
self.mCharGap = (bounds.x / 100) - boundsSingle.x
self.mLineHeight = bounds.y

self.builder.charWidth = bounds.x / 100.0
Expand All @@ -247,6 +250,7 @@ method fontSize*(self: GuiPlatform): float = self.ctx.fontSize
method lineDistance*(self: GuiPlatform): float = self.mLineDistance
method lineHeight*(self: GuiPlatform): float = self.mLineHeight
method charWidth*(self: GuiPlatform): float = self.mCharWidth
method charGap*(self: GuiPlatform): float = self.mCharGap

method measureText*(self: GuiPlatform, text: string): Vec2 = self.getFont(self.ctx.font, self.ctx.fontSize).typeset(text).layoutBounds()

Expand Down
1 change: 1 addition & 0 deletions src/platform/platform.nim
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ method fontSize*(self: Platform): float {.base.} = discard
method lineDistance*(self: Platform): float {.base.} = discard
method lineHeight*(self: Platform): float {.base.} = discard
method charWidth*(self: Platform): float {.base.} = discard
method charGap*(self: Platform): float {.base.} = discard
method measureText*(self: Platform, text: string): Vec2 {.base.} = discard
method preventDefault*(self: Platform) {.base.} = discard

Expand Down
1 change: 1 addition & 0 deletions src/platform/terminal_platform.nim
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ method fontSize*(self: TerminalPlatform): float = 1
method lineDistance*(self: TerminalPlatform): float = 0
method lineHeight*(self: TerminalPlatform): float = 1
method charWidth*(self: TerminalPlatform): float = 1
method charGap*(self: TerminalPlatform): float = 0
method measureText*(self: TerminalPlatform, text: string): Vec2 = vec2(text.len.float, 1)

proc pushMask(self: TerminalPlatform, mask: Rect) =
Expand Down
8 changes: 4 additions & 4 deletions src/platform/widget_builder_text_document.nim
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ proc createLines(self: TextDocumentEditor, builder: UINodeBuilder, app: App, bac
if builder.renderLine(app, line, self.document.lines[i], self.document.lineIds[i], self.userId, column, cursorLine, i, lineNumbers, y, sizeToContentX, lineNumberWidth, lineNumberBounds.x, backgroundColor, textColor).getSome(cl):
result = cl.some

builder.currentChild.y = builder.currentChild.y - builder.currentChild.h
builder.currentChild.rawY = builder.currentChild.y - builder.currentChild.h

y = builder.currentChild.y
if not sizeToContentY and builder.currentChild.bounds.yh < 0:
Expand All @@ -195,16 +195,16 @@ proc createLines(self: TextDocumentEditor, builder: UINodeBuilder, app: App, bac
block: #cursor
let cursorLocation = result
let (x, y, w, h, text) = if cursorLocation.isSome:
var bounds = cursorLocation.get.bounds.transformRect(cursorLocation.get.node, linesPanel) - vec2(1, 0)
bounds.w += 1
var bounds = cursorLocation.get.bounds.transformRect(cursorLocation.get.node, linesPanel) - vec2(app.platform.charGap, 0)
bounds.w += app.platform.charGap
if not self.cursorVisible:
bounds.w = 0
(bounds.x.some, bounds.y.some, bounds.w, bounds.h, cursorLocation.get.text)
else:
(float32.none, float32.none, 0, 0, "")

builder.panel(&{UINodeFlag.FillBackground, AnimatePosition, MaskContent}, x = x, y = y, w = w, h = h, backgroundColor = color(0.7, 0.7, 1), userId = self.cursorsId.newPrimaryId):
builder.panel(&{DrawText, SizeToContentX, SizeToContentY}, x = 1, y = 0, text = text, textColor = color(0.4, 0.2, 2))
builder.panel(&{DrawText, SizeToContentX, SizeToContentY}, x = app.platform.charGap, y = 0, text = text, textColor = color(0.4, 0.2, 2))

defer:
self.lastContentBounds = currentNode.bounds
Expand Down
25 changes: 13 additions & 12 deletions src/platform/widget_builders.nim
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ import widget_builders_base, widget_builder_ast_document, widget_builder_text_do
import scripting_api except DocumentEditor, TextDocumentEditor, AstDocumentEditor
import vmath, bumpy, chroma

import ../../test_lib
import ui/node

logCategory "widget_builder"

proc updateStatusBar*(self: App, frameIndex: int, statusBarWidget: WPanel, completionsPanel: WPanel) =
var statusWidget: WText
var commandLineWidget: WPanel
Expand Down Expand Up @@ -34,28 +35,28 @@ proc updateStatusBar*(self: App, frameIndex: int, statusBarWidget: WPanel, compl
self.getCommandLineTextEditor.updateWidget(self, commandLineWidget, completionsPanel, frameIndex)
statusBarWidget.lastHierarchyChange = max(statusBarWidget.lastHierarchyChange, commandLineWidget.lastHierarchyChange)

var commandLineWidget: WPanel
var mainStack: WStack
var viewPanel: WPanel
var mainPanel: WPanel
var completionsPanel: WPanel
var widgetsPerEditor = initTable[EditorId, WPanel]()

proc updateWidgetTree*(self: App, frameIndex: int) =
# self.platform.builder.buildUINodes()

var headerColor = if self.commandLineMode: self.theme.color("tab.activeBackground", color(45/255, 45/255, 60/255)) else: self.theme.color("tab.inactiveBackground", color(45/255, 45/255, 45/255))
headerColor.a = 1

var rootFlags = &{FillX, FillY, OverlappingChildren, MaskContent}
let builder = self.platform.builder
builder.panel(rootFlags, backgroundColor = color(0, 0, 0)): # fullscreen overlay

builder.panel(&{FillX, FillY, LayoutVertical}): # main panel
builder.panel(&{FillX, SizeToContentY, LayoutHorizontal}): # status bar
builder.panel(&{FillX, FillY, LayoutVerticalReverse}): # main panel
builder.panel(&{FillX, SizeToContentY, LayoutHorizontalReverse, FillBackground}, backgroundColor = headerColor, pivot = vec2(0, 1)): # status bar
let textColor = self.theme.color("editor.foreground", color(225/255, 200/255, 200/255))
let text = if self.currentMode.len == 0: "normal" else: self.currentMode
builder.panel(&{SizeToContentX, SizeToContentY, DrawText}, text = text, textColor = textColor): #

builder.panel(&{SizeToContentX, SizeToContentY, DrawText}, text = text, textColor = textColor, pivot = vec2(1, 0)):
discard

builder.panel(&{FillX, FillY}): # main panel
builder.panel(&{FillX, SizeToContentY}, pivot = vec2(1, 0)):
self.getCommandLineTextEditor.createUI(builder, self)

builder.panel(&{FillX, FillY}, pivot = vec2(0, 1)): # main panel
let overlay = currentNode

let rects = self.layout.layoutViews(self.layout_props, rect(0, 0, 1, 1), self.views.len)
Expand Down
3 changes: 3 additions & 0 deletions src/rect_utils.nim
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,9 @@ proc mix*[T: SomeFloat](a: var Rect, b: Rect, v: T) =
a.w = a.w * (1.0 - v) + b.w * v
a.h = a.h * (1.0 - v) + b.h * v

proc almostEqual*(a, b: float32, epsilon: float32): bool =
result = abs(a - b) <= epsilon

proc almostEqual*(a, b: Rect, epsilon: float32): bool =
result = abs(a.x - b.x) <= epsilon and abs(a.y - b.y) <= epsilon and abs(a.w - b.w) <= epsilon and abs(a.h - b.h) <= epsilon

Expand Down
7 changes: 6 additions & 1 deletion src/text/text_document.nim
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,12 @@ proc fullPath*(self: TextDocument): string =
if self.appFile:
return fs.getApplicationFilePath(self.filename)

return self.filename.absolutePath
when not defined(js):
return self.filename.absolutePath

else:
log lvlError, fmt"[js] Unable to get full path for text document {self.filename}"
return self.filename

proc getLine*(self: TextDocument, line: int): string =
if line < self.lines.len:
Expand Down
Loading

0 comments on commit 182d8ef

Please sign in to comment.