#include "UI.hpp" #include "Model/GlobalStates.hpp" #include "Model/Project.hpp" #include "UI/Localization.hpp" #include "UI/States.hpp" #include #include #include #include #include namespace fs = std::filesystem; namespace { void LoadProjectAt(const std::filesystem::path& path) { auto& uis = UIState::GetInstance(); auto& gs = GlobalStates::GetInstance(); if (uis.CurrentProject) { uis.CloseCurrentProject(); } } void ProjectTab_Normal() { // TODO } void ProjectTab_NoProject() { auto ls = LocaleStrings::Instance.get(); auto& gs = GlobalStates::GetInstance(); auto& uis = UIState::GetInstance(); static std::string projectName; static std::string dirName; static fs::path dirPath; static bool dirNameIsValid = false; if (ImGui::Button(ls->NewProject.Get())) { auto vs = ImGui::GetMainViewport()->Size; // Viewport Size ImGui::SetNextWindowSize({ vs.x * 0.5f, vs.y * 0.5f }); ImGui::SetNextWindowPos({ vs.x / 2, vs.y / 2 }, ImGuiCond_Always, { 0.5f, 0.5f }); // Center window initially ImGui::OpenPopup(ls->TitleNewProject.Get()); } // Make it so that the modal dialog has a close button bool newProjectDialogDummyTrue = true; if (ImGui::BeginPopupModal(ls->TitleNewProject.Get(), &newProjectDialogDummyTrue)) { ImGui::InputTextWithHint("##ProjectName", ls->HintNewProjectName.Get(), &projectName); if (ImGui::InputTextWithHint("##ProjectPath", ls->HintNewProjectPath.Get(), &dirName)) { // Changed, validate value fs::path newPath(dirName); if (fs::exists(newPath)) { dirNameIsValid = true; dirPath = std::move(newPath); } else { dirNameIsValid = false; } } ImGui::SameLine(); if (ImGui::Button("...")) { // TODO file dialog } if (projectName.empty()) { ImGui::ErrorIcon(); ImGui::SameLine(); ImGui::Text(ls->ErrorNewProjectEmptyName.Get()); } if (!dirNameIsValid) { ImGui::ErrorIcon(); ImGui::SameLine(); ImGui::Text(ls->ErrorNewProjectInvalidPath.Get()); } ImGui::Spacing(); bool formValid = dirNameIsValid && !projectName.empty(); if (!formValid) { ImGui::PushItemFlag(ImGuiItemFlags_Disabled, false); ImGui::PushStyleVar(ImGuiStyleVar_Alpha, 0.5f * ImGui::GetStyle().Alpha); } if (ImGui::Button(ls->ActionNewProjectConfirm.Get())) { ImGui::CloseCurrentPopup(); auto project = Project::Create(std::move(projectName), dirPath); auto uptr = std::unique_ptr(new Project(std::move(project))); uis.SetCurrentProject(std::move(uptr)); // Dialog just got closed, reset states projectName = ""; dirName = ""; dirPath = fs::path{}; dirNameIsValid = false; } if (!formValid) { ImGui::PopItemFlag(); ImGui::PopStyleVar(); } ImGui::SameLine(); if (ImGui::Button(ls->ActionNewProjectCancel.Get())) { ImGui::CloseCurrentPopup(); } ImGui::EndPopup(); } if (ImGui::Button(ls->OpenProject.Get())) { // TODO } ImGui::Separator(); ImGui::Text(ls->RecentProjects.Get()); ImGui::SameLine(); if (ImGui::Button(ls->ActionClearRecentProjects.Get())) { gs.ClearRecentProjects(); } auto& recentProjects = gs.GetRecentProjects(); if (recentProjects.empty()) { ImGui::Text(ls->MessageNoRecentProjects.Get()); } for (auto it = recentProjects.begin(); it != recentProjects.end(); ++it) { auto& [path, recent] = *it; ImGui::Text(recent.c_str()); ImGui::SameLine(); if (ImGui::Button(ICON_FA_EDIT)) { LoadProjectAt(path); } if (ImGui::IsItemHovered()) { ImGui::SetTooltip(ls->TooltipOpenRecentProject.Get()); } ImGui::SameLine(); if (ImGui::Button(ICON_FA_TRASH)) { gs.RemoveRecentProject(std::distance(recentProjects.begin(), it)); } if (ImGui::IsItemHovered()) { ImGui::SetTooltip(ls->TooltipDeleteRecentProject.Get()); } } } } // namespace void UI::MainWindow() { auto ls = LocaleStrings::Instance.get(); auto& uis = UIState::GetInstance(); auto windowSize = ImGui::GetMainViewport()->Size; ImGui::SetNextWindowSize({ windowSize.x, windowSize.y }); ImGui::SetNextWindowPos({ 0, 0 }); ImGui::Begin("##MainWindow", nullptr, ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoResize); if (ImGui::BeginTabBar("##MainWindowTabs")) { if (ImGui::BeginTabItem(ls->TabSettings.Get())) { UI::SettingsTab(); ImGui::EndTabItem(); } if (ImGui::BeginTabItem(ls->TabProject.Get(), nullptr, ImGuiTabItemFlags_SetSelected)) { if (uis.CurrentProject) { ProjectTab_Normal(); } else { ProjectTab_NoProject(); } ImGui::EndTabItem(); } if (!uis.CurrentProject) { // No project open, simply skip all project specific tabs goto endTab; } if (ImGui::BeginTabItem(ls->TabDatabaseView.Get())) { UI::DatabaseViewTab(); ImGui::EndTabItem(); } if (ImGui::BeginTabItem(ls->TabItems.Get())) { UI::ItemsTab(); ImGui::EndTabItem(); } if (ImGui::BeginTabItem(ls->TabExport.Get())) { UI::ExportTab(); ImGui::EndTabItem(); } endTab: ImGui::EndTabBar(); } ImGui::End(); }