UI: Defer document destruction

This commit is contained in:
Luke Street
2026-04-30 17:46:36 -06:00
parent 62edb3abc6
commit cee6a24309
4 changed files with 38 additions and 10 deletions
+7
View File
@@ -64,6 +64,13 @@ void Document::listen(Rml::Element* element, Rml::EventId event,
std::make_unique<ScopedEventListener>(element, event, std::move(callback), capture));
}
bool Document::can_destroy() const {
if (mDocument == nullptr) {
return true;
}
return *mDocument->GetProperty(Rml::PropertyId::Visibility) == Rml::Style::Visibility::Hidden;
}
bool Document::handle_nav_command(Rml::Event& event, NavCommand cmd) {
return false;
}
+2
View File
@@ -24,6 +24,8 @@ public:
listen(mDocument, event, std::move(callback), capture);
}
bool can_destroy() const;
protected:
virtual bool handle_nav_command(Rml::Event& event, NavCommand cmd);
+4 -2
View File
@@ -19,11 +19,13 @@ Popup::Popup() : Document("res/rml/popup.rml"), mRoot(mDocument->GetElementById(
// TODO
});
mTabBar->add_tab("Editor", [] { push_document(std::make_unique<EditorWindow>()); });
mTabBar->add_tab("Reset", [] {
mTabBar->add_tab("Reset", [this] {
// TODO
mTabBar->set_active_tab(-1);
});
mTabBar->add_tab("Exit", [] {
mTabBar->add_tab("Exit", [this] {
// TODO
mTabBar->set_active_tab(-1);
});
// Hide document after transition completion
+25 -8
View File
@@ -18,7 +18,12 @@ void load_font(const char* filename, bool fallback = false) {
}
bool sInitialized = false;
std::vector<std::unique_ptr<Document> > sDocuments;
struct OpenDocument {
std::unique_ptr<Document> doc;
bool pendingDestroy = false;
};
std::vector<OpenDocument> sDocuments;
} // namespace
@@ -52,7 +57,7 @@ Document& push_document(std::unique_ptr<Document> doc, bool show) noexcept {
doc->hide();
}
Document& ret = *doc;
sDocuments.push_back(std::move(doc));
sDocuments.push_back({std::move(doc)});
if (show) {
ret.show();
}
@@ -60,23 +65,35 @@ Document& push_document(std::unique_ptr<Document> doc, bool show) noexcept {
}
void pop_document() noexcept {
sDocuments.pop_back();
for (auto it = sDocuments.rbegin(); it != sDocuments.rend(); ++it) {
if (!it->pendingDestroy) {
it->doc->hide();
it->pendingDestroy = true;
break;
}
}
if (auto* doc = top_document()) {
doc->show();
}
}
Document* top_document() noexcept {
if (sDocuments.empty()) {
return nullptr;
for (auto it = sDocuments.rbegin(); it != sDocuments.rend(); ++it) {
if (!it->pendingDestroy) {
return it->doc.get();
}
}
return sDocuments.back().get();
return nullptr;
}
void update() noexcept {
for (const auto& doc : sDocuments) {
doc->update();
for (size_t i = 0; i < sDocuments.size(); ++i) {
sDocuments[i].doc->update();
}
sDocuments.erase(
std::remove_if(sDocuments.begin(), sDocuments.end(),
[](const OpenDocument& doc) { return doc.pendingDestroy && doc.doc->can_destroy(); }),
sDocuments.end());
}
std::filesystem::path resource_path(const std::filesystem::path& filename) noexcept {