git.haldean.org e / ede99e3
simpler focus model haldean 1 year, 5 months ago
2 changed file(s) with 32 addition(s) and 46 deletion(s). Raw diff Collapse all Expand all
4949 tb: newTerminalBuffer(size.w, size.h),
5050 layers: newSeq[Layer]())
5151
52 proc lrc(c: Comp): (Layer, Layer, Layer) =
53 var
54 lcenter: Layer = nil
55 lleft: Layer = nil
56 lright: Layer = nil
52 proc toplayers(c: Comp): array[LayoutTag, Layer] =
53 var res: array[LayoutTag, Layer]
5754 for layer in c.layers:
58 case layer.tag
59 of center:
60 if lcenter == nil:
61 lcenter = layer
62 of right:
63 if lright == nil:
64 lright = layer
65 of left:
66 if lleft == nil:
67 lleft = layer
68 else:
69 discard
70 return (lleft, lcenter, lright)
55 if res[layer.tag] == nil:
56 res[layer.tag] = layer
57 return res
7158
7259 proc layout(c: var Comp) =
73 let (lleft, lcenter, lright) = lrc(c)
60 let ls = toplayers(c)
7461
7562 var present = 0
7663 let full = rectFromSize(c.size)
77 if lleft != nil:
64 if ls[left] != nil:
7865 present = bitor(present, 0b100)
79 lleft.dst = full
80 if lcenter != nil:
66 ls[left].dst = full
67 if ls[center] != nil:
8168 present = bitor(present, 0b010)
82 lcenter.dst = full
83 if lright != nil:
69 ls[center].dst = full
70 if ls[right] != nil:
8471 present = bitor(present, 0b001)
85 lright.dst = full
72 ls[right].dst = full
8673
8774 case present
8875 of 0b010, 0b100, 0b001: discard
8976 of 0b110:
9077 let split = int(ceil(c.size.w.float * 0.33))
91 lleft.dst.hi.x = split - 1
92 lcenter.dst.lo.x = split
78 ls[left].dst.hi.x = split - 1
79 ls[center].dst.lo.x = split
9380 of 0b011:
9481 let split = int(ceil(c.size.w.float * 0.67))
95 lcenter.dst.hi.x = split - 1
96 lright.dst.lo.x = split
82 ls[center].dst.hi.x = split - 1
83 ls[right].dst.lo.x = split
9784 of 0b101:
9885 let split = int(c.size.w.float * 0.5)
99 lleft.dst.hi.x = split - 1
100 lright.dst.lo.x = split
86 ls[left].dst.hi.x = split - 1
87 ls[right].dst.lo.x = split
10188 of 0b111:
10289 let lsplit = int(ceil(c.size.w.float * 0.25))
10390 let rsplit = int(ceil(c.size.w.float * 0.75))
104 lleft.dst.hi.x = lsplit - 1
105 lcenter.dst.lo.x = lsplit
106 lcenter.dst.hi.x = rsplit - 1
107 lright.dst.lo.x = rsplit
91 ls[left].dst.hi.x = lsplit - 1
92 ls[center].dst.lo.x = lsplit
93 ls[center].dst.hi.x = rsplit - 1
94 ls[right].dst.lo.x = rsplit
10895 else: discard
10996
11097 proc paint*(c: var Comp) =
132119 proc focused*(c: Comp): Layer =
133120 return c.layers[c.focus]
134121
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
122 proc focusnext*(c: var Comp, fwd: bool = true) =
123 if fwd:
124 c.focus = (c.focus + 1) mod c.layers.len
125 else:
126 if c.focus == 0:
127 c.focus = c.layers.len - 1
128 else:
129 c.focus -= 1
140130
141131 proc `size=`*(c: var Comp, s: Size) =
142132 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)
46 of Key.Tab:
47 c.focusnext()
5248 of Key.J:
5349 c.focused().src.y += 1
5450 of Key.K: