From 1fe319d234e828c17fccb8a5226b16dd54ff0a00 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Camilla=20L=C3=B6wy?= <elmindreda@glfw.org>
Date: Thu, 16 Nov 2017 11:34:42 +0100
Subject: [PATCH] Cocoa: Filter out duplicate size events

Fixes #1085.
---
 README.md            |  1 +
 src/cocoa_platform.h |  4 ++++
 src/cocoa_window.m   | 28 +++++++++++++++++++++++++---
 3 files changed, 30 insertions(+), 3 deletions(-)

diff --git a/README.md b/README.md
index d9de5f85..1aa9bf12 100644
--- a/README.md
+++ b/README.md
@@ -246,6 +246,7 @@ information on what to include when reporting a bug.
 - [Cocoa] Bugfix: Some characters did not repeat due to Press and Hold (#1010)
 - [Cocoa] Bugfix: Window title was lost when full screen or undecorated (#1082)
 - [Cocoa] Bugfix: Window was resized twice when entering full screen (#1085)
+- [Cocoa] Bugfix: Duplicate size events were not filtered (#1085)
 - [WGL] Added support for `WGL_EXT_colorspace` for OpenGL ES contexts
 - [WGL] Added support for `WGL_ARB_create_context_no_error`
 - [GLX] Added support for `GLX_ARB_create_context_no_error`
diff --git a/src/cocoa_platform.h b/src/cocoa_platform.h
index 61d0ee91..d5cc2379 100644
--- a/src/cocoa_platform.h
+++ b/src/cocoa_platform.h
@@ -88,6 +88,10 @@ typedef struct _GLFWwindowNS
 
     GLFWbool        maximized;
 
+    // Cached window and framebuffer sizes used to filter out duplicate events
+    int             width, height;
+    int             fbWidth, fbHeight;
+
     // The total sum of the distances the cursor has been warped
     // since the last cursor motion event was processed
     // This is kept to counteract Cocoa doing the same internally
diff --git a/src/cocoa_window.m b/src/cocoa_window.m
index 58d6362d..16fbb7bf 100644
--- a/src/cocoa_window.m
+++ b/src/cocoa_window.m
@@ -267,8 +267,21 @@ static const NSRange kEmptyRange = { NSNotFound, 0 };
     const NSRect contentRect = [window->ns.view frame];
     const NSRect fbRect = [window->ns.view convertRectToBacking:contentRect];
 
-    _glfwInputFramebufferSize(window, fbRect.size.width, fbRect.size.height);
-    _glfwInputWindowSize(window, contentRect.size.width, contentRect.size.height);
+    if (fbRect.size.width != window->ns.fbWidth ||
+        fbRect.size.height != window->ns.fbHeight)
+    {
+        window->ns.fbWidth  = fbRect.size.width;
+        window->ns.fbHeight = fbRect.size.height;
+        _glfwInputFramebufferSize(window, fbRect.size.width, fbRect.size.height);
+    }
+
+    if (contentRect.size.width != window->ns.width ||
+        contentRect.size.height != window->ns.height)
+    {
+        window->ns.width  = contentRect.size.width;
+        window->ns.height = contentRect.size.height;
+        _glfwInputWindowSize(window, contentRect.size.width, contentRect.size.height);
+    }
 }
 
 - (void)windowDidMove:(NSNotification *)notification
@@ -551,7 +564,13 @@ static const NSRange kEmptyRange = { NSNotFound, 0 };
     const NSRect contentRect = [window->ns.view frame];
     const NSRect fbRect = [window->ns.view convertRectToBacking:contentRect];
 
-    _glfwInputFramebufferSize(window, fbRect.size.width, fbRect.size.height);
+    if (fbRect.size.width != window->ns.fbWidth ||
+        fbRect.size.height != window->ns.fbHeight)
+    {
+        window->ns.fbWidth  = fbRect.size.width;
+        window->ns.fbHeight = fbRect.size.height;
+        _glfwInputFramebufferSize(window, fbRect.size.width, fbRect.size.height);
+    }
 }
 
 - (void)drawRect:(NSRect)rect
@@ -1095,6 +1114,9 @@ static GLFWbool createNativeWindow(_GLFWwindow* window,
     [window->ns.object setAcceptsMouseMovedEvents:YES];
     [window->ns.object setRestorable:NO];
 
+    _glfwPlatformGetWindowSize(window, &window->ns.width, &window->ns.height);
+    _glfwPlatformGetFramebufferSize(window, &window->ns.fbWidth, &window->ns.fbHeight);
+
     return GLFW_TRUE;
 }