diff --git a/src/app.c b/src/app.c index 00d7e9c8..c43116a1 100644 --- a/src/app.c +++ b/src/app.c @@ -104,11 +104,12 @@ void app_entry_point(void) /* Startup window & renderer */ struct sys_window window = sys_window_alloc(); - - /* Read window settings from file */ - struct sys_window_settings window_settings = settings_default_window_settings(&window); - settings_read_from_file(&window_settings); - sys_window_update_settings(&window, &window_settings); + { + /* Read window settings from file */ + struct sys_window_settings window_settings = settings_default_window_settings(&window); + settings_read_from_file(&window_settings); + sys_window_update_settings(&window, &window_settings); + } renderer_startup(&window); @@ -129,6 +130,8 @@ void app_entry_point(void) user_startup(&window); playback_startup(); + + /* Show window */ sys_window_show(&window); /* Wait for app_quit() */ diff --git a/src/settings.c b/src/settings.c index 62b1f098..880e2927 100644 --- a/src/settings.c +++ b/src/settings.c @@ -61,7 +61,6 @@ INTERNAL void deserialize_window_settings(struct buffer json_bytes, struct sys_w goto end; } - const struct json_val *minimized = json_object_get(window_settings_json, STR("minimized")); const struct json_val *maximized = json_object_get(window_settings_json, STR("maximized")); const struct json_val *fullscreen = json_object_get(window_settings_json, STR("fullscreen")); const struct json_val *width = json_object_get(window_settings_json, STR("width")); @@ -69,13 +68,10 @@ INTERNAL void deserialize_window_settings(struct buffer json_bytes, struct sys_w const struct json_val *x = json_object_get(window_settings_json, STR("x")); const struct json_val *y = json_object_get(window_settings_json, STR("y")); - if (!(json_is_bool(minimized) && json_bool(minimized))) { - /* Only trust pos/size settings if window wasn't closed while minimized */ - settings->floating_x = json_is_number(x) ? (i32)clamp_f64(json_number(x), I32_MIN, I32_MAX) : settings->floating_x; - settings->floating_y = json_is_number(y) ? (i32)clamp_f64(json_number(y), I32_MIN, I32_MAX) : settings->floating_y; - settings->floating_width = json_is_number(width) ? (i32)clamp_f64(json_number(width), I32_MIN, I32_MAX) : settings->floating_width; - settings->floating_height = json_is_number(height) ? (i32)clamp_f64(json_number(height), I32_MIN, I32_MAX) : settings->floating_height; - } + settings->floating_x = json_is_number(x) ? (i32)clamp_f64(json_number(x), I32_MIN, I32_MAX) : settings->floating_x; + settings->floating_y = json_is_number(y) ? (i32)clamp_f64(json_number(y), I32_MIN, I32_MAX) : settings->floating_y; + settings->floating_width = json_is_number(width) ? (i32)clamp_f64(json_number(width), I32_MIN, I32_MAX) : settings->floating_width; + settings->floating_height = json_is_number(height) ? (i32)clamp_f64(json_number(height), I32_MIN, I32_MAX) : settings->floating_height; settings->flags = json_is_bool(maximized) ? (json_bool(maximized) ? (settings->flags | SYS_WINDOW_SETTINGS_FLAG_MAXIMIZED) : (settings->flags & ~SYS_WINDOW_SETTINGS_FLAG_MAXIMIZED)) : settings->flags; settings->flags = json_is_bool(fullscreen) ? (json_bool(fullscreen) ? (settings->flags | SYS_WINDOW_SETTINGS_FLAG_FULLSCREEN) : (settings->flags & ~SYS_WINDOW_SETTINGS_FLAG_FULLSCREEN)) : settings->flags; diff --git a/src/sys.h b/src/sys.h index 48fbbc23..413b303c 100644 --- a/src/sys.h +++ b/src/sys.h @@ -282,8 +282,6 @@ struct sys_window_settings sys_window_get_settings(struct sys_window *sys_window void sys_window_show(struct sys_window *sys_window); -void sys_window_set_cursor_pos(struct sys_window *sys_window, struct v2 pos); - struct v2 sys_window_get_size(struct sys_window *sys_window); struct v2 sys_window_get_monitor_size(struct sys_window *sys_window); diff --git a/src/sys_win32.c b/src/sys_win32.c index 08334c79..37a6f5e1 100644 --- a/src/sys_win32.c +++ b/src/sys_win32.c @@ -53,8 +53,6 @@ struct win32_window { i32 monitor_width; i32 monitor_height; - i32 border_left; - i32 border_top; /* NOTE: width & height are unaffected by window minimization (they retain * their pre-minimized values) */ i32 x, y, width, height; @@ -590,7 +588,7 @@ INTERNAL void window_thread_entry_point(void *arg) SetCursorPos(p.x, p.y); } - /* Stop keep in window */ + /* Stop clipping cursor */ if (cursor_flags & WIN32_WINDOW_CURSOR_SET_FLAG_DISABLE_CLIP) { ClipCursor(NULL); } @@ -613,6 +611,7 @@ INTERNAL void window_thread_entry_point(void *arg) window->cursor_set_flags = 0; } + /* Process message */ switch (msg.message) { case WM_QUIT: { win32_window_process_event(window, (struct sys_event) { .kind = SYS_EVENT_KIND_QUIT }); @@ -720,16 +719,12 @@ INTERNAL void win32_update_window_from_system(struct win32_window *window) } } - /* Window borders */ - window->border_left = client_rect.left - window_rect.left; - window->border_top = client_rect.top - window_rect.top; - /* Window dimensions */ i32 x = client_rect.left; i32 y = client_rect.top; i32 width = client_rect.right - client_rect.left; i32 height = client_rect.bottom - client_rect.top; - if (!(window->flags & SYS_WINDOW_SETTINGS_FLAG_MINIMIZED)) { + if (!(window->settings.flags & SYS_WINDOW_SETTINGS_FLAG_MINIMIZED)) { window->x = x; window->y = y; window->width = width; @@ -755,49 +750,52 @@ INTERNAL void win32_update_window_from_settings(struct win32_window *window, str struct sys_window_settings old_settings = window->settings; window->settings = *settings; - //u32 win32_flags = SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER | SWP_NOOWNERZORDER | SWP_FRAMECHANGED; - u32 win32_flags = SWP_NOZORDER | SWP_NOOWNERZORDER; - i32 x = 0; - i32 y = 0; - i32 width = 0; - i32 height = 0; + i32 show_cmd = SW_HIDE; + if (window->flags & SYS_WINDOW_FLAG_SHOWING) { + show_cmd = SW_NORMAL; + if (settings->flags & SYS_WINDOW_SETTINGS_FLAG_MAXIMIZED) { + show_cmd = SW_SHOWMAXIMIZED; + } else if (settings->flags & SYS_WINDOW_SETTINGS_FLAG_MINIMIZED) { + show_cmd = SW_MINIMIZE; + } + } + + RECT rect = { 0 }; b32 old_fullscreen = old_settings.flags & SYS_WINDOW_SETTINGS_FLAG_FULLSCREEN; b32 fullscreen = settings->flags & SYS_WINDOW_SETTINGS_FLAG_FULLSCREEN; - /* FIXME: Window in wrong size when coming out of fullscreen */ if (fullscreen) { - x = 0; - y = 0; - width = window->monitor_width; - height = window->monitor_height; - } else { - x = settings->floating_x - window->border_left; - y = settings->floating_y - window->border_top; - width = settings->floating_width + (window->border_left * 2); - /* I'm unsure why border_left being added to border_top is needed to - * retain the correct height. Coincidence? */ - height = settings->floating_height + (window->border_left + window->border_top); - } - - if (fullscreen != old_fullscreen) { - if (fullscreen) { + if (!old_fullscreen) { + /* Entering fullscreen */ SetWindowLongPtrA(hwnd, GWL_STYLE, WS_POPUP); - } else { + } + rect = (RECT) { + .left = 0, + .top = 0, + .right = window->monitor_width, + .bottom = window->monitor_height + }; + } else { + if (old_fullscreen) { + /* Leaving fullscreen */ SetWindowLongPtrA(hwnd, GWL_STYLE, WS_OVERLAPPEDWINDOW); } - win32_flags |= SWP_SHOWWINDOW; + rect = (RECT) { + .left = settings->floating_x, + .top = settings->floating_y, + .right = settings->floating_x + settings->floating_width, + .bottom = settings->floating_y + settings->floating_height + }; + AdjustWindowRect(&rect, WS_OVERLAPPEDWINDOW, false); } - SetWindowPos( - hwnd, - 0, - x, - y, - width, - height, - win32_flags - ); + WINDOWPLACEMENT wp = { + .length = sizeof(WINDOWPLACEMENT), + .showCmd = show_cmd, + .rcNormalPosition = rect + }; + SetWindowPlacement(hwnd, &wp); SetWindowTextA(hwnd, settings->title); } @@ -805,7 +803,7 @@ INTERNAL void win32_update_window_from_settings(struct win32_window *window, str INTERNAL void win32_window_wake(struct win32_window *window) { /* Post a blank message to the window's thread message queue to wake it. */ - PostThreadMessageA(window->tid, WM_NULL, 0, 0); + PostMessageA(window->hwnd, WM_NULL, 0, 0); } INTERNAL LRESULT CALLBACK win32_window_proc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam) @@ -813,15 +811,6 @@ INTERNAL LRESULT CALLBACK win32_window_proc(HWND hwnd, UINT msg, WPARAM wparam, struct win32_window *window = (struct win32_window *)GetWindowLongPtrA(hwnd, GWLP_USERDATA); if (!window) { - /* Window should be retrievable unless we're in any of these - * msgs triggered during initialization / destruction */ -#if 0 - ASSERT(msg == WM_GETMINMAXINFO - || msg == WM_NCCREATE - || msg == WM_NCCALCSIZE - || msg == WM_CREATE - || msg == WM_NCDESTROY); -#endif return DefWindowProcA(hwnd, msg, wparam, lparam); }