git.haldean.org e / 667d384
focus support haldean 1 year, 5 months ago
3 changed file(s) with 42 addition(s) and 10 deletion(s). Raw diff Collapse all Expand all
00 e
11 e.exe
2 err.txt
66
77 type
88 LayoutTag* {.pure.} = enum
9 center, left, right
9 center, left, right,
10 floatCenter
1011
1112 Layer* = ref object
1213 blit*: Blit
1819 size: Size
1920 tb: illwill.TerminalBuffer
2021 layers: seq[Layer]
22 focus: int
2123
2224 proc size*(l: Layer): Size = l.dst.size()
2325 proc size*(c: Comp): Size = c.size
4749 tb: newTerminalBuffer(size.w, size.h),
4850 layers: newSeq[Layer]())
4951
50 proc layout(c: var Comp) =
52 proc lrc(c: Comp): (Layer, Layer, Layer) =
5153 var
5254 lcenter: Layer = nil
5355 lleft: Layer = nil
6365 of left:
6466 if lleft == nil:
6567 lleft = layer
68 else:
69 discard
70 return (lleft, lcenter, lright)
71
72 proc layout(c: var Comp) =
73 let (lleft, lcenter, lright) = lrc(c)
6674
6775 var present = 0
6876 let full = rectFromSize(c.size)
102110 proc paint*(c: var Comp) =
103111 c.layout()
104112 c.tb.clear()
105 for layer in c.layers:
113 for index, layer in c.layers:
106114 for point in pointsInside(@[layer.dst]):
107 c.tb.write(point.x, point.y, layer.get(point)[0].toUTF8)
115 let ch = layer.get(point)
116 c.tb[point.x, point.y] = TerminalChar(
117 ch: ch[0],
118 fg: fgNone,
119 bg: if index == c.focus: bgBlack else: bgWhite,
120 style: {})
108121 c.tb.display()
109122
110123 proc pushlayer*(c: var Comp, l: Layer) =
111 c.layers.add(l)
124 c.layers.insert(l, 0)
125 c.focus = c.layers.len - 1
112126
113127 proc poplayer*(c: var Comp) =
114 c.layers.delete(c.layers.len - 1)
128 let i = c.layers.len - 1
129 c.layers.delete(i)
130 c.focus = 0
131
132 proc focused*(c: Comp): Layer =
133 return c.layers[c.focus]
134
135 proc refocus*(c: var Comp, l: Layer) =
136 for i, layer in c.layers:
137 if l == layer:
138 c.focus = i
139 return
115140
116141 proc `size=`*(c: var Comp, s: Size) =
117142 c.tb = newTerminalBuffer(s.w, s.h)
4343 case k
4444 of Key.Escape, Key.Q:
4545 exitProc()
46 of Key.One:
47 c.refocus(lx)
48 of Key.Two:
49 c.refocus(ly)
50 of Key.Three:
51 c.refocus(lz)
4652 of Key.J:
47 lx.src.y += 1
53 c.focused().src.y += 1
4854 of Key.K:
49 lx.src.y -= 1
55 c.focused().src.y -= 1
5056 of Key.H:
51 lx.src.x += 1
57 c.focused().src.x += 1
5258 of Key.L:
53 lx.src.x -= 1
59 c.focused().src.x -= 1
5460 of Key.None:
5561 discard
5662 else: