Portals0.7.0 · dev (544a5c68c72e · 2026-11-28 UTC)
Loading...
Searching...
No Matches
managed_navigation.hpp
1// Copyright (c) 2026 ttldtor.
2// SPDX-License-Identifier: BSL-1.0
3#pragma once
4
5#include <portals/model_position_finite.hpp>
6
7namespace portals::detail {
9class ModelViewHistory;
10struct ModelViewCheckpoint;
11struct PreparedModelViewChange;
12struct ModelRestoreChannels;
13
14struct ManagedNavigationRecord {
15 ManagedState<NavigationPosition> current;
16 ManagedState<NavigationSelection> selection;
17 ManagedState<NavigationPosition> reading;
18 ManagedScene *owner{};
19 std::weak_ptr<const void> lifetime;
20 std::weak_ptr<ManagedModelSource> model;
21 NodeId scene;
22 ModelResource resource;
23 SourceStamp session;
24 NavigationOptions options;
25 std::optional<ModelBookmark> bookmark;
26 std::optional<ModelBookmark> readingBookmark;
27 PositionChangeMap changes;
28 std::optional<PositionRequest> request;
29 std::stop_source cancellation{std::nostopstate};
30 std::uint64_t sequence{};
31 std::uint64_t operation{};
32 std::uint64_t frameRevision{};
33 bool finite{};
34 bool closed{};
35 bool admittingEdit{};
36 bool editVetoed{};
37 std::unique_ptr<std::vector<std::weak_ptr<EditNavigationParticipant>>> editors;
38 std::expected<void, TreeError> beforeEditTransition(const NavigationPosition *next = nullptr,
39 const std::shared_ptr<ManagedModelSource> &source = {});
40 using Capture =
41 std::function<std::expected<ModelBookmark, TreeError>(const std::shared_ptr<ManagedNavigationRecord> &, RowId)>;
42 using Synchronize = std::function<std::expected<void, TreeError>(const std::shared_ptr<ManagedNavigationRecord> &)>;
43 std::shared_ptr<Capture> capture;
44 std::shared_ptr<Synchronize> synchronize;
45 std::shared_ptr<ModelViewHistory> history;
46
47 struct Publication {
48 std::shared_ptr<Property<NavigationPosition>> currentOwner;
49 std::shared_ptr<Property<NavigationSelection>> selectionOwner;
50 std::shared_ptr<Property<NavigationPosition>> readingOwner;
51 Property<NavigationPosition>::PreparedChange current;
52 Property<NavigationSelection>::PreparedChange selection;
53 Property<NavigationPosition>::PreparedChange reading;
54 };
55
56 std::expected<Publication, TreeError>
57 preparePublication(NavigationPosition nextCurrent, NavigationSelection nextSelection, NavigationPosition nextReading);
58 std::expected<void, TreeError> validatePublication(const Publication &publication) const;
59 std::expected<void, TreeError> invalidatePublication(const Publication &publication) const;
60 static void installPublication(const Publication &publication) noexcept;
61 static std::optional<SchedulerError> publishPublication(const Publication &publication);
62
63 std::expected<void, TreeError> setPosition(std::optional<ModelBookmark> value, PositionResolution resolution,
64 bool readingAnchor = false);
65 void cancel() noexcept;
66 void close() noexcept;
67};
68
69struct ManagedNavigationBinding {
70 Node<Widget> node;
71 std::weak_ptr<ManagedNavigationRecord> context;
72 NavigationBindingOptions options;
73 using Stage = std::function<std::expected<std::unique_ptr<PreparedModelViewChange>, ManagedError>(
74 const std::shared_ptr<ManagedModelSource> &, ModelRebindOptions, const std::shared_ptr<ModelViewHistory> &,
75 const ModelViewCheckpoint *, const void *)>;
76 Stage stage;
77 std::function<void(const ModelViewCheckpoint *, const ModelRestoreChannels *)> installed;
78 std::function<std::expected<void, TreeError>()> before;
79 std::function<std::expected<void, TreeError>()> after;
80};
81
82struct LogCheckpointEntry {
83 // Credit is released after all metadata storage owned by this entry.
84 SourceCredit credit;
85 SourceCredit metadataCredit;
86 ModelResource resource;
87 std::weak_ptr<const void> lifetime;
88 LogReadingCheckpoint value;
89 std::unique_ptr<LogCheckpointEntry> next;
90};
91
92struct LogCheckpointChange {
93 SourceCredit metadataCredit;
94 std::unique_ptr<LogCheckpointEntry> added;
95 LogCheckpointEntry *existing{};
96 ModelResource resource;
97 std::weak_ptr<const void> lifetime;
98 LogReadingCheckpoint value;
99 bool replaceMetadata{};
100};
101
102struct LogNavigationSession {
103 SourceCredit credit;
104 std::shared_ptr<SourceBudget> budget;
105 std::weak_ptr<const void> lifetime;
106 std::weak_ptr<const void> binding;
107 NodeId scene;
108 LogCheckpointOptions options;
109 LogRestoreStatus status;
110 std::unique_ptr<LogCheckpointEntry> entries;
111 bool closed{};
112
113 bool implicit{};
114
115 LogCheckpointEntry *find(ModelResource resource) const noexcept;
116 void prune() noexcept;
117 void reset(ModelResource resource) noexcept;
118 void close() noexcept;
119 std::expected<LogCheckpointChange, SourceError>
120 stage(ModelResource resource, std::weak_ptr<const void> resourceLifetime, LogReadingCheckpoint value);
121 void commit(LogCheckpointChange change) noexcept;
122};
123
124struct ManagedNavigationRegistry {
125 std::vector<std::shared_ptr<ManagedNavigationRecord>> contexts =
126 std::vector<std::shared_ptr<ManagedNavigationRecord>>(0);
127 std::vector<std::shared_ptr<ManagedNavigationBinding>> bindings =
128 std::vector<std::shared_ptr<ManagedNavigationBinding>>(0);
129 std::vector<std::shared_ptr<LogNavigationSession>> logSessions =
130 std::vector<std::shared_ptr<LogNavigationSession>>(0);
131 bool refreshAfterLayout{};
132};
133
134TreeError navigationTreeError(SourceError error) noexcept;
136} // namespace portals::detail
TreeError
Checked tree failures; rejected operations never start the requested callback.
Definition widget_types.hpp:26