From 9b89b7ab7208cac0d7da91bb9a1811d6738c82a7 Mon Sep 17 00:00:00 2001 From: Romain Vimont Date: Mon, 14 Jun 2021 21:24:51 +0200 Subject: [PATCH] Center the window on resize-to-fit When removing the black borders (by double-clicking on them, or by pressing MOD+w), the window is resized to fit the device screen, but its top-left position was left unchanged. Instead, move the window so that the new window area is at the center of the old window area. Refs #2387 --- app/src/screen.c | 32 ++++++++++++++++++++++++-------- 1 file changed, 24 insertions(+), 8 deletions(-) diff --git a/app/src/screen.c b/app/src/screen.c index c70a95e3..99327b3b 100644 --- a/app/src/screen.c +++ b/app/src/screen.c @@ -41,6 +41,18 @@ get_window_size(const struct screen *screen) { return size; } +static struct point +get_window_position(const struct screen *screen) { + int x; + int y; + SDL_GetWindowPosition(screen->window, &x, &y); + + struct point point; + point.x = x; + point.y = y; + return point; +} + // set the window size to be applied when fullscreen is disabled static void set_window_size(struct screen *screen, struct size new_size) { @@ -122,13 +134,6 @@ get_optimal_size(struct size current_size, struct size content_size) { return window_size; } -// same as get_optimal_size(), but read the current size from the window -static inline struct size -get_optimal_window_size(const struct screen *screen, struct size content_size) { - struct size window_size = get_window_size(screen); - return get_optimal_size(window_size, content_size); -} - // initially, there is no current size, so use the frame size as current size // req_width and req_height, if not 0, are the sizes requested by the user static inline struct size @@ -662,9 +667,20 @@ screen_resize_to_fit(struct screen *screen) { return; } + struct point point = get_window_position(screen); + struct size window_size = get_window_size(screen); + struct size optimal_size = - get_optimal_window_size(screen, screen->content_size); + get_optimal_size(window_size, screen->content_size); + + // Center the window related to the device screen + assert(optimal_size.width <= window_size.width); + assert(optimal_size.height <= window_size.height); + uint32_t new_x = point.x + (window_size.width - optimal_size.width) / 2; + uint32_t new_y = point.y + (window_size.height - optimal_size.height) / 2; + SDL_SetWindowSize(screen->window, optimal_size.width, optimal_size.height); + SDL_SetWindowPosition(screen->window, new_x, new_y); LOGD("Resized to optimal size: %ux%u", optimal_size.width, optimal_size.height); }