Subject: Re: bug#72986: Disabling menu-bar-mode changes size of new frames

From: martin rudalics <[email-masked]>
Date: Sat, 14 Sep 2024 16:43:29 +0200

> Here is the frame size history:

Thanks.

 > x_create_frame_2 (0), MS=160x175
 > xg_frame_set_char_size, invisible, PS=1328x1260, XS=1328x1260, DS=1328x1260
 > xg_frame_set_char_size (5), MS=32x70 IH IV
 > x_make_frame_visible
 > MapNotify, not hidden & not iconified, PS=1328x1260, DS=1328x1260
 > ConfigureNotify, PS=1328x1260, XS=400x340, DS=1328x1260

Here we go again with the small size, reject it and ask for the previous
size ...

 > xg_frame_resized, rejected, PS=1328x1260, XS=400x340, DS=1328x1260
 > tool-bar-lines (2), MS=160x175
 > xg_frame_set_char_size, visible, PS=1328x1260, XS=1328x1260, DS=1328x1260
 > ConfigureNotify, PS=1328x1260, XS=2560x1346, DS=1328x1260

... but now all of a sudden we should handle a frame size about twice as
large which we reject again ...

 > xg_frame_resized, rejected, PS=1328x1260, XS=2560x1346, DS=1328x1260
 > ConfigureNotify, PS=1328x1260, XS=2560x1326, DS=1328x1260
 > xg_frame_resized, rejected, PS=1328x1260, XS=2560x1326, DS=1328x1260
 > set_window_configuration (4), MS=160x175 IH IV
 >
 > Note that something quite different happens when I open the second frame:
 > it ends up full screen, but the Emacs window only occupies the same space
 > as the initial window (roughly half the screen wide, and a little shorter
 > than the screen).

... and never fill accordingly because we don't like it.  This was a bad
idea.  The second call of gtk_window_resize simply has to get the same
dimensions as the first one as in the first version of this patch.

Just that the "first version" of this patch had another problem: The
sizes we ask for in gtk_window_resize via outer_height and outer_width
include tool and menu bars.  The sizes reported by ConfigureNotify are
those of the native rectangle only.  I hopefully fixed that now.  Please
try again and tell me the histories you get here for the initial frame
and the second frame both with and without the menu bar by evaluating
(frame--size-history).

 > frame-char-width: 16
 > frame-char-height: 35

Times twice gets us the MS=32x70 above.

Next I'd like to know what you see with

(let ((frame (make-frame '((visibility . nil)))))
   (make-frame-visible frame))

and

(let ((frame (make-frame '((visibility . nil) (menu-bar-lines . 0)))))
   (make-frame-visible frame))

both with an unpatched Emacs and a patched one.  Here the second frame
appears exactly where the first one is, so we would have to care about
the placement ourselves.

I only want to know whether this "visibly" improves the behavior you
observe (no need to look at the history of these frames).  The idea is
that we could make creation of the second frame smoother so that you
don't see its initial size.

martin

diff --git a/src/frame.c b/src/frame.c
index 7f4bf274ad9..6b6f6aa3c5c 100644
--- a/src/frame.c
+++ b/src/frame.c
@@ -6769,7 +6769,7 @@ focus (where a frame immediately loses focus when it's left by the mouse
 
 The function `frame--size-history' displays the value of this variable
 in a more readable form.  */);
-    frame_size_history = Qnil;
+  frame_size_history = Fcons (make_fixnum (100), Qnil);
 
   DEFVAR_BOOL ("tooltip-reuse-hidden-frame", tooltip_reuse_hidden_frame,
 	       doc: /* Non-nil means reuse hidden tooltip frames.
diff --git a/src/gtkutil.c b/src/gtkutil.c
index d57627f152f..649cc3c8030 100644
--- a/src/gtkutil.c
+++ b/src/gtkutil.c
@@ -1129,11 +1129,47 @@ xg_set_geometry (struct frame *f)
     }
 }
 
+static struct frame *last_resize_frame = NULL;
+static int last_resize_height = -1;
+static int last_resize_width = -1;
+static int last_resize_count = 0;
+
 /** Function to handle resize of native frame F to WIDTH and HEIGHT
     pixels after we got a ConfigureNotify event.  */
 void
 xg_frame_resized (struct frame *f, int width, int height)
 {
+#ifndef HAVE_PGTK
+  if (f == last_resize_frame
+      && (width != last_resize_width - FRAME_TOOLBAR_WIDTH (f)
+	  || height != (last_resize_height
+			- FRAME_MENUBAR_HEIGHT (f)
+			- FRAME_TOOLBAR_HEIGHT (f)))
+      && last_resize_count <= 3)
+    /* We did not get what we wanted, retry.  */
+    {
+      if (CONSP (frame_size_history))
+	frame_size_history_extra
+	  (f, build_string ("xg_frame_resized, rejected"),
+	   FRAME_PIXEL_WIDTH (f), FRAME_PIXEL_HEIGHT (f), width, height,
+	   last_resize_width, last_resize_height);
+
+      gtk_window_resize (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)),
+			 last_resize_width, last_resize_height);
+
+      last_resize_count++;
+
+      return;
+    }
+  else
+    /* We either got what we asked for or lost the battle.  */
+    {
+      last_resize_frame = NULL;
+      last_resize_height = -1;
+      last_resize_width = -1;
+      last_resize_count = 0;
+    }
+#endif
   /* Ignore case where size of native rectangle didn't change.  */
   if (width != FRAME_PIXEL_WIDTH (f)
       || height != FRAME_PIXEL_HEIGHT (f)
@@ -1297,19 +1333,20 @@ xg_frame_set_char_size (struct frame *f, int width, int height)
   else
     {
 #ifndef HAVE_PGTK
+      last_resize_frame = f;
+      last_resize_width = outer_width;
+      last_resize_height = outer_height;
+      last_resize_count = 0;
+
       gtk_window_resize (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)),
 			 outer_width, outer_height);
 #else
       if (FRAME_GTK_OUTER_WIDGET (f))
-	{
-	  gtk_window_resize (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)),
-			     outer_width, outer_height);
-	}
+	gtk_window_resize (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)),
+			   outer_width, outer_height);
       else
-	{
-	  gtk_widget_set_size_request (FRAME_GTK_WIDGET (f),
-				       outer_width, outer_height);
-	}
+	gtk_widget_set_size_request (FRAME_GTK_WIDGET (f),
+				     outer_width, outer_height);
 #endif
       fullscreen = Qnil;
     }
@@ -1327,10 +1364,16 @@ xg_frame_set_char_size (struct frame *f, int width, int height)
   if (FRAME_VISIBLE_P (f) && !was_visible)
     {
       if (CONSP (frame_size_history))
-	frame_size_history_extra
-	  (f, build_string ("xg_frame_set_char_size, visible"),
-	   FRAME_PIXEL_WIDTH (f), FRAME_PIXEL_HEIGHT (f), width, height,
-	   f->new_width, f->new_height);
+	{
+	  frame_size_history_extra
+	    (f, build_string ("xg_frame_set_char_size, visible"),
+	     FRAME_PIXEL_WIDTH (f), FRAME_PIXEL_HEIGHT (f), width, height,
+	     f->new_width, f->new_height);
+
+	  if (gwidth > 0 || gheight > 0)
+	    frame_size_history_extra
+	      (f, build_string (" gvalues"), gwidth, gheight, -1, -1, -1, -1);
+	}
 
       /* Must call this to flush out events */
       (void)gtk_events_pending ();