Skip to content

Commit

Permalink
Fix initial window size being incorrect, also use a better hack when …
Browse files Browse the repository at this point in the history
…adjusting DPI scaling changes
  • Loading branch information
CasualPokePlayer committed May 5, 2024
1 parent 70cdf9c commit 8a22507
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 22 deletions.
15 changes: 9 additions & 6 deletions GSR/GSR.cs
Original file line number Diff line number Diff line change
Expand Up @@ -359,26 +359,29 @@ public int MainLoop()

_mainWindow.NewFrame();

var statusBarHeight = 0f;
_imGuiMenuBar.RunMenuBar();
// position of the emu window is below the menu bar
var barHeight = ImGui.GetFrameHeight();

var statusBarHeight = 0.0f;
if (!_config.HideStatusBar)
{
_osdManager.RunStatusBar();
statusBarHeight = ImGui.GetFrameHeight();
statusBarHeight = barHeight;
}

var vp = ImGui.GetMainViewport();
ImGui.SetNextWindowPos(vp.Pos);
ImGui.SetNextWindowSize(vp.Size - new Vector2(0, statusBarHeight));
ImGui.SetNextWindowPos(vp.Pos + new Vector2(0, barHeight));
ImGui.SetNextWindowSize(vp.Size - new Vector2(0, barHeight + statusBarHeight));

ImGui.PushStyleVar(ImGuiStyleVar.WindowRounding, 0);
ImGui.PushStyleVar(ImGuiStyleVar.WindowBorderSize, 0);
ImGui.PushStyleVar(ImGuiStyleVar.WindowPadding, new Vector2(0, 0));

if (ImGui.Begin("GSR", ImGuiWindowFlags.NoDecoration | ImGuiWindowFlags.NoMove | ImGuiWindowFlags.MenuBar | ImGuiWindowFlags.NoBringToFrontOnFocus))
if (ImGui.Begin("GSR", ImGuiWindowFlags.NoDecoration | ImGuiWindowFlags.NoMove | ImGuiWindowFlags.NoBringToFrontOnFocus))
{
DrawEmu();
ImGui.PopStyleVar(3);
_imGuiMenuBar.RunMenuBar();
_imGuiModals.RunModals();
}
else
Expand Down
4 changes: 2 additions & 2 deletions GSR/Gui/ImGuiMenuBar.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ internal sealed class ImGuiMenuBar(Config config, EmuManager emuManager, RomLoad
{
public void RunMenuBar()
{
if (ImGui.BeginMenuBar())
if (ImGui.BeginMainMenuBar())
{
if (ImGui.BeginMenu("File"))
{
Expand Down Expand Up @@ -233,7 +233,7 @@ public void RunMenuBar()
ImGui.EndMenu();
}

ImGui.EndMenuBar();
ImGui.EndMainMenuBar();
}
}
}
23 changes: 9 additions & 14 deletions GSR/Gui/ImGuiWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -554,6 +554,10 @@ public ImGuiWindow(string windowName, Config config, bool isMainWindow)
// this doesn't matter too much in dark mode
// but without a border light mode is terrible
style.FrameBorderSize = 1;

// this must be done in order to scaling to properly take effect for e.g. GetFrameHeight() calls
ImGui.NewFrame();
ImGui.EndFrame();
}
catch
{
Expand Down Expand Up @@ -922,20 +926,11 @@ public void NewFrame()
SetFont(dpiScale);
_dpiScale = dpiScale;

// can't use SetWindowSize, ImGui.GetFrameHeight() is not up to date
if (!_isFullscreen)
{
var newWidth = _lastWidth * _lastScale;
var newHeight = _lastHeight * _lastScale;
// nasty hack to account for ImGui.GetFrameHeight() not being up to date
var frameHeight = io.Fonts.Fonts[0].FontSize + ImGui.GetStyle().FramePadding.Y * 2.0f;
newHeight += (int)(frameHeight * _lastBars);
SdlRenderer.GetRendererOutputSize(out var lastDisplayW, out var lastDisplayH);
SDL_GetWindowSize(SdlWindow, out var lastWindowWidth, out var lastWindowHeight);
newWidth = newWidth * lastWindowWidth / lastDisplayW;
newHeight = newHeight * lastWindowHeight / lastDisplayH;
SDL_SetWindowSize(SdlWindow, newWidth, newHeight);
}
// get ImGui.GetFrameHeight() up to date (important for SetWindowSize)
ImGui.NewFrame();
ImGui.EndFrame();

SetWindowSize(_lastWidth, _lastHeight, _lastScale, _lastBars);
}
}

Expand Down

0 comments on commit 8a22507

Please sign in to comment.