Skip to content

Commit

Permalink
Center the window on resize-to-fit
Browse files Browse the repository at this point in the history
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 <#2387>
  • Loading branch information
rom1v committed Jun 14, 2021
1 parent 7343b23 commit 9b89b7a
Showing 1 changed file with 24 additions and 8 deletions.
32 changes: 24 additions & 8 deletions app/src/screen.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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);
}
Expand Down

0 comments on commit 9b89b7a

Please sign in to comment.