|
| 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 | +} |
0 commit comments