Subject: Re: bug#28872: [PATCH v4] Fix NS frame resizing issues (bug#40200, bug#28872)

From: Alan Third <[email-masked]>
Date: Sat, 18 Apr 2020 16:26:22 +0100

On Sat, Apr 18, 2020 at 10:12:27PM +0800, HaiJun Zhang wrote:
> I can reproduce it with the following lines in ~/.emacs:
> 
> (setq inhibit-startup-message t
>    inhibit-startup-buffer-menu t)
> 
> (setq default-frame-alist ‘(
>        (width . 85)
>        (height . 35)
>        (vertical-scroll-bars . nil)
>        (horizontal-scroll-bars . nil)
>        ))
> 
> (set-frame-font “Monospace 16”)

Can you please try the attached patch.
-- 
Alan Third
>From 836c19eae4802a3aedee887669425f949971eb4a Mon Sep 17 00:00:00 2001
From: Alan Third <[email-masked]>
Date: Sat, 18 Apr 2020 16:22:06 +0100
Subject: [PATCH] Fix initial frame resizing issue on NS (bug#40200)

* src/nsterm.m ([EmacsView viewDidResize:]): Don't try to determine
the old size when not drawing to the buffer.
---
 src/nsterm.m | 51 +++++++++++++++++++++------------------------------
 1 file changed, 21 insertions(+), 30 deletions(-)

diff --git a/src/nsterm.m b/src/nsterm.m
index 9cd1c9d860..d85a579e38 100644
--- a/src/nsterm.m
+++ b/src/nsterm.m
@@ -7322,48 +7322,39 @@ - (void)viewDidEndLiveResize
 - (void)viewDidResize:(NSNotification *)notification
 {
   NSRect frame = [self frame];
-  int oldw, oldh, neww, newh;
+  int neww, newh;
 
   if (! FRAME_LIVE_P (emacsframe))
     return;
 
+  NSTRACE ("[EmacsView viewDidResize]");
+
+  neww = (int)NSWidth (frame);
+  newh = (int)NSHeight (frame);
+  NSTRACE_SIZE ("New size", NSMakeSize (neww, newh));
+
 #ifdef NS_DRAW_TO_BUFFER
-#if MAC_OS_X_VERSION_MIN_REQUIRED < 101400
   if ([self wantsUpdateLayer])
     {
-#endif
       CGFloat scale = [[self window] backingScaleFactor];
-      oldw = (CGFloat)CGBitmapContextGetWidth (drawingBuffer) / scale;
-      oldh = (CGFloat)CGBitmapContextGetHeight (drawingBuffer) / scale;
-#if MAC_OS_X_VERSION_MIN_REQUIRED < 101400
-    }
-  else
-    {
-#endif
-#endif /* NS_DRAW_TO_BUFFER */
-#if !defined (NS_DRAW_TO_BUFFER) || MAC_OS_X_VERSION_MIN_REQUIRED < 101400
-      oldw = FRAME_PIXEL_WIDTH (emacsframe);
-      oldh = FRAME_PIXEL_HEIGHT (emacsframe);
-#endif
-#if defined (NS_DRAW_TO_BUFFER) && MAC_OS_X_VERSION_MIN_REQUIRED < 101400
-    }
-#endif
-
-  neww = (int)NSWidth (frame);
-  newh = (int)NSHeight (frame);
+      int oldw = (CGFloat)CGBitmapContextGetWidth (drawingBuffer) / scale;
+      int oldh = (CGFloat)CGBitmapContextGetHeight (drawingBuffer) / scale;
 
-  NSTRACE ("[EmacsView viewDidResize]");
+      NSTRACE_SIZE ("Original size", NSMakeSize (oldw, oldh));
 
-  /* Don't want to do anything when the view size hasn't changed. */
-  if ((oldh == newh && oldw == neww))
-    {
-      NSTRACE_MSG ("No change");
-      return;
+      /* Don't want to do anything when the view size hasn't changed. */
+      if ((oldh == newh && oldw == neww))
+        {
+          NSTRACE_MSG ("No change");
+          return;
+        }
     }
+#endif
 
-  NSTRACE_SIZE ("Original size", NSMakeSize (oldw, oldh));
-  NSTRACE_SIZE ("New size", NSMakeSize (neww, newh));
-
+  /* I'm not sure if it's safe to call this every time the view
+     changes size, as Emacs may already know about the change.
+     Unfortunately there doesn't seem to be a bullet-proof method of
+     determining whether we need to call it or not.  */
   change_frame_size (emacsframe,
                      FRAME_PIXEL_TO_TEXT_WIDTH (emacsframe, neww),
                      FRAME_PIXEL_TO_TEXT_HEIGHT (emacsframe, newh),
-- 
2.26.1