diff --git a/include/settings.hpp b/include/settings.hpp index 32784539..e280de46 100644 --- a/include/settings.hpp +++ b/include/settings.hpp @@ -30,6 +30,8 @@ extern bool headless; /** vcvrack.com user token */ extern std::string token; +/** Whether the window is maximized */ +extern bool windowMaximized; /** Size of window in pixels */ extern math::Vec windowSize; /** Position in window in pixels */ diff --git a/include/window.hpp b/include/window.hpp index 2346d598..04e5194d 100644 --- a/include/window.hpp +++ b/include/window.hpp @@ -60,10 +60,8 @@ struct Window { Window(); ~Window(); - math::Vec getPosition(); math::Vec getSize(); void setSize(math::Vec size); - void updatePosSizeSettings(); void run(); void step(); /** Takes a screenshot of the screen and saves it to a PNG file. */ diff --git a/src/settings.cpp b/src/settings.cpp index 4c8f920e..00131f15 100644 --- a/src/settings.cpp +++ b/src/settings.cpp @@ -20,6 +20,7 @@ bool devMode = false; bool headless = false; std::string token; +bool windowMaximized = false; math::Vec windowSize = math::Vec(1024, 768); math::Vec windowPos = math::Vec(NAN, NAN); float zoom = 0.25; @@ -90,6 +91,8 @@ json_t* toJson() { json_object_set_new(rootJ, "token", json_string(token.c_str())); + json_object_set_new(rootJ, "windowMaximized", json_boolean(windowMaximized)); + json_t* windowSizeJ = json_pack("[f, f]", windowSize.x, windowSize.y); json_object_set_new(rootJ, "windowSize", windowSizeJ); @@ -194,6 +197,10 @@ void fromJson(json_t* rootJ) { if (tokenJ) token = json_string_value(tokenJ); + json_t* windowMaximizedJ = json_object_get(rootJ, "windowMaximized"); + if (windowMaximizedJ) + windowMaximized = json_boolean_value(windowMaximizedJ); + json_t* windowSizeJ = json_object_get(rootJ, "windowSize"); if (windowSizeJ) { double x, y; diff --git a/src/window.cpp b/src/window.cpp index 63b3a167..f68c8f78 100644 --- a/src/window.cpp +++ b/src/window.cpp @@ -91,15 +91,32 @@ struct Window::Internal { }; -static void windowMaximizeCallback(GLFWwindow* win, int maximized) { - Window* window = (Window*) glfwGetWindowUserPointer(win); - window->updatePosSizeSettings(); +static void windowPosCallback(GLFWwindow* win, int x, int y) { + // Window* window = (Window*) glfwGetWindowUserPointer(win); + if (glfwGetWindowAttrib(win, GLFW_MAXIMIZED)) + return; + if (glfwGetWindowMonitor(win)) + return; + settings::windowPos = math::Vec(x, y); + // DEBUG("windowPosCallback %d %d", x, y); } static void windowSizeCallback(GLFWwindow* win, int width, int height) { - Window* window = (Window*) glfwGetWindowUserPointer(win); - window->updatePosSizeSettings(); + // Window* window = (Window*) glfwGetWindowUserPointer(win); + if (glfwGetWindowAttrib(win, GLFW_MAXIMIZED)) + return; + if (glfwGetWindowMonitor(win)) + return; + settings::windowSize = math::Vec(width, height); + // DEBUG("windowSizeCallback %d %d", width, height); +} + + +static void windowMaximizeCallback(GLFWwindow* win, int maximized) { + // Window* window = (Window*) glfwGetWindowUserPointer(win); + settings::windowMaximized = maximized; + // DEBUG("windowMaximizeCallback %d", maximized); } @@ -251,14 +268,12 @@ Window::Window() { INFO("Window content scale: %f", contentScale); glfwSetWindowSizeLimits(win, minWindowSize.x, minWindowSize.y, GLFW_DONT_CARE, GLFW_DONT_CARE); - if (settings::windowSize.isZero()) { - glfwMaximizeWindow(win); + glfwSetWindowSize(win, settings::windowSize.x, settings::windowSize.y); + if (settings::windowPos.isFinite()) { + glfwSetWindowPos(win, settings::windowPos.x, settings::windowPos.y); } - else { - glfwSetWindowSize(win, settings::windowSize.x, settings::windowSize.y); - if (settings::windowPos.isFinite()) { - glfwSetWindowPos(win, settings::windowPos.x, settings::windowPos.y); - } + if (settings::windowMaximized) { + glfwMaximizeWindow(win); } glfwShowWindow(win); @@ -271,8 +286,9 @@ Window::Window() { internal->monitorRefreshRate = monitorMode->refreshRate; // Set window callbacks - glfwSetWindowMaximizeCallback(win, windowMaximizeCallback); + glfwSetWindowPosCallback(win, windowPosCallback); glfwSetWindowSizeCallback(win, windowSizeCallback); + glfwSetWindowMaximizeCallback(win, windowMaximizeCallback); glfwSetMouseButtonCallback(win, mouseButtonCallback); // Call this ourselves, but on every frame instead of only when the mouse moves // glfwSetCursorPosCallback(win, cursorPosCallback); @@ -342,13 +358,6 @@ Window::~Window() { } -math::Vec Window::getPosition() { - int x, y; - glfwGetWindowPos(win, &x, &y); - return math::Vec(x, y); -} - - math::Vec Window::getSize() { int width, height; glfwGetWindowSize(win, &width, &height); @@ -362,18 +371,6 @@ void Window::setSize(math::Vec size) { } -void Window::updatePosSizeSettings() { - if (glfwGetWindowAttrib(win, GLFW_MAXIMIZED)) { - settings::windowSize = math::Vec(); - settings::windowPos = math::Vec(); - } - else { - settings::windowSize = getSize(); - settings::windowPos = getPosition(); - } -} - - void Window::run() { internal->frame = 0; while (!glfwWindowShouldClose(win)) {