{gfx,module}: fix Text update bug caused by anyUpdate reset, add peekAnyUpdate/peekUpdate

Signed-off-by: Emi Gutekanst <emi@hexops.com>
This commit is contained in:
Emi Gutekanst 2025-01-10 20:10:58 -07:00
parent 1d01c91536
commit aab0eb62f2
2 changed files with 26 additions and 8 deletions

View file

@ -259,7 +259,7 @@ pub fn tick(text: *Text, core: *mach.Core) !void {
const any_updated = blk: { const any_updated = blk: {
for (pipeline_children.items) |text_id| { for (pipeline_children.items) |text_id| {
if (!text.objects.is(text_id)) continue; if (!text.objects.is(text_id)) continue;
if (text.objects.anyUpdated(text_id)) break :blk true; if (text.objects.peekAnyUpdated(text_id)) break :blk true;
} }
break :blk false; break :blk false;
}; };

View file

@ -311,23 +311,43 @@ pub fn Objects(options: ObjectsOptions, comptime T: type) type {
} }
/// If options have tracking enabled, this returns true when the given field has been set /// If options have tracking enabled, this returns true when the given field has been set
/// using the set() or setAll() methods. A subsequent call to .updated() or .anyUpdated() /// using the set() or setAll() methods. A subsequent call to .updated(), .anyUpdated(), etc.
/// will return false until another set() or setAll() call is made. /// will return false until another set() or setAll() call is made.
pub fn updated(objs: *@This(), id: ObjectID, field_name: anytype) bool { pub fn updated(objs: *@This(), id: ObjectID, field_name: anytype) bool {
return objs.updatedOptions(id, field_name, false);
}
/// Same as updated(), but doesn't alter the behavior of subsequent .updated(), .anyUpdated(),
/// etc. calls
pub fn peekUpdated(objs: *@This(), id: ObjectID, field_name: anytype) bool {
return objs.updatedOptions(id, field_name, true);
}
inline fn updatedOptions(objs: *@This(), id: ObjectID, field_name: anytype, comptime peek: bool) bool {
if (!options.track_fields) return false; if (!options.track_fields) return false;
const unpacked = objs.validateAndUnpack(id, "updated"); const unpacked = objs.validateAndUnpack(id, "updated");
const field_index = std.meta.fieldIndex(T, @tagName(field_name)).?; const field_index = std.meta.fieldIndex(T, @tagName(field_name)).?;
const updated_fields = &(objs.internal.updated orelse return false); const updated_fields = &(objs.internal.updated orelse return false);
const updated_index = unpacked.index * @typeInfo(T).@"struct".fields.len + field_index; const updated_index = unpacked.index * @typeInfo(T).@"struct".fields.len + field_index;
const updated_value = updated_fields.isSet(updated_index); const updated_value = updated_fields.isSet(updated_index);
updated_fields.unset(updated_index); if (!peek) updated_fields.unset(updated_index);
return updated_value; return updated_value;
} }
/// If options have tracking enabled, this returns true when any field has been set using /// If options have tracking enabled, this returns true when any field has been set using
/// the set() or setAll() methods. A subsequent call to .updated() or .anyUpdated() will /// the set() or setAll() methods. A subsequent call to .updated(), .anyUpdated(), etc. will
/// return false until another set() or setAll() call is made. /// return false until another set() or setAll() call is made.
pub fn anyUpdated(objs: *@This(), id: ObjectID) bool { pub fn anyUpdated(objs: *@This(), id: ObjectID) bool {
return objs.anyUpdatedOptions(id, false);
}
/// Same as anyUpdated(), but doesn't alter the behavior of subsequent .updated(), .anyUpdated(),
/// etc. calls
pub fn peekAnyUpdated(objs: *@This(), id: ObjectID) bool {
return objs.anyUpdatedOptions(id, true);
}
inline fn anyUpdatedOptions(objs: *@This(), id: ObjectID, comptime peek: bool) bool {
if (!options.track_fields) return false; if (!options.track_fields) return false;
const unpacked = objs.validateAndUnpack(id, "updated"); const unpacked = objs.validateAndUnpack(id, "updated");
const updated_fields = &(objs.internal.updated orelse return false); const updated_fields = &(objs.internal.updated orelse return false);
@ -335,10 +355,8 @@ pub fn Objects(options: ObjectsOptions, comptime T: type) type {
inline for (0..@typeInfo(T).@"struct".fields.len) |field_index| { inline for (0..@typeInfo(T).@"struct".fields.len) |field_index| {
const updated_index = unpacked.index * @typeInfo(T).@"struct".fields.len + field_index; const updated_index = unpacked.index * @typeInfo(T).@"struct".fields.len + field_index;
const updated_value = updated_fields.isSet(updated_index); const updated_value = updated_fields.isSet(updated_index);
updated_fields.unset(updated_index); if (!peek) updated_fields.unset(updated_index);
if (updated_value) { if (updated_value) any_updated = true;
any_updated = true;
}
} }
return any_updated; return any_updated;
} }