Skip to content

Commit acf111d

Browse files
authored
Avalonia: Fix main window not draggable by touch (#4869)
1 parent 2187aa4 commit acf111d

2 files changed

Lines changed: 105 additions & 3 deletions

File tree

‎src/UniGetUI.Avalonia/Views/MainWindow.axaml‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -312,10 +312,14 @@
312312

313313
<!-- Drag area — sits behind everything so interactive controls still work -->
314314
<Border Grid.Column="0" Grid.ColumnSpan="3"
315+
x:Name="TitleBarDragArea"
315316
Background="{DynamicResource MicaPageBackground}"
316317
automation:AutomationProperties.AccessibilityView="Raw"
317318
IsHitTestVisible="True"
318-
PointerPressed="TitleBar_PointerPressed"/>
319+
PointerPressed="TitleBar_PointerPressed"
320+
PointerMoved="TitleBar_PointerMoved"
321+
PointerReleased="TitleBar_PointerReleased"
322+
PointerCaptureLost="TitleBar_PointerCaptureLost"/>
319323

320324
<!-- Left: hamburger + app name -->
321325
<StackPanel x:Name="HamburgerPanel"

‎src/UniGetUI.Avalonia/Views/MainWindow.axaml.cs‎

Lines changed: 100 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -828,9 +828,20 @@ public struct MONITORINFO
828828
private void CloseButton_Click(object? sender, RoutedEventArgs e)
829829
=> Close();
830830

831+
// Manual title-bar drag state. Only used for touch/pen on Windows (see TitleBar_PointerPressed),
832+
// where Avalonia's BeginMoveDrag drives an OS modal loop the finger can't feed. Bound to the
833+
// owning pointer so a second contact can't hijack or end an in-progress drag.
834+
private IPointer? _titleBarDragPointer;
835+
private Point _titleBarDragOrigin;
836+
private bool _restoreThenDrag; // press began while maximized → restore on first real move
837+
831838
private void TitleBar_PointerPressed(object? sender, PointerPressedEventArgs e)
832839
{
833-
if (!e.GetCurrentPoint(this).Properties.IsLeftButtonPressed)
840+
// Manual drag only on Windows + touch/pen. Mouse keeps the OS move (Aero Snap), and on
841+
// macOS/Linux the native BeginMoveDrag already handles touch (and Wayland forbids self-positioning).
842+
bool manualDrag = OperatingSystem.IsWindows() && e.Pointer.Type != PointerType.Mouse;
843+
844+
if (!manualDrag && !e.GetCurrentPoint(this).Properties.IsLeftButtonPressed)
834845
return;
835846

836847
if (e.ClickCount == 2)
@@ -841,7 +852,94 @@ private void TitleBar_PointerPressed(object? sender, PointerPressedEventArgs e)
841852
return;
842853
}
843854

844-
BeginMoveDrag(e);
855+
if (!manualDrag)
856+
{
857+
BeginMoveDrag(e);
858+
return;
859+
}
860+
861+
// Touch/pen on Windows: move the window manually (issue #4866).
862+
if (_titleBarDragPointer is not null)
863+
return;
864+
865+
_titleBarDragPointer = e.Pointer;
866+
_titleBarDragOrigin = e.GetPosition(this);
867+
// Dragging a maximized window restores it first (matches the native mouse gesture).
868+
_restoreThenDrag = WindowState == WindowState.Maximized;
869+
e.Pointer.Capture(TitleBarDragArea);
870+
e.Handled = true;
871+
}
872+
873+
private void TitleBar_PointerMoved(object? sender, PointerEventArgs e)
874+
{
875+
if (e.Pointer != _titleBarDragPointer)
876+
return;
877+
878+
Vector delta = e.GetPosition(this) - _titleBarDragOrigin;
879+
880+
// Started on a maximized window: ignore tiny jitter (so a tap doesn't restore), then
881+
// restore-and-reposition under the finger before the normal drag takes over.
882+
if (_restoreThenDrag)
883+
{
884+
if (Math.Abs(delta.X) < 4 && Math.Abs(delta.Y) < 4)
885+
return;
886+
RestoreForTouchDrag(e.GetPosition(this));
887+
e.Handled = true;
888+
return;
889+
}
890+
891+
if (delta.X == 0 && delta.Y == 0)
892+
return;
893+
894+
// GetPosition is window-relative and Position is in screen pixels; scale converts between
895+
// them. Closed loop: the origin stays fixed and delta is re-measured against the moved
896+
// window each event, so the sub-pixel remainder is held in the geometry and re-applied
897+
// rather than accumulating. Round (not truncate) to keep the residual symmetric and <0.5px.
898+
double scale = RenderScaling;
899+
Position += new PixelVector((int)Math.Round(delta.X * scale), (int)Math.Round(delta.Y * scale));
900+
e.Handled = true;
901+
}
902+
903+
// Restores a maximized window mid-drag and re-anchors it under the finger, matching the native
904+
// mouse gesture. The finger's screen point and its horizontal fraction of the title bar are
905+
// captured while still maximized; after WindowState.Normal the window is placed so that same
906+
// fraction of the (now restored) width sits under the finger. On Win32 the restore is
907+
// synchronous — ShowWindow sends WM_SIZE inline, so Bounds already reflects the restored size.
908+
private void RestoreForTouchDrag(Point grab)
909+
{
910+
double fraction = Bounds.Width > 0 ? Math.Clamp(grab.X / Bounds.Width, 0, 1) : 0.5;
911+
double titleY = grab.Y;
912+
PixelPoint fingerScreen = this.PointToScreen(grab);
913+
914+
_restoreThenDrag = false;
915+
WindowState = WindowState.Normal;
916+
917+
double scale = RenderScaling;
918+
var origin = new Point(fraction * Bounds.Width, titleY);
919+
Position = fingerScreen - new PixelVector(
920+
(int)Math.Round(origin.X * scale),
921+
(int)Math.Round(origin.Y * scale));
922+
_titleBarDragOrigin = origin;
923+
}
924+
925+
private void TitleBar_PointerReleased(object? sender, PointerReleasedEventArgs e)
926+
{
927+
if (e.Pointer != _titleBarDragPointer)
928+
return;
929+
930+
_titleBarDragPointer = null;
931+
_restoreThenDrag = false;
932+
e.Pointer.Capture(null);
933+
e.Handled = true;
934+
}
935+
936+
private void TitleBar_PointerCaptureLost(object? sender, PointerCaptureLostEventArgs e)
937+
{
938+
if (e.Pointer == _titleBarDragPointer)
939+
{
940+
_titleBarDragPointer = null;
941+
_restoreThenDrag = false;
942+
}
845943
}
846944

847945
private void SearchBox_KeyDown(object? sender, KeyEventArgs e)

0 commit comments

Comments
 (0)