Skip to content

Commit 013b9d3

Browse files
Add mouse clicking debounce sample
1 parent 55297b0 commit 013b9d3

File tree

3 files changed

+63
-1
lines changed

3 files changed

+63
-1
lines changed
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<Page
2+
x:Class="ExtensionsExperiment.Samples.DispatcherQueueExtensions.MouseDebounceSample"
3+
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
4+
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
5+
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
6+
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
7+
mc:Ignorable="d"
8+
Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
9+
10+
<StackPanel Spacing="8">
11+
<Button Click="Button_Click" Content="Click Me"/>
12+
<TextBlock x:Name="ResultText"/>
13+
</StackPanel>
14+
</Page>
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
// See the LICENSE file in the project root for more information.
4+
5+
using CommunityToolkit.WinUI;
6+
#if WINAPPSDK
7+
using DispatcherQueue = Microsoft.UI.Dispatching.DispatcherQueue;
8+
using DispatcherQueueTimer = Microsoft.UI.Dispatching.DispatcherQueueTimer;
9+
#else
10+
using DispatcherQueue = Windows.System.DispatcherQueue;
11+
using DispatcherQueueTimer = Windows.System.DispatcherQueueTimer;
12+
#endif
13+
14+
namespace ExtensionsExperiment.Samples.DispatcherQueueExtensions;
15+
16+
[ToolkitSample(id: nameof(MouseDebounceSample), "DispatcherQueueTimer Debounce Mouse", description: "A sample for showing how to use the DispatcherQueueTimer Debounce extension to smooth mouse input.")]
17+
[ToolkitSampleNumericOption("Interval", 400, 300, 1000)]
18+
public sealed partial class MouseDebounceSample : Page
19+
{
20+
public DispatcherQueueTimer _debounceTimer = DispatcherQueue.GetForCurrentThread().CreateTimer();
21+
22+
private int _count = 0;
23+
24+
public MouseDebounceSample()
25+
{
26+
InitializeComponent();
27+
}
28+
29+
private void Button_Click(object sender, RoutedEventArgs e)
30+
{
31+
var interval = this.GeneratedPropertyMetadata?.FirstOrDefault(vm => vm.Name == "Interval")?.Value as double?;
32+
33+
if (interval != null)
34+
{
35+
_debounceTimer.Debounce(() =>
36+
{
37+
ResultText.Text = $"You hit the button {++_count} times!";
38+
},
39+
interval: TimeSpan.FromMilliseconds(interval.Value),
40+
// By being on the leading edge, we ignore inputs past the first for the duration of the interval
41+
immediate: true);
42+
}
43+
}
44+
}

components/Extensions/samples/DispatcherQueueTimerExtensions.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,14 @@ The `DispatcherQueueTimerExtensions` static class provides a collection of exten
1818

1919
The `DispatcherQueueTimerExtensions` static class currently exposes a single extension method, `Debounce`. This is a standard technique used to rate-limit input from a user to not overload requests on an underlying service of query elsewhere.
2020

21-
It can be used in a number of ways, but most simply like so:
21+
It can be used in a number of ways, but most simply like so as a keyboard limiter:
2222

2323
> [!SAMPLE KeyboardDebounceSample]
2424
25+
Or for preventing multiple inputs from occuring accidentally (e.g. ignoring a double/multi-click):
26+
27+
> [!SAMPLE MouseDebounceSample]
28+
2529
## Examples
2630

2731
You can find more examples in the [unit tests](https://github.com/CommunityToolkit/Windows/blob/rel/8.1.240916/components/Extensions/tests/DispatcherQueueTimerExtensionTests.cs).

0 commit comments

Comments
 (0)