Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor PersistentWindowSettings and persist window minimized state #1609

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions libs/s25main/Settings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,7 @@
windows.persistentSettings[window.first].lastPos.x = iniWindow->getIntValue("pos_x");
windows.persistentSettings[window.first].lastPos.y = iniWindow->getIntValue("pos_y");
windows.persistentSettings[window.first].isOpen = iniWindow->getIntValue("is_open");
windows.persistentSettings[window.first].isMinimized = iniWindow->getValue("is_minimized", false);

Check warning on line 353 in libs/s25main/Settings.cpp

View check run for this annotation

Codecov / codecov/patch

libs/s25main/Settings.cpp#L353

Added line #L353 was not covered by tests
}
} catch(std::runtime_error& e)
{
Expand Down Expand Up @@ -502,6 +503,7 @@
iniWindow->setValue("pos_x", windows.persistentSettings[window.first].lastPos.x);
iniWindow->setValue("pos_y", windows.persistentSettings[window.first].lastPos.y);
iniWindow->setValue("is_open", windows.persistentSettings[window.first].isOpen);
iniWindow->setValue("is_minimized", windows.persistentSettings[window.first].isMinimized);

Check warning on line 506 in libs/s25main/Settings.cpp

View check run for this annotation

Codecov / codecov/patch

libs/s25main/Settings.cpp#L506

Added line #L506 was not covered by tests
}

bfs::path settingsPathIngame = RTTRCONFIG.ExpandPath(s25::resources::ingameOptions);
Expand Down
8 changes: 3 additions & 5 deletions libs/s25main/Settings.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,9 @@ bool checkPort(int port);

struct PersistentWindowSettings
{
DrawPoint lastPos;
bool isOpen;

PersistentWindowSettings(DrawPoint lastPos, bool isOpen) : lastPos(lastPos), isOpen(isOpen) {}
PersistentWindowSettings() : lastPos(DrawPoint::Invalid()), isOpen(false) {}
DrawPoint lastPos = DrawPoint::Invalid();
bool isOpen = false;
bool isMinimized = false;
};

/// Configuration class
Expand Down
34 changes: 21 additions & 13 deletions libs/s25main/ingameWindows/IngameWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ IngameWindow::IngameWindow(unsigned id, const DrawPoint& pos, const Extent& size
contentOffsetEnd.x = LOADER.GetImageN("resource", 39)->getWidth(); // right border
contentOffsetEnd.y = LOADER.GetImageN("resource", 40)->getHeight(); // bottom bar

const auto it = SETTINGS.windows.persistentSettings.find(GetGUIID());
windowSettings_ = (it == SETTINGS.windows.persistentSettings.cend() ? nullptr : &it->second);

// For compatibility we treat the given height as the window height, not the content height
// First we have to make sure the size is not to small
Window::Resize(elMax(contentOffset + contentOffsetEnd, GetSize()));
Expand All @@ -51,12 +54,19 @@ IngameWindow::IngameWindow(unsigned id, const DrawPoint& pos, const Extent& size
// Save to settings that window is open
SaveOpenStatus(true);

// Restore minimized state
if(windowSettings_ && windowSettings_->isMinimized)
{
isMinimized_ = true;
Extent minimizedSize(GetSize().x, contentOffset.y + contentOffsetEnd.y);
Window::Resize(minimizedSize);
}

// Load last position or center the window
if(pos == posLastOrCenter)
{
const auto windowSettings = SETTINGS.windows.persistentSettings.find(GetGUIID());
if(windowSettings != SETTINGS.windows.persistentSettings.cend() && windowSettings->second.lastPos.isValid())
SetPos(windowSettings->second.lastPos);
if(windowSettings_ && windowSettings_->lastPos.isValid())
SetPos(windowSettings_->lastPos);
else
MoveToCenter();
} else if(pos == posCenter)
Expand Down Expand Up @@ -111,11 +121,8 @@ void IngameWindow::SetPos(DrawPoint newPos)
newPos.y = screenSize.y - GetSize().y;

// if possible save the position to settings
const auto windowSettings = SETTINGS.windows.persistentSettings.find(GetGUIID());
if(windowSettings != SETTINGS.windows.persistentSettings.cend())
{
windowSettings->second.lastPos = newPos;
}
if(windowSettings_)
windowSettings_->lastPos = newPos;

Window::SetPos(newPos);
}
Expand All @@ -133,6 +140,10 @@ void IngameWindow::SetMinimized(bool minimized)
fullSize.y = iwHeight + contentOffset.y + contentOffsetEnd.y;
this->isMinimized_ = minimized;
Resize(fullSize);

// if possible save the minimized state to settings
if(windowSettings_)
windowSettings_->isMinimized = isMinimized_;
}

void IngameWindow::MouseLeftDown(const MouseCoords& mc)
Expand Down Expand Up @@ -361,11 +372,8 @@ bool IngameWindow::IsMessageRelayAllowed() const

void IngameWindow::SaveOpenStatus(bool isOpen) const
{
auto windowSettings = SETTINGS.windows.persistentSettings.find(GetGUIID());
if(windowSettings != SETTINGS.windows.persistentSettings.cend())
{
windowSettings->second.isOpen = isOpen;
}
if(windowSettings_)
windowSettings_->isOpen = isOpen;
}

Rect IngameWindow::GetButtonBounds(IwButton btn) const
Expand Down
2 changes: 2 additions & 0 deletions libs/s25main/ingameWindows/IngameWindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

class glArchivItem_Bitmap;
class MouseCoords;
struct PersistentWindowSettings;
template<typename T>
struct Point;

Expand Down Expand Up @@ -126,4 +127,5 @@ class IngameWindow : public Window
bool isMoving;
CloseBehavior closeBehavior_;
helpers::EnumArray<ButtonState, IwButton> buttonStates_;
PersistentWindowSettings* windowSettings_;
};
34 changes: 34 additions & 0 deletions tests/s25Main/UI/testIngameWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@
//
// SPDX-License-Identifier: GPL-2.0-or-later

#include "DrawPoint.h"
#include "Point.h"
#include "PointOutput.h"
#include "Settings.h"
#include "WindowManager.h"
#include "controls/ctrlButton.h"
#include "controls/ctrlComboBox.h"
Expand All @@ -12,13 +15,15 @@
#include "controls/ctrlProgress.h"
#include "desktops/dskGameLobby.h"
#include "helpers/format.hpp"
#include "ingameWindows/IngameWindow.h"
#include "ingameWindows/iwConnecting.h"
#include "ingameWindows/iwDirectIPConnect.h"
#include "ingameWindows/iwHelp.h"
#include "ingameWindows/iwMapGenerator.h"
#include "ingameWindows/iwMsgbox.h"
#include "uiHelper/uiHelpers.hpp"
#include "gameTypes/GameTypesOutput.h"
#include "gameData/const_gui_ids.h"
#include "rttr/test/random.hpp"
#include "s25util/StringConversion.h"
#include <turtle/mock.hpp>
Expand Down Expand Up @@ -194,4 +199,33 @@ BOOST_AUTO_TEST_CASE(ConnectingWindow)
}
}

BOOST_AUTO_TEST_CASE(SaveAndRestoreMinimized)
{
constexpr auto id = CGI_MINIMAP;
auto it = SETTINGS.windows.persistentSettings.find(id);
BOOST_REQUIRE(it != SETTINGS.windows.persistentSettings.end());

{
it->second.isMinimized = false;

IngameWindow wnd(id, IngameWindow::posLastOrCenter, Extent(100, 100), "Test Window", nullptr);
BOOST_TEST(!wnd.IsMinimized());
BOOST_TEST(wnd.GetSize() == Extent(100, 100));

wnd.SetMinimized(true);
BOOST_TEST(it->second.isMinimized);
}

{
it->second.isMinimized = true;

IngameWindow wnd(id, IngameWindow::posLastOrCenter, Extent(100, 100), "Test Window", nullptr);
BOOST_TEST(wnd.IsMinimized());
BOOST_TEST(wnd.GetSize() != Extent(100, 100));

wnd.SetMinimized(false);
BOOST_TEST(!it->second.isMinimized);
}
}

BOOST_AUTO_TEST_SUITE_END()