From b31d4fb5ed873a0763fc4a451838603780db4e45 Mon Sep 17 00:00:00 2001 From: Andrew Belt Date: Sat, 4 Nov 2017 17:19:23 -0400 Subject: [PATCH] Set window position/size from settings.json --- include/gui.hpp | 5 +++++ src/gui.cpp | 29 +++++++++++++++++++++++++++++ src/settings.cpp | 28 ++++++++++++++++++++++++++++ 3 files changed, 62 insertions(+) diff --git a/include/gui.hpp b/include/gui.hpp index 3d64809f..9df01144 100644 --- a/include/gui.hpp +++ b/include/gui.hpp @@ -32,6 +32,11 @@ void guiCursorLock(); void guiCursorUnlock(); bool guiIsModPressed(); bool guiIsShiftPressed(); +Vec guiGetWindowSize(); +void guiSetWindowSize(Vec size); +Vec guiGetWindowPos(); +void guiSetWindowPos(Vec pos); +bool guiIsMaximized(); } // namespace rack diff --git a/src/gui.cpp b/src/gui.cpp index 01de9ab1..809695f3 100644 --- a/src/gui.cpp +++ b/src/gui.cpp @@ -404,6 +404,35 @@ bool guiIsShiftPressed() { return glfwGetKey(gWindow, GLFW_KEY_LEFT_SHIFT) == GLFW_PRESS || glfwGetKey(gWindow, GLFW_KEY_RIGHT_SHIFT) == GLFW_PRESS; } +Vec guiGetWindowSize() { + int width, height; + glfwGetWindowSize(gWindow, &width, &height); + return Vec(width, height); +} + +void guiSetWindowSize(Vec size) { + int width = size.x; + int height = size.y; + glfwSetWindowSize(gWindow, width, height); +} + +Vec guiGetWindowPos() { + int x, y; + glfwGetWindowPos(gWindow, &x, &y); + return Vec(x, y); +} + +void guiSetWindowPos(Vec pos) { + int x = pos.x; + int y = pos.y; + glfwSetWindowPos(gWindow, x, y); +} + +bool guiIsMaximized() { + return glfwGetWindowAttrib(gWindow, GLFW_MAXIMIZED); +} + + //////////////////// // resources diff --git a/src/settings.cpp b/src/settings.cpp index 95ea7ed9..86e2875a 100644 --- a/src/settings.cpp +++ b/src/settings.cpp @@ -17,6 +17,18 @@ static json_t *settingsToJson() { json_t *tokenJ = json_string(gToken.c_str()); json_object_set_new(rootJ, "token", tokenJ); + if (!guiIsMaximized()) { + // windowSize + Vec windowSize = guiGetWindowSize(); + json_t *windowSizeJ = json_pack("[f, f]", windowSize.x, windowSize.y); + json_object_set_new(rootJ, "windowSize", windowSizeJ); + + // windowPos + Vec windowPos = guiGetWindowPos(); + json_t *windowPosJ = json_pack("[f, f]", windowPos.x, windowPos.y); + json_object_set_new(rootJ, "windowPos", windowPosJ); + } + // opacity float opacity = gToolbar->wireOpacitySlider->value; json_t *opacityJ = json_real(opacity); @@ -57,6 +69,22 @@ static void settingsFromJson(json_t *rootJ) { if (tokenJ) gToken = json_string_value(tokenJ); + // windowSize + json_t *windowSizeJ = json_object_get(rootJ, "windowSize"); + if (windowSizeJ) { + double width, height; + json_unpack(windowSizeJ, "[F, F]", &width, &height); + guiSetWindowSize(Vec(width, height)); + } + + // windowPos + json_t *windowPosJ = json_object_get(rootJ, "windowPos"); + if (windowPosJ) { + double x, y; + json_unpack(windowPosJ, "[F, F]", &x, &y); + guiSetWindowPos(Vec(x, y)); + } + // opacity json_t *opacityJ = json_object_get(rootJ, "wireOpacity"); if (opacityJ)