Skip to content

Commit 70f7ab8

Browse files
Register Keyboard Handlers for RadialGauge once and make lambda functions static
Contributes to #4784
1 parent b2ea7ba commit 70f7ab8

File tree

1 file changed

+75
-52
lines changed

1 file changed

+75
-52
lines changed

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

Lines changed: 75 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,81 @@ public RadialGauge()
185185

186186
SmallChange = 1;
187187
LargeChange = 10;
188+
189+
// Small step
190+
AddKeyboardAccelerator(VirtualKeyModifiers.None, VirtualKey.Left, static (_, kaea) =>
191+
{
192+
if (kaea.Element is RadialGauge gauge)
193+
{
194+
gauge.Value = Math.Max(gauge.Minimum, gauge.Value - Math.Max(gauge.StepSize, gauge.SmallChange));
195+
kaea.Handled = true;
196+
}
197+
});
198+
199+
AddKeyboardAccelerator(VirtualKeyModifiers.None, VirtualKey.Up, static (_, kaea) =>
200+
{
201+
if (kaea.Element is RadialGauge gauge)
202+
{
203+
gauge.Value = Math.Min(gauge.Maximum, gauge.Value + Math.Max(gauge.StepSize, gauge.SmallChange));
204+
kaea.Handled = true;
205+
}
206+
});
207+
208+
AddKeyboardAccelerator(VirtualKeyModifiers.None, VirtualKey.Right, static (_, kaea) =>
209+
{
210+
if (kaea.Element is RadialGauge gauge)
211+
{
212+
gauge.Value = Math.Min(gauge.Maximum, gauge.Value + Math.Max(gauge.StepSize, gauge.SmallChange));
213+
kaea.Handled = true;
214+
}
215+
});
216+
217+
AddKeyboardAccelerator(VirtualKeyModifiers.None, VirtualKey.Down, static (_, kaea) =>
218+
{
219+
if (kaea.Element is RadialGauge gauge)
220+
{
221+
gauge.Value = Math.Max(gauge.Minimum, gauge.Value - Math.Max(gauge.StepSize, gauge.SmallChange));
222+
kaea.Handled = true;
223+
}
224+
});
225+
226+
// Large step
227+
AddKeyboardAccelerator(VirtualKeyModifiers.Control, VirtualKey.Left, static (_, kaea) =>
228+
{
229+
if (kaea.Element is RadialGauge gauge)
230+
{
231+
gauge.Value = Math.Max(gauge.Minimum, gauge.Value - Math.Max(gauge.StepSize, gauge.LargeChange));
232+
kaea.Handled = true;
233+
}
234+
});
235+
236+
AddKeyboardAccelerator(VirtualKeyModifiers.Control, VirtualKey.Up, static (_, kaea) =>
237+
{
238+
if (kaea.Element is RadialGauge gauge)
239+
{
240+
gauge.Value = Math.Min(gauge.Maximum, gauge.Value + Math.Max(gauge.StepSize, gauge.LargeChange));
241+
kaea.Handled = true;
242+
}
243+
});
244+
245+
AddKeyboardAccelerator(VirtualKeyModifiers.Control, VirtualKey.Right, static (_, kaea) =>
246+
{
247+
if (kaea.Element is RadialGauge gauge)
248+
{
249+
gauge.Value = Math.Min(gauge.Maximum, gauge.Value + Math.Max(gauge.StepSize, gauge.LargeChange));
250+
kaea.Handled = true;
251+
}
252+
});
253+
254+
AddKeyboardAccelerator(VirtualKeyModifiers.Control, VirtualKey.Down, static (_, kaea) =>
255+
{
256+
if (kaea.Element is RadialGauge gauge)
257+
{
258+
gauge.Value = Math.Max(gauge.Minimum, gauge.Value - Math.Max(gauge.StepSize, gauge.LargeChange));
259+
kaea.Handled = true;
260+
}
261+
});
262+
188263
Unloaded += RadialGauge_Unloaded;
189264
}
190265

@@ -195,8 +270,6 @@ private void ThemeListener_ThemeChanged(ThemeListener sender)
195270

196271
private void RadialGauge_Unloaded(object sender, RoutedEventArgs e)
197272
{
198-
// Unregister event handlers.
199-
KeyboardAccelerators.Clear();
200273
ThemeListener.ThemeChanged -= ThemeListener_ThemeChanged;
201274
PointerReleased -= RadialGauge_PointerReleased;
202275
Unloaded -= RadialGauge_Unloaded;
@@ -415,56 +488,6 @@ protected override void OnApplyTemplate()
415488
_tickBrush = ReadLocalValue(TickBrushProperty) as SolidColorBrush;
416489
_foreground = ReadLocalValue(ForegroundProperty) as SolidColorBrush;
417490

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-
468491
PointerReleased += RadialGauge_PointerReleased;
469492
ThemeListener.ThemeChanged += ThemeListener_ThemeChanged;
470493

0 commit comments

Comments
 (0)