Skip to content

Commit 784d1c5

Browse files
committed
Fix text centring layout issue
1 parent 0fb3db5 commit 784d1c5

4 files changed

Lines changed: 23 additions & 34 deletions

File tree

build.zig.zon

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
.{
22
.name = .engine,
3-
.version = "0.10.6",
3+
.version = "0.11.0",
44
.fingerprint = 0xe8a81a8d0aa558d5,
55
.minimum_zig_version = "0.15.2",
66
.dependencies = .{

src/checkbox.zig

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ pub fn Checkbox(comptime T: type) type {
5959
.shrinks, .grows => {
6060
// Growing or shrinking, our task here is to find
6161
// the minimum that would be needed.
62-
element.layout_label(display.scale, parent_inner_width);
62+
_ = element.layout_label(display.scale, parent_inner_width);
6363
//err("{s} {s} use width {d}", .{ self.name, @tagName(self.type), choose });
6464
return element.rect.width + element.pad.left + element.type.checkbox.checkbox_size.width;
6565
},
@@ -81,7 +81,7 @@ pub fn Checkbox(comptime T: type) type {
8181
// needs to be done here as the width of the label may have changed.
8282
switch (element.layout.y) {
8383
.shrinks, .grows => {
84-
element.layout_label(display.scale, parent_inner_width);
84+
_ = element.layout_label(display.scale, parent_inner_width);
8585
//err("{s} {s} use grows height {d} (parent_width={d})", .{ self.name, @tagName(self.type), mm.max_height, parent_width });
8686
return element.rect.height;
8787
},

src/element.zig

Lines changed: 18 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -369,14 +369,14 @@ pub fn Element(comptime T: type) type {
369369
});
370370
}
371371
if (self.style != .normal) {
372-
_ = try out.write(" align=");
372+
_ = try out.write(" style=");
373373
_ = try out.write(@tagName(self.style));
374374
}
375375
if (self.child_align.x != .start or self.child_align.y != .start) {
376376
_ = try out.write(" align=");
377-
_ = try out.write(@tagName(self.layout.x));
377+
_ = try out.write(@tagName(self.child_align.x));
378378
_ = try out.write("-");
379-
_ = try out.write(@tagName(self.layout.y));
379+
_ = try out.write(@tagName(self.child_align.y));
380380
}
381381
_ = try out.write(" layout=");
382382
_ = try out.write(@tagName(self.layout.x));
@@ -1216,17 +1216,18 @@ pub fn Element(comptime T: type) type {
12161216
/// by a certain offset amount.
12171217
///
12181218
/// `parent_inner_width` should be the actual width the parent is
1219-
/// passing down to this child element, minus left and right padding, and
1220-
/// clamped to the min/max width (including padding)
1219+
/// willing/able to down to this child element, minus the parent
1220+
/// left and right padding, and clamped to the min/max
1221+
/// width (including padding)
12211222
pub inline fn layout_label(
12221223
element: *Self,
12231224
display_scale: f32,
12241225
parent_inner_width: f32,
1225-
) void {
1226+
) f32 {
12261227
std.debug.assert(element.type == .label or element.type == .checkbox);
12271228

1228-
if (element.type == .label and element.type.label.text.len == 0) return;
1229-
if (element.type == .checkbox and element.type.checkbox.text.len == 0) return;
1229+
if (element.type == .label and element.type.label.text.len == 0) return 0;
1230+
if (element.type == .checkbox and element.type.checkbox.text.len == 0) return 0;
12301231

12311232
const text_height = switch (element.type) {
12321233
.label => element.type.label.text_size,
@@ -1238,6 +1239,7 @@ pub fn Element(comptime T: type) type {
12381239
.checkbox => element.type.checkbox.elements.items,
12391240
else => unreachable,
12401241
};
1242+
if (children.len == 0) return 0;
12411243

12421244
// Track the minimum needed width. Remember the longest line. Include
12431245
// any left/right padding.
@@ -1317,11 +1319,16 @@ pub fn Element(comptime T: type) type {
13171319
);
13181320

13191321
// Align words to centre or right if requested.
1320-
if (children.len == 0) return;
1322+
// centre and end alignment might need the `grows`
1323+
// full width, or the `shrinks` minimum width.
13211324
if (element.child_align.x == .centre or element.child_align.x == .end) {
13221325
var line_start: usize = 0;
13231326
var line_end: usize = 0;
1324-
const usable_width = wrap_at;
1327+
const usable_width = switch (element.layout.x) {
1328+
.grows => wrap_at,
1329+
.shrinks => needed_width - element.pad.left - element.pad.right,
1330+
.fixed => element.rect.width - element.pad.left - element.pad.right,
1331+
};
13251332
while (true) : (line_end += 1) {
13261333
if (line_end + 1 == children.len) {
13271334
element.do_word_alignment(
@@ -1343,23 +1350,7 @@ pub fn Element(comptime T: type) type {
13431350
}
13441351
}
13451352

1346-
if (false) {
1347-
const t = switch (element.type) {
1348-
.button => element.type.button.translated,
1349-
.label => element.type.label.translated,
1350-
.checkbox => element.type.checkbox.translated,
1351-
else => "",
1352-
};
1353-
trace("label {s} {t} \"{s}\" wrap={d} need={d} selected={d} (max_parent_width={d})", .{
1354-
element.name,
1355-
element.layout.x,
1356-
t,
1357-
wrap_at,
1358-
needed_width,
1359-
element.rect.width,
1360-
parent_inner_width,
1361-
});
1362-
}
1353+
return needed_width;
13631354
}
13641355

13651356
pub fn keypress(

src/label.zig

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ pub fn Label(comptime T: type) type {
5959
.shrinks, .grows => {
6060
// Growing or shrinking, our task here is to find
6161
// the minimum that would be needed.
62-
element.layout_label(display.scale, allowed_width);
62+
_ = element.layout_label(display.scale, allowed_width);
6363
return element.rect.width;
6464
},
6565
.fixed => {
@@ -89,12 +89,10 @@ pub fn Label(comptime T: type) type {
8989
// needs to be done here as the width of the label may have changed.
9090
switch (element.layout.y) {
9191
.shrinks, .grows => {
92-
element.layout_label(display.scale, allowed_width);
93-
//err("{s} {s} use grows height {d} (parent_width={d})", .{ self.name, @tagName(self.type), mm.max_height, parent_width });
92+
_ = element.layout_label(display.scale, allowed_width);
9493
return element.rect.height;
9594
},
9695
.fixed => {
97-
//err("{s} {s} use fixed height {d} (parent_width={d})", .{ self.name, @tagName(self.type), self.height, parent_width });
9896
return element.rect.height;
9997
},
10098
}

0 commit comments

Comments
 (0)