Make live resize code a little cleaner

This commit is contained in:
Kovid Goyal 2019-02-28 19:42:24 +05:30
parent 3bd1ca0ac3
commit b1bd581065
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
3 changed files with 32 additions and 15 deletions

View File

@ -807,16 +807,22 @@ process_pending_resizes(double now) {
global_state.has_pending_resizes = false;
for (size_t i = 0; i < global_state.num_os_windows; i++) {
OSWindow *w = global_state.os_windows + i;
if (w->has_pending_resizes) {
if (w->has_live_resize_information) {
if (!w->live_resize_in_progress) update_os_window_viewport(w, true);
if (w->live_resize.in_progress) {
bool update_viewport = false;
if (w->live_resize.from_os_notification) {
if (w->live_resize.os_says_resize_complete || (now - w->live_resize.last_resize_event_at) > 1) update_viewport = true;
} else {
if (now - w->last_resize_event_at >= RESIZE_DEBOUNCE_TIME) update_os_window_viewport(w, true);
if (now - w->live_resize.last_resize_event_at >= RESIZE_DEBOUNCE_TIME) update_viewport = true;
else {
global_state.has_pending_resizes = true;
set_maximum_wait(RESIZE_DEBOUNCE_TIME - now + w->last_resize_event_at);
set_maximum_wait(RESIZE_DEBOUNCE_TIME - now + w->live_resize.last_resize_event_at);
}
}
if (update_viewport) {
static const LiveResizeInfo empty = {0};
update_os_window_viewport(w, true);
w->live_resize = empty;
}
}
}
}

View File

@ -44,7 +44,6 @@ update_os_window_viewport(OSWindow *window, bool notify_boss) {
bool dpi_changed = (xr != 0.0 && xr != window->viewport_x_ratio) || (yr != 0.0 && yr != window->viewport_y_ratio) || (xdpi != window->logical_dpi_x) || (ydpi != window->logical_dpi_y);
window->viewport_size_dirty = true;
window->has_pending_resizes = false;
window->viewport_width = MAX(window->viewport_width, 100);
window->viewport_height = MAX(window->viewport_height, 100);
window->window_width = MAX(w, 100);
@ -131,9 +130,13 @@ window_iconify_callback(GLFWwindow *window, int iconified UNUSED) {
static void
live_resize_callback(GLFWwindow *w, bool started) {
if (!set_callback_window(w)) return;
global_state.callback_os_window->has_live_resize_information = true;
global_state.callback_os_window->live_resize_in_progress = started;
if (!started) request_tick_callback();
global_state.callback_os_window->live_resize.from_os_notification = true;
global_state.callback_os_window->live_resize.in_progress = true;
global_state.has_pending_resizes = true;
if (!started) {
global_state.callback_os_window->live_resize.os_says_resize_complete = true;
request_tick_callback();
}
global_state.callback_os_window = NULL;
}
@ -142,8 +145,9 @@ framebuffer_size_callback(GLFWwindow *w, int width, int height) {
if (!set_callback_window(w)) return;
if (width >= min_width && height >= min_height) {
OSWindow *window = global_state.callback_os_window;
window->has_pending_resizes = true; global_state.has_pending_resizes = true;
window->last_resize_event_at = monotonic();
global_state.has_pending_resizes = true;
window->live_resize.in_progress = true;
window->live_resize.last_resize_event_at = monotonic();
#ifdef __APPLE__
// On cocoa request_tick_callback() does not work while the window
// is being resized, because that happens in a sub-loop. With semi-transparent
@ -166,8 +170,8 @@ dpi_change_callback(GLFWwindow *w, float x_scale UNUSED, float y_scale UNUSED) {
// Ensure update_os_window_viewport() is called in the near future, it will
// take care of DPI changes.
OSWindow *window = global_state.callback_os_window;
window->has_pending_resizes = true; global_state.has_pending_resizes = true;
window->last_resize_event_at = monotonic();
window->live_resize.in_progress = true; global_state.has_pending_resizes = true;
window->live_resize.last_resize_event_at = monotonic();
global_state.callback_os_window = NULL;
request_tick_callback();
}

View File

@ -106,6 +106,14 @@ typedef struct {
enum RENDER_STATE { RENDER_FRAME_NOT_REQUESTED, RENDER_FRAME_REQUESTED, RENDER_FRAME_READY };
typedef struct {
double last_resize_event_at;
bool in_progress;
bool from_os_notification;
bool os_says_resize_complete;
} LiveResizeInfo;
typedef struct {
void *handle;
id_type id;
@ -125,8 +133,7 @@ typedef struct {
PyObject *window_title;
bool is_key_pressed[MAX_KEY_COUNT];
bool viewport_size_dirty;
double last_resize_event_at;
bool has_live_resize_information, live_resize_in_progress;
LiveResizeInfo live_resize;
bool has_pending_resizes, is_semi_transparent, shown_once, is_damaged;
uint32_t offscreen_texture_id;
unsigned int clear_count;