Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Format Code #67

Merged
merged 6 commits into from
Jan 12, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 6 additions & 7 deletions .github/workflows/build-pr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,18 @@ jobs:
steps:
- uses: actions/checkout@v1

- uses: iffy/install-nim@v4
with:
version: ${{ matrix.nimversion }}
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

- name: Cache packages
uses: actions/cache@v3
with:
path: ~/.nimble
key: ${{ runner.os }}-${{ hashFiles('figuro.nimble') }}

- uses: iffy/install-nim@v4
with:
version: ${{ matrix.nimversion }}
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

- name: Install Nimble
run: |
nimble install nimble
Expand All @@ -40,5 +40,4 @@ jobs:

- name: Build Tests
run: |
cd figuro/
nim test
9 changes: 1 addition & 8 deletions figuro/common/extras.nim
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import std/[strformat, macros]
# import typography/font
import std/macros
import patty

export patty
Expand All @@ -22,12 +21,6 @@ iterator reverseIndex*[T](a: openArray[T]): (int, T) {.inline.} =
yield (i, a[i])
dec i

# proc repr*(font: Font): string =
# if font.isNil:
# result = "Font(nil)"
# else:
# result = fmt"Font({font.typeface.name=}, {font.size=}, {font.weight=})"

macro variants*(name, code: untyped) =
## convenience wrapper for Patty variant macros
result = quote do:
Expand Down
29 changes: 14 additions & 15 deletions figuro/common/glyphs.nim
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import uimaths
export uimaths

type

TypefaceId* = Hash
FontId* = Hash
GlyphId* = Hash
Expand All @@ -28,34 +27,34 @@ type

GlyphFont* = object
fontId*: FontId
size*: float32 ## Font size in pixels.
lineHeight*: float32 = -1.0 ## The line height in pixels or autoLineHeight for the font's default line height.
size*: float32 ## Font size in pixels.
lineHeight*: float32 = -1.0
## The line height in pixels or autoLineHeight for the font's default line height.

UiFont* = object
typefaceId*: TypefaceId
size*: UICoord ## Font size in pixels.
lineHeight*: UICoord = -1.0'ui ## The line height in pixels or autoLineHeight for the font's default line height.
size*: UICoord ## Font size in pixels.
lineHeight*: UICoord = -1.0'ui
## The line height in pixels or autoLineHeight for the font's default line height.
fontCase*: FontCase
underline*: bool ## Apply an underline.
strikethrough*: bool ## Apply a strikethrough.
underline*: bool ## Apply an underline.
strikethrough*: bool ## Apply a strikethrough.
noKerningAdjustments*: bool ## Optionally disable kerning pair adjustments

GlyphArrangement* = object
contentHash*: Hash
lines*: seq[Slice[int]] ## The (start, stop) of the lines of text.
spans*: seq[Slice[int]] ## The (start, stop) of the spans in the text.
fonts*: seq[GlyphFont] ## The font for each span.
runes*: seq[Rune] ## The runes of the text.
positions*: seq[Vec2] ## The positions of the glyphs for each rune.
lines*: seq[Slice[int]] ## The (start, stop) of the lines of text.
spans*: seq[Slice[int]] ## The (start, stop) of the spans in the text.
fonts*: seq[GlyphFont] ## The font for each span.
runes*: seq[Rune] ## The runes of the text.
positions*: seq[Vec2] ## The positions of the glyphs for each rune.
selectionRects*: seq[Rect] ## The selection rects for each glyph.

TextSpan* = object
text*: string
font*: UiFont

proc newFont*(
typeface: TypefaceId
): UiFont {.raises: [].} =
proc newFont*(typeface: TypefaceId): UiFont {.raises: [].} =
result = UiFont()
result.typefaceId = typeface
result.size = 12'ui
Expand Down
6 changes: 1 addition & 5 deletions figuro/common/nodes/basics.nim
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,7 @@ when defined(compilervm):
else:
{.pragma: runtimeVar, global.}

type
NodeID* = int64

type NodeID* = int64

type
NodeKind* = enum
Expand Down Expand Up @@ -100,5 +98,3 @@ type
ImageStyle* = object
name*: string
color*: Color


16 changes: 7 additions & 9 deletions figuro/common/nodes/csstheme.nim
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

import ../../ui/basiccss
import ui
import pkg/pretty
Expand Down Expand Up @@ -30,7 +29,7 @@ proc checkMatch*(sel: CssSelector, node: Figuro): bool =
# echo "failed id check"
return

if has(sel.cssType):
if has(sel.cssType):
if node.widgetName == sel.cssType:
# echo "matched type! node: ", $node
discard
Expand All @@ -45,7 +44,7 @@ proc checkMatch*(sel: CssSelector, node: Figuro): bool =
else:
# echo "failed class check"
return

return true

proc apply*(prop: CssProperty, node: Figuro) =
Expand All @@ -62,12 +61,13 @@ proc apply*(prop: CssProperty, node: Figuro) =
discard
_:
discard

match prop.value:
MissingCssValue:
raise newException(ValueError, "missing css value!")
CssColor(c):
# echo "\tapply color: ", c.repr
case prop.name:
case prop.name
of "background":
node.fill = c
of "border-color":
Expand All @@ -77,7 +77,7 @@ proc apply*(prop: CssProperty, node: Figuro) =
discard
CssSize(cx):
# echo "\tapply size: ", cx.repr
case prop.name:
case prop.name
of "border-width":
setCxFixed(cx, node.stroke.weight)
of "border-radius":
Expand All @@ -88,7 +88,6 @@ proc apply*(prop: CssProperty, node: Figuro) =
CssVarName(n):
echo "Warning: ", "unhandled css variable: ", prop.repr


proc eval*(rule: CssBlock, node: Figuro) =
# print rule.selectors

Expand All @@ -102,7 +101,7 @@ proc eval*(rule: CssBlock, node: Figuro) =
sel = rule.selectors[^i]
# print "SEL: ", sel
# print "comb: ", combinator
case combinator:
case combinator
of skNone, skPseudo:
matched = matched and sel.checkMatch(node)
if not matched:
Expand All @@ -113,7 +112,6 @@ proc eval*(rule: CssBlock, node: Figuro) =
matched = false
else:
matched = matched and sel.checkMatch(node.parent[])

of skDescendent:
var parentMatched = false
for p in node.parents():
Expand All @@ -128,7 +126,7 @@ proc eval*(rule: CssBlock, node: Figuro) =

# echo "selMatch: ", matched, " idx: ", i, "\n"
combinator = sel.combinator

if matched:
# echo "matched node: ", node.uid
# print rule.selectors
Expand Down
10 changes: 4 additions & 6 deletions figuro/common/nodes/render.nim
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import basics
export basics

type

NodeIdx* = distinct int

Node* = object
Expand Down Expand Up @@ -44,15 +43,14 @@ type

import pretty

proc `$`*(id: NodeIdx): string = "NodeIdx(" & $(int(id)) & ")"
proc `$`*(id: NodeIdx): string =
"NodeIdx(" & $(int(id)) & ")"

proc `+`*(a, b: NodeIdx): NodeIdx {.borrow.}
proc `<=`*(a, b: NodeIdx): bool {.borrow.}
proc `==`*(a, b: NodeIdx): bool {.borrow.}

iterator childIndex*(
nodes: seq[Node],
current: NodeIdx
): NodeIdx =
iterator childIndex*(nodes: seq[Node], current: NodeIdx): NodeIdx =
let id = nodes[current.int].uid
let childCnt = nodes[current.int].childCount
# print "\nchildIndex: ", current,
Expand Down
44 changes: 21 additions & 23 deletions figuro/common/nodes/transfer.nim
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

import ui as ui
import render as render
import ../../shared
Expand All @@ -7,33 +6,33 @@ type
RenderList* = object
nodes*: seq[Node]
rootIds*: seq[NodeIdx]

RenderNodes* = OrderedTable[ZLevel, RenderList]

type
RenderTree* = ref object
id*: int
name*: string
children*: seq[RenderTree]
type RenderTree* = ref object
id*: int
name*: string
children*: seq[RenderTree]

func `[]`*(a: RenderTree, idx: int): RenderTree =
if a.children.len() == 0:
return RenderTree(name: "Missing")
a.children[idx]

func `==`*(a, b: RenderTree): bool =
if a.isNil and b.isNil: return true
if a.isNil or b.isNil: return false
if a.isNil and b.isNil:
return true
if a.isNil or b.isNil:
return false
`==`(a[], b[])

proc toTree*(nodes: seq[Node],
idx = 0.NodeIdx,
depth = 1): RenderTree =
proc toTree*(nodes: seq[Node], idx = 0.NodeIdx, depth = 1): RenderTree =
let n = nodes[idx.int]
result = RenderTree(id: n.uid, name: $n.name)
# echo " ".repeat(depth), "toTree:idx: ", idx.int
for ci in nodes.childIndex(idx):
# echo " ".repeat(depth), "toTree:cidx: ", ci.int
result.children.add toTree(nodes, ci, depth+1)
result.children.add toTree(nodes, ci, depth + 1)

proc toTree*(list: RenderList): RenderTree =
result = RenderTree(name: "pseudoRoot")
Expand All @@ -55,7 +54,7 @@ proc findRoot*(list: RenderList, node: Node): Node =
if n.uid == result.parent:
result = n
break

if curr.uid == result.uid:
return

Expand Down Expand Up @@ -83,8 +82,7 @@ proc add*(list: var RenderList, node: Node) =
# "` node: ", node.uid, " `", node.name, "` "
# echo " nodeRoot: ", findRoot(list, node).uid
let nr = findRoot(list, node)
if nr.uid != lastRoot.uid and
node.uid != list.nodes[^1].uid:
if nr.uid != lastRoot.uid and node.uid != list.nodes[^1].uid:
# echo "rootIds:add: ", node.uid, " // ", node.parent, " ", node.name
list.rootIds.add(list.nodes.len().NodeIdx)
list.nodes.add(node)
Expand All @@ -110,7 +108,7 @@ proc convert*(current: Figuro): render.Node =
result.transparency = current.transparency
result.stroke = current.stroke

case current.kind:
case current.kind
of nkRectangle:
if current.shadow.isSome:
let orig = current.shadow.get()
Expand All @@ -134,11 +132,9 @@ proc convert*(current: Figuro): render.Node =
else:
discard

proc convert*(renders: var RenderNodes,
current: Figuro,
parent: NodeID,
maxzlvl: ZLevel
) =
proc convert*(
renders: var RenderNodes, current: Figuro, parent: NodeID, maxzlvl: ZLevel
) =
# echo "convert:node: ", current.uid, " parent: ", parent
var render = current.convert()
render.parent = parent
Expand All @@ -155,11 +151,13 @@ proc convert*(renders: var RenderNodes,
let chlvl = child.zlevel
renders.convert(child, current.uid, chlvl)


proc copyInto*(uis: Figuro): RenderNodes =
result = initOrderedTable[ZLevel, RenderList]()
result.convert(uis, -1.NodeID, 0.ZLevel)

result.sort(proc(x, y: auto): int = cmp(x[0],y[0]))
result.sort(
proc(x, y: auto): int =
cmp(x[0], y[0])
)
# echo "nodes:len: ", result.len()
# printRenders(result)
Loading
Loading