From a5640e9f3589e951bd11f73188f204500d05294f Mon Sep 17 00:00:00 2001 From: Andrea Mancuso Date: Sat, 21 Nov 2026 21:22:20 +0100 Subject: [PATCH] Expose the visible revision for stamping events XFrames::VisibleRevision() returns the native revision of the tree that frames show. It is an atomic stored where the revision advances, under the visibility locks that rendering also holds, so an event raised while a frame is built belongs to exactly that revision, and any thread may read it. The shared C library stamps events with it, so that xframes-core can resolve a widget event against the tree it was drawn from (xframes-core#15). Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_0186WhMU3KjnmjR7Y4r49GrT --- packages/dear-imgui/cpp/app/include/xframes.h | 5 +++++ .../dear-imgui/cpp/app/src/xframes_commit.cpp | 1 + packages/dear-imgui/cpp/tests/xframes_test.cpp | 17 ++++++++++++++++- 3 files changed, 22 insertions(+), 1 deletion(-) diff --git a/packages/dear-imgui/cpp/app/include/xframes.h b/packages/dear-imgui/cpp/app/include/xframes.h index 81e3245b..bb0563f6 100644 --- a/packages/dear-imgui/cpp/app/include/xframes.h +++ b/packages/dear-imgui/cpp/app/include/xframes.h @@ -60,6 +60,7 @@ class XFrames { std::mutex m_commitMutex; uint64_t m_nativeSequence = 0; uint64_t m_nativeRevision = 0; + std::atomic m_visibleRevision{0}; std::unordered_set m_publicationOwnedIds; std::atomic m_surfaceQuarantined{false}; std::atomic m_runtimeDisposed{false}; @@ -197,6 +198,10 @@ class XFrames { // Synchronous structural API. uint64 counters use decimal strings on the wire. xframes::CommitResult ApplyCommit(std::string_view serializedCommit); json GetCommitState(); + // The native revision of the tree that frames show: stored where the revision + // advances, under the visibility locks. An event raised while a frame is built + // belongs to this tree. Safe to read from any thread. + uint64_t VisibleRevision() const { return m_visibleRevision.load(std::memory_order_acquire); } diff --git a/packages/dear-imgui/cpp/app/src/xframes_commit.cpp b/packages/dear-imgui/cpp/app/src/xframes_commit.cpp index 27d03694..d3751cd6 100644 --- a/packages/dear-imgui/cpp/app/src/xframes_commit.cpp +++ b/packages/dear-imgui/cpp/app/src/xframes_commit.cpp @@ -124,6 +124,7 @@ CommitResult XFrames::ApplyCommitOperations(CommitBatch& batch) { if (!m_frameScheduler.Invalidate(FrameReason::Publication)) throw std::overflow_error("Invalidation generation exhausted during publication"); result.nativeRevision = ++m_nativeRevision; + m_visibleRevision.store(m_nativeRevision, std::memory_order_release); result.status = "applied"; } catch (const std::exception& error) { // Quarantine is established under the visibility locks even if diff --git a/packages/dear-imgui/cpp/tests/xframes_test.cpp b/packages/dear-imgui/cpp/tests/xframes_test.cpp index 4fd97420..1ca02a88 100644 --- a/packages/dear-imgui/cpp/tests/xframes_test.cpp +++ b/packages/dear-imgui/cpp/tests/xframes_test.cpp @@ -85,7 +85,9 @@ class XFramesTest : public ::testing::Test { }; } void Internal(int id, const json& operation) { auto payload = operation.dump(); xf->QueueElementInternalOp(id, payload); } - void SetCounters(uint64_t sequence, uint64_t revision) { xf->m_nativeSequence = sequence; xf->m_nativeRevision = revision; } + void SetCounters(uint64_t sequence, uint64_t revision) { + xf->m_nativeSequence = sequence; xf->m_nativeRevision = revision; xf->m_visibleRevision = revision; + } bool LastRequestExpired() { bool expired = false; auto subscription = rpp::composite_disposable_wrapper::make(); @@ -317,6 +319,19 @@ TEST_F(XFramesTest, PublicationCompetingWritersCannotOverwriteAStaleTree) { EXPECT_EQ(xf->GetCommitState()["nativeRevision"], "2"); } +// Events are stamped with the visible revision: the tree every frame drawn since +// the last applied publication shows. It moves only when a publication applies. +TEST_F(XFramesTest, VisibleRevisionFollowsAppliedPublicationsOnly) { + EXPECT_EQ(xf->VisibleRevision(), 0u); + Publish({{0, {1}}, {1, {}}}, {Create(1)}); + EXPECT_EQ(xf->VisibleRevision(), 1u); + Reject(Wire({{0, {1}}, {1, {}}}, {Create(1)}), "duplicate_id", 0); + EXPECT_EQ(xf->VisibleRevision(), 1u); + Publish(); + EXPECT_EQ(xf->VisibleRevision(), 2u); + EXPECT_EQ(std::to_string(xf->VisibleRevision()), xf->GetCommitState()["nativeRevision"].get()); +} + // "childLists": "changed" carries only the child lists that changed. An omitted // list is the previous one; a created node without an assignment has none. namespace {