diff options
author | rtk0c <[email protected]> | 2021-06-05 14:58:09 -0700 |
---|---|---|
committer | rtk0c <[email protected]> | 2021-06-05 14:58:09 -0700 |
commit | d3fa128d5408673b0ea8d3ba3435c38b258a5e7a (patch) | |
tree | 20ff77021bae4dc69f0bba4d1b65330e0301600a /core | |
parent | beb1f3969e13af72bd9098d484b693e397cf7235 (diff) |
Merge UIState into GlobalStates
Diffstat (limited to 'core')
-rw-r--r-- | core/CMakeLists.txt | 1 | ||||
-rw-r--r-- | core/src/Entrypoint/main.cpp | 7 | ||||
-rw-r--r-- | core/src/Model/GlobalStates.cpp | 25 | ||||
-rw-r--r-- | core/src/Model/GlobalStates.hpp | 5 | ||||
-rw-r--r-- | core/src/UI/States.cpp | 44 | ||||
-rw-r--r-- | core/src/UI/States.hpp | 23 | ||||
-rw-r--r-- | core/src/UI/UI_DatabaseView.cpp | 8 | ||||
-rw-r--r-- | core/src/UI/UI_Items.cpp | 13 | ||||
-rw-r--r-- | core/src/UI/UI_MainWindow.cpp | 21 | ||||
-rw-r--r-- | core/src/UI/UI_Templates.cpp | 16 | ||||
-rw-r--r-- | core/src/UI/UI_Workflows.cpp | 14 | ||||
-rw-r--r-- | core/src/UI/fwd.hpp | 3 |
12 files changed, 64 insertions, 116 deletions
diff --git a/core/CMakeLists.txt b/core/CMakeLists.txt index bae51a1..8483981 100644 --- a/core/CMakeLists.txt +++ b/core/CMakeLists.txt @@ -70,7 +70,6 @@ add_source_group(MODEL_WORKFLOW_VALUES_MODULE_SOURCES set(UI_MODULE_SOURCES src/UI/Localization.cpp - src/UI/States.cpp src/UI/UI_DatabaseView.cpp src/UI/UI_Items.cpp src/UI/UI_MainWindow.cpp diff --git a/core/src/Entrypoint/main.cpp b/core/src/Entrypoint/main.cpp index 152a1c8..d6f2cdf 100644 --- a/core/src/Entrypoint/main.cpp +++ b/core/src/Entrypoint/main.cpp @@ -1,7 +1,6 @@ #include "Entrypoint/Backend.hpp" #include "Model/GlobalStates.hpp" #include "UI/Localization.hpp" -#include "UI/States.hpp" #include "UI/UI.hpp" #include "Utils/I18n.hpp" #include "Utils/ScopeGuard.hpp" @@ -163,12 +162,6 @@ int main(int argc, char* argv[]) GlobalStates::Shutdown(); }; - UIState::Init(); - DEFER - { - UIState::Shutdown(); - }; - // Main loop backend->RunUntilWindowClose(&UI::MainWindow); diff --git a/core/src/Model/GlobalStates.cpp b/core/src/Model/GlobalStates.cpp index 2230367..a449afb 100644 --- a/core/src/Model/GlobalStates.cpp +++ b/core/src/Model/GlobalStates.cpp @@ -56,6 +56,9 @@ void GlobalStates::Init(std::filesystem::path userDataDir) void GlobalStates::Shutdown() { if (!globalStateInstance) return; + + globalStateInstance->SetCurrentProject(nullptr); + if (globalStateInstance->mDirty) { globalStateInstance->WriteToDisk(); } @@ -111,6 +114,28 @@ void GlobalStates::RemoveRecentProject(int idx) MarkDirty(); } +bool GlobalStates::HasCurrentProject() const +{ + return mCurrentProject != nullptr; +} + +Project* GlobalStates::GetCurrentProject() const +{ + return mCurrentProject.get(); +} + +void GlobalStates::SetCurrentProject(std::unique_ptr<Project> project) +{ + if (mCurrentProject) { + mCurrentProject->WriteToDisk(); + mCurrentProject = nullptr; + } + if (project) { + MoveProjectToTop(*project); + } + mCurrentProject = std::move(project); +} + void GlobalStates::WriteToDisk() const { Json::Value root; diff --git a/core/src/Model/GlobalStates.hpp b/core/src/Model/GlobalStates.hpp index 6970642..cc41bd5 100644 --- a/core/src/Model/GlobalStates.hpp +++ b/core/src/Model/GlobalStates.hpp @@ -28,6 +28,7 @@ public: private: std::vector<RecentProject> mRecentProjects; + std::unique_ptr<Project> mCurrentProject; mutable bool mDirty = false; public: @@ -40,6 +41,10 @@ public: void MoveProjectToTop(const Project& project); void RemoveRecentProject(int idx); + bool HasCurrentProject() const; + Project* GetCurrentProject() const; + void SetCurrentProject(std::unique_ptr<Project> project); + // TODO async autosaving to prevent data loss on crash void WriteToDisk() const; diff --git a/core/src/UI/States.cpp b/core/src/UI/States.cpp deleted file mode 100644 index b7b4c9f..0000000 --- a/core/src/UI/States.cpp +++ /dev/null @@ -1,44 +0,0 @@ -#include "States.hpp" - -#include "Model/GlobalStates.hpp" -#include "Model/Project.hpp" - -#include <memory> -#include <utility> - -static std::unique_ptr<UIState> uiStateInstance; - -void UIState::Init() -{ - uiStateInstance = std::make_unique<UIState>(); -} - -void UIState::Shutdown() -{ - if (uiStateInstance) { - uiStateInstance->CloseCurrentProject(); - uiStateInstance = nullptr; - } -} - -UIState& UIState::GetInstance() -{ - return *uiStateInstance; -} - -void UIState::SetCurrentProject(std::unique_ptr<Project> project) -{ - CloseCurrentProject(); - if (project) { - GlobalStates::GetInstance().MoveProjectToTop(*project); - } - CurrentProject = std::move(project); -} - -void UIState::CloseCurrentProject() -{ - if (CurrentProject) { - CurrentProject->WriteToDisk(); - CurrentProject = nullptr; - } -} diff --git a/core/src/UI/States.hpp b/core/src/UI/States.hpp deleted file mode 100644 index d314494..0000000 --- a/core/src/UI/States.hpp +++ /dev/null @@ -1,23 +0,0 @@ -#pragma once - -#include "cplt_fwd.hpp" - -#include <imgui.h> -#include <memory> - -/// Minimal state shared by all UI components, such as database, items, export, etc. -/// Note that global components (settings) is not supposed to access these. -class UIState -{ -public: - static void Init(); - static void Shutdown(); - static UIState& GetInstance(); - -public: - std::unique_ptr<Project> CurrentProject; - -public: - void SetCurrentProject(std::unique_ptr<Project> project); - void CloseCurrentProject(); -}; diff --git a/core/src/UI/UI_DatabaseView.cpp b/core/src/UI/UI_DatabaseView.cpp index 06326ae..bd0efa5 100644 --- a/core/src/UI/UI_DatabaseView.cpp +++ b/core/src/UI/UI_DatabaseView.cpp @@ -1,9 +1,9 @@ #include "UI.hpp" #include "Model/Filter.hpp" +#include "Model/GlobalStates.hpp" #include "Model/Project.hpp" #include "UI/Localization.hpp" -#include "UI/States.hpp" #include "Utils/ScopeGuard.hpp" #include "Utils/Time.hpp" @@ -650,14 +650,14 @@ public: void UI::DatabaseViewTab() { auto ls = LocaleStrings::Instance.get(); - auto& uis = UIState::GetInstance(); + auto& gs = GlobalStates::GetInstance(); static Project* currentProject = nullptr; static SalesTableView sales; static PurchasesTableView purchases; - if (currentProject != uis.CurrentProject.get()) { - currentProject = uis.CurrentProject.get(); + if (currentProject != gs.GetCurrentProject()) { + currentProject = gs.GetCurrentProject(); sales.OnProjectChanged(currentProject); purchases.OnProjectChanged(currentProject); } diff --git a/core/src/UI/UI_Items.cpp b/core/src/UI/UI_Items.cpp index 7ed8f80..d006166 100644 --- a/core/src/UI/UI_Items.cpp +++ b/core/src/UI/UI_Items.cpp @@ -3,7 +3,6 @@ #include "Model/GlobalStates.hpp" #include "Model/Project.hpp" #include "UI/Localization.hpp" -#include "UI/States.hpp" #include <imgui.h> #include <imgui_stdlib.h> @@ -32,7 +31,7 @@ ActionResult ItemEditor(ItemList<T>& list, T* item) }; auto ls = LocaleStrings::Instance.get(); - auto& uis = UIState::GetInstance(); + auto& gs = GlobalStates::GetInstance(); static bool duplicateName = false; @@ -117,7 +116,7 @@ void ItemListEntries(ItemList<T>& list, int& selectedIdx) constexpr int kColumns = 1 /* Name column */ + kHasDescription + kHasEmail + kHasStock + kHasPrice; auto ls = LocaleStrings::Instance.get(); - auto& uis = UIState::GetInstance(); + auto& gs = GlobalStates::GetInstance(); if (ImGui::BeginTable("", kColumns, ImGuiTableFlags_Borders)) { @@ -241,19 +240,19 @@ void ItemListEditor(ItemList<T>& list) void UI::ItemsTab() { auto ls = LocaleStrings::Instance.get(); - auto& uis = UIState::GetInstance(); + auto& gs = GlobalStates::GetInstance(); if (ImGui::BeginTabBar("ItemViewTabs")) { if (ImGui::BeginTabItem(ls->ProductCategoryName.Get())) { - ItemListEditor(uis.CurrentProject->Products); + ItemListEditor(gs.GetCurrentProject()->Products); ImGui::EndTabItem(); } if (ImGui::BeginTabItem(ls->FactoryCategoryName.Get())) { - ItemListEditor(uis.CurrentProject->Factories); + ItemListEditor(gs.GetCurrentProject()->Factories); ImGui::EndTabItem(); } if (ImGui::BeginTabItem(ls->CustomerCategoryName.Get())) { - ItemListEditor(uis.CurrentProject->Customers); + ItemListEditor(gs.GetCurrentProject()->Customers); ImGui::EndTabItem(); } ImGui::EndTabBar(); diff --git a/core/src/UI/UI_MainWindow.cpp b/core/src/UI/UI_MainWindow.cpp index 1838aec..18f6731 100644 --- a/core/src/UI/UI_MainWindow.cpp +++ b/core/src/UI/UI_MainWindow.cpp @@ -3,7 +3,6 @@ #include "Model/GlobalStates.hpp" #include "Model/Project.hpp" #include "UI/Localization.hpp" -#include "UI/States.hpp" #include <IconsFontAwesome.h> #include <imgui.h> @@ -19,10 +18,9 @@ void ProjectTab_Normal() { auto ls = LocaleStrings::Instance.get(); auto& gs = GlobalStates::GetInstance(); - auto& uis = UIState::GetInstance(); if (ImGui::Button(ls->Close.Get())) { - uis.CloseCurrentProject(); + gs.SetCurrentProject(nullptr); return; } ImGui::SameLine(); @@ -30,15 +28,14 @@ void ProjectTab_Normal() // TODO } - ImGui::Text("%s%s", ls->ActiveProjectName.Get(), uis.CurrentProject->GetName().c_str()); - ImGui::Text("%s%s", ls->ActiveProjectPath.Get(), uis.CurrentProject->GetPathString().c_str()); + ImGui::Text("%s%s", ls->ActiveProjectName.Get(), gs.GetCurrentProject()->GetName().c_str()); + ImGui::Text("%s%s", ls->ActiveProjectPath.Get(), gs.GetCurrentProject()->GetPathString().c_str()); } void ProjectTab_NoProject() { auto ls = LocaleStrings::Instance.get(); auto& gs = GlobalStates::GetInstance(); - auto& uis = UIState::GetInstance(); bool openedDummy = true; bool openErrorDialog = false; @@ -90,7 +87,7 @@ void ProjectTab_NoProject() if (ImGui::Button(ls->DialogConfirm.Get(), !dirNameIsValid || projectName.empty())) { ImGui::CloseCurrentPopup(); - uis.SetCurrentProject(std::make_unique<Project>(std::move(dirPath), std::move(projectName))); + gs.SetCurrentProject(std::make_unique<Project>(std::move(dirPath), std::move(projectName))); // Dialog just got closed, reset states projectName.clear(); @@ -115,7 +112,7 @@ void ProjectTab_NoProject() try { // Project's constructor wants a path to directory containing cplt_project.json - uis.SetCurrentProject(std::make_unique<Project>(path.parent_path())); + gs.SetCurrentProject(std::make_unique<Project>(path.parent_path())); openErrorDialog = false; } catch (const std::exception& e) { openErrorDialog = true; @@ -151,7 +148,7 @@ void ProjectTab_NoProject() ImGui::SameLine(); if (ImGui::Button(ICON_FA_FOLDER_OPEN)) { try { - uis.SetCurrentProject(std::make_unique<Project>(path)); + gs.SetCurrentProject(std::make_unique<Project>(path)); openErrorDialog = false; } catch (const std::exception& e) { openErrorDialog = true; @@ -191,7 +188,7 @@ void ProjectTab_NoProject() void UI::MainWindow() { auto ls = LocaleStrings::Instance.get(); - auto& uis = UIState::GetInstance(); + auto& gs = GlobalStates::GetInstance(); auto windowSize = ImGui::GetMainViewport()->Size; ImGui::SetNextWindowSize({ windowSize.x, windowSize.y }); @@ -204,14 +201,14 @@ void UI::MainWindow() } if (ImGui::BeginTabItem(ls->ProjectTab.Get(), nullptr)) { - if (uis.CurrentProject) { + if (gs.HasCurrentProject()) { ProjectTab_Normal(); } else { ProjectTab_NoProject(); } ImGui::EndTabItem(); } - if (!uis.CurrentProject) { + if (!gs.HasCurrentProject()) { // No project open, simply skip all project specific tabs goto endTab; } diff --git a/core/src/UI/UI_Templates.cpp b/core/src/UI/UI_Templates.cpp index cf53461..4f6e69e 100644 --- a/core/src/UI/UI_Templates.cpp +++ b/core/src/UI/UI_Templates.cpp @@ -1,10 +1,10 @@ #include "UI.hpp" +#include "Model/GlobalStates.hpp" #include "Model/Project.hpp" #include "Model/Template/TableTemplate.hpp" #include "Model/Template/Template.hpp" #include "UI/Localization.hpp" -#include "UI/States.hpp" #include <imgui.h> #include <imgui_extra_math.h> @@ -141,8 +141,8 @@ struct DrawTemplateList_State void DrawTemplateList(DrawTemplateList_State& state) { - auto& uis = UIState::GetInstance(); - auto& templates = uis.CurrentProject->GetTemplates(); + auto& gs = GlobalStates::GetInstance(); + auto& templates = gs.GetCurrentProject()->GetTemplates(); for (auto& info : templates) { if (ImGui::Selectable(info.Name.c_str(), state.SelectedTemplate == &info)) { @@ -160,7 +160,7 @@ void DrawTemplateList(DrawTemplateList_State& state) void UI::TemplatesTab() { auto ls = LocaleStrings::Instance.get(); - auto& uis = UIState::GetInstance(); + auto& gs = GlobalStates::GetInstance(); bool openedDummy = true; static std::unique_ptr<TemplateUI> openTemplate; @@ -210,7 +210,7 @@ void UI::TemplatesTab() newNameError = NameSelectionError::Empty; } - auto& templates = uis.CurrentProject->GetTemplates(); + auto& templates = gs.GetCurrentProject()->GetTemplates(); if (templates.find(newName) != templates.end()) { newNameError = NameSelectionError::Duplicated; } @@ -257,7 +257,7 @@ void UI::TemplatesTab() } if (ImGui::Button(ls->DialogConfirm.Get(), IsInputValid())) { - auto& project = *uis.CurrentProject; + auto& project = *gs.GetCurrentProject(); project.InsertTemplate( newName, @@ -290,7 +290,7 @@ void UI::TemplatesTab() } if (ImGui::Button(ls->DialogConfirm.Get(), IsInputValid())) { - auto& project = *uis.CurrentProject; + auto& project = *gs.GetCurrentProject(); project.RenameTemplate( state.SelectedTemplate->Name, @@ -316,7 +316,7 @@ void UI::TemplatesTab() assert(state.SelectedTemplate != nullptr); if (ImGui::Button(ls->DialogConfirm.Get())) { - uis.CurrentProject->RemoveTemplate(state.SelectedTemplate->Name); + gs.GetCurrentProject()->RemoveTemplate(state.SelectedTemplate->Name); } ImGui::SameLine(); if (ImGui::Button(ls->DialogCancel.Get())) { diff --git a/core/src/UI/UI_Workflows.cpp b/core/src/UI/UI_Workflows.cpp index ac023c9..51cc4f8 100644 --- a/core/src/UI/UI_Workflows.cpp +++ b/core/src/UI/UI_Workflows.cpp @@ -1,5 +1,6 @@ #include "UI.hpp" +#include "Model/GlobalStates.hpp" #include "Model/Project.hpp" #include "Model/Workflow/Nodes/DocumentNodes.hpp" #include "Model/Workflow/Nodes/NumericNodes.hpp" @@ -7,7 +8,6 @@ #include "Model/Workflow/Nodes/UserInputNodes.hpp" #include "Model/Workflow/Workflow.hpp" #include "UI/Localization.hpp" -#include "UI/States.hpp" #include "Utils/Macros.hpp" #include <imgui.h> @@ -366,8 +366,8 @@ struct DrawTemplateList_State void DrawTemplateList(DrawTemplateList_State& state) { - auto& uis = UIState::GetInstance(); - auto& workflows = uis.CurrentProject->GetWorkflows(); + auto& gs = GlobalStates::GetInstance(); + auto& workflows = gs.GetCurrentProject()->GetWorkflows(); // TODO sort the list for (auto& info : workflows) { @@ -386,7 +386,7 @@ void DrawTemplateList(DrawTemplateList_State& state) void UI::WorkflowsTab() { auto ls = LocaleStrings::Instance.get(); - auto& uis = UIState::GetInstance(); + auto& gs = GlobalStates::GetInstance(); bool openedDummy = true; static std::unique_ptr<WorkflowUI> openWorkflow; @@ -439,14 +439,14 @@ void UI::WorkflowsTab() newNameError = NameSelectionError::Empty; } - auto& workflows = uis.CurrentProject->GetWorkflows(); + auto& workflows = gs.GetCurrentProject()->GetWorkflows(); if (workflows.find(newName) != workflows.end()) { newNameError = NameSelectionError::Duplicated; } } if (ImGui::Button(ls->DialogConfirm.Get(), newName.empty())) { - auto& project = uis.CurrentProject; + auto project = gs.GetCurrentProject(); project->RenameWorkflow(state.SelectedWorkflow->Name, newName); state.SelectedWorkflow = &project->GetWorkflows().at(newName); } @@ -473,7 +473,7 @@ void UI::WorkflowsTab() } if (ImGui::BeginPopupModal("Delete confirmation")) { if (ImGui::Button(ls->DialogConfirm.Get())) { - uis.CurrentProject->RemoveWorkflow(state.SelectedWorkflow->Name); + gs.GetCurrentProject()->RemoveWorkflow(state.SelectedWorkflow->Name); } ImGui::SameLine(); if (ImGui::Button(ls->DialogCancel.Get())) { diff --git a/core/src/UI/fwd.hpp b/core/src/UI/fwd.hpp index 45facbe..9b88370 100644 --- a/core/src/UI/fwd.hpp +++ b/core/src/UI/fwd.hpp @@ -3,9 +3,6 @@ // Localization.hpp class LocaleStrings; -// States.hpp -class UIState; - // UI.hpp namespace ImGui { enum class IconType; |