From 1f5f8ee4c74dd331fed106365eb864e37bbf6239 Mon Sep 17 00:00:00 2001 From: Christian Heinemann Date: Thu, 10 Oct 2024 18:52:13 +0200 Subject: [PATCH] + bugfix for asynchronous login + use singleton macro --- source/Base/Definitions.h | 16 ++++ source/Gui/ActivateUserDialog.cpp | 4 +- source/Gui/AlienImGui.cpp | 40 ++++----- source/Gui/BrowserWindow.cpp | 26 +++--- source/Gui/CreateUserDialog.cpp | 2 +- source/Gui/DelayedExecutionController.cpp | 7 -- source/Gui/DelayedExecutionController.h | 6 +- source/Gui/DeleteUserDialog.cpp | 6 +- source/Gui/FileTransferController.cpp | 6 +- source/Gui/GenericFileDialogs.cpp | 6 -- source/Gui/GenericFileDialogs.h | 3 +- source/Gui/GenomeEditorWindow.cpp | 10 +-- source/Gui/GettingStartedWindow.cpp | 16 ++-- source/Gui/ImageToPatternDialog.cpp | 2 +- source/Gui/InspectorWindow.cpp | 10 +-- source/Gui/LogWindow.cpp | 4 +- source/Gui/LoginController.cpp | 98 +++++++++++++++++------ source/Gui/LoginController.h | 18 ++++- source/Gui/LoginDialog.cpp | 58 +++++--------- source/Gui/LoginDialog.h | 6 -- source/Gui/MainWindow.cpp | 12 +-- source/Gui/MessageDialog.cpp | 8 +- source/Gui/MessageDialog.h | 6 +- source/Gui/MultiplierWindow.cpp | 2 +- source/Gui/NewPasswordDialog.cpp | 4 +- source/Gui/OverlayMessageController.cpp | 2 +- source/Gui/PatternAnalysisDialog.cpp | 4 +- source/Gui/PatternEditorWindow.cpp | 10 +-- source/Gui/ResetPasswordDialog.cpp | 2 +- source/Gui/SelectionWindow.cpp | 6 +- source/Gui/SimulationParametersWindow.cpp | 8 +- source/Gui/SimulationView.cpp | 10 +-- source/Gui/SpatialControlWindow.cpp | 6 +- source/Gui/StartupController.cpp | 4 +- source/Gui/StyleRepository.cpp | 6 -- source/Gui/StyleRepository.h | 9 +-- source/Gui/TemporalControlWindow.cpp | 6 +- source/Gui/UploadSimulationDialog.cpp | 4 +- source/Network/NetworkService.cpp | 2 +- source/PersisterImpl/PersisterWorker.cpp | 5 +- 40 files changed, 242 insertions(+), 218 deletions(-) diff --git a/source/Base/Definitions.h b/source/Base/Definitions.h index 1a8673271..368fb7d9f 100644 --- a/source/Base/Definitions.h +++ b/source/Base/Definitions.h @@ -69,3 +69,19 @@ inline uint8_t toUInt8(T const& value) _##name = std::move(name); \ return *this; \ } + +#define MAKE_SINGLETON(ClassName) \ + public: \ + static ClassName& get() \ + { \ + static ClassName instance; \ + return instance; \ + } \ + \ + private: \ + ClassName() = default; \ + ~ClassName() = default; \ + ClassName(ClassName const&) = delete; \ + ClassName& operator=(ClassName const&) = delete; \ + ClassName(ClassName&&) = delete; \ + ClassName& operator=(ClassName&&) = delete \ diff --git a/source/Gui/ActivateUserDialog.cpp b/source/Gui/ActivateUserDialog.cpp index af37fc5c0..e30e74301 100644 --- a/source/Gui/ActivateUserDialog.cpp +++ b/source/Gui/ActivateUserDialog.cpp @@ -85,9 +85,9 @@ void _ActivateUserDialog::onActivateUser() result |= NetworkService::login(errorCode, _userName, _password, _userInfo); } if (!result) { - MessageDialog::getInstance().information("Error", "An error occurred on the server. Your entered code may be incorrect.\nPlease try to register again."); + MessageDialog::get().information("Error", "An error occurred on the server. Your entered code may be incorrect.\nPlease try to register again."); } else { - MessageDialog::getInstance().information( + MessageDialog::get().information( "Information", "The user '" + _userName + "' has been successfully created.\nYou are logged in and are now able to upload your own simulations\nor upvote others by likes."); diff --git a/source/Gui/AlienImGui.cpp b/source/Gui/AlienImGui.cpp index 08a79c136..3fa689534 100644 --- a/source/Gui/AlienImGui.cpp +++ b/source/Gui/AlienImGui.cpp @@ -112,8 +112,8 @@ bool AlienImGui::SliderFloat2(SliderFloat2Parameters const& parameters, float& v void AlienImGui::SliderInputFloat(SliderInputFloatParameters const& parameters, float& value) { - auto textWidth = StyleRepository::getInstance().scale(parameters._textWidth); - auto inputWidth = StyleRepository::getInstance().scale(parameters._inputWidth); + auto textWidth = StyleRepository::get().scale(parameters._textWidth); + auto inputWidth = StyleRepository::get().scale(parameters._inputWidth); ImGui::SetNextItemWidth( ImGui::GetContentRegionAvail().x - textWidth - inputWidth @@ -217,7 +217,7 @@ bool AlienImGui::InputOptionalInt(InputIntParameters const& parameters, std::opt bool AlienImGui::InputFloat(InputFloatParameters const& parameters, float& value) { - auto textWidth = StyleRepository::getInstance().scale(parameters._textWidth); + auto textWidth = StyleRepository::get().scale(parameters._textWidth); ImGuiInputTextFlags flags = parameters._readOnly ? ImGuiInputTextFlags_ReadOnly : ImGuiInputTextFlags_None; ImGui::SetNextItemWidth(ImGui::GetContentRegionAvail().x - textWidth); @@ -242,7 +242,7 @@ bool AlienImGui::InputFloat(InputFloatParameters const& parameters, float& value void AlienImGui::InputFloat2(InputFloat2Parameters const& parameters, float& value1, float& value2) { - auto textWidth = StyleRepository::getInstance().scale(parameters._textWidth); + auto textWidth = StyleRepository::get().scale(parameters._textWidth); ImGuiInputTextFlags flags = parameters._readOnly ? ImGuiInputTextFlags_ReadOnly : ImGuiInputTextFlags_None; ImGui::SetNextItemWidth(ImGui::GetContentRegionAvail().x - textWidth); @@ -333,7 +333,7 @@ bool AlienImGui::InputText(InputTextParameters const& parameters, char* buffer, auto width = parameters._width != 0.0f ? scale(parameters._width) : ImGui::GetContentRegionAvail().x; ImGui::SetNextItemWidth(width - scale(parameters._textWidth)); if (parameters._monospaceFont) { - ImGui::PushFont(StyleRepository::getInstance().getMonospaceMediumFont()); + ImGui::PushFont(StyleRepository::get().getMonospaceMediumFont()); } ImGuiInputTextFlags flags = 0; if (parameters._readOnly) { @@ -385,10 +385,10 @@ void AlienImGui::InputTextMultiline(InputTextMultilineParameters const& paramete static char buffer[1024*16]; StringHelper::copy(buffer, IM_ARRAYSIZE(buffer), text); - auto textWidth = StyleRepository::getInstance().scale(parameters._textWidth); + auto textWidth = StyleRepository::get().scale(parameters._textWidth); auto height = parameters._height == 0 ? ImGui::GetContentRegionAvail().y - : StyleRepository::getInstance().scale(parameters._height); + : StyleRepository::get().scale(parameters._height); auto id = parameters._hint.empty() ? ("##" + parameters._name).c_str() : ("##" + parameters._hint).c_str(); ImGui::InputTextEx( ("##" + parameters._name).c_str(), @@ -418,7 +418,7 @@ namespace bool AlienImGui::Combo(ComboParameters& parameters, int& value, bool* enabled) { - auto textWidth = StyleRepository::getInstance().scale(parameters._textWidth); + auto textWidth = StyleRepository::get().scale(parameters._textWidth); const char** items = new const char*[parameters._values.size()]; for (int i = 0; i < parameters._values.size(); ++i) { @@ -710,14 +710,14 @@ void AlienImGui::Text(std::string const& text) void AlienImGui::BoldText(std::string const& text) { - ImGui::PushFont(StyleRepository::getInstance().getSmallBoldFont()); + ImGui::PushFont(StyleRepository::get().getSmallBoldFont()); AlienImGui::Text(text); ImGui::PopFont(); } void AlienImGui::MonospaceText(std::string const& text) { - ImGui::PushFont(StyleRepository::getInstance().getMonospaceMediumFont()); + ImGui::PushFont(StyleRepository::get().getMonospaceMediumFont()); ImGui::PushStyleColor(ImGuiCol_Text, (ImVec4)Const::MonospaceColor); Text(text); ImGui::PopStyleColor(); @@ -818,7 +818,7 @@ void AlienImGui::ColorButtonWithPicker(ColorButtonWithPickerParameters const& pa ("##" + parameters._name).c_str(), imGuiColor, ImGuiColorEditFlags_NoBorder, - {ImGui::GetContentRegionAvail().x - StyleRepository::getInstance().scale(parameters._textWidth), 0}); + {ImGui::GetContentRegionAvail().x - StyleRepository::get().scale(parameters._textWidth), 0}); if (openColorPicker) { ImGui::OpenPopup("colorpicker"); imGuiBackupColor = imGuiColor; @@ -923,7 +923,7 @@ bool AlienImGui::ToolbarButton(std::string const& text) { auto id = std::to_string(ImGui::GetID(text.c_str())); - ImGui::PushFont(StyleRepository::getInstance().getIconFont()); + ImGui::PushFont(StyleRepository::get().getIconFont()); ImGui::PushStyleVar(ImGuiStyleVar_ButtonTextAlign, {0.5f, 0.75f}); auto color = Const::ToolbarButtonTextColor; float h, s, v; @@ -947,7 +947,7 @@ bool AlienImGui::SelectableToolbarButton(std::string const& text, int& value, in { auto id = std::to_string(ImGui::GetID(text.c_str())); - ImGui::PushFont(StyleRepository::getInstance().getIconFont()); + ImGui::PushFont(StyleRepository::get().getIconFont()); ImGui::PushStyleVar(ImGuiStyleVar_ButtonTextAlign, {0.5f, 0.75f}); auto color = Const::ToolbarButtonTextColor; float h, s, v; @@ -1036,7 +1036,7 @@ bool AlienImGui::BeginTreeNode(TreeNodeParameters const& parameters) } ImGuiTreeNodeFlags treeNodeClosedFlags = ImGuiTreeNodeFlags_FramePadding | ImGuiTreeNodeFlags_Framed; ImGuiTreeNodeFlags treeNodeOpenFlags = treeNodeClosedFlags | ImGuiTreeNodeFlags_DefaultOpen; - ImGui::PushFont(StyleRepository::getInstance().getSmallBoldFont()); + ImGui::PushFont(StyleRepository::get().getSmallBoldFont()); bool result = ImGui::TreeNodeEx(parameters._text.c_str(), parameters._defaultOpen ? treeNodeOpenFlags : treeNodeClosedFlags); ImGui::PopFont(); ImGui::PopStyleColor(3); @@ -1050,7 +1050,7 @@ void AlienImGui::EndTreeNode() bool AlienImGui::Button(ButtonParameters const& parameters) { - auto width = ImGui::GetContentRegionAvail().x - StyleRepository::getInstance().scale(parameters._textWidth); + auto width = ImGui::GetContentRegionAvail().x - StyleRepository::get().scale(parameters._textWidth); auto result = ImGui::Button(parameters._buttonText.c_str(), {width, 0}); ImGui::SameLine(); @@ -1150,8 +1150,8 @@ bool AlienImGui::ShowPreviewDescription(PreviewDescription const& desc, float& z auto drawTextWithShadow = [&drawList, &cellSize](std::string const& text, float posX, float posY) { drawList->AddText( - StyleRepository::getInstance().getLargeFont(), cellSize / 2, {posX + 1.0f, posY + 1.0f}, Const::ExecutionNumberOverlayShadowColor, text.c_str()); - drawList->AddText(StyleRepository::getInstance().getLargeFont(), cellSize / 2, {posX, posY}, Const::ExecutionNumberOverlayColor, text.c_str()); + StyleRepository::get().getLargeFont(), cellSize / 2, {posX + 1.0f, posY + 1.0f}, Const::ExecutionNumberOverlayShadowColor, text.c_str()); + drawList->AddText(StyleRepository::get().getLargeFont(), cellSize / 2, {posX, posY}, Const::ExecutionNumberOverlayColor, text.c_str()); }; auto result = false; @@ -1229,7 +1229,7 @@ bool AlienImGui::ShowPreviewDescription(PreviewDescription const& desc, float& z case SymbolPreviewDescription::Type::Infinity: { if (zoom > ZoomLevelForConnections) { drawList->AddText( - StyleRepository::getInstance().getIconFont(), + StyleRepository::get().getIconFont(), cellSize / 2, {pos.x - cellSize * 0.4f, pos.y - cellSize * 0.2f}, Const::GenomePreviewInfinitySymbolColor, @@ -1323,7 +1323,7 @@ bool AlienImGui::ShowPreviewDescription(PreviewDescription const& desc, float& z } if (cell.selfReplicator) { drawList->AddText( - StyleRepository::getInstance().getIconFont(), + StyleRepository::get().getIconFont(), cellSize / 4, {cellPos.x - length * 2, cellPos.y + length}, Const::GenomePreviewSelfReplicatorColor, @@ -1839,7 +1839,7 @@ void AlienImGui::BasicInputColorMatrix(BasicInputColorMatrixParameters const& _basicSilderExpanded.insert(toggleButtonId); } } - auto textWidth = StyleRepository::getInstance().scale(parameters._textWidth); + auto textWidth = StyleRepository::get().scale(parameters._textWidth); ImGui::SameLine(); diff --git a/source/Gui/BrowserWindow.cpp b/source/Gui/BrowserWindow.cpp index 57a24dfab..2ed5e0c74 100644 --- a/source/Gui/BrowserWindow.cpp +++ b/source/Gui/BrowserWindow.cpp @@ -426,7 +426,7 @@ void _BrowserWindow::processUserList() ImGui::PushID("User list"); - auto& styleRepository = StyleRepository::getInstance(); + auto& styleRepository = StyleRepository::get(); static ImGuiTableFlags flags = ImGuiTableFlags_Resizable | ImGuiTableFlags_Reorderable | ImGuiTableFlags_Hideable | ImGuiTableFlags_RowBg | ImGuiTableFlags_BordersOuter | ImGuiTableFlags_BordersV | ImGuiTableFlags_NoBordersInBody | ImGuiTableFlags_ScrollY | ImGuiTableFlags_ScrollX; @@ -759,7 +759,7 @@ bool _BrowserWindow::processResourceNameField(NetworkResourceTreeTO const& treeT ImGui::SameLine(); if (!isOwner(treeTO) && _lastSessionData.isNew(leaf.rawTO)) { - auto font = StyleRepository::getInstance().getSmallBoldFont(); + auto font = StyleRepository::get().getSmallBoldFont(); auto origSize = font->Scale; font->Scale *= 0.65f; ImGui::PushFont(font); @@ -1127,7 +1127,7 @@ void _BrowserWindow::processShortenedText(std::string const& text, bool bold) { if (substrings.empty()) { return; } - auto& styleRepository = StyleRepository::getInstance(); + auto& styleRepository = StyleRepository::get(); auto textSize = ImGui::CalcTextSize(substrings.at(0).c_str()); auto needDetailButton = textSize.x > ImGui::GetContentRegionAvail().x || substrings.size() > 1; auto cursorPos = ImGui::GetCursorPosX() + ImGui::GetContentRegionAvail().x - styleRepository.scale(15.0f); @@ -1231,7 +1231,7 @@ void _BrowserWindow::processPendingRequestIds() auto criticalErrors = _persisterController->fetchAllErrorInfos(SenderId{BrowserSenderId}); if (!criticalErrors.empty()) { - MessageDialog::getInstance().information("Error", criticalErrors); + MessageDialog::get().information("Error", criticalErrors); } } @@ -1276,7 +1276,7 @@ void _BrowserWindow::onDownloadResource(BrowserLeaf const& leaf) SerializedSimulation serializedSim; if (!cachedSimulation.has_value()) { if (!NetworkService::downloadResource(serializedSim.mainData, serializedSim.auxiliaryData, serializedSim.statistics, leaf.rawTO->id)) { - MessageDialog::getInstance().information("Error", "Failed to download " + dataTypeString + "."); + MessageDialog::get().information("Error", "Failed to download " + dataTypeString + "."); return; } } @@ -1285,7 +1285,7 @@ void _BrowserWindow::onDownloadResource(BrowserLeaf const& leaf) DeserializedSimulation deserializedSim; if (!cachedSimulation.has_value()) { if (!SerializerService::deserializeSimulationFromStrings(deserializedSim, serializedSim)) { - MessageDialog::getInstance().information("Error", "Failed to load simulation. Your program version may not match."); + MessageDialog::get().information("Error", "Failed to load simulation. Your program version may not match."); return; } _simulationCache.insertOrAssign(leaf.rawTO->id, deserializedSim); @@ -1330,14 +1330,14 @@ void _BrowserWindow::onDownloadResource(BrowserLeaf const& leaf) } else { std::vector genome; if (!SerializerService::deserializeGenomeFromString(genome, serializedSim.mainData)) { - MessageDialog::getInstance().information("Error", "Failed to load genome. Your program version may not match."); + MessageDialog::get().information("Error", "Failed to load genome. Your program version may not match."); return; } _editorController->setOn(true); _editorController->getGenomeEditorWindow()->openTab(GenomeDescriptionService::convertBytesToDescription(genome)); } if (VersionChecker::isVersionNewer(leaf.rawTO->version)) { - MessageDialog::getInstance().information( + MessageDialog::get().information( "Warning", "The download was successful but the " + dataTypeString +" was generated using a more recent\n" "version of ALIEN. Consequently, the " + dataTypeString + "might not function as expected.\n" @@ -1364,7 +1364,7 @@ void _BrowserWindow::onReplaceResource(BrowserLeaf const& leaf) SerializedSimulation serializedSim; if (!SerializerService::serializeSimulationToStrings(serializedSim, deserializedSim)) { - MessageDialog::getInstance().information("Replace simulation", "The simulation could not be serialized for replacing."); + MessageDialog::get().information("Replace simulation", "The simulation could not be serialized for replacing."); return; } mainData = serializedSim.mainData; @@ -1403,7 +1403,7 @@ void _BrowserWindow::onReplaceResource(BrowserLeaf const& leaf) onRefresh(); }); }; - MessageDialog::getInstance().yesNo("Delete", "Do you really want to replace the content of the selected item?", func); + MessageDialog::get().yesNo("Delete", "Do you really want to replace the content of the selected item?", func); } void _BrowserWindow::onEditResource(NetworkResourceTreeTO const& treeTO) @@ -1448,7 +1448,7 @@ void _BrowserWindow::onMoveResource(NetworkResourceTreeTO const& treeTO) delayedExecution([rawTOs = rawTOs, this] { for (auto const& rawTO : rawTOs) { if (!NetworkService::moveResource(rawTO->id, rawTO->workspaceType)) { - MessageDialog::getInstance().information("Error", "Failed to move item."); + MessageDialog::get().information("Error", "Failed to move item."); refreshIntern(true); return; } @@ -1463,7 +1463,7 @@ void _BrowserWindow::onDeleteResource(NetworkResourceTreeTO const& treeTO) auto rawTOs = NetworkResourceService::getMatchingRawTOs(treeTO, currentWorkspace.rawTOs); auto message = treeTO->isLeaf() ? "Do you really want to delete the selected item?" : "Do you really want to delete the selected folder?"; - MessageDialog::getInstance().yesNo("Delete", message, [rawTOs = rawTOs, this]() { + MessageDialog::get().yesNo("Delete", message, [rawTOs = rawTOs, this]() { //remove resources form workspace for (WorkspaceType workspaceType = 0; workspaceType < WorkspaceType_Count; ++workspaceType) { @@ -1482,7 +1482,7 @@ void _BrowserWindow::onDeleteResource(NetworkResourceTreeTO const& treeTO) delayedExecution([rawTOs = rawTOs, this] { for (auto const& rawTO : rawTOs) { if (!NetworkService::deleteResource(rawTO->id)) { - MessageDialog::getInstance().information("Error", "Failed to delete item. Please try again later."); + MessageDialog::get().information("Error", "Failed to delete item. Please try again later."); refreshIntern(true); return; } diff --git a/source/Gui/CreateUserDialog.cpp b/source/Gui/CreateUserDialog.cpp index 65ec8737a..26dc0f9f9 100644 --- a/source/Gui/CreateUserDialog.cpp +++ b/source/Gui/CreateUserDialog.cpp @@ -64,7 +64,7 @@ void _CreateUserDialog::onCreateUser() if (NetworkService::createUser(_userName, _password, _email)) { _activateUserDialog->open(_userName, _password, _userInfo); } else { - MessageDialog::getInstance().information( + MessageDialog::get().information( "Error", "An error occurred on the server. This could be related to the fact that\n" ICON_FA_CARET_RIGHT " your user name or email address is already in use,\n" ICON_FA_CARET_RIGHT " or your user " diff --git a/source/Gui/DelayedExecutionController.cpp b/source/Gui/DelayedExecutionController.cpp index 5fb30bfbc..fcfadbcf1 100644 --- a/source/Gui/DelayedExecutionController.cpp +++ b/source/Gui/DelayedExecutionController.cpp @@ -3,13 +3,6 @@ #include "AlienImGui.h" #include "DelayedExecutionController.h" - -DelayedExecutionController& DelayedExecutionController::getInstance() -{ - static DelayedExecutionController instance; - return instance; -} - void DelayedExecutionController::process() { std::vector delayedExecDatas; diff --git a/source/Gui/DelayedExecutionController.h b/source/Gui/DelayedExecutionController.h index 146688847..03a04e6a1 100644 --- a/source/Gui/DelayedExecutionController.h +++ b/source/Gui/DelayedExecutionController.h @@ -4,9 +4,9 @@ class DelayedExecutionController { -public: - static DelayedExecutionController& getInstance(); + MAKE_SINGLETON(DelayedExecutionController); +public: void process(); void executeLater(std::function const& execFunc); @@ -22,5 +22,5 @@ class DelayedExecutionController inline void delayedExecution(std::function const& execFunc) { - DelayedExecutionController::getInstance().executeLater(execFunc); + DelayedExecutionController::get().executeLater(execFunc); } \ No newline at end of file diff --git a/source/Gui/DeleteUserDialog.cpp b/source/Gui/DeleteUserDialog.cpp index 3476d7c23..8b6a530c2 100644 --- a/source/Gui/DeleteUserDialog.cpp +++ b/source/Gui/DeleteUserDialog.cpp @@ -31,7 +31,7 @@ void _DeleteUserDialog::processIntern() if (_reenteredPassword == *NetworkService::getPassword()) { onDelete(); } else { - MessageDialog::getInstance().information("Error", "The password does not match."); + MessageDialog::get().information("Error", "The password does not match."); } _reenteredPassword.clear(); } @@ -50,8 +50,8 @@ void _DeleteUserDialog::onDelete() auto userName = *NetworkService::getLoggedInUserName(); if (NetworkService::deleteUser()) { _browserWindow->onRefresh(); - MessageDialog::getInstance().information("Information", "The user '" + userName + "' has been deleted.\nYou are logged out."); + MessageDialog::get().information("Information", "The user '" + userName + "' has been deleted.\nYou are logged out."); } else { - MessageDialog::getInstance().information("Error", "An error occurred on the server."); + MessageDialog::get().information("Error", "An error occurred on the server."); } } diff --git a/source/Gui/FileTransferController.cpp b/source/Gui/FileTransferController.cpp index a2a37fb5f..b87773f1e 100644 --- a/source/Gui/FileTransferController.cpp +++ b/source/Gui/FileTransferController.cpp @@ -32,7 +32,7 @@ void FileTransferController::init( void FileTransferController::onOpenSimulation() { - GenericFileDialogs::getInstance().showOpenFileDialog( + GenericFileDialogs::get().showOpenFileDialog( "Open simulation", "Simulation file (*.sim){.sim},.*", _startingPath, [&](std::filesystem::path const& path) { auto firstFilename = ifd::FileDialog::Instance().GetResult(); auto firstFilenameCopy = firstFilename; @@ -48,7 +48,7 @@ void FileTransferController::onOpenSimulation() void FileTransferController::onSaveSimulation() { - GenericFileDialogs::getInstance().showSaveFileDialog( + GenericFileDialogs::get().showSaveFileDialog( "Save simulation", "Simulation file (*.sim){.sim},.*", _startingPath, [&](std::filesystem::path const& path) { auto firstFilename = ifd::FileDialog::Instance().GetResult(); auto firstFilenameCopy = firstFilename; @@ -111,6 +111,6 @@ void FileTransferController::process() auto criticalErrors = _persisterController->fetchAllErrorInfos(SenderId{FileTransferSenderId}); if (!criticalErrors.empty()) { - MessageDialog::getInstance().information("Error", criticalErrors); + MessageDialog::get().information("Error", criticalErrors); } } diff --git a/source/Gui/GenericFileDialogs.cpp b/source/Gui/GenericFileDialogs.cpp index f1a46571e..40862a689 100644 --- a/source/Gui/GenericFileDialogs.cpp +++ b/source/Gui/GenericFileDialogs.cpp @@ -6,12 +6,6 @@ #include "AlienImGui.h" #include "WindowController.h" -GenericFileDialogs& GenericFileDialogs::getInstance() -{ - static GenericFileDialogs instance; - return instance; -} - void GenericFileDialogs::showOpenFileDialog( std::string const& title, std::string const& filter, diff --git a/source/Gui/GenericFileDialogs.h b/source/Gui/GenericFileDialogs.h index 45d30b995..ee843f3c9 100644 --- a/source/Gui/GenericFileDialogs.h +++ b/source/Gui/GenericFileDialogs.h @@ -8,9 +8,8 @@ class GenericFileDialogs { + MAKE_SINGLETON(GenericFileDialogs); public: - static GenericFileDialogs& getInstance(); - void showOpenFileDialog( std::string const& title, std::string const& filter, diff --git a/source/Gui/GenomeEditorWindow.cpp b/source/Gui/GenomeEditorWindow.cpp index 950660f31..32d9a4bc5 100644 --- a/source/Gui/GenomeEditorWindow.cpp +++ b/source/Gui/GenomeEditorWindow.cpp @@ -460,7 +460,7 @@ void _GenomeEditorWindow::processConstructionSequence(TabData& tab) if (_expandNodes) { ImGui::SetNextItemOpen(*_expandNodes); } - ImGui::PushFont(StyleRepository::getInstance().getSmallBoldFont()); + ImGui::PushFont(StyleRepository::get().getSmallBoldFont()); auto treeNodeOpen = ImGui::TreeNodeEx((generateShortDescription(index, cell, shapeGeneratorResult, isFirstOrLast) + "###").c_str(), flags); ImGui::PopFont(); @@ -838,14 +838,14 @@ void _GenomeEditorWindow::processSubGenomeWidgets(TabData const& tab, Descriptio void _GenomeEditorWindow::onOpenGenome() { - GenericFileDialogs::getInstance().showOpenFileDialog("Open genome", "Genome (*.genome){.genome},.*", _startingPath, [&](std::filesystem::path const& path) { + GenericFileDialogs::get().showOpenFileDialog("Open genome", "Genome (*.genome){.genome},.*", _startingPath, [&](std::filesystem::path const& path) { auto firstFilename = ifd::FileDialog::Instance().GetResult(); auto firstFilenameCopy = firstFilename; _startingPath = firstFilenameCopy.remove_filename().string(); std::vector genomeData; if (!SerializerService::deserializeGenomeFromFile(genomeData, firstFilename.string())) { - MessageDialog::getInstance().information("Open genome", "The selected file could not be opened."); + MessageDialog::get().information("Open genome", "The selected file could not be opened."); } else { openTab(GenomeDescriptionService::convertBytesToDescription(genomeData), false); } @@ -854,7 +854,7 @@ void _GenomeEditorWindow::onOpenGenome() void _GenomeEditorWindow::onSaveGenome() { - GenericFileDialogs::getInstance().showSaveFileDialog( + GenericFileDialogs::get().showSaveFileDialog( "Save genome", "Genome (*.genome){.genome},.*", _startingPath, [&](std::filesystem::path const& path) { auto firstFilename = ifd::FileDialog::Instance().GetResult(); auto firstFilenameCopy = firstFilename; @@ -863,7 +863,7 @@ void _GenomeEditorWindow::onSaveGenome() auto const& selectedTab = _tabDatas.at(_selectedTabIndex); auto genomeData = GenomeDescriptionService::convertDescriptionToBytes(selectedTab.genome); if (!SerializerService::serializeGenomeToFile(firstFilename.string(), genomeData)) { - MessageDialog::getInstance().information("Save genome", "The selected file could not be saved."); + MessageDialog::get().information("Save genome", "The selected file could not be saved."); } }); } diff --git a/source/Gui/GettingStartedWindow.cpp b/source/Gui/GettingStartedWindow.cpp index 78b5dc294..6e17dbd40 100644 --- a/source/Gui/GettingStartedWindow.cpp +++ b/source/Gui/GettingStartedWindow.cpp @@ -382,7 +382,7 @@ void _GettingStartedWindow::processIntern() //ImGui::Dummy(ImVec2(0.0f, 20.0f)); - //ImGui::PushFont(StyleRepository::getInstance().getMonospaceMediumFont()); + //ImGui::PushFont(StyleRepository::get().getMonospaceMediumFont()); //auto windowWidth = ImGui::GetWindowSize().x; //auto weblink = "https://alien-project.gitbook.io/docs"; //auto textWidth = ImGui::CalcTextSize(weblink).x; @@ -406,46 +406,46 @@ void _GettingStartedWindow::drawTitle() { ImGui::PushStyleColor(ImGuiCol_Text, (ImU32)Const::HeadlineColor); - ImGui::PushFont(StyleRepository::getInstance().getMediumFont()); + ImGui::PushFont(StyleRepository::get().getMediumFont()); ImGui::Text("What is "); ImGui::PopFont(); ImGui::SameLine(); AlienImGui::NegativeSpacing(); - ImGui::PushFont(StyleRepository::getInstance().getMediumBoldFont()); + ImGui::PushFont(StyleRepository::get().getMediumBoldFont()); ImGui::Text("A"); ImGui::PopFont(); ImGui::SameLine(); AlienImGui::NegativeSpacing(); AlienImGui::NegativeSpacing(); - ImGui::PushFont(StyleRepository::getInstance().getMediumFont()); + ImGui::PushFont(StyleRepository::get().getMediumFont()); ImGui::Text("rtificial "); ImGui::PopFont(); ImGui::SameLine(); AlienImGui::NegativeSpacing(); - ImGui::PushFont(StyleRepository::getInstance().getMediumBoldFont()); + ImGui::PushFont(StyleRepository::get().getMediumBoldFont()); ImGui::Text("LI"); ImGui::PopFont(); ImGui::SameLine(); AlienImGui::NegativeSpacing(); AlienImGui::NegativeSpacing(); - ImGui::PushFont(StyleRepository::getInstance().getMediumFont()); + ImGui::PushFont(StyleRepository::get().getMediumFont()); ImGui::Text("fe "); ImGui::PopFont(); ImGui::SameLine(); AlienImGui::NegativeSpacing(); - ImGui::PushFont(StyleRepository::getInstance().getMediumBoldFont()); + ImGui::PushFont(StyleRepository::get().getMediumBoldFont()); ImGui::Text("EN"); ImGui::PopFont(); ImGui::SameLine(); AlienImGui::NegativeSpacing(); AlienImGui::NegativeSpacing(); - ImGui::PushFont(StyleRepository::getInstance().getMediumFont()); + ImGui::PushFont(StyleRepository::get().getMediumFont()); ImGui::Text("vironment ?"); ImGui::PopFont(); diff --git a/source/Gui/ImageToPatternDialog.cpp b/source/Gui/ImageToPatternDialog.cpp index 57a339e1d..a05f743ba 100644 --- a/source/Gui/ImageToPatternDialog.cpp +++ b/source/Gui/ImageToPatternDialog.cpp @@ -78,7 +78,7 @@ namespace void _ImageToPatternDialog::show() { - GenericFileDialogs::getInstance().showOpenFileDialog( + GenericFileDialogs::get().showOpenFileDialog( "Open image", "Image (*.png){.png},.*", _startingPath, [&](std::filesystem::path const& path) { auto firstFilename = ifd::FileDialog::Instance().GetResult(); diff --git a/source/Gui/InspectorWindow.cpp b/source/Gui/InspectorWindow.cpp index 3fc6ad7be..6b307af53 100644 --- a/source/Gui/InspectorWindow.cpp +++ b/source/Gui/InspectorWindow.cpp @@ -61,8 +61,8 @@ void _InspectorWindow::process() return; } auto width = calcWindowWidth(); - auto height = isCell() ? StyleRepository::getInstance().scale(370.0f) - : StyleRepository::getInstance().scale(70.0f); + auto height = isCell() ? StyleRepository::get().scale(370.0f) + : StyleRepository::get().scale(70.0f); auto borderlessRendering = _simController->getSimulationParameters().borderlessRendering; ImGui::SetNextWindowBgAlpha(Const::WindowAlpha * ImGui::GetStyle().Alpha); ImGui::SetNextWindowSize({width, height}, ImGuiCond_Appearing); @@ -77,7 +77,7 @@ void _InspectorWindow::process() } ImDrawList* drawList = ImGui::GetBackgroundDrawList(); auto entityPos = Viewport::mapWorldToViewPosition(DescriptionEditService::getPos(entity), borderlessRendering); - auto factor = StyleRepository::getInstance().scale(1); + auto factor = StyleRepository::get().scale(1); drawList->AddLine( {windowPos.x + 15.0f * factor, windowPos.y - 5.0f * factor}, @@ -755,9 +755,9 @@ void _InspectorWindow::processParticle(ParticleDescription particle) float _InspectorWindow::calcWindowWidth() const { if (isCell()) { - return StyleRepository::getInstance().scale(CellWindowWidth); + return StyleRepository::get().scale(CellWindowWidth); } else { - return StyleRepository::getInstance().scale(ParticleWindowWidth); + return StyleRepository::get().scale(ParticleWindowWidth); } } diff --git a/source/Gui/LogWindow.cpp b/source/Gui/LogWindow.cpp index c26139ebc..7affb8fab 100644 --- a/source/Gui/LogWindow.cpp +++ b/source/Gui/LogWindow.cpp @@ -24,10 +24,10 @@ _LogWindow::~_LogWindow() void _LogWindow::processIntern() { - auto& styleRepository = StyleRepository::getInstance(); + auto& styleRepository = StyleRepository::get(); if (ImGui::BeginChild( "##", ImVec2(0, ImGui::GetContentRegionAvail().y - styleRepository.scale(40.0f)), true, ImGuiWindowFlags_HorizontalScrollbar)) { - ImGui::PushFont(StyleRepository::getInstance().getMonospaceMediumFont()); + ImGui::PushFont(StyleRepository::get().getMonospaceMediumFont()); ImGui::PushStyleColor(ImGuiCol_Text, (ImVec4)Const::MonospaceColor); for (auto const& logMessage : _logger->getMessages(_verbose ? Priority::Unimportant : Priority::Important) | boost::adaptors::reversed) { diff --git a/source/Gui/LoginController.cpp b/source/Gui/LoginController.cpp index fd0a4522c..2e4ad6336 100644 --- a/source/Gui/LoginController.cpp +++ b/source/Gui/LoginController.cpp @@ -7,36 +7,33 @@ #include "PersisterInterface/SenderInfo.h" #include "MessageDialog.h" +#include "ActivateUserDialog.h" +#include "BrowserWindow.h" namespace { auto constexpr LoginSenderId = "Login"; } -LoginController& LoginController::get() -{ - static LoginController instance; - return instance; -} - -void LoginController::init(SimulationController const& simController, PersisterController const& persisterController) +void LoginController::init( + SimulationController const& simController, + PersisterController const& persisterController, + ActivateUserDialog const& activateUserDialog, + BrowserWindow const& browserWindow) { _simController = simController; _persisterController = persisterController; + _activateUserDialog = activateUserDialog; + _browserWindow = browserWindow; auto& settings = GlobalSettings::getInstance(); - _remember = settings.getBool("dialogs.login.remember", _remember); - _shareGpuInfo = settings.getBool("dialogs.login.share gpu info", _shareGpuInfo); + _remember = settings.getBool("controller.login.remember", _remember); + _shareGpuInfo = settings.getBool("controller.login.share gpu info", _shareGpuInfo); if (_remember) { - auto userName = settings.getString("dialogs.login.user name", ""); - auto password = settings.getString("dialogs.login.password", ""); - - if (!userName.empty()) { - persisterController->scheduleLogin( - SenderInfo{.senderId = SenderId{LoginSenderId}, .wishResultData = false, .wishErrorInfo = true}, - LoginRequestData{.userName = userName, .password = password, .userInfo = getUserInfo()}); - } + _userName = settings.getString("dialogs.login.user name", ""); + _password = settings.getString("dialogs.login.password", ""); + onLogin(); } } @@ -45,26 +42,55 @@ void LoginController::shutdown() saveSettings(); } +void LoginController::onLogin() +{ + if (!_userName.empty()) { + _pendingLoginRequestIds.emplace_back(_persisterController->scheduleLogin( + SenderInfo{.senderId = SenderId{LoginSenderId}, .wishResultData = true, .wishErrorInfo = true}, + LoginRequestData{.userName = _userName, .password = _password, .userInfo = getUserInfo()})); + if (!_remember) { + _userName.clear(); + _password.clear(); + } + } +} + void LoginController::process() { + std::vector newLoginRequestIds; + for (auto const& requestId : _pendingLoginRequestIds) { + auto state = _persisterController->getRequestState(requestId); + if (state == PersisterRequestState::Finished) { + auto const& data = _persisterController->fetchLoginData(requestId); + if (data.unknownUser) { + auto& settings = GlobalSettings::getInstance(); + auto userName = settings.getString("dialogs.login.user name", ""); + auto password = settings.getString("dialogs.login.password", ""); + _activateUserDialog->open(userName, password, getUserInfo()); + } + saveSettings(); + _browserWindow->onRefresh(); + } + if (state == PersisterRequestState::InQueue || state == PersisterRequestState::InProgress) { + newLoginRequestIds.emplace_back(requestId); + } + } + _pendingLoginRequestIds = newLoginRequestIds; + auto criticalErrors = _persisterController->fetchAllErrorInfos(SenderId{LoginSenderId}); if (!criticalErrors.empty()) { - MessageDialog::getInstance().information("Login failed", criticalErrors); + MessageDialog::get().information("Error", criticalErrors); } } void LoginController::saveSettings() { auto& settings = GlobalSettings::getInstance(); - settings.setBool("dialogs.login.remember", _remember); - settings.setBool("dialogs.login.share gpu info", _shareGpuInfo); + settings.setBool("controller.login.remember", _remember); + settings.setBool("controller.login.share gpu info", _shareGpuInfo); if (_remember) { - auto userName = NetworkService::getLoggedInUserName(); - auto password = NetworkService::getPassword(); - if (userName.has_value() && password.has_value()) { - settings.setString("dialogs.login.user name", *userName); - settings.setString("dialogs.login.password", *password); - } + settings.setString("dialogs.login.user name", _userName); + settings.setString("dialogs.login.password", _password); } } @@ -88,6 +114,26 @@ void LoginController::setRemember(bool value) _remember = value; } +std::string const& LoginController::getUserName() const +{ + return _userName; +} + +void LoginController::setUserName(std::string const& value) +{ + _userName = value; +} + +std::string const& LoginController::getPassword() const +{ + return _password; +} + +void LoginController::setPassword(std::string const& value) +{ + _password = value; +} + UserInfo LoginController::getUserInfo() { UserInfo result; diff --git a/source/Gui/LoginController.h b/source/Gui/LoginController.h index 7b28221de..4af56b8c1 100644 --- a/source/Gui/LoginController.h +++ b/source/Gui/LoginController.h @@ -5,12 +5,14 @@ class LoginController { + MAKE_SINGLETON(LoginController); public: - static LoginController& get(); - void init(SimulationController const& simController, PersisterController const& persisterController); + void init(SimulationController const& simController, PersisterController const& persisterController, ActivateUserDialog const& activateUserDialog, BrowserWindow const& browserWindow); void shutdown(); + void onLogin(); + void process(); void saveSettings(); @@ -21,12 +23,24 @@ class LoginController bool isRemember() const; void setRemember(bool value); + std::string const& getUserName() const; + void setUserName(std::string const& value); + + std::string const& getPassword() const; + void setPassword(std::string const& value); + UserInfo getUserInfo(); private: SimulationController _simController; PersisterController _persisterController; + ActivateUserDialog _activateUserDialog; + BrowserWindow _browserWindow; + + std::vector _pendingLoginRequestIds; bool _shareGpuInfo = true; bool _remember = true; + std::string _userName; + std::string _password; }; diff --git a/source/Gui/LoginDialog.cpp b/source/Gui/LoginDialog.cpp index 8f1ff1c63..be1dbafb0 100644 --- a/source/Gui/LoginDialog.cpp +++ b/source/Gui/LoginDialog.cpp @@ -19,14 +19,12 @@ _LoginDialog::_LoginDialog( SimulationController const& simController, PersisterController const& persisterController, - BrowserWindow const& browserWindow, CreateUserDialog const& createUserDialog, ActivateUserDialog const& activateUserDialog, ResetPasswordDialog const& resetPasswordDialog) : _AlienDialog("Login") , _simController(simController) , _persisterController(persisterController) - , _browserWindow(browserWindow) , _createUserDialog(createUserDialog) , _activateUserDialog(activateUserDialog) , _resetPasswordDialog(resetPasswordDialog) @@ -51,34 +49,37 @@ void _LoginDialog::processIntern() AlienImGui::Separator(); - AlienImGui::InputText(AlienImGui::InputTextParameters().hint("User name").textWidth(0), _userName); - AlienImGui::InputText(AlienImGui::InputTextParameters().hint("Password").password(true).textWidth(0), _password); + auto& loginController = LoginController::get(); + auto userName = loginController.getUserName(); + AlienImGui::InputText(AlienImGui::InputTextParameters().hint("User name").textWidth(0), userName); + loginController.setUserName(userName); + + auto password= loginController.getPassword(); + AlienImGui::InputText(AlienImGui::InputTextParameters().hint("Password").password(true).textWidth(0), password); + loginController.setPassword(password); + AlienImGui::Separator(); ImGui::Spacing(); - auto remember = LoginController::get().isRemember(); + auto remember = loginController.isRemember(); AlienImGui::ToggleButton(AlienImGui::ToggleButtonParameters().name("Remember").tooltip(Const::LoginRememberTooltip), remember); - LoginController::get().setRemember(remember); + loginController.setRemember(remember); - auto shareGpuInfo = LoginController::get().shareGpuInfo(); + auto shareGpuInfo = loginController.shareGpuInfo(); AlienImGui::ToggleButton( AlienImGui::ToggleButtonParameters() .name("Share GPU model info") .tooltip(Const::LoginShareGpuInfoTooltip1 + _simController->getGpuName() + "\n" + Const::LoginShareGpuInfoTooltip2), shareGpuInfo); - LoginController::get().setShareGpuInfo(shareGpuInfo); + loginController.setShareGpuInfo(shareGpuInfo); ImGui::Dummy({0, ImGui::GetContentRegionAvail().y - scale(50.0f)}); AlienImGui::Separator(); - ImGui::BeginDisabled(_userName.empty() || _password.empty()); + ImGui::BeginDisabled(userName.empty() || password.empty()); if (AlienImGui::Button("Login")) { close(); - onLogin(); - if (!remember) { - _userName.clear(); - _password.clear(); - } + loginController.onLogin(); } ImGui::EndDisabled(); ImGui::SetItemDefaultFocus(); @@ -87,18 +88,18 @@ void _LoginDialog::processIntern() AlienImGui::VerticalSeparator(); ImGui::SameLine(); - ImGui::BeginDisabled(_userName.empty() || _password.empty()); + ImGui::BeginDisabled(userName.empty() || password.empty()); if (AlienImGui::Button("Create user")) { close(); - _createUserDialog->open(_userName, _password, LoginController::get().getUserInfo()); + _createUserDialog->open(userName, password, LoginController::get().getUserInfo()); } ImGui::EndDisabled(); ImGui::SameLine(); - ImGui::BeginDisabled(_userName.empty()); + ImGui::BeginDisabled(userName.empty()); if (AlienImGui::Button("Reset password")) { close(); - _resetPasswordDialog->open(_userName, LoginController::get().getUserInfo()); + _resetPasswordDialog->open(userName, LoginController::get().getUserInfo()); } ImGui::EndDisabled(); @@ -110,24 +111,3 @@ void _LoginDialog::processIntern() close(); } } - -void _LoginDialog::onLogin() -{ - LoginErrorCode errorCode; - - auto userInfo = LoginController::get().getUserInfo(); - - if (!NetworkService::login(errorCode, _userName, _password, userInfo)) { - switch (errorCode) { - case LoginErrorCode_UnknownUser: { - _activateUserDialog->open(_userName, _password, userInfo); - } break; - default: { - MessageDialog::getInstance().information("Error", "Login failed."); - } break; - } - return; - } - _browserWindow->onRefresh(); - LoginController::get().saveSettings(); -} diff --git a/source/Gui/LoginDialog.h b/source/Gui/LoginDialog.h index 940b59da8..b10614beb 100644 --- a/source/Gui/LoginDialog.h +++ b/source/Gui/LoginDialog.h @@ -12,7 +12,6 @@ class _LoginDialog : public _AlienDialog _LoginDialog( SimulationController const& simController, PersisterController const& persisterController, - BrowserWindow const& browserWindow, CreateUserDialog const& createUserDialog, ActivateUserDialog const& activateUserDialog, ResetPasswordDialog const& resetPasswordDialog); @@ -21,15 +20,10 @@ class _LoginDialog : public _AlienDialog private: void processIntern(); - void onLogin(); - SimulationController _simController; PersisterController _persisterController; BrowserWindow _browserWindow; CreateUserDialog _createUserDialog; ActivateUserDialog _activateUserDialog; ResetPasswordDialog _resetPasswordDialog; - - std::string _userName; - std::string _password; }; diff --git a/source/Gui/MainWindow.cpp b/source/Gui/MainWindow.cpp index 42b07517b..21071dd7a 100644 --- a/source/Gui/MainWindow.cpp +++ b/source/Gui/MainWindow.cpp @@ -118,7 +118,7 @@ _MainWindow::_MainWindow(SimulationController const& simController, PersisterCon } //init services - StyleRepository::getInstance().init(); + StyleRepository::get().init(); NetworkService::init(); //init controllers, windows and dialogs @@ -153,7 +153,7 @@ _MainWindow::_MainWindow(SimulationController const& simController, PersisterCon _createUserDialog = std::make_shared<_CreateUserDialog>(_activateUserDialog); _newPasswordDialog = std::make_shared<_NewPasswordDialog>(_simController, _browserWindow); _resetPasswordDialog = std::make_shared<_ResetPasswordDialog>(_newPasswordDialog); - _loginDialog = std::make_shared<_LoginDialog>(_simController, _persisterController, _browserWindow, _createUserDialog, _activateUserDialog, _resetPasswordDialog); + _loginDialog = std::make_shared<_LoginDialog>(_simController, _persisterController, _createUserDialog, _activateUserDialog, _resetPasswordDialog); _uploadSimulationDialog = std::make_shared<_UploadSimulationDialog>( _browserWindow, _loginDialog, _simController, _editorController->getGenomeEditorWindow()); _editSimulationDialog = std::make_shared<_EditSimulationDialog>(_browserWindow); @@ -164,7 +164,7 @@ _MainWindow::_MainWindow(SimulationController const& simController, PersisterCon _autosaveWindow = std::make_shared<_AutosaveWindow>(_simController, _persisterController); OverlayMessageController::get().init(_persisterController); FileTransferController::get().init(_persisterController, _simController, _temporalControlWindow); - LoginController::get().init(_simController, _persisterController); + LoginController::get().init(_simController, _persisterController, _activateUserDialog, _browserWindow); //cyclic references _browserWindow->registerCyclicReferences(_loginDialog, _uploadSimulationDialog, _editSimulationDialog, _editorController->getGenomeEditorWindow()); @@ -737,8 +737,8 @@ void _MainWindow::processDialogs() _newPasswordDialog->process(); _exitDialog->process(); - MessageDialog::getInstance().process(); - GenericFileDialogs::getInstance().process(); + MessageDialog::get().process(); + GenericFileDialogs::get().process(); } void _MainWindow::processWindows() @@ -761,7 +761,7 @@ void _MainWindow::processControllers() _autosaveController->process(); _editorController->process(); OverlayMessageController::get().process(); - DelayedExecutionController::getInstance().process(); + DelayedExecutionController::get().process(); FileTransferController::get().process(); LoginController::get().process(); } diff --git a/source/Gui/MessageDialog.cpp b/source/Gui/MessageDialog.cpp index 844548362..297006822 100644 --- a/source/Gui/MessageDialog.cpp +++ b/source/Gui/MessageDialog.cpp @@ -9,12 +9,6 @@ #include "AlienImGui.h" #include "WindowController.h" -MessageDialog& MessageDialog::getInstance() -{ - static MessageDialog instance; - return instance; -} - void MessageDialog::process() { if (!_show) { @@ -45,7 +39,7 @@ void MessageDialog::information(std::string const& title, std::vector const& yesFunction) diff --git a/source/Gui/MessageDialog.h b/source/Gui/MessageDialog.h index 4b66cdb01..142a6094b 100644 --- a/source/Gui/MessageDialog.h +++ b/source/Gui/MessageDialog.h @@ -7,9 +7,9 @@ class MessageDialog { -public: - static MessageDialog& getInstance(); + MAKE_SINGLETON(MessageDialog); +public: void process(); void information(std::string const& title, std::string const& message); @@ -35,5 +35,5 @@ class MessageDialog inline void showMessage(std::string const& title, std::string const& message) { - MessageDialog::getInstance().information(title, message); + MessageDialog::get().information(title, message); } diff --git a/source/Gui/MultiplierWindow.cpp b/source/Gui/MultiplierWindow.cpp index 856adb0cd..8556adb7a 100644 --- a/source/Gui/MultiplierWindow.cpp +++ b/source/Gui/MultiplierWindow.cpp @@ -156,7 +156,7 @@ void _MultiplierWindow::onBuild() auto result = DescriptionEditService::randomMultiply( _origSelection, _randomParameters, _simController->getWorldSize(), std::move(data), overlappingCheckSuccessful); if (!overlappingCheckSuccessful) { - MessageDialog::getInstance().information("Random multiplication", "Non-overlapping copies could not be created."); + MessageDialog::get().information("Random multiplication", "Non-overlapping copies could not be created."); } return result; } diff --git a/source/Gui/NewPasswordDialog.cpp b/source/Gui/NewPasswordDialog.cpp index c579f0fa0..b910fd10f 100644 --- a/source/Gui/NewPasswordDialog.cpp +++ b/source/Gui/NewPasswordDialog.cpp @@ -64,9 +64,9 @@ void _NewPasswordDialog::onNewPassword() result |= NetworkService::login(errorCode, _userName, _newPassword, _userInfo); } if (!result) { - MessageDialog::getInstance().information("Error", "An error occurred on the server. Your entered code may be incorrect.\nPlease try to reset the password again."); + MessageDialog::get().information("Error", "An error occurred on the server. Your entered code may be incorrect.\nPlease try to reset the password again."); return; } - MessageDialog::getInstance().information("Information", "The password has been successfully set.\nYou are logged in."); + MessageDialog::get().information("Information", "The password has been successfully set.\nYou are logged in."); _browserWindow->onRefresh(); } diff --git a/source/Gui/OverlayMessageController.cpp b/source/Gui/OverlayMessageController.cpp index f527ca4e8..23e03b4d5 100644 --- a/source/Gui/OverlayMessageController.cpp +++ b/source/Gui/OverlayMessageController.cpp @@ -137,7 +137,7 @@ void OverlayMessageController::processMessage() } ImDrawList* drawList = ImGui::GetForegroundDrawList(); - auto& styleRep = StyleRepository::getInstance(); + auto& styleRep = StyleRepository::get(); auto center = ImGui::GetMainViewport()->Size; center.x /= 2; auto textColorFront = ImColor::HSV(0.5f, 0.0f, 1.0f, textAlpha); diff --git a/source/Gui/PatternAnalysisDialog.cpp b/source/Gui/PatternAnalysisDialog.cpp index fca9ce145..e2d54243c 100644 --- a/source/Gui/PatternAnalysisDialog.cpp +++ b/source/Gui/PatternAnalysisDialog.cpp @@ -57,7 +57,7 @@ void _PatternAnalysisDialog::saveRepetitiveActiveClustersToFiles(std::string con std::ofstream file; file.open(filename, std::ios_base::out); if (!file) { - MessageDialog::getInstance().information("Pattern analysis", "The analysis result could not be saved to the specified file."); + MessageDialog::get().information("Pattern analysis", "The analysis result could not be saved to the specified file."); return; } @@ -95,7 +95,7 @@ void _PatternAnalysisDialog::saveRepetitiveActiveClustersToFiles(std::string con messageStream << "Representative cell networks are save from `cluster" << std::setfill('0') << std::setw(6) << 0 << ".sim` to `cluster" << std::setfill('0') << std::setw(6) << partitionData.size() - 1 << ".sim`."; } - MessageDialog::getInstance().information("Analysis result", messageStream.str()); + MessageDialog::get().information("Analysis result", messageStream.str()); } auto _PatternAnalysisDialog::calcPartitionData() const -> std::map diff --git a/source/Gui/PatternEditorWindow.cpp b/source/Gui/PatternEditorWindow.cpp index ae20927bd..41bad0769 100644 --- a/source/Gui/PatternEditorWindow.cpp +++ b/source/Gui/PatternEditorWindow.cpp @@ -176,7 +176,7 @@ void _PatternEditorWindow::processIntern() AlienImGui::SliderInputFloatParameters() .name("Angle") .textWidth(RightColumnWidth) - .inputWidth(StyleRepository::getInstance().scale(50.0f)) + .inputWidth(StyleRepository::get().scale(50.0f)) .min(-180.0f) .max(180.0f) .format("%.1f"), @@ -327,7 +327,7 @@ void _PatternEditorWindow::processIntern() void _PatternEditorWindow::onOpenPattern() { - GenericFileDialogs::getInstance().showOpenFileDialog( + GenericFileDialogs::get().showOpenFileDialog( "Open pattern", "Pattern file (*.sim){.sim},.*", _startingPath, [&](std::filesystem::path const& path) { auto firstFilename = ifd::FileDialog::Instance().GetResult(); auto firstFilenameCopy = firstFilename; @@ -339,14 +339,14 @@ void _PatternEditorWindow::onOpenPattern() _simController->addAndSelectSimulationData(DataDescription(content)); _editorModel->update(); } else { - MessageDialog::getInstance().information("Open pattern", "The selected file could not be opened."); + MessageDialog::get().information("Open pattern", "The selected file could not be opened."); } }); } void _PatternEditorWindow::onSavePattern() { - GenericFileDialogs::getInstance().showSaveFileDialog( + GenericFileDialogs::get().showSaveFileDialog( "Save pattern", "Pattern file (*.sim){.sim},.*", _startingPath, [&](std::filesystem::path const& path) { auto firstFilename = ifd::FileDialog::Instance().GetResult(); auto firstFilenameCopy = firstFilename; @@ -354,7 +354,7 @@ void _PatternEditorWindow::onSavePattern() auto content = _simController->getSelectedClusteredSimulationData(_editorModel->isRolloutToClusters()); if (!SerializerService::serializeContentToFile(firstFilename.string(), content)) { - MessageDialog::getInstance().information("Save pattern", "The selected pattern could not be saved to the specified file."); + MessageDialog::get().information("Save pattern", "The selected pattern could not be saved to the specified file."); } }); } diff --git a/source/Gui/ResetPasswordDialog.cpp b/source/Gui/ResetPasswordDialog.cpp index 8e403376c..64f6068ce 100644 --- a/source/Gui/ResetPasswordDialog.cpp +++ b/source/Gui/ResetPasswordDialog.cpp @@ -55,7 +55,7 @@ void _ResetPasswordDialog::onResetPassword() if (NetworkService::resetPassword(_userName, _email)) { _newPasswordDialog->open(_userName, _userInfo); } else { - MessageDialog::getInstance().information( + MessageDialog::get().information( "Error", "An error occurred on the server. This could be related to the fact that the\nemail address is wrong."); } } diff --git a/source/Gui/SelectionWindow.cpp b/source/Gui/SelectionWindow.cpp index 0c8ce7e97..0290a0fc4 100644 --- a/source/Gui/SelectionWindow.cpp +++ b/source/Gui/SelectionWindow.cpp @@ -15,21 +15,21 @@ void _SelectionWindow::processIntern() { auto selection = _editorModel->getSelectionShallowData(); ImGui::Text("Cells"); - ImGui::PushFont(StyleRepository::getInstance().getLargeFont()); + ImGui::PushFont(StyleRepository::get().getLargeFont()); ImGui::PushStyleColor(ImGuiCol_Text, Const::TextDecentColor); ImGui::TextUnformatted(StringHelper::format(selection.numCells).c_str()); ImGui::PopStyleColor(); ImGui::PopFont(); ImGui::Text("Connected cells"); - ImGui::PushFont(StyleRepository::getInstance().getLargeFont()); + ImGui::PushFont(StyleRepository::get().getLargeFont()); ImGui::PushStyleColor(ImGuiCol_Text, Const::TextDecentColor); ImGui::TextUnformatted(StringHelper::format(selection.numClusterCells).c_str()); ImGui::PopStyleColor(); ImGui::PopFont(); ImGui::Text("Energy particles"); - ImGui::PushFont(StyleRepository::getInstance().getLargeFont()); + ImGui::PushFont(StyleRepository::get().getLargeFont()); ImGui::PushStyleColor(ImGuiCol_Text, Const::TextDecentColor); ImGui::TextUnformatted(StringHelper::format(selection.numParticles).c_str()); ImGui::PopStyleColor(); diff --git a/source/Gui/SimulationParametersWindow.cpp b/source/Gui/SimulationParametersWindow.cpp index cfc7e62a5..79445ed10 100644 --- a/source/Gui/SimulationParametersWindow.cpp +++ b/source/Gui/SimulationParametersWindow.cpp @@ -2434,7 +2434,7 @@ void _SimulationParametersWindow::onDeleteTab(int index) void _SimulationParametersWindow::onOpenParameters() { - GenericFileDialogs::getInstance().showOpenFileDialog( + GenericFileDialogs::get().showOpenFileDialog( "Open simulation parameters", "Simulation parameters (*.parameters){.parameters},.*", _startingPath, [&](std::filesystem::path const& path) { auto firstFilename = ifd::FileDialog::Instance().GetResult(); auto firstFilenameCopy = firstFilename; @@ -2442,7 +2442,7 @@ void _SimulationParametersWindow::onOpenParameters() SimulationParameters parameters; if (!SerializerService::deserializeSimulationParametersFromFile(parameters, firstFilename.string())) { - MessageDialog::getInstance().information("Open simulation parameters", "The selected file could not be opened."); + MessageDialog::get().information("Open simulation parameters", "The selected file could not be opened."); } else { _simController->setSimulationParameters(parameters); } @@ -2451,7 +2451,7 @@ void _SimulationParametersWindow::onOpenParameters() void _SimulationParametersWindow::onSaveParameters() { - GenericFileDialogs::getInstance().showSaveFileDialog( + GenericFileDialogs::get().showSaveFileDialog( "Save simulation parameters", "Simulation parameters (*.parameters){.parameters},.*", _startingPath, [&](std::filesystem::path const& path) { auto firstFilename = ifd::FileDialog::Instance().GetResult(); auto firstFilenameCopy = firstFilename; @@ -2459,7 +2459,7 @@ void _SimulationParametersWindow::onSaveParameters() auto parameters = _simController->getSimulationParameters(); if (!SerializerService::serializeSimulationParametersToFile(firstFilename.string(), parameters)) { - MessageDialog::getInstance().information("Save simulation parameters", "The selected file could not be saved."); + MessageDialog::get().information("Save simulation parameters", "The selected file could not be saved."); } }); } diff --git a/source/Gui/SimulationView.cpp b/source/Gui/SimulationView.cpp index c8c6c1ef1..ea7d69e0d 100644 --- a/source/Gui/SimulationView.cpp +++ b/source/Gui/SimulationView.cpp @@ -190,7 +190,7 @@ void _SimulationView::draw(bool renderSimulation) auto textWidth = scale(300.0f); auto textHeight = scale(80.0f); ImDrawList* drawList = ImGui::GetBackgroundDrawList(); - auto& styleRep = StyleRepository::getInstance(); + auto& styleRep = StyleRepository::get(); auto right = ImGui::GetMainViewport()->Pos.x + ImGui::GetMainViewport()->Size.x; auto bottom = ImGui::GetMainViewport()->Pos.y + ImGui::GetMainViewport()->Size.y; auto maxLength = std::max(right, bottom); @@ -307,13 +307,13 @@ void _SimulationView::updateImageFromSimulation() {viewPos.x - 2.0f * fontSizeUnit, viewPos.y + 0.5f * fontSizeUnit}, fontSizeUnit / 5, ImColor::HSV(0.0f, 1.0f, 0.7f, 1.0f)); } drawList->AddText( - StyleRepository::getInstance().getMediumFont(), + StyleRepository::get().getMediumFont(), fontSizeUnit, {viewPos.x - 1.7f * fontSizeUnit, viewPos.y}, Const::CellFunctionOverlayShadowColor, text.c_str()); drawList->AddText( - StyleRepository::getInstance().getMediumFont(), + StyleRepository::get().getMediumFont(), fontSizeUnit, {viewPos.x - 1.7f * fontSizeUnit + 1, viewPos.y + 1}, Const::CellFunctionOverlayColor, @@ -325,13 +325,13 @@ void _SimulationView::updateImageFromSimulation() Viewport::mapWorldToViewPosition({overlayElement.pos.x - 0.12f, overlayElement.pos.y - 0.25f}, parameters.borderlessRendering); auto fontSize = Viewport::getZoomFactor() / 2; drawList->AddText( - StyleRepository::getInstance().getLargeFont(), + StyleRepository::get().getLargeFont(), fontSize, {viewPos.x, viewPos.y}, Const::ExecutionNumberOverlayShadowColor, std::to_string(overlayElement.executionOrderNumber).c_str()); drawList->AddText( - StyleRepository::getInstance().getLargeFont(), + StyleRepository::get().getLargeFont(), fontSize, {viewPos.x + 1, viewPos.y + 1}, Const::ExecutionNumberOverlayColor, diff --git a/source/Gui/SpatialControlWindow.cpp b/source/Gui/SpatialControlWindow.cpp index d7ba7466b..f56fd7cc7 100644 --- a/source/Gui/SpatialControlWindow.cpp +++ b/source/Gui/SpatialControlWindow.cpp @@ -50,7 +50,7 @@ void _SpatialControlWindow::processIntern() if (ImGui::BeginChild("##", ImVec2(0, 0), false, ImGuiWindowFlags_HorizontalScrollbar)) { ImGui::Text("World size"); - ImGui::PushFont(StyleRepository::getInstance().getLargeFont()); + ImGui::PushFont(StyleRepository::get().getLargeFont()); ImGui::PushStyleColor(ImGuiCol_Text, Const::TextDecentColor); auto worldSize = _simController->getWorldSize(); ImGui::TextUnformatted( @@ -59,14 +59,14 @@ void _SpatialControlWindow::processIntern() ImGui::PopFont(); ImGui::Text("Zoom factor"); - ImGui::PushFont(StyleRepository::getInstance().getLargeFont()); + ImGui::PushFont(StyleRepository::get().getLargeFont()); ImGui::PushStyleColor(ImGuiCol_Text, Const::TextDecentColor); ImGui::TextUnformatted(StringHelper::format(Viewport::getZoomFactor(), 2).c_str()); ImGui::PopStyleColor(); ImGui::PopFont(); ImGui::Text("Center position"); - ImGui::PushFont(StyleRepository::getInstance().getLargeFont()); + ImGui::PushFont(StyleRepository::get().getLargeFont()); ImGui::PushStyleColor(ImGuiCol_Text, Const::TextDecentColor); auto centerPos = Viewport::getCenterInWorldPos(); ImGui::TextUnformatted( diff --git a/source/Gui/StartupController.cpp b/source/Gui/StartupController.cpp index d971b9b37..4007129ce 100644 --- a/source/Gui/StartupController.cpp +++ b/source/Gui/StartupController.cpp @@ -71,7 +71,7 @@ void _StartupController::process() _state = State::FadeOutLoadingScreen; } if (requestedSimState == PersisterRequestState::Error) { - MessageDialog::getInstance().information("Error", "The default simulation file could not be read.\nAn empty simulation will be created."); + MessageDialog::get().information("Error", "The default simulation file could not be read.\nAn empty simulation will be created."); DeserializedSimulation deserializedSim; deserializedSim.auxiliaryData.generalSettings.worldSizeX = 1000; @@ -145,7 +145,7 @@ void _StartupController::activate() void _StartupController::processLoadingScreen() { - auto& styleRep = StyleRepository::getInstance(); + auto& styleRep = StyleRepository::get(); auto center = ImGui::GetMainViewport()->GetCenter(); auto bottom = ImGui::GetMainViewport()->Pos.y + ImGui::GetMainViewport()->Size.y; ImGui::SetNextWindowPos(center, ImGuiCond_Appearing, ImVec2(0.5f, 0.5f)); diff --git a/source/Gui/StyleRepository.cpp b/source/Gui/StyleRepository.cpp index d515f71b3..0d0dc493b 100644 --- a/source/Gui/StyleRepository.cpp +++ b/source/Gui/StyleRepository.cpp @@ -19,12 +19,6 @@ #include "WindowController.h" -StyleRepository& StyleRepository::getInstance() -{ - static StyleRepository instance; - return instance; -} - void StyleRepository::init() { auto scaleFactor = WindowController::getContentScaleFactor(); diff --git a/source/Gui/StyleRepository.h b/source/Gui/StyleRepository.h index 3f1ded26c..20089ccd6 100644 --- a/source/Gui/StyleRepository.h +++ b/source/Gui/StyleRepository.h @@ -104,10 +104,9 @@ namespace Const class StyleRepository { -public: - static StyleRepository& getInstance(); - StyleRepository(StyleRepository const&) = delete; + MAKE_SINGLETON(StyleRepository); +public : void init(); ImFont* getIconFont() const; @@ -128,8 +127,6 @@ class StyleRepository float scaleInverse(float value) const; private: - StyleRepository() = default; - ImFont* _iconFont = nullptr; ImFont* _smallBoldFont = nullptr; ImFont* _mediumBoldFont = nullptr; @@ -143,5 +140,5 @@ class StyleRepository inline float scale(float value) { - return StyleRepository::getInstance().scale(value); + return StyleRepository::get().scale(value); } diff --git a/source/Gui/TemporalControlWindow.cpp b/source/Gui/TemporalControlWindow.cpp index 9d4bbdad5..af1e6d23c 100644 --- a/source/Gui/TemporalControlWindow.cpp +++ b/source/Gui/TemporalControlWindow.cpp @@ -68,7 +68,7 @@ void _TemporalControlWindow::processTpsInfo() { ImGui::Text("Time steps per second"); - ImGui::PushFont(StyleRepository::getInstance().getLargeFont()); + ImGui::PushFont(StyleRepository::get().getLargeFont()); ImGui::PushStyleColor(ImGuiCol_Text, Const::TextDecentColor /*0xffa07050*/); ImGui::TextUnformatted(StringHelper::format(_simController->getTps(), 1).c_str()); ImGui::PopStyleColor(); @@ -79,7 +79,7 @@ void _TemporalControlWindow::processTotalTimestepsInfo() { ImGui::Text("Total time steps"); - ImGui::PushFont(StyleRepository::getInstance().getLargeFont()); + ImGui::PushFont(StyleRepository::get().getLargeFont()); ImGui::PushStyleColor(ImGuiCol_Text, Const::TextDecentColor); ImGui::TextUnformatted(StringHelper::format(_simController->getCurrentTimestep()).c_str()); ImGui::PopStyleColor(); @@ -90,7 +90,7 @@ void _TemporalControlWindow::processRealTimeInfo() { ImGui::Text("Real-time"); - ImGui::PushFont(StyleRepository::getInstance().getLargeFont()); + ImGui::PushFont(StyleRepository::get().getLargeFont()); ImGui::PushStyleColor(ImGuiCol_Text, Const::TextDecentColor); ImGui::TextUnformatted(StringHelper::format(_simController->getRealTime()).c_str()); ImGui::PopStyleColor(); diff --git a/source/Gui/UploadSimulationDialog.cpp b/source/Gui/UploadSimulationDialog.cpp index dc43cc179..2089d3553 100644 --- a/source/Gui/UploadSimulationDialog.cpp +++ b/source/Gui/UploadSimulationDialog.cpp @@ -105,7 +105,7 @@ void _UploadSimulationDialog::processIntern() AlienImGui::InputTextMultilineParameters() .hint("Description (optional)") .textWidth(0) - .height(ImGui::GetContentRegionAvail().y - StyleRepository::getInstance().scale(70.0f)), + .height(ImGui::GetContentRegionAvail().y - StyleRepository::get().scale(70.0f)), _resourceDescription); ImGui::PopID(); @@ -156,7 +156,7 @@ void _UploadSimulationDialog::onUpload() SerializedSimulation serializedSim; if (!SerializerService::serializeSimulationToStrings(serializedSim, deserializedSim)) { - MessageDialog::getInstance().information( + MessageDialog::get().information( "Upload simulation", "The simulation could not be serialized for uploading."); return; } diff --git a/source/Network/NetworkService.cpp b/source/Network/NetworkService.cpp index e5d11b5d9..2e35fc818 100644 --- a/source/Network/NetworkService.cpp +++ b/source/Network/NetworkService.cpp @@ -173,7 +173,7 @@ bool NetworkService::login(LoginErrorCode& errorCode, std::string const& userNam _password = password; } - errorCode = false; + errorCode = 0; std::stringstream stream(result->body); boost::property_tree::ptree tree; boost::property_tree::read_json(stream, tree); diff --git a/source/PersisterImpl/PersisterWorker.cpp b/source/PersisterImpl/PersisterWorker.cpp index 77b45309f..f9cdf73a9 100644 --- a/source/PersisterImpl/PersisterWorker.cpp +++ b/source/PersisterImpl/PersisterWorker.cpp @@ -243,8 +243,11 @@ _PersisterWorker::PersisterRequestResultOrError _PersisterWorker::processRequest request->getSenderInfo().senderId, PersisterErrorInfo{"Login failed."}); } + if (errorCode == LoginErrorCode_UnknownUser) { + return std::make_shared<_LoginRequestResult>(request->getRequestId(), LoginResultData{.unknownUser = true}); + } } - return std::make_shared<_LoginRequestResult>(request->getRequestId(), LoginResultData{.unknownUser = (errorCode == LoginErrorCode_UnknownUser)}); + return std::make_shared<_LoginRequestResult>(request->getRequestId(), LoginResultData{.unknownUser = false}); } _PersisterWorker::PersisterRequestResultOrError _PersisterWorker::processRequest(std::unique_lock& lock, GetNetworkResourcesRequest const& request)