Skip to content

Commit

Permalink
+ bugfix for asynchronous login
Browse files Browse the repository at this point in the history
+ use singleton macro
  • Loading branch information
chrxh committed Oct 10, 2024
1 parent eec3c1e commit 1f5f8ee
Show file tree
Hide file tree
Showing 40 changed files with 242 additions and 218 deletions.
16 changes: 16 additions & 0 deletions source/Base/Definitions.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 \
4 changes: 2 additions & 2 deletions source/Gui/ActivateUserDialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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.");
Expand Down
40 changes: 20 additions & 20 deletions source/Gui/AlienImGui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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(),
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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);
Expand All @@ -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();

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -1839,7 +1839,7 @@ void AlienImGui::BasicInputColorMatrix(BasicInputColorMatrixParameters<T> const&
_basicSilderExpanded.insert(toggleButtonId);
}
}
auto textWidth = StyleRepository::getInstance().scale(parameters._textWidth);
auto textWidth = StyleRepository::get().scale(parameters._textWidth);

ImGui::SameLine();

Expand Down
26 changes: 13 additions & 13 deletions source/Gui/BrowserWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
}
}

Expand Down Expand Up @@ -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;
}
}
Expand All @@ -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);
Expand Down Expand Up @@ -1330,14 +1330,14 @@ void _BrowserWindow::onDownloadResource(BrowserLeaf const& leaf)
} else {
std::vector<uint8_t> 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"
Expand All @@ -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;
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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;
}
Expand All @@ -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) {
Expand All @@ -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;
}
Expand Down
2 changes: 1 addition & 1 deletion source/Gui/CreateUserDialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 "
Expand Down
7 changes: 0 additions & 7 deletions source/Gui/DelayedExecutionController.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,6 @@
#include "AlienImGui.h"
#include "DelayedExecutionController.h"


DelayedExecutionController& DelayedExecutionController::getInstance()
{
static DelayedExecutionController instance;
return instance;
}

void DelayedExecutionController::process()
{
std::vector<ExecutionData> delayedExecDatas;
Expand Down
6 changes: 3 additions & 3 deletions source/Gui/DelayedExecutionController.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@

class DelayedExecutionController
{
public:
static DelayedExecutionController& getInstance();
MAKE_SINGLETON(DelayedExecutionController);

public:
void process();

void executeLater(std::function<void(void)> const& execFunc);
Expand All @@ -22,5 +22,5 @@ class DelayedExecutionController

inline void delayedExecution(std::function<void(void)> const& execFunc)
{
DelayedExecutionController::getInstance().executeLater(execFunc);
DelayedExecutionController::get().executeLater(execFunc);
}
6 changes: 3 additions & 3 deletions source/Gui/DeleteUserDialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand All @@ -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.");
}
}
Loading

0 comments on commit 1f5f8ee

Please sign in to comment.