Skip to content

Commit

Permalink
Restore last window size and position when leaving fullscreen
Browse files Browse the repository at this point in the history
  • Loading branch information
stuarthayhurst committed Dec 18, 2023
1 parent ea8e533 commit 8225cb0
Showing 1 changed file with 31 additions and 17 deletions.
48 changes: 31 additions & 17 deletions src/ammonite/window.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,13 @@ namespace ammonite {
GLFWwindow* windowPtr = nullptr;
bool isFullscreen = false;
bool closeWindow = false;

struct WindowGeom {
int width = 0;
int height = 0;
int xPos = 0;
int yPos = 0;
} lastWindowGeom;
}

namespace {
Expand Down Expand Up @@ -63,6 +70,21 @@ namespace ammonite {
bool* closeWindowPtr = (bool*)userPtr;
*closeWindowPtr = true;
}

static void storeWindowGeometry() {
//Get window frame size, content size and position
int frameLeft = 0, frameRight = 0, frameTop = 0, frameBottom = 0;
glfwGetWindowFrameSize(windowPtr, &frameLeft, &frameTop,
&frameRight, &frameBottom);
glfwGetWindowSize(windowPtr, &lastWindowGeom.width, &lastWindowGeom.height);
glfwGetWindowPos(windowPtr, &lastWindowGeom.xPos, &lastWindowGeom.yPos);

//Apply frame dimension corrections
lastWindowGeom.width += frameLeft + frameRight;
lastWindowGeom.height += frameTop + frameBottom;
lastWindowGeom.xPos -= frameLeft;
lastWindowGeom.yPos -= frameTop;
}
}

GLFWwindow* getWindowPtr() {
Expand Down Expand Up @@ -243,26 +265,18 @@ namespace ammonite {
return;
}

//Set window to windowed mode
if (!fullscreen) {
int width = ammonite::settings::runtime::getWidth();
int height = ammonite::settings::runtime::getHeight();
int x = 0, y = 0;

//Get the current monitor to window to
GLFWmonitor* currentMonitor = glfwGetWindowMonitor(windowPtr);
if (currentMonitor != nullptr) {
glfwGetMonitorPos(currentMonitor, &x, &y);
}

glfwSetWindowMonitor(windowPtr, nullptr, ++x, ++y, width, height, GLFW_DONT_CARE);
//Handle new window mode
if (fullscreen) {
//Store windowed geometry and then fullscreen
storeWindowGeometry();
setFullscreenMonitor(getCurrentMonitor());
} else {
//Set window to windowed mode, using last geometry
glfwSetWindowMonitor(windowPtr, nullptr, lastWindowGeom.xPos, lastWindowGeom.yPos,
lastWindowGeom.width, lastWindowGeom.height, GLFW_DONT_CARE);

isFullscreen = false;
return;
}

//Fullscreen on current monitor
setFullscreenMonitor(getCurrentMonitor());
}
}
}

0 comments on commit 8225cb0

Please sign in to comment.