Skip to content

Commit 1c1645b

Browse files
author
Jay
committed
Improved on_resize event passing
1 parent 2cede81 commit 1c1645b

File tree

2 files changed

+36
-8
lines changed

2 files changed

+36
-8
lines changed

build.zig

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,17 +59,18 @@ fn define_sdl_module(
5959
// doesnt work here.
6060
const sdl_dep = b.dependency("sdl", .{});
6161
const ttf_dep = b.dependency("sdl_ttf", .{});
62+
6263
const headers = b.addTranslateC(.{
6364
.root_source_file = ttf_dep.path("include/SDL3_ttf/SDL_ttf.h"),
6465
.target = target.*,
6566
.optimize = optimize.*,
6667
});
6768
headers.addIncludePath(sdl_dep.path("include"));
6869
headers.addIncludePath(ttf_dep.path("include"));
69-
add_translatec_headers(b, target, headers);
70-
7170
const module = headers.addModule("sdl");
7271
add_libs(b, target, module);
72+
add_translatec_headers(b, target, headers);
73+
7374
return module;
7475
}
7576

src/engine.zig

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2337,6 +2337,7 @@ const TextElement = struct {
23372337
};
23382338

23392339
/// A texture is held in memory for the entire duration it might be needed.
2340+
///
23402341
/// If a texture is in use by more than one element, then the `references`
23412342
/// counter keeps track of how many elements are currently depending on
23422343
/// this texture.
@@ -2991,11 +2992,20 @@ pub const Display = struct {
29912992
var expander_weights: f32 = 0;
29922993

29932994
// Make sure this element never exceeds its maximum.
2995+
var panel_resized = false;
29942996
if (parent.layout.x == .grows and parent.maximum.width > 0) {
2995-
parent.rect.width = @min(parent.rect.width, parent.maximum.width);
2997+
const new_width = @min(parent.rect.width, parent.maximum.width);
2998+
if (parent.rect.width != new_width) {
2999+
parent.rect.width = new_width;
3000+
panel_resized = true;
3001+
}
29963002
}
29973003
if (parent.layout.y == .grows and parent.maximum.height > 0) {
2998-
parent.rect.height = @min(parent.rect.height, parent.maximum.height);
3004+
const new_height = @min(parent.rect.height, parent.maximum.height);
3005+
if (parent.rect.height != new_height) {
3006+
parent.rect.height = new_height;
3007+
panel_resized = true;
3008+
}
29993009
}
30003010

30013011
// # Step 1
@@ -3010,6 +3020,7 @@ pub const Display = struct {
30103020
if (element.visible == .hidden) {
30113021
continue;
30123022
}
3023+
var child_resized = false;
30133024
if ((dev_build or dev_mode) and element.layout.position == .float) {
30143025
if (element.layout.x == .grows) {
30153026
err("floating items cant grow. {s} {s}", .{ element.name, @tagName(element.type) });
@@ -3024,15 +3035,23 @@ pub const Display = struct {
30243035
.grows => {
30253036
// Grow to the maximum the parent will allow
30263037
element.rect.x = 0;
3027-
element.rect.width = parent.rect.width - (parent.pad.left + parent.pad.right);
3028-
if (element.maximum.width > 0 and element.rect.width > element.maximum.width) {
3029-
element.rect.width = element.maximum.width;
3038+
var new_width = parent.rect.width - (parent.pad.left + parent.pad.right);
3039+
if (element.maximum.width > 0 and new_width > element.maximum.width) {
3040+
new_width = element.maximum.width;
3041+
}
3042+
if (element.rect.width != new_width) {
3043+
element.rect.width = new_width;
3044+
child_resized = true;
30303045
}
30313046
},
30323047
.shrinks => {
30333048
// Shrink to the smallest the children will allow
30343049
// Shrink to the left, centre, or right.
3035-
element.rect.width = element.shrink_width(self, parent.rect.width);
3050+
const new_width = element.shrink_width(self, parent.rect.width);
3051+
if (element.rect.width != new_width) {
3052+
element.rect.width = new_width;
3053+
child_resized = true;
3054+
}
30363055
switch (element.child_align.x) {
30373056
.start => element.rect.x = 0,
30383057
.end => element.rect.x = parent.rect.width - element.rect.width,
@@ -3043,6 +3062,10 @@ pub const Display = struct {
30433062
// No shrinking or growing applies.
30443063
},
30453064
}
3065+
if (child_resized and element.on_resized.func != null) {
3066+
debug("element {s} resized. callback = {any}", .{ element.name, element.on_resized.func != null });
3067+
_ = element.on_resized.func.?(element.on_resized.ptr, self, element);
3068+
}
30463069
switch (element.layout.y) {
30473070
.grows => {
30483071
// Grow to the maximum the parent will allow
@@ -3094,6 +3117,10 @@ pub const Display = struct {
30943117
self.do_relayout(child);
30953118
}
30963119
}
3120+
3121+
if (panel_resized and parent.on_resized.func != null) {
3122+
_ = parent.on_resized.func.?(parent.on_resized.ptr, self, parent);
3123+
}
30973124
}
30983125

30993126
inline fn relayout_centre(_: *Display, parent: *Element) void {

0 commit comments

Comments
 (0)