Browse Source

macOS set Padding to Dock size. Half if hidden.

Ruben 5 months ago
parent
commit
98c9733288

+ 23 - 2
src/PicView.Avalonia.MacOS/App.axaml.cs

@@ -67,6 +67,11 @@ public class App : Application, IPlatformSpecificService, IPlatformWindowService
                 _mainWindow = new MacMainWindow();
                 desktop.MainWindow = _mainWindow;
             },DispatcherPriority.Send);
+
+            if (!settingsExists || Settings.WindowProperties.Padding < 0)
+            {
+                await DockSizeHelper.SetDockSizeAsync().ConfigureAwait(false);
+            }
         
             _vm = new MainViewModel(this, this);
         
@@ -94,6 +99,19 @@ public class App : Application, IPlatformSpecificService, IPlatformWindowService
                 }
             };
             Current.UrlsOpened -= handler;
+            
+            // Check if dock size has changed
+            var dockSize = await DockSizeHelper.GetDockSizeAsync().ConfigureAwait(false);
+            // ReSharper disable once CompareOfFloatsByEqualityOperator
+            if (Settings.WindowProperties.Padding != dockSize)
+            {
+                Settings.WindowProperties.Padding = dockSize;
+                if (Settings.WindowProperties.AutoFit)
+                {
+                    await WindowResizing.SetSizeAsync(_vm);
+                }
+            }
+        
         }
         catch (Exception)
         {
@@ -231,8 +249,11 @@ public class App : Application, IPlatformSpecificService, IPlatformWindowService
     }
     
     #endregion
-    
-    public double Padding { get; set; } // TODO should be the width or height of the dock. Half if auto-hiding
+
+    public double Padding
+    {
+        get => Settings.WindowProperties.Padding / ScreenHelper.ScreenSize.Scaling;
+    }
 
     public int CombinedTitleButtonsWidth { get; set; } = 165;
     

+ 40 - 0
src/PicView.Avalonia.MacOS/WindowImpl/DockSizeHelper.cs

@@ -0,0 +1,40 @@
+using PicView.Core.MacOS.AppleScripts;
+
+namespace PicView.Avalonia.MacOS.WindowImpl;
+
+public  static class DockSizeHelper
+{
+    private const string Script = """
+
+                                  set dockSize to do shell script "defaults read com.apple.dock tilesize"
+                                  set dockHidden to do shell script "defaults read com.apple.dock autohide"
+                                  return dockSize & "," & dockHidden
+
+                                  """;
+    
+    public static async Task SetDockSizeAsync()
+    {
+        var result = await GetDockSizeAsync();
+        Settings.WindowProperties.Padding = result;
+    }
+    
+    public static async Task<double> GetDockSizeAsync()
+    {
+        var result = await AppleScriptManager.ExecuteAppleScriptWithResultAsync(Script);
+        if (!string.IsNullOrWhiteSpace(result))
+        {
+            var parts = result.Split(',');
+            if (parts.Length == 2 && int.TryParse(parts[0], out var dockSize) && int.TryParse(parts[1], out var hidden))
+            {
+                if (hidden == 1)
+                {
+                    // ReSharper disable once PossibleLossOfFraction
+                    return dockSize / 2;
+                }
+
+                return dockSize;
+            }
+        }
+        return -1;
+    }
+}

+ 8 - 5
src/PicView.Avalonia/StartUp/StartUpHelper.cs

@@ -92,7 +92,7 @@ public static class StartUpHelper
         ScreenHelper.UpdateScreenSize(window);
         if (!RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
         {
-            if (Settings.WindowProperties.Padding <= 0)
+            if (Settings.WindowProperties.Padding < 0)
             {
                 Settings.WindowProperties.Padding = 45;
             }
@@ -158,11 +158,14 @@ public static class StartUpHelper
         Application.Current.Name = "PicView";
         
         vm.AssociationsViewModel ??= new FileAssociationsViewModel();
-        
-        if (Settings.UIProperties.OpenInSameWindow)
+
+        if (!RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
         {
-            // No other instance is running, create named pipe server
-            _ = IPC.StartListeningForArguments(vm);
+            if (Settings.UIProperties.OpenInSameWindow)
+            {
+                // No other instance is running, create named pipe server
+                _ = IPC.StartListeningForArguments(vm);
+            }
         }
     }
 

+ 35 - 0
src/PicView.Core.MacOS/AppleScripts/AppleScriptManager.cs

@@ -1,4 +1,5 @@
 using System.Diagnostics;
+using PicView.Core.DebugTools;
 
 namespace PicView.Core.MacOS.AppleScripts;
 
@@ -78,4 +79,38 @@ public static class AppleScriptManager
         var lastOutput = output.Last().Trim().ToLowerInvariant();
         return lastOutput is "true" or "1";
     }
+    
+    public static async Task<string?> ExecuteAppleScriptWithResultAsync(string appleScript)
+    {
+        var scriptPath = Path.Combine(Path.GetTempPath(), $"picview_script_{Guid.NewGuid():N}.scpt");
+        await File.WriteAllTextAsync(scriptPath, appleScript);
+
+        var process = new Process
+        {
+            StartInfo = new ProcessStartInfo
+            {
+                FileName = "osascript",
+                Arguments = scriptPath,
+                RedirectStandardOutput = true,
+                RedirectStandardError = true,
+                UseShellExecute = false,
+                CreateNoWindow = true
+            }
+        };
+
+        process.Start();
+        var output = await process.StandardOutput.ReadToEndAsync();
+        await process.WaitForExitAsync();
+
+        try
+        {
+            File.Delete(scriptPath);
+        }
+        catch (Exception ex)
+        {
+            DebugHelper.LogDebug(nameof(AppleScriptManager), nameof(ExecuteAppleScriptWithResultAsync), ex);
+        }
+
+        return process.ExitCode == 0 ? output.Trim() : null;
+    }
 }

+ 1 - 1
src/PicView.Core/Config/AppSettings.cs

@@ -24,7 +24,7 @@ public class WindowProperties
     public bool Maximized { get; set; } = false;
     public bool Fullscreen { get; set; } = false;
     public bool KeepCentered { get; set; } = false;
-    public double Padding { get; set; } = 0;
+    public double Padding { get; set; } = -1;
 }
 
 public class UIProperties