From 30b5adde17a38d6a88f8e8cb582288176f217f23 Mon Sep 17 00:00:00 2001
From: Jeff Young <jeff@rokeby.ie>
Date: Sun, 26 May 2024 14:37:05 +0100
Subject: [PATCH] Fix spacing/scaling of indicators on MacOS.

Also pushes indicator scaling improvements to GerbView.
---
 3d-viewer/dialogs/appearance_controls_3D.cpp |  4 +---
 common/widgets/indicator_icon.cpp            | 18 ++++++++++++++----
 gerbview/widgets/layer_widget.cpp            | 19 ++++++++++---------
 include/widgets/ui_common.h                  |  7 +++++++
 pcbnew/widgets/appearance_controls.cpp       |  6 ++----
 5 files changed, 34 insertions(+), 20 deletions(-)

diff --git a/3d-viewer/dialogs/appearance_controls_3D.cpp b/3d-viewer/dialogs/appearance_controls_3D.cpp
index db06e48312..6dcb86f5da 100644
--- a/3d-viewer/dialogs/appearance_controls_3D.cpp
+++ b/3d-viewer/dialogs/appearance_controls_3D.cpp
@@ -100,8 +100,6 @@ APPEARANCE_CONTROLS_3D::APPEARANCE_CONTROLS_3D( EDA_3D_VIEWER_FRAME* aParent,
 {
     DPI_SCALING_COMMON dpi( nullptr, m_frame );
 
-    const int c_indicatorSizeDIP = 10;
-
     int screenHeight  = wxSystemSettings::GetMetric( wxSYS_SCREEN_Y );
     m_pointSize       = wxSystemSettings::GetFont( wxSYS_DEFAULT_GUI_FONT ).GetPointSize();
 
@@ -154,7 +152,7 @@ APPEARANCE_CONTROLS_3D::APPEARANCE_CONTROLS_3D( EDA_3D_VIEWER_FRAME* aParent,
                                                  KeyNameFromKeyCode( VIEWPORT_SWITCH_KEY ),
                                                  KeyNameFromKeyCode( VIEWPORT_SWITCH_KEY ) ) );
 
-    if( screenHeight <= 900 && m_pointSize >= FromDIP( c_indicatorSizeDIP ) )
+    if( screenHeight <= 900 && m_pointSize >= FromDIP( KIUI::c_IndicatorSizeDIP ) )
         m_pointSize = m_pointSize * 8 / 10;
 
     m_cbLayerPresets->Bind( wxEVT_CHOICE, &APPEARANCE_CONTROLS_3D::onLayerPresetChanged, this );
diff --git a/common/widgets/indicator_icon.cpp b/common/widgets/indicator_icon.cpp
index ebd74ff68a..fb8625e596 100644
--- a/common/widgets/indicator_icon.cpp
+++ b/common/widgets/indicator_icon.cpp
@@ -21,6 +21,7 @@
  * 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
  */
 
+#include <kiplatform/ui.h>
 #include <widgets/indicator_icon.h>
 #include <wx/event.h>
 #include <wx/settings.h>
@@ -75,9 +76,12 @@ wxImage createBlankImage( int size )
     wxImage image( size, size );
 
     image.InitAlpha();
+
     for( int y = 0; y < size; ++y )
+    {
         for( int x = 0; x < size; ++x )
             image.SetAlpha( x, y, wxIMAGE_ALPHA_TRANSPARENT );
+    }
 
 #ifdef __WXMSW__
     // wxWidgets on Windows chokes on an empty fully transparent bitmap and draws it
@@ -162,14 +166,20 @@ wxBitmap createDiamond( int size, double aScaleFactor, wxColour aColour )
 
 ROW_ICON_PROVIDER::ROW_ICON_PROVIDER( int aSizeDIP, wxWindow* aWindow )
 {
-    auto toPhys = [&]( int dip )
-    {
-        return aWindow->ToPhys( aWindow->FromDIP( dip ) );
-    };
+    auto toPhys =
+            [&]( int dip )
+            {
+                return aWindow->ToPhys( aWindow->FromDIP( dip ) );
+            };
 
     double   scale = aWindow->GetDPIScaleFactor();
     wxColour shadowColor = wxSystemSettings().GetColour( wxSYS_COLOUR_3DDKSHADOW );
 
+#ifdef __WXMAC__
+    // Adjust for Retina
+    scale /= KIPLATFORM::UI::GetPixelScaleFactor( aWindow );
+#endif
+
     m_blankBitmap = wxBitmap( createBlankImage( toPhys( aSizeDIP ) ) );
     m_blankBitmap.SetScaleFactor( scale );
 
diff --git a/gerbview/widgets/layer_widget.cpp b/gerbview/widgets/layer_widget.cpp
index ddb65b1202..7acf773ca8 100644
--- a/gerbview/widgets/layer_widget.cpp
+++ b/gerbview/widgets/layer_widget.cpp
@@ -446,12 +446,14 @@ void LAYER_WIDGET::insertRenderRow( int aRow, const ROW& aSpec )
         bmb->SetToolTip( _( "Left double click or middle click for color change" ) );
         m_RenderFlexGridSizer->wxSizer::Insert( index+col, bmb, 0, flags );
 
-        bmb->Bind( wxEVT_RIGHT_DOWN, [this, bmb, renderName] ( wxMouseEvent& aEvt ) {
-            OnRightDownRender( aEvt, bmb, renderName );
-        } );
-        cb->Bind( wxEVT_RIGHT_DOWN, [this, bmb, renderName] ( wxMouseEvent& aEvt ) {
-            OnRightDownRender( aEvt, bmb, renderName );
-        } );
+        bmb->Bind( wxEVT_RIGHT_DOWN, [this, bmb, renderName] ( wxMouseEvent& aEvt )
+                                     {
+                                         OnRightDownRender( aEvt, bmb, renderName );
+                                     } );
+        cb->Bind( wxEVT_RIGHT_DOWN, [this, bmb, renderName] ( wxMouseEvent& aEvt )
+                                    {
+                                        OnRightDownRender( aEvt, bmb, renderName );
+                                    } );
 
         // could add a left click handler on the color button that toggles checkbox.
     }
@@ -488,13 +490,12 @@ LAYER_WIDGET::LAYER_WIDGET( wxWindow* aParent, wxWindow* aFocusOwner, wxWindowID
     wxPanel( aParent, id, pos, size, style ),
     m_smallestLayerString( wxT( "M...M" ) )
 {
-    int indicatorSize = ConvertDialogToPixels( wxSize( 6, 6 ) ).x;
-    m_IconProvider = new ROW_ICON_PROVIDER( indicatorSize, this );
+    m_IconProvider = new ROW_ICON_PROVIDER( KIUI::c_IndicatorSizeDIP, this );
 
     int pointSize = wxSystemSettings::GetFont( wxSYS_DEFAULT_GUI_FONT ).GetPointSize();
     int screenHeight = wxSystemSettings::GetMetric( wxSYS_SCREEN_Y );
 
-    if( screenHeight <= 900 && pointSize >= indicatorSize )
+    if( screenHeight <= 900 && pointSize >= FromDIP( KIUI::c_IndicatorSizeDIP ) )
         pointSize = pointSize * 8 / 10;
 
     m_PointSize = pointSize;
diff --git a/include/widgets/ui_common.h b/include/widgets/ui_common.h
index ae06f45e1a..119c054561 100644
--- a/include/widgets/ui_common.h
+++ b/include/widgets/ui_common.h
@@ -48,6 +48,13 @@ class wxMenu;
 namespace KIUI
 {
 
+#ifdef __WXMAC__
+    const int c_IndicatorSizeDIP = 6;
+#else
+    const int c_IndicatorSizeDIP = 10;
+#endif
+
+
 /**
  * Get the standard margin around a widget in the KiCad UI
  * @return margin in pixels
diff --git a/pcbnew/widgets/appearance_controls.cpp b/pcbnew/widgets/appearance_controls.cpp
index 2fa9f4f749..951869aecb 100644
--- a/pcbnew/widgets/appearance_controls.cpp
+++ b/pcbnew/widgets/appearance_controls.cpp
@@ -413,10 +413,8 @@ APPEARANCE_CONTROLS::APPEARANCE_CONTROLS( PCB_BASE_FRAME* aParent, wxWindow* aFo
     // Correct the min size from wxformbuilder not using fromdip
     SetMinSize( FromDIP( GetMinSize() ) );
 
-    const int c_indicatorSizeDIP = 10;
-
     int screenHeight  = wxSystemSettings::GetMetric( wxSYS_SCREEN_Y );
-    m_iconProvider    = new ROW_ICON_PROVIDER( c_indicatorSizeDIP, this );
+    m_iconProvider    = new ROW_ICON_PROVIDER( KIUI::c_IndicatorSizeDIP, this );
     m_pointSize       = wxSystemSettings::GetFont( wxSYS_DEFAULT_GUI_FONT ).GetPointSize();
 
     m_layerPanelColour = m_panelLayers->GetBackgroundColour().ChangeLightness( 110 );
@@ -465,7 +463,7 @@ APPEARANCE_CONTROLS::APPEARANCE_CONTROLS( PCB_BASE_FRAME* aParent, wxWindow* aFo
 
     m_txtNetFilter->SetHint( _( "Filter nets" ) );
 
-    if( screenHeight <= 900 && m_pointSize >= FromDIP( c_indicatorSizeDIP ) )
+    if( screenHeight <= 900 && m_pointSize >= FromDIP( KIUI::c_IndicatorSizeDIP ) )
         m_pointSize = m_pointSize * 8 / 10;
 
     wxFont font = m_notebook->GetFont();