Draw a widget with a repeated key under a derived key instead of rejecting the frame, with laws

This commit is contained in:
waozi committed 2026-10-24 03:35:20 -03:00
1 parent b8ce649beb
commit db5d286d74
4 files changed
+79 -10

No files matched your search

+41 -5
View File
@@ -12,6 +12,7 @@
#import "semantic"
#import "session"
#import "widget_kind"
#import "widget_key"
#import "vec"
// A retained node keeps its identity only when its key, kind, and parent key
@@ -333,6 +334,10 @@
// The widget that made the frame fail, so hosts can report it.
error_key: u64
error_kind: s32
// The first key submitted twice under one parent this frame; the
// repeat was drawn under a derived key (see widget_key).
repeated_key: u64
repeated_kind: s32
}
trees: [9]TreeStore;
@@ -769,6 +774,8 @@
trees[slot].error = cast(FrameStatus)FrameOk
trees[slot].error_key = cast(u64)0
trees[slot].error_kind = 0
trees[slot].repeated_key = cast(u64)0
trees[slot].repeated_kind = 0
trees[slot].staged_identity_generation =
trees[slot].next_identity_generation
trees[slot].identity_order_unchanged = true
@@ -915,10 +922,25 @@
}
}
if !node.auto_key && TreeExplicitExists(session, parent_generation, key) {
trees[slot].error = cast(FrameStatus)FrameDuplicateKey
trees[slot].error_key = key
trees[slot].error_kind = kind
return -1
// A repeated key keeps the frame: the widget is drawn under a
// derived key and the repeat is reported, never a blank screen.
attempt: s32 = 0
derived: u64 = WidgetRepeatedKey(key, attempt)
while attempt < WIDGET_KEY_ATTEMPTS && TreeExplicitExists(session, parent_generation, derived) {
attempt += 1
derived = WidgetRepeatedKey(key, attempt)
}
if attempt >= WIDGET_KEY_ATTEMPTS {
trees[slot].error = cast(FrameStatus)FrameDuplicateKey
trees[slot].error_key = key
trees[slot].error_kind = kind
return -1
}
if trees[slot].repeated_key == cast(u64)0 {
trees[slot].repeated_key = key
trees[slot].repeated_kind = kind
}
node.key = derived
}
node.kind = kind
node.measured_bounds = bounds
@@ -980,7 +1002,7 @@
}
if !node.auto_key {
capacity: s32 = cast(s32)trees[slot].new_slots.count
hash: u64 = TreeIdentityHash(parent_generation, key, 0)
hash: u64 = TreeIdentityHash(parent_generation, node.key, 0)
position: s32 = cast(s32)(hash & cast(u64)(capacity - 1))
while trees[slot].new_slots[position] >= 0 {
position = (position + 1) & (capacity - 1)
@@ -1181,6 +1203,20 @@
return trees[session.slot].error_kind
}
// The first key the frame received twice under one parent, or 0, and its
// kind. The frame still drew; the app should give the widgets own keys.
#program_export
TreeFrameRepeatedKey :: (session: Session) -> u64 {
if !SessionValid(session) { return cast(u64)0 }
return trees[session.slot].repeated_key
}
#program_export
TreeFrameRepeatedKind :: (session: Session) -> s32 {
if !SessionValid(session) { return 0 }
return trees[session.slot].repeated_kind
}
#program_export
TreeIsBuilding :: (session: Session) -> bool {
return trees[SessionIndex(session)].building
+16
View File
@@ -0,0 +1,16 @@
#import "session"
// A widget given a key that its parent already holds this frame is drawn
// under a derived key instead of failing the whole frame, so one repeated
// key never blanks a screen. Derived keys are parts of the repeated key with
// negative part numbers, which no widget uses for its own parts; the host
// reports the repeat for the app to fix.
WIDGET_KEY_ATTEMPTS :: 64
WidgetRepeatedPart :: (attempt: s32) -> s32 {
return -2 - attempt
}
WidgetRepeatedKey :: (key: u64, attempt: s32) -> u64 {
return WidgetPartKey(key, WidgetRepeatedPart(attempt))
}
+11
View File
@@ -0,0 +1,11 @@
#import "widget_key"
// Laws for repeated widget keys. Each states the expected behavior
// independently of the implementation; `ziran check` proves it over every
// listed input. An implementation edit may not change this file.
// A repeated key becomes a part no widget uses for its own parts, and each
// attempt takes a different part, so a frame finds a free key and draws.
#law RepeatedPartsAreTheirOwn forall attempt: 0..63 => WidgetRepeatedPart(attempt) < -1;
#law AttemptsDiffer forall first: 0..63, second: 0..63 => first == second || WidgetRepeatedPart(first) != WidgetRepeatedPart(second);
#law EnoughAttempts custom WIDGET_KEY_ATTEMPTS >= 8;
+11 -5
View File
@@ -131,18 +131,24 @@ Check :: () -> s32 {
if BeginFrame(first, cast(u64)7, bounds) !=
cast(FrameStatus)FrameOk { return 11 }
TreeSubmit(first, cast(u64)40, 0, WidgetKindText, bounds)
if TreeSubmit(first, cast(u64)40, 0, WidgetKindButton, bounds) >= 0 ||
EndFrame(first) != cast(FrameStatus)FrameDuplicateKey ||
TreeCount(first) != 7 || PendingPaintCount(first) != 0 {
// A repeated key never blanks the frame: the second widget draws under
// a derived key, and the frame reports the repeat for the app to fix.
text: s32 = TreeSubmit(first, cast(u64)40, 0, WidgetKindText, bounds)
repeated: s32 = TreeSubmit(first, cast(u64)40, 0, WidgetKindButton, bounds)
if text < 0 || repeated < 0 || EndFrame(first) != cast(FrameStatus)FrameOk ||
TreeNodeAt(first, repeated).key == cast(u64)40 ||
TreeNodeAt(first, text).key != cast(u64)40 ||
TreeFrameRepeatedKey(first) != cast(u64)40 ||
TreeFrameRepeatedKind(first) != WidgetKindButton {
return 12
}
committed: s32 = TreeCount(first)
if BeginFrame(first, cast(u64)7, bounds) !=
cast(FrameStatus)FrameOk { return 13 }
a = TreeSubmit(first, cast(u64)11, 0, WidgetKindGroup, bounds)
if !TreePushScope(first, a) ||
EndFrame(first) != cast(FrameStatus)FrameInvalidScope ||
TreeCount(first) != 7 { return 14 }
TreeCount(first) != committed || TreeFrameRepeatedKey(first) != cast(u64)0 { return 14 }
if !CloseSession(first) || SessionValid(first) ||
!SessionValid(second) || TreeCount(second) != 2 { return 15 }