From 698b217c9d2909ee1c5f0585058a0f51a4a3cbf8 Mon Sep 17 00:00:00 2001
From: Christopher Chavez <chrischavez@gmx.us>
Date: Fri, 7 Aug 2020 11:49:00 -0500
Subject: [PATCH 2/5] Aqua: add wrapper for NSWindow backingScaleFactor

Based on implementation recently removed from XGetImage()
---
 macosx/tkMacOSXImage.c   | 41 ++++++++++++++++++++++++++++++++++++++++
 macosx/tkMacOSXPrivate.h | 14 ++++++++++++++
 2 files changed, 55 insertions(+)

diff --git macosx/tkMacOSXImage.c macosx/tkMacOSXImage.c
index 77932fea8..d502def4f 100644
--- macosx/tkMacOSXImage.c
+++ macosx/tkMacOSXImage.c
@@ -576,6 +576,47 @@ CreateCGImageFromDrawableRect(
     return result;
 }
 
+/*
+ *----------------------------------------------------------------------
+ *
+ * TkMacOSXNSWindowBackingScaleFactor --
+ *
+ *	Compatibility wrapper for NSWindow backingScaleFactor
+ *	property (present only on macOS 10.7+).
+ *
+ * Results:
+ *	Returns the backingScaleFactor for the given NSWindow,
+ *	which is currently either 2.0 for a window on a Retina
+ *	display, or 1.0 on a non-Retina display.
+ *
+ * Side effects:
+ *	None.
+ *
+ *----------------------------------------------------------------------
+ */
+
+TkMacOSXNSWindowHasBackingScaleFactorEnum tkMacOSXNSWindowHasBackingScaleFactor =
+	NSWINDOW_HAS_BACKING_SCALE_FACTOR_UNKNOWN;
+
+CGFloat
+TkMacOSXNSWindowBackingScaleFactor(
+    NSWindow *win)
+{
+// Q: Why is this checking for clang? gcc implying 10.6 build?
+// Usability of instancesRespondToSelector: or @selector() syntax?
+#ifdef __clang__
+    if (tkMacOSXNSWindowHasBackingScaleFactor == NSWINDOW_HAS_BACKING_SCALE_FACTOR_UNKNOWN) {
+	tkMacOSXNSWindowHasBackingScaleFactor = [NSWindow
+		instancesRespondToSelector:@selector(backingScaleFactor)]
+		? NSWINDOW_HAS_BACKING_SCALE_FACTOR_YES : NSWINDOW_HAS_BACKING_SCALE_FACTOR_NO;
+    }
+    if (tkMacOSXNSWindowHasBackingScaleFactor == NSWINDOW_HAS_BACKING_SCALE_FACTOR_YES) {
+	return [win backingScaleFactor];
+    }
+#endif
+    return 1.0;
+}
+
 /*
  *----------------------------------------------------------------------
  *
diff --git macosx/tkMacOSXPrivate.h macosx/tkMacOSXPrivate.h
index 8e57641a2..83d10b908 100644
--- macosx/tkMacOSXPrivate.h
+++ macosx/tkMacOSXPrivate.h
@@ -206,11 +206,24 @@ typedef struct TkMacOSXDrawingContext {
     CGRect portBounds;
 } TkMacOSXDrawingContext;
 
+
+/*
+ * Enum for determining whether NSWindow has the backingScaleFactor
+ * property at runtime.
+ */
+
+typedef enum {
+    NSWINDOW_HAS_BACKING_SCALE_FACTOR_UNKNOWN,
+    NSWINDOW_HAS_BACKING_SCALE_FACTOR_NO,
+    NSWINDOW_HAS_BACKING_SCALE_FACTOR_YES
+} TkMacOSXNSWindowHasBackingScaleFactorEnum;
+
 /*
  * Variables internal to TkAqua.
  */
 
 MODULE_SCOPE long tkMacOSXMacOSXVersion;
+MODULE_SCOPE TkMacOSXNSWindowHasBackingScaleFactorEnum tkMacOSXNSWindowHasBackingScaleFactor;
 
 /*
  * Prototypes for TkMacOSXRegion.c.
@@ -293,6 +306,7 @@ MODULE_SCOPE void       TkMacOSXWinNSBounds(TkWindow *winPtr, NSView *view,
 MODULE_SCOPE Bool       TkMacOSXInDarkMode(Tk_Window tkwin);
 MODULE_SCOPE void	TkMacOSXDrawAllViews(ClientData clientData);
 MODULE_SCOPE unsigned long TkMacOSXClearPixel(void);
+MODULE_SCOPE CGFloat	TkMacOSXNSWindowBackingScaleFactor(NSWindow *win);
 
 #pragma mark Private Objective-C Classes
 
-- 
2.24.3 (Apple Git-128)

