Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion SecRandom.Core.Tests/QuickDrawLongPressGestureTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,13 @@ public void DragBeyondTheThreshold_CancelsTheHold()
new Point(100 + QuickDrawLongPressGesture.DragCancelDistance + 1, 100)));
}

[Fact]
public void MouseDragOfEighteenDipsCancelsTheHold()
{
Assert.True(QuickDrawLongPressGesture.ShouldCancelForDrag(
new Point(0, 0), new Point(18, 0), isTouchOrPen: false));
}

[Theory]
[InlineData(true)]
[InlineData(false)]
Expand Down Expand Up @@ -186,4 +193,4 @@ public void OrbCenter_StaysInsideThePanelWhileFollowingThePointer()
Point middle = QuickDrawLongPressGesture.ResolveOrbCenter(new Point(100, 160), window, 56);
Assert.Equal(new Point(100, 160), middle);
}
}
}
46 changes: 46 additions & 0 deletions SecRandom.Mobile.Tests/QuickDrawNotificationThemeTests.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
using System.Reflection;
using Avalonia;
using Avalonia.Controls;
using Avalonia.Headless;
using Avalonia.Headless.XUnit;
using Avalonia.Input;
using Avalonia.Input.Raw;
using Avalonia.Media;
using Avalonia.Styling;
using Avalonia.Threading;
Expand All @@ -24,6 +27,49 @@ namespace SecRandom.Mobile.Tests;

public sealed class QuickDrawNotificationThemeTests
{
[AvaloniaFact]
public void MouseDragDoesNotCountTowardThreeClicksToClose()
{
var dataRoot = Path.Combine(Path.GetTempPath(), "SecRandom.QuickDrawClicks", Guid.NewGuid().ToString("N"));
var previousHost = IAppHost.Host;
Window? window = null;
GetUtilsMethod("ConfigureDataRoot").Invoke(null, [dataRoot]);
using var audio = new DrawAudioService(null!, NullLogger<DrawAudioService>.Instance);
var handler = new MainConfigHandler(NullLogger<MainConfigHandler>.Instance, new TestConfigService(new MainConfigModel()));
using var viewModel = new QuickDrawPageViewModel(handler, null!, null!, null!, null!, audio,
NullLogger<QuickDrawPageViewModel>.Instance, null!, null!, null!, null!);
using var provider = new ServiceCollection().AddSingleton(viewModel).BuildServiceProvider();
try
{
IAppHost.Host = new TestHost(provider);
window = new Window { Content = new QuickDrawPage(), Width = 460, Height = 320 };
window.Show();
Dispatcher.UIThread.RunJobs();

window.MouseDown(new Point(100, 100), MouseButton.Left, RawInputModifiers.None);
window.MouseMove(new Point(120, 100), RawInputModifiers.LeftMouseButton);
window.MouseUp(new Point(120, 100), MouseButton.Left, RawInputModifiers.None);
Assert.True(window.IsVisible);

for (var i = 0; i < 3; i++)
{
window.MouseDown(new Point(100, 100), MouseButton.Left, RawInputModifiers.None);
window.MouseMove(new Point(102, 101), RawInputModifiers.LeftMouseButton);
window.MouseUp(new Point(102, 101), MouseButton.Left, RawInputModifiers.None);
Assert.Equal(i < 2, window.IsVisible);
}
}
finally
{
if (window?.IsVisible == true)
window.Close();
IAppHost.Host = previousHost;
GetUtilsMethod("ResetDataRootForTests").Invoke(null, null);
if (Directory.Exists(dataRoot))
Directory.Delete(dataRoot, recursive: true);
}
}

[AvaloniaTheory]
[InlineData(true)]
[InlineData(false)]
Expand Down
1 change: 1 addition & 0 deletions SecRandom/Views/MainPages/QuickDrawPage.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
BorderThickness="1"
CornerRadius="8"
PointerPressed="RootBorder_OnPointerPressed"
PointerMoved="RootBorder_OnPointerMoved"
PointerReleased="RootBorder_OnPointerReleased">
<Grid Margin="10 8"
RowDefinitions="*,Auto">
Expand Down
41 changes: 36 additions & 5 deletions SecRandom/Views/MainPages/QuickDrawPage.axaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -264,21 +264,52 @@ private void ApplyCardInk(AdaptiveTheme? adaptive, bool customImage)
ViewModel.ApplyNotificationInk(new SolidColorBrush(ink));
}

private PointerPressedEventArgs? _quickDrawPressEvent;
private Point _quickDrawDragStartPoint;
private bool _quickDrawIsDragging;

private void RootBorder_OnPointerPressed(object? sender, PointerPressedEventArgs e)
{
if (!e.GetCurrentPoint(this).Properties.IsLeftButtonPressed
|| TopLevel.GetTopLevel(this) is not Window window)
if (!e.Pointer.IsPrimary || !e.GetCurrentPoint(this).Properties.IsLeftButtonPressed
|| TopLevel.GetTopLevel(this) is not Window)
return;

_quickDrawIsDragging = false;
_quickDrawDragStartPoint = e.GetPosition(this);
_quickDrawPressEvent = e;
}

private void RootBorder_OnPointerMoved(object? sender, PointerEventArgs e)
{
if (_quickDrawIsDragging)
return;
if (_quickDrawPressEvent is null || e.Pointer != _quickDrawPressEvent.Pointer)
return;
if (TopLevel.GetTopLevel(this) is not Window window)
return;
if (!e.GetCurrentPoint(this).Properties.IsLeftButtonPressed)
return;

window.BeginMoveDrag(e);
var delta = e.GetPosition(this) - _quickDrawDragStartPoint;
if (Math.Abs(delta.X) < 4 && Math.Abs(delta.Y) < 4)
return;

_quickDrawIsDragging = true;
window.BeginMoveDrag(_quickDrawPressEvent);
}

private void RootBorder_OnPointerReleased(object? sender, PointerReleasedEventArgs e)
{
if (e.InitialPressMouseButton != MouseButton.Left)
if (e.InitialPressMouseButton != MouseButton.Left
|| _quickDrawPressEvent is null
|| e.Pointer != _quickDrawPressEvent.Pointer)
return;

RegisterClick();
if (!_quickDrawIsDragging)
RegisterClick();

_quickDrawIsDragging = false;
_quickDrawPressEvent = null;
}

private void ViewModel_OnPropertyChanged(object? sender, PropertyChangedEventArgs e)
Expand Down
Loading