Skip to content

Commit

Permalink
Refactor and adjustments for dragging window between multiple monitor…
Browse files Browse the repository at this point in the history
… of different sizes and scaling #26
  • Loading branch information
Ruben2776 committed Oct 1, 2024
1 parent 657b81c commit f3384fc
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 20 deletions.
2 changes: 1 addition & 1 deletion src/PicView.Avalonia.Win32/Views/WinMainWindow.axaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public WinMainWindow()
});
ScalingChanged += (_, _) =>
{
ScreenHelper.ScreenSize = ScreenHelper.GetScreenSize(this);
ScreenHelper.UpdateScreenSize(this);
WindowHelper.SetSize(DataContext as MainViewModel);
};
PointerExited += (_, _) =>
Expand Down
31 changes: 17 additions & 14 deletions src/PicView.Avalonia/UI/ScreenHelper.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
using Avalonia.Controls;

namespace PicView.Avalonia.UI;

public class ScreenSize(double workingAreaWidth, double workingAreaHeight, double scaling)
public readonly struct ScreenSize(double workingAreaWidth, double workingAreaHeight, double scaling)
{
public double WorkingAreaWidth { get; init; } = workingAreaWidth;
public double WorkingAreaHeight { get; init; } = workingAreaHeight;
Expand All @@ -11,20 +10,24 @@ public class ScreenSize(double workingAreaWidth, double workingAreaHeight, doubl

public static class ScreenHelper
{
public static ScreenSize? ScreenSize { get; set; }
public static ScreenSize? GetScreenSize(Window window)
private static readonly Lock _lock = new();
public static ScreenSize ScreenSize { get; private set; }
public static void UpdateScreenSize(Window window)
{
var screen = window.Screens.ScreenFromVisual(window);
// Need to lock it to prevent multiple calls
lock (_lock)
{
var screen = window.Screens.ScreenFromVisual(window);

var monitorWidth = screen.Bounds.Width / screen.Scaling;
var monitorHeight = screen.Bounds.Height / screen.Scaling;

var monitorWidth = screen.Bounds.Width / screen.Scaling;
var monitorHeight = screen.Bounds.Height / screen.Scaling;

return new ScreenSize(monitorWidth, monitorHeight, screen.Scaling)
{
WorkingAreaWidth = monitorWidth,
WorkingAreaHeight = monitorHeight,
Scaling = screen.Scaling,
};
ScreenSize = new ScreenSize(monitorWidth, monitorHeight, screen.Scaling)
{
WorkingAreaWidth = monitorWidth,
WorkingAreaHeight = monitorHeight,
Scaling = screen.Scaling,
};
}
}
}
2 changes: 1 addition & 1 deletion src/PicView.Avalonia/UI/StartUpHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public static void Start(MainViewModel vm, bool settingsExists, IClassicDesktopS

w.Show();
vm.IsLoading = true;
ScreenHelper.ScreenSize = ScreenHelper.GetScreenSize(w);
ScreenHelper.UpdateScreenSize(w);
UIHelper.SetControls(desktop);
vm.UpdateLanguage();
vm.GetFlipped = vm.Flip;
Expand Down
26 changes: 23 additions & 3 deletions src/PicView.Avalonia/UI/WindowHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,35 @@ public static void WindowDragAndDoubleClickBehavior(Window window, PointerPresse
_ = MaximizeRestore();
return;
}


var currentScreen = ScreenHelper.ScreenSize;
window.BeginMoveDrag(e);
ScreenHelper.ScreenSize = ScreenHelper.GetScreenSize(window);
var screen = window.Screens.ScreenFromVisual(window);
if (screen != null)
{
if (screen.WorkingArea.Width != currentScreen.WorkingAreaWidth ||
screen.WorkingArea.Height != currentScreen.WorkingAreaHeight || screen.Scaling != currentScreen.Scaling)
{
ScreenHelper.UpdateScreenSize(window);
SetSize(window.DataContext as MainViewModel);
}
}
}

public static void WindowDragBehavior(Window window, PointerPressedEventArgs e)
{
var currentScreen = ScreenHelper.ScreenSize;
window.BeginMoveDrag(e);
ScreenHelper.ScreenSize = ScreenHelper.GetScreenSize(window);
var screen = window.Screens.ScreenFromVisual(window);
if (screen != null)
{
if (screen.WorkingArea.Width != currentScreen.WorkingAreaWidth ||
screen.WorkingArea.Height != currentScreen.WorkingAreaHeight || screen.Scaling != currentScreen.Scaling)
{
ScreenHelper.UpdateScreenSize(window);
SetSize(window.DataContext as MainViewModel);
}
}
}

public static void InitializeWindowSizeAndPosition(Window window)
Expand Down
2 changes: 1 addition & 1 deletion src/PicView.Avalonia/Views/MainView.axaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ private void PointerPressedBehavior(object? sender, PointerPressedEventArgs e)
if (MainKeyboardShortcuts.ShiftDown)
{
var hostWindow = (Window)VisualRoot!;
WindowHelper.WindowDragAndDoubleClickBehavior(hostWindow, e);
WindowHelper.WindowDragBehavior(hostWindow, e);
}

MainKeyboardShortcuts.ClearKeyDownModifiers();
Expand Down

0 comments on commit f3384fc

Please sign in to comment.