Subject: Re: bug#54623: No scroll bars on macOS builds

From: Alan Third <[email-masked]>
Date: Wed, 30 Mar 2022 22:52:59 +0100

On Tue, Mar 29, 2022 at 02:44:28PM +0200, Lars Ingebrigtsen wrote:
> Angelo Graziosi <[email-masked]> writes:
> 
> > Emacs builds on macOS 10.13.6 do not show scroll bars even if both
> > vertical and horizontal ones are enabled. The attached screenshot
> > refers to 'Emacs -Q'.
> 
> I can't reproduce this on Macos 12.1.

I can't reproduce it on 10.14 either. I suspect it's some subtle
difference in layer handling between 10.13 and 10.14+.

> > The first build in which scroll bars are gone or hidden looks
> >
> > 2)
> > commit: a4d2c88cdee90a3e4863a62c4ff69896d0c1a347
> > Alan Third 2021-07-31 11:13:05 +0100
> > Simplify macOS drawing code
> 
> Perhaps Alan has some comments; added to the CCs.

Maybe the attached will help.
-- 
Alan Third
>From aef95a32f2260fdadbbd0903b5d3bb0b1220a9db Mon Sep 17 00:00:00 2001
From: Alan Third <[email-masked]>
Date: Wed, 30 Mar 2022 22:40:03 +0100
Subject: [PATCH] Fix scrollbars on macOS 10.13 and below (bug#54623)

* src/nsterm.h (NSAppKitVersionNumber10_14): Required for
makeBackingLayer version check.
* src/nsterm.m (USING_EMACSLAYER): Check whether we're using layers
and whether the layer is an EmacsLayer class.
([EmacsView makeBackingLayer]): Only use EmacsLayer on macOS 10.14 and above.
([EmacsView lockFocus]):
([EmacsView unlockFocus]):
([EmacsView windowDidChangeBackingProperties:]):
([EmacsView copyRect:to:]): Replace check for wantsLayer with
USING_EMACSLAYER.
---
 src/nsterm.h |  4 ++++
 src/nsterm.m | 26 +++++++++++++++++---------
 2 files changed, 21 insertions(+), 9 deletions(-)

diff --git a/src/nsterm.h b/src/nsterm.h
index f027646123..ea92d8e893 100644
--- a/src/nsterm.h
+++ b/src/nsterm.h
@@ -1239,6 +1239,10 @@ #define NSAppKitVersionNumber10_7 1138
 #ifndef NSAppKitVersionNumber10_10
 #define NSAppKitVersionNumber10_10 1343
 #endif
+
+#ifndef NSAppKitVersionNumber10_14
+#define NSAppKitVersionNumber10_14 1671
+#endif
 #endif /* NS_IMPL_COCOA */

diff --git a/src/nsterm.m b/src/nsterm.m
index fd56094c28..b9ae512011 100644
--- a/src/nsterm.m
+++ b/src/nsterm.m
@@ -5954,6 +5954,9 @@ - (BOOL)fulfillService: (NSString *)name withArg: (NSString *)arg
 
 @implementation EmacsView
 
+#define USING_EMACSLAYER \
+  ([self wantsLayer] && [[self layer] isKindOfClass:[EmacsLayer class]])
+
 /* Needed to inform when window closed from lisp.  */
 - (void) setWindowClosing: (BOOL)closing
 {
@@ -7864,12 +7867,17 @@ - (instancetype)toggleToolbar: (id)sender
 #ifdef NS_IMPL_COCOA
 - (CALayer *)makeBackingLayer;
 {
-  EmacsLayer *l = [[EmacsLayer alloc]
-                    initWithColorSpace:[[[self window] colorSpace] CGColorSpace]];
-  [l setDelegate:(id)self];
-  [l setContentsScale:[[self window] backingScaleFactor]];
+  if (NSAppKitVersionNumber >= NSAppKitVersionNumber10_14)
+    {
+      EmacsLayer *l = [[EmacsLayer alloc]
+                        initWithColorSpace:[[[self window] colorSpace] CGColorSpace]];
+      [l setDelegate:(id)self];
+      [l setContentsScale:[[self window] backingScaleFactor]];
+
+      return l;
+    }
 
-  return l;
+  return [super makeBackingLayer];
 }

@@ -7877,7 +7885,7 @@ - (void)lockFocus
 {
   NSTRACE ("[EmacsView lockFocus]");
 
-  if ([self wantsLayer])
+  if (USING_EMACSLAYER)
     {
       CGContextRef context = [(EmacsLayer*)[self layer] getContext];
 
@@ -7897,7 +7905,7 @@ - (void)unlockFocus
 {
   NSTRACE ("[EmacsView unlockFocus]");
 
-  if ([self wantsLayer])
+  if (USING_EMACSLAYER)
     {
       [NSGraphicsContext setCurrentContext:nil];
       [self setNeedsDisplay:YES];
@@ -7917,7 +7925,7 @@ - (void)windowDidChangeBackingProperties:(NSNotification *)notification
 {
   NSTRACE ("EmacsView windowDidChangeBackingProperties:]");
 
-  if ([self wantsLayer])
+  if (USING_EMACSLAYER)
     {
       NSRect frame = [self frame];
       EmacsLayer *layer = (EmacsLayer *)[self layer];
@@ -7942,7 +7950,7 @@ - (void)copyRect:(NSRect)srcRect to:(NSPoint)dest
                                NSHeight (srcRect));
 
 #ifdef NS_IMPL_COCOA
-  if ([self wantsLayer])
+  if (USING_EMACSLAYER)
     {
       double scale = [[self window] backingScaleFactor];
       CGContextRef context = [(EmacsLayer *)[self layer] getContext];
-- 
2.35.1