From 0fe3b071524f8ef9a0b4312f8ffa70e02e8c90cb Mon Sep 17 00:00:00 2001 From: Yair <39923744+yaira2@users.noreply.github.com> Date: Tue, 26 Mar 2024 12:45:54 -0400 Subject: [PATCH 1/2] Fix: Fixed issue where selection wasn't cleared when using touch screen --- .../RectangleSelection_ListViewBase.cs | 49 ++++++++----------- 1 file changed, 20 insertions(+), 29 deletions(-) diff --git a/src/Files.App/UserControls/Selection/RectangleSelection_ListViewBase.cs b/src/Files.App/UserControls/Selection/RectangleSelection_ListViewBase.cs index 2bf7076a8685..00ac2068c545 100644 --- a/src/Files.App/UserControls/Selection/RectangleSelection_ListViewBase.cs +++ b/src/Files.App/UserControls/Selection/RectangleSelection_ListViewBase.cs @@ -6,9 +6,6 @@ using Microsoft.UI.Xaml.Controls; using Microsoft.UI.Xaml.Input; using Microsoft.UI.Xaml.Shapes; -using System; -using System.Collections.Generic; -using System.Linq; using Windows.Foundation; using Windows.System; using DispatcherQueue = Microsoft.UI.Dispatching.DispatcherQueue; @@ -41,18 +38,14 @@ public RectangleSelection_ListViewBase(ListViewBase uiElement, Rectangle selecti private void RectangleSelection_PointerMoved(object sender, PointerRoutedEventArgs e) { if (scrollViewer is null) - { return; - } var currentPoint = e.GetCurrentPoint(uiElement); var verticalOffset = scrollViewer.VerticalOffset; if (selectionState == SelectionState.Starting) { if (!HasMovedMinimalDelta(originDragPoint.X, originDragPoint.Y - verticalOffset, currentPoint.Position.X, currentPoint.Position.Y)) - { return; - } // Clear selected items once if the pointer is pressed and moved selectionStrategy.StartSelection(); @@ -73,13 +66,9 @@ private void RectangleSelection_PointerMoved(object sender, PointerRoutedEventAr try { if (rect.IntersectsWith(item.Value)) - { selectionStrategy.HandleIntersectionWithItem(item.Key); - } else - { selectionStrategy.HandleNoIntersectionWithItem(item.Key); - } } catch (ArgumentException) { @@ -131,7 +120,7 @@ private void RectangleSelection_PointerPressed(object sender, PointerRoutedEvent var verticalOffset = scrollViewer.VerticalOffset; originDragPoint.Y += verticalOffset; // Initial drag point relative to the top of the list (considering scrolled offset) - if (!e.GetCurrentPoint(uiElement).Properties.IsLeftButtonPressed || e.Pointer.PointerDeviceType == Microsoft.UI.Input.PointerDeviceType.Touch) + if (!e.GetCurrentPoint(uiElement).Properties.IsLeftButtonPressed && e.Pointer.PointerDeviceType != Microsoft.UI.Input.PointerDeviceType.Touch) { // Trigger only on left click, do not trigger with touch return; @@ -147,13 +136,16 @@ private void RectangleSelection_PointerPressed(object sender, PointerRoutedEvent selectionStrategy.HandleNoItemSelected(); - uiElement.PointerMoved -= RectangleSelection_PointerMoved; - uiElement.PointerMoved += RectangleSelection_PointerMoved; - if (selectionChanged is not null) + if (e.Pointer.PointerDeviceType != Microsoft.UI.Input.PointerDeviceType.Touch) { - // Unsunscribe from SelectionChanged event for performance - uiElement.SelectionChanged -= selectionChanged; + uiElement.PointerMoved -= RectangleSelection_PointerMoved; + uiElement.PointerMoved += RectangleSelection_PointerMoved; } + + // Unsunscribe from SelectionChanged event for performance + if (selectionChanged is not null) + uiElement.SelectionChanged -= selectionChanged; + uiElement.CapturePointer(e.Pointer); selectionState = SelectionState.Starting; } @@ -186,31 +178,34 @@ private void ScrollViewer_ViewChanged(object sender, ScrollViewerViewChangedEven private void RectangleSelection_PointerReleased(object sender, PointerRoutedEventArgs e) { - if (scrollViewer is null) return; + if (scrollViewer is null) + return; + + // Reset selection rectangle Canvas.SetLeft(selectionRectangle, 0); Canvas.SetTop(selectionRectangle, 0); selectionRectangle.Width = 0; selectionRectangle.Height = 0; - uiElement.PointerMoved -= RectangleSelection_PointerMoved; + // Unhook events + uiElement.PointerMoved -= RectangleSelection_PointerMoved; scrollViewer.ViewChanged -= ScrollViewer_ViewChanged; + uiElement.ReleasePointerCapture(e.Pointer); if (selectionChanged is not null) { // Restore and trigger SelectionChanged event uiElement.SelectionChanged -= selectionChanged; uiElement.SelectionChanged += selectionChanged; + + // Trigger SelectionChanged event if the selection has changed if (prevSelectedItems is null || !uiElement.SelectedItems.SequenceEqual(prevSelectedItems)) - { - // Trigger SelectionChanged event if the selection has changed selectionChanged(sender, null); - } } + + // Always trigger SelectionEnded to focus the file list when clicking on the empty space (#2977) if (selectionState == SelectionState.Active || e.OriginalSource is ListViewBase) - { - // Always trigger SelectionEnded to focus the file list when clicking on the empty space (#2977) OnSelectionEnded(); - } selectionStrategy = null; selectionState = SelectionState.Inactive; @@ -225,9 +220,7 @@ private void RectangleSelection_SizeChanged(object sender, object e) scrollViewer ??= DependencyObjectHelpers.FindChild(uiElement, sv => sv.VerticalScrollMode != ScrollMode.Disabled); if (scrollViewer is not null) - { uiElement.SizeChanged -= RectangleSelection_SizeChanged; - } } private void InitEvents(object sender, RoutedEventArgs e) @@ -246,9 +239,7 @@ private void InitEvents(object sender, RoutedEventArgs e) scrollViewer = DependencyObjectHelpers.FindChild(uiElement, sv => sv.VerticalScrollMode != ScrollMode.Disabled); if (scrollViewer is null) - { uiElement.SizeChanged += RectangleSelection_SizeChanged; - } } } } From efa9f7f0344b4c5cc586d676cb647761f17684a1 Mon Sep 17 00:00:00 2001 From: Yair <39923744+yaira2@users.noreply.github.com> Date: Tue, 26 Mar 2024 10:52:28 -0700 Subject: [PATCH 2/2] Update RectangleSelection_ListViewBase.cs --- .../Selection/RectangleSelection_ListViewBase.cs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/Files.App/UserControls/Selection/RectangleSelection_ListViewBase.cs b/src/Files.App/UserControls/Selection/RectangleSelection_ListViewBase.cs index 00ac2068c545..e3db56f7e015 100644 --- a/src/Files.App/UserControls/Selection/RectangleSelection_ListViewBase.cs +++ b/src/Files.App/UserControls/Selection/RectangleSelection_ListViewBase.cs @@ -122,7 +122,7 @@ private void RectangleSelection_PointerPressed(object sender, PointerRoutedEvent originDragPoint.Y += verticalOffset; // Initial drag point relative to the top of the list (considering scrolled offset) if (!e.GetCurrentPoint(uiElement).Properties.IsLeftButtonPressed && e.Pointer.PointerDeviceType != Microsoft.UI.Input.PointerDeviceType.Touch) { - // Trigger only on left click, do not trigger with touch + // Trigger only on left click or with a tap return; } @@ -136,6 +136,7 @@ private void RectangleSelection_PointerPressed(object sender, PointerRoutedEvent selectionStrategy.HandleNoItemSelected(); + // Don't hook events for touch if (e.Pointer.PointerDeviceType != Microsoft.UI.Input.PointerDeviceType.Touch) { uiElement.PointerMoved -= RectangleSelection_PointerMoved; @@ -243,4 +244,4 @@ private void InitEvents(object sender, RoutedEventArgs e) } } } -} \ No newline at end of file +}