Skip to content

Commit 06aec3e

Browse files
XamlBrewernmetulevArlodotexe
authored
Update RadialGauge to use KeyboardAccelerators (#4456)
* Update RadialGauge to use KeyboardAccelerators Replace KeyDown event handling by KeyboardAccelerators. This does not change the behavior. It improves performance by not reacting to just any key stroke. I also removes the call to CoreWindow.GetKeyState - an API that doesn't exist in WinUI 3. * Update RadialGauge.cs Fixed StyleCop issues * Formatted code, fixed remaining regressions * Update Microsoft.Toolkit.Uwp.UI.Controls.Input/RadialGauge/RadialGauge.cs Co-authored-by: Nikola Metulev <nmetulev@users.noreply.github.com> Co-authored-by: Arlo <arlo.godfrey@outlook.com>
1 parent 0249f73 commit 06aec3e

File tree

1 file changed

+65
-27
lines changed

1 file changed

+65
-27
lines changed

Microsoft.Toolkit.Uwp.UI.Controls.Input/RadialGauge/RadialGauge.cs

Lines changed: 65 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -193,34 +193,10 @@ private void ThemeListener_ThemeChanged(ThemeListener sender)
193193
OnColorsChanged();
194194
}
195195

196-
private void RadialGauge_KeyDown(object sender, KeyRoutedEventArgs e)
197-
{
198-
double step = SmallChange;
199-
var ctrl = Window.Current.CoreWindow.GetKeyState(VirtualKey.Control);
200-
if (ctrl.HasFlag(CoreVirtualKeyStates.Down))
201-
{
202-
step = LargeChange;
203-
}
204-
205-
step = Math.Max(StepSize, step);
206-
if ((e.Key == VirtualKey.Left) || (e.Key == VirtualKey.Down))
207-
{
208-
Value = Math.Max(Minimum, Value - step);
209-
e.Handled = true;
210-
return;
211-
}
212-
213-
if ((e.Key == VirtualKey.Right) || (e.Key == VirtualKey.Up))
214-
{
215-
Value = Math.Min(Maximum, Value + step);
216-
e.Handled = true;
217-
}
218-
}
219-
220196
private void RadialGauge_Unloaded(object sender, RoutedEventArgs e)
221197
{
222198
// Unregister event handlers.
223-
KeyDown -= RadialGauge_KeyDown;
199+
KeyboardAccelerators.Clear();
224200
ThemeListener.ThemeChanged -= ThemeListener_ThemeChanged;
225201
PointerReleased -= RadialGauge_PointerReleased;
226202
Unloaded -= RadialGauge_Unloaded;
@@ -439,10 +415,58 @@ protected override void OnApplyTemplate()
439415
_tickBrush = ReadLocalValue(TickBrushProperty) as SolidColorBrush;
440416
_foreground = ReadLocalValue(ForegroundProperty) as SolidColorBrush;
441417

442-
// Register event handlers.
418+
// Small step
419+
AddKeyboardAccelerator(VirtualKeyModifiers.None, VirtualKey.Left, (_, kaea) =>
420+
{
421+
Value = Math.Max(Minimum, Value - Math.Max(StepSize, SmallChange));
422+
kaea.Handled = true;
423+
});
424+
425+
AddKeyboardAccelerator(VirtualKeyModifiers.None, VirtualKey.Up, (_, kaea) =>
426+
{
427+
Value = Math.Min(Maximum, Value + Math.Max(StepSize, SmallChange));
428+
kaea.Handled = true;
429+
});
430+
431+
AddKeyboardAccelerator(VirtualKeyModifiers.None, VirtualKey.Right, (_, kaea) =>
432+
{
433+
Value = Math.Min(Maximum, Value + Math.Max(StepSize, SmallChange));
434+
kaea.Handled = true;
435+
});
436+
437+
AddKeyboardAccelerator(VirtualKeyModifiers.None, VirtualKey.Down, (_, kaea) =>
438+
{
439+
Value = Math.Max(Minimum, Value - Math.Max(StepSize, SmallChange));
440+
kaea.Handled = true;
441+
});
442+
443+
// Large step
444+
AddKeyboardAccelerator(VirtualKeyModifiers.Control, VirtualKey.Left, (_, kaea) =>
445+
{
446+
Value = Math.Max(Minimum, Value - Math.Max(StepSize, LargeChange));
447+
kaea.Handled = true;
448+
});
449+
450+
AddKeyboardAccelerator(VirtualKeyModifiers.Control, VirtualKey.Up, (_, kaea) =>
451+
{
452+
Value = Math.Min(Maximum, Value + Math.Max(StepSize, LargeChange));
453+
kaea.Handled = true;
454+
});
455+
456+
AddKeyboardAccelerator(VirtualKeyModifiers.Control, VirtualKey.Right, (_, kaea) =>
457+
{
458+
Value = Math.Min(Maximum, Value + Math.Max(StepSize, LargeChange));
459+
kaea.Handled = true;
460+
});
461+
462+
AddKeyboardAccelerator(VirtualKeyModifiers.Control, VirtualKey.Down, (_, kaea) =>
463+
{
464+
Value = Math.Max(Minimum, Value - Math.Max(StepSize, LargeChange));
465+
kaea.Handled = true;
466+
});
467+
443468
PointerReleased += RadialGauge_PointerReleased;
444469
ThemeListener.ThemeChanged += ThemeListener_ThemeChanged;
445-
KeyDown += RadialGauge_KeyDown;
446470

447471
// Apply color scheme.
448472
OnColorsChanged();
@@ -830,5 +854,19 @@ private double RoundToMultiple(double number, double multiple)
830854

831855
return number + modulo;
832856
}
857+
858+
private void AddKeyboardAccelerator(
859+
VirtualKeyModifiers keyModifiers,
860+
VirtualKey key,
861+
TypedEventHandler<KeyboardAccelerator, KeyboardAcceleratorInvokedEventArgs> handler)
862+
{
863+
var accelerator = new KeyboardAccelerator()
864+
{
865+
Modifiers = keyModifiers,
866+
Key = key
867+
};
868+
accelerator.Invoked += handler;
869+
KeyboardAccelerators.Add(accelerator);
870+
}
833871
}
834872
}

0 commit comments

Comments
 (0)