simpler focus model
haldean
2 years ago
49 | 49 | tb: newTerminalBuffer(size.w, size.h), |
50 | 50 | layers: newSeq[Layer]()) |
51 | 51 | |
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] | |
57 | 54 | 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 | |
71 | 58 | |
72 | 59 | proc layout(c: var Comp) = |
73 | let (lleft, lcenter, lright) = lrc(c) | |
60 | let ls = toplayers(c) | |
74 | 61 | |
75 | 62 | var present = 0 |
76 | 63 | let full = rectFromSize(c.size) |
77 | if lleft != nil: | |
64 | if ls[left] != nil: | |
78 | 65 | present = bitor(present, 0b100) |
79 | lleft.dst = full | |
80 | if lcenter != nil: | |
66 | ls[left].dst = full | |
67 | if ls[center] != nil: | |
81 | 68 | present = bitor(present, 0b010) |
82 | lcenter.dst = full | |
83 | if lright != nil: | |
69 | ls[center].dst = full | |
70 | if ls[right] != nil: | |
84 | 71 | present = bitor(present, 0b001) |
85 | lright.dst = full | |
72 | ls[right].dst = full | |
86 | 73 | |
87 | 74 | case present |
88 | 75 | of 0b010, 0b100, 0b001: discard |
89 | 76 | of 0b110: |
90 | 77 | 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 | |
93 | 80 | of 0b011: |
94 | 81 | 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 | |
97 | 84 | of 0b101: |
98 | 85 | 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 | |
101 | 88 | of 0b111: |
102 | 89 | let lsplit = int(ceil(c.size.w.float * 0.25)) |
103 | 90 | 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 | |
108 | 95 | else: discard |
109 | 96 | |
110 | 97 | proc paint*(c: var Comp) = |
132 | 119 | proc focused*(c: Comp): Layer = |
133 | 120 | return c.layers[c.focus] |
134 | 121 | |
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 | |
140 | 130 | |
141 | 131 | proc `size=`*(c: var Comp, s: Size) = |
142 | 132 | c.tb = newTerminalBuffer(s.w, s.h) |