Skip to content

Commit 4920ad4

Browse files
committed
Add an event handler for on_visibility
1 parent 723a2f2 commit 4920ad4

1 file changed

Lines changed: 10 additions & 4 deletions

File tree

src/engine.zig

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -410,6 +410,7 @@ pub const Element = struct {
410410
border_width: f32 = 0,
411411

412412
on_resized: BoolCallback = .{ .func = null },
413+
on_visibility: Callback = .{ .func = null },
413414

414415
type: union(ElementType) {
415416
panel: struct {
@@ -794,10 +795,13 @@ pub const Element = struct {
794795
}
795796
}
796797

797-
pub inline fn set_visibility(self: *Element, display: *Display, visible: Visibility) void {
798+
pub inline fn set_visibility(self: *Element, display: *Display, visible: Visibility) Allocator.Error!void {
798799
if (self.visible == visible) return;
799800
self.visible = visible;
800801
display.need_relayout = true;
802+
if (self.on_visibility.func != null) {
803+
try self.on_visibility.func.?(self.on_visibility.ptr, display, self, display.allocator);
804+
}
801805
}
802806

803807
/// set_texture replaces the current texture of an element with a new
@@ -2754,6 +2758,7 @@ pub const Display = struct {
27542758
.border_width = 0,
27552759
.type = .{ .panel = .{ .direction = .centre, .spacing = 0 } },
27562760
.on_resized = .{ .func = null },
2761+
.on_visibility = .{ .func = null },
27572762
},
27582763
animators: ArrayListUnmanaged(*Animator) = .empty,
27592764

@@ -2967,6 +2972,7 @@ pub const Display = struct {
29672972
.border_width = 0,
29682973
.type = .{ .panel = .{ .direction = .centre, .spacing = 0 } },
29692974
.on_resized = .{ .func = null },
2975+
.on_visibility = .{ .func = null },
29702976
};
29712977
display.root.rect.width = @as(f32, @floatFromInt(window_width)) * display.pixel_density;
29722978
display.root.rect.height = @as(f32, @floatFromInt(window_height)) * display.pixel_density;
@@ -3094,7 +3100,7 @@ pub const Display = struct {
30943100
/// Mark a top level panel as visible, and all other
30953101
/// top level panels as not visible. The visibility of the
30963102
/// _background_ and _menu_ panel is not altered.
3097-
pub fn choose_panel(self: *Display, gpa: Allocator, name: []const u8) void {
3103+
pub fn choose_panel(self: *Display, gpa: Allocator, name: []const u8) Allocator.Error!void {
30983104
const old_panel = self.current_panel();
30993105

31003106
var found = false;
@@ -3111,7 +3117,7 @@ pub const Display = struct {
31113117
} else {
31123118
debug("choose panel. ... -> {s}", .{name});
31133119
}
3114-
element.visible = .visible;
3120+
try element.set_visibility(self, .visible);
31153121
if (element.on_resized.func != null) {
31163122
self.need_relayout = true;
31173123
_ = element.on_resized.func.?(element.on_resized.ptr, self, element);
@@ -3125,7 +3131,7 @@ pub const Display = struct {
31253131
} else {
31263132
if (element.visible != .hidden) {
31273133
debug("choose_panel({s}) hiding panel {s}.", .{ name, element.name });
3128-
element.visible = .hidden;
3134+
try element.set_visibility(self, .hidden);
31293135
}
31303136
}
31313137
found = true;

0 commit comments

Comments
 (0)