From 0f894f721e08b159bac673f359560bfd8625af50 Mon Sep 17 00:00:00 2001 From: Vladimir Pantelic Date: Thu, 26 Oct 2017 15:20:56 +0200 Subject: [PATCH] remember window size and position in settings and apply at startup Signed-off-by: Vladimir Pantelic --- src/gui.cpp | 25 ++++++++++++++++++++++++- src/settings.cpp | 20 ++++++++++++++++++++ 2 files changed, 44 insertions(+), 1 deletion(-) diff --git a/src/gui.cpp b/src/gui.cpp index d6e82fb9..16165897 100644 --- a/src/gui.cpp +++ b/src/gui.cpp @@ -41,6 +41,10 @@ void windowSizeCallback(GLFWwindow* window, int width, int height) { gScene->box.size = Vec(width, height); } +void windowPosCallback(GLFWwindow* window, int width, int height) { + gScene->box.pos = Vec(width, height); +} + void mouseButtonCallback(GLFWwindow *window, int button, int action, int mods) { #ifdef ARCH_MAC // Ctrl-left click --> right click @@ -250,7 +254,8 @@ void guiInit() { // glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2); // glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); // glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); - glfwWindowHint(GLFW_MAXIMIZED, GLFW_TRUE); + glfwWindowHint(GLFW_MAXIMIZED, GLFW_FALSE); + glfwWindowHint(GLFW_RESIZABLE, GLFW_TRUE); glfwWindowHint(GLFW_DOUBLEBUFFER, GLFW_TRUE); gWindow = glfwCreateWindow(640, 480, "", NULL, NULL); if (!gWindow) { @@ -263,6 +268,7 @@ void guiInit() { glfwSwapInterval(1); glfwSetWindowSizeCallback(gWindow, windowSizeCallback); + glfwSetWindowPosCallback(gWindow, windowPosCallback); glfwSetMouseButtonCallback(gWindow, mouseButtonStickyCallback); // glfwSetCursorPosCallback(gWindow, cursorPosCallback); glfwSetCursorEnterCallback(gWindow, cursorEnterCallback); @@ -308,6 +314,23 @@ void guiDestroy() { void guiRun() { assert(gWindow); + { + // check if we have a valid window size from settings and apply it + int width = (int)gScene->box.size.x; + int height = (int)gScene->box.size.y; + int x = (int)gScene->box.pos.x; + int y = (int)gScene->box.pos.y; + if( width > 0 && height > 0 && x >= 0 && y >= 0 ) { + glfwSetWindowSize(gWindow, width, height); + glfwSetWindowPos(gWindow, x, y); + } else { + glfwMaximizeWindow(gWindow); + // help me Obi Wan: if we do not do this, widgets are not correclty drawn + int width, height; + glfwGetWindowSize(gWindow, &width, &height); + glfwSetWindowSize(gWindow, width, height); + } + } { int width, height; glfwGetWindowSize(gWindow, &width, &height); diff --git a/src/settings.cpp b/src/settings.cpp index 95ea7ed9..bcac14ab 100644 --- a/src/settings.cpp +++ b/src/settings.cpp @@ -48,6 +48,10 @@ static json_t *settingsToJson() { json_t *lastPathJ = json_string(gRackWidget->lastPath.c_str()); json_object_set_new(rootJ, "lastPath", lastPathJ); + // window size and position + json_t *winJ = json_pack("[i, i, i, i]", (int)gScene->box.size.x, (int)gScene->box.size.y, (int)gScene->box.pos.x, (int)gScene->box.pos.y ); + json_object_set_new(rootJ, "window", winJ); + return rootJ; } @@ -95,6 +99,22 @@ static void settingsFromJson(json_t *rootJ) { json_t *lastPathJ = json_object_get(rootJ, "lastPath"); if (lastPathJ) gRackWidget->lastPath = json_string_value(lastPathJ); + + // window size and position + json_t *winJ = json_object_get(rootJ, "window"); + int w = 0; + int h = 0; + int x = 0; + int y = 0; + json_unpack(winJ, "[i, i, i, i]", &w, &h, &x, &y); + if( x < 0 ) + x = 0; + if( y < 0 ) + y = 0; + if(w > 0 && h > 0) { + gScene->box.size = Vec(w, h); + gScene->box.pos = Vec(x, y); + } }