fix temporary pages

This commit is contained in:
MihailRis 2025-01-05 23:41:00 +03:00
parent 48aad8d974
commit 435a8299b7
3 changed files with 31 additions and 21 deletions

View File

@ -13,8 +13,10 @@ bool Menu::has(const std::string& name) {
pageSuppliers.find(name) != pageSuppliers.end(); pageSuppliers.find(name) != pageSuppliers.end();
} }
void Menu::addPage(const std::string& name, const std::shared_ptr<UINode>& panel) { void Menu::addPage(
pages[name] = Page {name, panel}; const std::string& name, const std::shared_ptr<UINode>& panel, bool temporal
) {
pages[name] = Page {name, panel, temporal};
} }
void Menu::removePage(const std::string& name) { void Menu::removePage(const std::string& name) {
@ -25,26 +27,26 @@ void Menu::addSupplier(const std::string& name, const supplier<std::shared_ptr<U
pageSuppliers[name] = pageSupplier; pageSuppliers[name] = pageSupplier;
} }
std::shared_ptr<UINode> Menu::fetchPage(const std::string& name) { Page Menu::fetchPage(const std::string& name) {
auto found = pages.find(name); auto found = pages.find(name);
if (found == pages.end()) { if (found == pages.end()) {
auto supplier = pageSuppliers.find(name); auto supplier = pageSuppliers.find(name);
if (supplier == pageSuppliers.end()) { if (supplier == pageSuppliers.end()) {
if (pagesLoader) { if (pagesLoader) {
return pagesLoader(name); return {name, pagesLoader(name), false};
} }
return nullptr; return {};
} else { } else {
return supplier->second(); return {name, supplier->second(), false};
// supplied pages caching is not implemented // supplied pages caching is not implemented
} }
} else { } else {
return found->second.panel; return found->second;
} }
} }
void Menu::setPage(const std::string &name, bool history) { void Menu::setPage(const std::string &name, bool history) {
Page page {name, fetchPage(name)}; Page page = fetchPage(name);
if (page.panel == nullptr) { if (page.panel == nullptr) {
throw std::runtime_error("no page found"); throw std::runtime_error("no page found");
} }
@ -54,7 +56,7 @@ void Menu::setPage(const std::string &name, bool history) {
void Menu::setPage(Page page, bool history) { void Menu::setPage(Page page, bool history) {
if (current.panel) { if (current.panel) {
Container::remove(current.panel); Container::remove(current.panel);
if (history) { if (history && !current.temporal) {
pageStack.push(current); pageStack.push(current);
} }
} }
@ -70,8 +72,8 @@ void Menu::back() {
pageStack.pop(); pageStack.pop();
auto updated = fetchPage(page.name); auto updated = fetchPage(page.name);
if (updated) { if (updated.panel) {
page.panel = updated; page.panel = updated.panel;
} }
setPage(page, false); setPage(page, false);

View File

@ -8,6 +8,7 @@ namespace gui {
struct Page { struct Page {
std::string name; std::string name;
std::shared_ptr<UINode> panel; std::shared_ptr<UINode> panel;
bool temporal = false;
}; };
using page_loader_func = std::function<std::shared_ptr<UINode>(const std::string& name)>; using page_loader_func = std::function<std::shared_ptr<UINode>(const std::string& name)>;
@ -31,14 +32,21 @@ namespace gui {
/// @param history previous page will not be saved in history if false /// @param history previous page will not be saved in history if false
void setPage(const std::string &name, bool history=true); void setPage(const std::string &name, bool history=true);
void setPage(Page page, bool history=true); void setPage(Page page, bool history=true);
void addPage(const std::string& name, const std::shared_ptr<UINode>& panel); void addPage(
const std::string& name,
const std::shared_ptr<UINode>& panel,
bool temporal = false
);
void removePage(const std::string& name); void removePage(const std::string& name);
std::shared_ptr<UINode> fetchPage(const std::string& name); Page fetchPage(const std::string& name);
/// @brief Add page supplier used if page is not found /// @brief Add page supplier used if page is not found
/// @param name page name /// @param name page name
/// @param pageSupplier page supplier function /// @param pageSupplier page supplier function
void addSupplier(const std::string& name, const supplier<std::shared_ptr<UINode>>& pageSupplier); void addSupplier(
const std::string& name,
const supplier<std::shared_ptr<UINode>>& pageSupplier
);
/// @brief Page loader is called if accessed page is not found /// @brief Page loader is called if accessed page is not found
void setPageLoader(page_loader_func loader); void setPageLoader(page_loader_func loader);

View File

@ -36,12 +36,12 @@ void guiutil::alert(
auto menu = engine.getGUI()->getMenu(); auto menu = engine.getGUI()->getMenu();
runnable on_hidden_final = [on_hidden, menu, &engine]() { runnable on_hidden_final = [on_hidden, menu, &engine]() {
menu->removePage("<alert>");
if (on_hidden) { if (on_hidden) {
on_hidden(); on_hidden();
} else { } else {
menu->back(); menu->back();
} }
menu->removePage("<alert>");
}; };
auto label = std::make_shared<Label>(text); auto label = std::make_shared<Label>(text);
@ -63,7 +63,7 @@ void guiutil::alert(
on_hidden_final(); on_hidden_final();
return true; return true;
})); }));
menu->addPage("<alert>", panel); menu->addPage("<alert>", panel, true);
menu->setPage("<alert>"); menu->setPage("<alert>");
} }
@ -87,21 +87,21 @@ void guiutil::confirm(
auto menu = engine.getGUI()->getMenu(); auto menu = engine.getGUI()->getMenu();
runnable on_confirm_final = [on_confirm, menu, &engine]() { runnable on_confirm_final = [on_confirm, menu, &engine]() {
menu->removePage("<confirm>");
if (on_confirm) { if (on_confirm) {
on_confirm(); on_confirm();
} else { } else {
menu->back(); menu->back();
} }
menu->removePage("<confirm>");
}; };
runnable on_deny_final = [on_deny, menu, &engine]() { runnable on_deny_final = [on_deny, menu, &engine]() {
menu->removePage("<confirm>");
if (on_deny) { if (on_deny) {
on_deny(); on_deny();
} else { } else {
menu->back(); menu->back();
} }
menu->removePage("<confirm>");
}; };
subpanel->add(std::make_shared<Button>(yestext, glm::vec4(8.f), [=](GUI*){ subpanel->add(std::make_shared<Button>(yestext, glm::vec4(8.f), [=](GUI*){
@ -123,7 +123,7 @@ void guiutil::confirm(
})); }));
panel->refresh(); panel->refresh();
menu->addPage("<confirm>", panel); menu->addPage("<confirm>", panel, true);
menu->setPage("<confirm>"); menu->setPage("<confirm>");
} }
@ -166,6 +166,6 @@ void guiutil::confirm_with_memo(
panel->add(subpanel); panel->add(subpanel);
panel->refresh(); panel->refresh();
menu->addPage("<confirm>", panel); menu->addPage("<confirm>", panel, true);
menu->setPage("<confirm>"); menu->setPage("<confirm>");
} }