-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
acf2aec
commit 62654c8
Showing
26 changed files
with
1,174 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<configuration> | ||
<packageSources> | ||
<clear /> | ||
<add key="nuget.org" value="https://api.nuget.org/v3/index.json" /> | ||
</packageSources> | ||
|
||
<packageSourceMapping> | ||
<packageSource key="nuget.org"> | ||
<package pattern="*" /> | ||
</packageSource> | ||
</packageSourceMapping> | ||
</configuration> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<Application x:Class="SwipeNavigation.App" | ||
xmlns="https://github.com/avaloniaui" | ||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" | ||
RequestedThemeVariant="Default"> | ||
<Application.Styles> | ||
<FluentTheme /> | ||
</Application.Styles> | ||
</Application> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
using Avalonia; | ||
using Avalonia.Controls.ApplicationLifetimes; | ||
using Avalonia.Markup.Xaml; | ||
using SwipeNavigation.Views; | ||
|
||
namespace SwipeNavigation; | ||
public partial class App : Application | ||
{ | ||
public override void Initialize() | ||
{ | ||
AvaloniaXamlLoader.Load(this); | ||
} | ||
|
||
public override void OnFrameworkInitializationCompleted() | ||
{ | ||
if (ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop) | ||
{ | ||
desktop.MainWindow = new MainWindow(); | ||
} | ||
|
||
base.OnFrameworkInitializationCompleted(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
using Avalonia.Interactivity; | ||
|
||
namespace SwipeNavigation.Gestures; | ||
public static class CustomGestures | ||
{ | ||
public static readonly RoutedEvent<SwipeGestureEventArgs> SwipeGestureEvent = | ||
RoutedEvent.Register<SwipeGestureEventArgs>( | ||
"Swipe", RoutingStrategies.Bubble, typeof(CustomGestures)); | ||
|
||
public static readonly RoutedEvent<SwipeGestureEndedEventArgs> SwipeGestureEndedEvent = | ||
RoutedEvent.Register<SwipeGestureEndedEventArgs>( | ||
"SwipeEnded", RoutingStrategies.Bubble, typeof(CustomGestures)); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
using Avalonia; | ||
using Avalonia.Interactivity; | ||
|
||
namespace SwipeNavigation.Gestures; | ||
public class SwipeGestureEndedEventArgs : RoutedEventArgs | ||
{ | ||
public int Id { get; } | ||
public Vector? Delta { get; } | ||
public SwipeDirection Direction { get; } | ||
|
||
public SwipeGestureEndedEventArgs(int id, Vector? delta, SwipeDirection direction) : | ||
base(CustomGestures.SwipeGestureEndedEvent) | ||
{ | ||
Id = id; | ||
Delta = delta; | ||
Direction = direction; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
using Avalonia; | ||
using Avalonia.Interactivity; | ||
|
||
namespace SwipeNavigation.Gestures; | ||
public class SwipeGestureEventArgs : RoutedEventArgs | ||
{ | ||
public int Id { get; } | ||
public Vector? Delta { get; } | ||
public SwipeDirection ExpectedDirection { get; } | ||
|
||
private static int _nextId = 1; | ||
|
||
internal static int GetNextFreeId() => _nextId++; | ||
|
||
public SwipeGestureEventArgs(int id, Vector? delta, SwipeDirection expectedDirection) | ||
: base(CustomGestures.SwipeGestureEvent) | ||
{ | ||
Id = id; | ||
Delta = delta; | ||
ExpectedDirection = expectedDirection; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
using Avalonia.Input.GestureRecognizers; | ||
using Avalonia.Input; | ||
using Avalonia; | ||
|
||
namespace SwipeNavigation.Gestures; | ||
|
||
public enum SwipeDirection | ||
{ | ||
Left, | ||
Right, | ||
Bottom, | ||
Top, | ||
None | ||
} | ||
|
||
/// <summary> | ||
/// Implements a swipe gesture recognizer that can detect swipe gestures on top of a page | ||
/// </summary> | ||
public partial class SwipeGestureRecognizer : GestureRecognizer | ||
{ | ||
private Point _initialPosition; | ||
private int _gestureId; | ||
private IPointer? _tracking; | ||
private bool _pullInProgress; | ||
private SwipeDirection _expectedDirection = SwipeDirection.None; | ||
|
||
public SwipeGestureRecognizer() { } | ||
|
||
protected override void PointerCaptureLost(IPointer pointer) | ||
{ | ||
if (_tracking == pointer) | ||
{ | ||
EndSwipe(null); | ||
} | ||
} | ||
|
||
protected override void PointerMoved(PointerEventArgs e) | ||
{ | ||
if (_tracking == e.Pointer && Target is Visual visual) | ||
{ | ||
var currentPosition = e.GetPosition(visual); | ||
Capture(e.Pointer); | ||
|
||
var delta = new Vector(currentPosition.X - _initialPosition.X, currentPosition.Y - _initialPosition.Y); | ||
_expectedDirection = CalculateDirection(delta); | ||
|
||
_pullInProgress = true; | ||
var pullEventArgs = new SwipeGestureEventArgs(_gestureId, delta, _expectedDirection); | ||
Target?.RaiseEvent(pullEventArgs); | ||
|
||
e.Handled = pullEventArgs.Handled; | ||
} | ||
} | ||
|
||
protected override void PointerPressed(PointerPressedEventArgs e) | ||
{ | ||
if (Target != null && Target is Visual visual && (e.Pointer.Type == PointerType.Touch || e.Pointer.Type == PointerType.Pen)) | ||
{ | ||
var position = e.GetPosition(visual); | ||
|
||
_gestureId = SwipeGestureEventArgs.GetNextFreeId(); | ||
_tracking = e.Pointer; | ||
_initialPosition = position; | ||
} | ||
} | ||
|
||
protected override void PointerReleased(PointerReleasedEventArgs e) | ||
{ | ||
if (_tracking == e.Pointer && _pullInProgress && Target is Visual visual) | ||
{ | ||
var currentPosition = e.GetPosition(visual); | ||
var delta = new Vector(currentPosition.X - _initialPosition.X, currentPosition.Y - _initialPosition.Y); | ||
_expectedDirection = CalculateDirection(delta); | ||
|
||
EndSwipe(delta); | ||
} | ||
} | ||
|
||
private SwipeDirection CalculateDirection(Vector delta) | ||
{ | ||
if (delta.X <= -SwipeThreshold && CanSwipeLeft) | ||
return SwipeDirection.Left; | ||
else if (delta.X >= SwipeThreshold && CanSwipeRight) | ||
return SwipeDirection.Right; | ||
else if (delta.Y <= -SwipeThreshold && CanSwipeUp) | ||
return SwipeDirection.Top; | ||
else if (delta.Y >= SwipeThreshold && CanSwipeDown) | ||
return SwipeDirection.Bottom; | ||
else | ||
return SwipeDirection.None; | ||
} | ||
|
||
private void EndSwipe(Vector? delta) | ||
{ | ||
_tracking = null; | ||
_initialPosition = default; | ||
_pullInProgress = false; | ||
|
||
Target?.RaiseEvent(new SwipeGestureEndedEventArgs(_gestureId, delta, _expectedDirection)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
using Avalonia; | ||
|
||
namespace SwipeNavigation.Gestures; | ||
public partial class SwipeGestureRecognizer | ||
{ | ||
public static readonly StyledProperty<int> SwipeThresholdProperty = | ||
AvaloniaProperty.Register<SwipeGestureRecognizer, int>(nameof(SwipeThreshold), 50); | ||
|
||
/// <summary> | ||
/// Gets or sets the threshold in pixels that must be exceeded for a swipe to have its Direction set. | ||
/// </summary> | ||
public int SwipeThreshold | ||
{ | ||
get => GetValue(SwipeThresholdProperty); | ||
set => SetValue(SwipeThresholdProperty, value); | ||
} | ||
|
||
public static readonly StyledProperty<bool> CanSwipeLeftProperty = | ||
AvaloniaProperty.Register<SwipeGestureRecognizer, bool>(nameof(CanSwipeLeft)); | ||
|
||
/// <summary> | ||
/// Gets or sets the CanSwipeLeft property. This StyledProperty | ||
/// indicates .... | ||
/// </summary> | ||
public bool CanSwipeLeft | ||
{ | ||
get => GetValue(CanSwipeLeftProperty); | ||
set => SetValue(CanSwipeLeftProperty, value); | ||
} | ||
|
||
public static readonly StyledProperty<bool> CanSwipeRightProperty = | ||
AvaloniaProperty.Register<SwipeGestureRecognizer, bool>(nameof(CanSwipeRight)); | ||
|
||
/// <summary> | ||
/// Gets or sets the CanSwipeRight property. This StyledProperty | ||
/// indicates .... | ||
/// </summary> | ||
public bool CanSwipeRight | ||
{ | ||
get => GetValue(CanSwipeRightProperty); | ||
set => SetValue(CanSwipeRightProperty, value); | ||
} | ||
|
||
public static readonly StyledProperty<bool> CanSwipeUpProperty = | ||
AvaloniaProperty.Register<SwipeGestureRecognizer, bool>(nameof(CanSwipeUp)); | ||
|
||
/// <summary> | ||
/// Gets or sets the CanSwipeUp property. This StyledProperty | ||
/// indicates .... | ||
/// </summary> | ||
public bool CanSwipeUp | ||
{ | ||
get => GetValue(CanSwipeUpProperty); | ||
set => SetValue(CanSwipeUpProperty, value); | ||
} | ||
|
||
public static readonly StyledProperty<bool> CanSwipeDownProperty = | ||
AvaloniaProperty.Register<SwipeGestureRecognizer, bool>(nameof(CanSwipeDown)); | ||
|
||
/// <summary> | ||
/// Gets or sets the CanSwipeDown property. This StyledProperty | ||
/// indicates .... | ||
/// </summary> | ||
public bool CanSwipeDown | ||
{ | ||
get => GetValue(CanSwipeDownProperty); | ||
set => SetValue(CanSwipeDownProperty, value); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<UserControl x:Class="SwipeNavigation.Views.MainView" | ||
xmlns="https://github.com/avaloniaui" | ||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" | ||
xmlns:c="using:SwipeNavigation.Controls" | ||
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" | ||
xmlns:g="using:SwipeNavigation.Gestures" | ||
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" | ||
xmlns:v="using:SwipeNavigation.Views" | ||
d:DesignHeight="450" | ||
d:DesignWidth="800" | ||
mc:Ignorable="d"> | ||
<Panel> | ||
<c:TransitioningPageControl> | ||
<!-- Example of how to attach via XAML. Already attached by TransitioningPageControl itself though --> | ||
<!--<c:TransitioningPageControl.GestureRecognizers> | ||
<g:SwipeGestureRecognizer SwipeThreshold="50" CanSwipeLeft="True" CanSwipeRight="True" /> | ||
</c:TransitioningPageControl.GestureRecognizers>--> | ||
|
||
<v:PageOne /> | ||
<v:PageTwo /> | ||
<v:PageThree /> | ||
</c:TransitioningPageControl> | ||
</Panel> | ||
</UserControl> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
using Avalonia.Controls; | ||
|
||
namespace SwipeNavigation.Views; | ||
public partial class MainView : UserControl | ||
{ | ||
public MainView() | ||
{ | ||
InitializeComponent(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<Window x:Class="SwipeNavigation.Views.MainWindow" | ||
xmlns="https://github.com/avaloniaui" | ||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" | ||
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" | ||
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" | ||
xmlns:v="using:SwipeNavigation.Views" | ||
Title="SwipeNavigation" | ||
d:DesignHeight="450" | ||
d:DesignWidth="800" | ||
mc:Ignorable="d"> | ||
<v:MainView /> | ||
</Window> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
using Avalonia.Controls; | ||
|
||
namespace SwipeNavigation.Views; | ||
public partial class MainWindow : Window | ||
{ | ||
public MainWindow() | ||
{ | ||
InitializeComponent(); | ||
} | ||
} |
Oops, something went wrong.