Skip to content

Commit 4d5e14c

Browse files
committed
rename: some layout properties
1 parent eb3b0d4 commit 4d5e14c

File tree

2 files changed

+98
-58
lines changed

2 files changed

+98
-58
lines changed

src/sigui/layouts.nim

Lines changed: 63 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,17 @@ type
1313

1414
Layout* = ref object of Uiobj
1515
orientation*: Property[LayoutOrientation]
16-
spacing*, wrapSpacing*: Property[float32]
17-
fillWithSpaces*, wrapFillWithSpaces*: Property[bool]
18-
hugContent*: Property[bool]
16+
17+
hugContent*: Property[bool] = true.property
1918
wrapHugContent*: Property[bool] = true.property
20-
alignment*, wrapAlignment*: Property[LayoutAlignment]
19+
20+
align*: Property[LayoutAlignment]
21+
fillContainer*: Property[bool]
22+
23+
alignContent*, wrapAlignContent*: Property[LayoutAlignment]
24+
25+
gap*, wrapGap*: Property[float32]
26+
fillWithSpaces*, wrapFillWithSpaces*: Property[bool]
2127
consistentSpacing*: Property[bool]
2228

2329
wrap*: Property[bool]
@@ -28,7 +34,7 @@ type
2834
inRepositionProcess: bool
2935

3036
InLayout* = ref object of Uiobj
31-
alignment*: Property[LayoutAlignment]
37+
align*: Property[LayoutAlignment]
3238
fillContainer*: Property[bool]
3339

3440
isChangingW, isChangingH: bool
@@ -69,8 +75,8 @@ proc reposition(this: Layout) =
6975
for child in this.childs:
7076
if child.visibility == collapsed: continue
7177
if x != 0:
72-
x += this.spacing[]
73-
rows[^1].freeSpace -= this.spacing[]
78+
x += this.gap[]
79+
rows[^1].freeSpace -= this.gap[]
7480
x += child.get_w
7581
inc i
7682

@@ -81,7 +87,7 @@ proc reposition(this: Layout) =
8187
(this.lengthBeforeWrap[] > 0 and x > this.lengthBeforeWrap[])
8288
)
8389
):
84-
rows[^1].freeSpace += this.spacing[]
90+
rows[^1].freeSpace += this.gap[]
8591
rows[^1].h = h
8692
i = 1
8793
x = child.get_w
@@ -104,13 +110,13 @@ proc reposition(this: Layout) =
104110

105111
block:
106112
let freeYSpace =
107-
if this.wrapFillWithSpaces[]: rows.mapit(it.h).foldl(a + this.wrapSpacing[] + b)
113+
if this.wrapFillWithSpaces[]: rows.mapit(it.h).foldl(a + this.wrapGap[] + b)
108114
else: 0'f32
109115

110116
var y =
111117
if this.wrapfillWithSpaces[]: 0'f32
112118
else:
113-
case this.wrapAlignment[]
119+
case this.wrapAlignContent[]
114120
of start: 0'f32
115121
of center: freeYSpace / 2
116122
of `end`: freeYSpace
@@ -120,7 +126,7 @@ proc reposition(this: Layout) =
120126
x =
121127
if this.fillWithSpaces[]: 0'f32
122128
else:
123-
case this.alignment[]
129+
case this.alignContent[]
124130
of start: 0'f32
125131
of center: freeSpace / 2
126132
of `end`: freeSpace
@@ -132,39 +138,57 @@ proc reposition(this: Layout) =
132138
for child in row:
133139
child.set_x(x)
134140

135-
if child of InLayout:
136-
if child.InLayout.fillContainer[]:
141+
let fillContainer =
142+
if child of InLayout: child.InLayout.fillContainer[]
143+
else: this.fillWithSpaces[]
144+
145+
let align =
146+
if child of InLayout: child.InLayout.align[]
147+
else: this.align[]
148+
149+
if fillContainer:
150+
if child of InLayout:
137151
for child in child.childs:
138152
child.set_h(this.get_h)
139-
child.set_y(0)
140153
else:
141-
case child.InLayout.alignment[]
142-
of start:
143-
child.set_y(y)
144-
of center:
145-
child.set_y(y + h / 2 - child.get_h / 2)
146-
of `end`:
147-
child.set_y(y + h - child.get_h)
148-
154+
child.set_h(this.get_h)
155+
156+
child.set_y(0)
157+
149158
else:
150-
child.set_y(y)
159+
case align
160+
of start:
161+
child.set_y(y)
162+
of center:
163+
child.set_y(y + h / 2 - child.get_h / 2)
164+
of `end`:
165+
child.set_y(y + h - child.get_h)
151166

152-
x += child.get_w + this.spacing[] + spaceBetween
167+
x += child.get_w + this.gap[] + spaceBetween
153168

154-
y += h + this.wrapSpacing[] + (if rows.len > 1: freeYSpace / (rows.len.float32 - 1) else: 0)
169+
y += h + this.wrapGap[] + (if rows.len > 1: freeYSpace / (rows.len.float32 - 1) else: 0)
155170

156171
if this.hugContent[]:
157172
if this.childs.len > 0:
158173
this.set_w(rows.mapit(it.childs[^1].get_x + it.childs[^1].get_w).foldl(max(a, b), 0'f32))
159174
else:
160175
this.set_w(0)
176+
161177
if this.wrapHugContent[]:
162178
if this.childs.len > 0:
163179
this.set_h(rows[^1].childs.mapit(it.get_y + it.get_h).foldl(max(a, b), 0'f32))
164180
else:
165181
this.set_w(0)
166182

167183

184+
template spacing*(this: Layout): Property[float32] = this.gap
185+
template wrapSpacing*(this: Layout): Property[float32] = this.wrapGap
186+
template alignment*(this: Layout): Property[float32] = this.alignContent
187+
template wrapAlignment*(this: Layout): Property[float32] = this.wrapAlignContent
188+
189+
template alignment*(this: InLayout): Property[float32] = this.align
190+
191+
168192
method addChild*(this: Layout, child: Uiobj) =
169193
if this.newChildsObject != nil:
170194
this.newChildsObject.addChild(child)
@@ -177,7 +201,7 @@ method addChild*(this: Layout, child: Uiobj) =
177201
# todo: disconnect if child is no loger child
178202

179203
if child of InLayout:
180-
child.InLayout.alignment.changed.connectTo this: reposition(this)
204+
child.InLayout.align.changed.connectTo this: reposition(this)
181205
child.InLayout.fillContainer.changed.connectTo this: reposition(this)
182206

183207
reposition(this)
@@ -214,15 +238,22 @@ method init*(this: Layout) =
214238

215239
doRepositionWhenChanged w
216240
doRepositionWhenChanged h
217-
doRepositionWhenChanged spacing
218-
doRepositionWhenChanged wrapSpacing
219-
doRepositionWhenChanged fillWithSpaces
220-
doRepositionWhenChanged wrapFillWithSpaces
241+
221242
doRepositionWhenChanged hugContent
222243
doRepositionWhenChanged wrapHugContent
223-
doRepositionWhenChanged alignment
224-
doRepositionWhenChanged wrapAlignment
244+
245+
doRepositionWhenChanged align
246+
doRepositionWhenChanged fillContainer
247+
248+
doRepositionWhenChanged alignContent
249+
doRepositionWhenChanged wrapAlignContent
250+
251+
doRepositionWhenChanged gap
252+
doRepositionWhenChanged wrapGap
253+
doRepositionWhenChanged fillWithSpaces
254+
doRepositionWhenChanged wrapFillWithSpaces
225255
doRepositionWhenChanged consistentSpacing
256+
226257
doRepositionWhenChanged wrap
227258
doRepositionWhenChanged elementsBeforeWrap
228259
doRepositionWhenChanged lengthBeforeWrap

tests/t_todoapp.nim

Lines changed: 35 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -44,16 +44,20 @@ test "todo app":
4444
right = addTask.left - 10
4545
text = "sample task"
4646
this.textObj[].font[] = typeface.withSize(32)
47+
48+
this.onKeyDown enter:
49+
mouse.mouseDownAndUpInside.emit()
4750

4851
- UiRect() as addTask:
4952
right = parent.right - 5
5053
this.fillVertical(parent, 4)
51-
this.binding w: this.h[]
52-
this.binding color:
54+
w := this.h[]
55+
56+
radius = 5
57+
color = binding:
5358
if mouse.pressed[]: color(0.43, 0.15, 0.76).lighten(0.1)
5459
elif mouse.hovered[]: color(0.43, 0.15, 0.76).lighten(0.2)
5560
else: color(0.43, 0.15, 0.76)
56-
radius = 5
5761

5862
- this.color.transition(0.4's):
5963
easing = outCubicEasing
@@ -66,9 +70,13 @@ test "todo app":
6670

6771
- MouseArea() as mouse:
6872
this.fill parent
73+
6974
this.mouseDownAndUpInside.connectTo this:
75+
if taskName.text[] == "": return
7076
app.tasks.add((name: taskName.text[], complete: false.property))
7177
app.tasksChanged.emit()
78+
taskName.pushState()
79+
taskName.text[] = ""
7280

7381
- ClipRect():
7482
this.fillHorizontal(parent, 20)
@@ -79,7 +87,7 @@ test "todo app":
7987
this.fill parent
8088

8189
- Uiobj():
82-
this.binding w: parent.w[]
90+
w := parent.w[]
8391

8492
- this.y.transition(0.2's):
8593
easing = outSquareEasing
@@ -94,37 +102,38 @@ test "todo app":
94102
this.binding w: parent.w[]
95103

96104
orientation = vertical
97-
spacing = 0
98-
hugContent = true
105+
gap = 10
99106

100107
for i in 0..app.tasks.high:
101108
template task: auto = app.tasks[i]
102109

103-
- UiText():
104-
this.text[] = task.name
105-
106-
this.binding font:
107-
let it = typeface.withSize(24)
108-
it.strikethrough = task.complete[]
109-
it
110-
111-
this.binding color:
112-
if mouse.pressed[]: color(0.2, 0.2, 0.2)
113-
elif mouse.hovered[]: color(0.4, 0.4, 0.4)
114-
else: color(0, 0, 0)
115-
116-
- MouseArea() as mouse:
117-
this.fill parent
118-
this.mouseDownAndUpInside.connectTo this:
119-
task.complete[] = not task.complete[]
110+
- Layout():
111+
gap = 10
112+
align = center
120113

121114
- Switch(isOn: task.complete[].property):
122-
left = parent.right + 10
123-
centerY = parent.center
124115
color = color(0.43, 0.15, 0.76)
125116

126-
this.binding isOn: task.complete[]
117+
isOn := task.complete[]
127118
this.bindingValue task.complete[]: this.isOn[]
119+
120+
- UiText():
121+
text = task.name
122+
123+
this.binding font:
124+
let it = typeface.withSize(24)
125+
it.strikethrough = task.complete[]
126+
it
127+
128+
this.binding color:
129+
if mouse.pressed[]: color(0.2, 0.2, 0.2)
130+
elif mouse.hovered[]: color(0.4, 0.4, 0.4)
131+
else: color(0, 0, 0)
132+
133+
- MouseArea() as mouse:
134+
this.fill parent
135+
this.mouseDownAndUpInside.connectTo this:
136+
task.complete[] = not task.complete[]
128137

129138
app.tasksChanged.connectTo app:
130139
app.layout[] = Layout()

0 commit comments

Comments
 (0)