Model UI capture lifetime in ui core
This commit is contained in:
@@ -158,11 +158,68 @@ pp::foundation::Status NodeLifetimeTree::destroy_subtree(NodeHandle node) noexce
|
||||
slot->children.clear();
|
||||
slot->connections.clear();
|
||||
slot->parent = {};
|
||||
release_captures_for_node(node);
|
||||
slot->alive = false;
|
||||
free_nodes_.push_back(node.slot);
|
||||
return pp::foundation::Status::success();
|
||||
}
|
||||
|
||||
void NodeLifetimeTree::clear() noexcept
|
||||
{
|
||||
free_nodes_.clear();
|
||||
for (std::uint32_t index = 0; index < nodes_.size(); ++index) {
|
||||
auto& slot = nodes_[index];
|
||||
slot.alive = false;
|
||||
slot.parent = {};
|
||||
slot.children.clear();
|
||||
slot.connections.clear();
|
||||
free_nodes_.push_back(index);
|
||||
}
|
||||
|
||||
free_connections_.clear();
|
||||
for (std::uint32_t index = 0; index < connections_.size(); ++index) {
|
||||
auto& slot = connections_[index];
|
||||
slot.alive = false;
|
||||
slot.node = {};
|
||||
slot.callback = nullptr;
|
||||
free_connections_.push_back(index);
|
||||
}
|
||||
|
||||
captures_ = {};
|
||||
}
|
||||
|
||||
pp::foundation::Status NodeLifetimeTree::capture(UiCaptureKind kind, NodeHandle node) noexcept
|
||||
{
|
||||
if (node_slot(node) == nullptr) {
|
||||
return pp::foundation::Status::invalid_argument("UI capture requires a live node");
|
||||
}
|
||||
|
||||
captures_[capture_index(kind)] = node;
|
||||
return pp::foundation::Status::success();
|
||||
}
|
||||
|
||||
pp::foundation::Status NodeLifetimeTree::release_capture(UiCaptureKind kind, NodeHandle node) noexcept
|
||||
{
|
||||
auto& captured = captures_[capture_index(kind)];
|
||||
if (captured != node) {
|
||||
return pp::foundation::Status::invalid_argument("UI capture release requires the captured node");
|
||||
}
|
||||
|
||||
captured = {};
|
||||
return pp::foundation::Status::success();
|
||||
}
|
||||
|
||||
pp::foundation::Result<NodeHandle> NodeLifetimeTree::captured_node(UiCaptureKind kind) const noexcept
|
||||
{
|
||||
const auto captured = captures_[capture_index(kind)];
|
||||
if (node_slot(captured) == nullptr) {
|
||||
return pp::foundation::Result<NodeHandle>::failure(
|
||||
pp::foundation::Status::invalid_argument("UI capture slot is empty"));
|
||||
}
|
||||
|
||||
return pp::foundation::Result<NodeHandle>::success(captured);
|
||||
}
|
||||
|
||||
pp::foundation::Result<UiConnection> NodeLifetimeTree::connect(NodeHandle node, Callback callback)
|
||||
{
|
||||
if (node_slot(node) == nullptr) {
|
||||
@@ -339,6 +396,18 @@ pp::foundation::Result<NodeHandle> NodeLifetimeTree::allocate_node(NodeHandle pa
|
||||
return pp::foundation::Result<NodeHandle>::success(node);
|
||||
}
|
||||
|
||||
std::size_t NodeLifetimeTree::capture_index(UiCaptureKind kind) const noexcept
|
||||
{
|
||||
switch (kind) {
|
||||
case UiCaptureKind::pointer:
|
||||
return 0U;
|
||||
case UiCaptureKind::keyboard:
|
||||
return 1U;
|
||||
}
|
||||
|
||||
return 0U;
|
||||
}
|
||||
|
||||
void NodeLifetimeTree::unlink_from_parent(NodeHandle node) noexcept
|
||||
{
|
||||
const auto* slot = node_slot(node);
|
||||
@@ -376,4 +445,13 @@ void NodeLifetimeTree::release_connection(UiConnection connection) noexcept
|
||||
free_connections_.push_back(connection.slot);
|
||||
}
|
||||
|
||||
void NodeLifetimeTree::release_captures_for_node(NodeHandle node) noexcept
|
||||
{
|
||||
for (auto& capture : captures_) {
|
||||
if (capture == node) {
|
||||
capture = {};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user