Skip to content

Commit

Permalink
Save/LoadLastRunWindowBounds: also save DpiWindowSizeFactor / restore…
Browse files Browse the repository at this point in the history
… size with DPI handling
  • Loading branch information
pthom committed Jul 5, 2024
1 parent 004a648 commit 1a53360
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 4 deletions.
3 changes: 1 addition & 2 deletions src/hello_imgui/internal/backend_impls/abstract_runner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -703,6 +703,7 @@ void AbstractRunner::Setup()
Impl_Select_Gl_Version();
#endif

SetupDpiAwareParams();
PrepareWindowGeometry();

auto fnRenderCallbackDuringResize = [this]()
Expand All @@ -729,8 +730,6 @@ void AbstractRunner::Setup()

Impl_SetWindowIcon();

SetupDpiAwareParams();

// This should be done before Impl_LinkPlatformAndRenderBackends()
// because, in the case of glfw ImGui_ImplGlfw_InstallCallbacks
// will chain the user callbacks with ImGui callbacks;
Expand Down
48 changes: 46 additions & 2 deletions src/hello_imgui/internal/hello_imgui_ini_settings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -173,11 +173,13 @@ namespace HelloImGui

void SaveLastRunWindowBounds(const std::string& iniPartsFilename, const ScreenBounds& windowBounds)
{
auto& dpiAwareParams = HelloImGui::GetRunnerParams()->dpiAwareParams;
IniParts iniParts = IniParts::LoadFromFile(iniPartsFilename);

ini::IniFile iniFile;
iniFile["AppWindow"]["WindowPosition"] = IntPairToString(windowBounds.position);
iniFile["AppWindow"]["WindowSize"] = IntPairToString(windowBounds.size);
iniFile["AppWindow"]["DpiWindowSizeFactor"] = dpiAwareParams.dpiWindowSizeFactor;
std::string iniContent = iniFile.encode();

iniParts.SetIniPart("AppWindow", iniContent);
Expand Down Expand Up @@ -206,23 +208,65 @@ namespace HelloImGui
ScreenBounds screenBounds;
bool failed = false;

if (iniFile.find("AppWindow") == iniFile.end())
return std::nullopt;
auto & appWindowSection = iniFile["AppWindow"];

// Read Window Position
{
auto strValue = iniFile["AppWindow"]["WindowPosition"].as<std::string>();
if (appWindowSection.find("WindowPosition") == appWindowSection.end())
return std::nullopt;
auto strValue = appWindowSection["WindowPosition"].as<std::string>();
auto intPair = StringToIntPair(strValue);
if (intPair[0] >= 0)
screenBounds.position = intPair;
else
failed = true;
}
// Read Window Size
{
auto strValue = iniFile["AppWindow"]["WindowSize"].as<std::string>();
if (appWindowSection.find("WindowSize") == appWindowSection.end())
return std::nullopt;
auto strValue = appWindowSection["WindowSize"].as<std::string>();
auto intPair = StringToIntPair(strValue);
if (intPair[0] >= 0)
screenBounds.size = intPair;
else
failed = true;
}

// Read DPI Window Size Factor
// If needed, change the size to match the current DPI versus the DPI when the size was saved
// (we want the window to "look" as big as it was when saved, even if the DPI has changed,
// or if we are on a different monitor / computer / OS)
{
// DpiWindowSizeFactor was added late, so it may not be present in the ini file
if (appWindowSection.find("DpiWindowSizeFactor") != appWindowSection.end())
{
float dpiWindowSizeFactor_WhenSaved = iniFile["AppWindow"]["DpiWindowSizeFactor"].as<float>();
bool isDpiSane = (dpiWindowSizeFactor_WhenSaved >= 0.1) || (dpiWindowSizeFactor_WhenSaved <= 10.0);
if (isDpiSane)
{
float dpiWindowSizeFactor_Now = HelloImGui::GetRunnerParams()->dpiAwareParams.dpiWindowSizeFactor;
float ratio = dpiWindowSizeFactor_Now / dpiWindowSizeFactor_WhenSaved;
if (ratio != 1.f)
{
auto applyRatio = [](int v, float ratio) -> int
{
return static_cast<int>(static_cast<float>(v) * ratio);
};
screenBounds.position[0] = applyRatio(screenBounds.position[0], ratio);
screenBounds.position[1] = applyRatio(screenBounds.position[1], ratio);
screenBounds.size[0] = applyRatio(screenBounds.size[0], ratio);
screenBounds.size[1] = applyRatio(screenBounds.size[1], ratio);
}
}
}
}
{

}

if (failed)
return std::nullopt;
else
Expand Down

0 comments on commit 1a53360

Please sign in to comment.