Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update RadialGauge to use KeyboardAccelerators #4456

Merged
merged 6 commits into from
Oct 11, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
92 changes: 65 additions & 27 deletions Microsoft.Toolkit.Uwp.UI.Controls.Input/RadialGauge/RadialGauge.cs
Original file line number Diff line number Diff line change
Expand Up @@ -193,34 +193,10 @@ private void ThemeListener_ThemeChanged(ThemeListener sender)
OnColorsChanged();
}

private void RadialGauge_KeyDown(object sender, KeyRoutedEventArgs e)
{
double step = SmallChange;
var ctrl = Window.Current.CoreWindow.GetKeyState(VirtualKey.Control);
if (ctrl.HasFlag(CoreVirtualKeyStates.Down))
{
step = LargeChange;
}

step = Math.Max(StepSize, step);
if ((e.Key == VirtualKey.Left) || (e.Key == VirtualKey.Down))
{
Value = Math.Max(Minimum, Value - step);
e.Handled = true;
return;
}

if ((e.Key == VirtualKey.Right) || (e.Key == VirtualKey.Up))
{
Value = Math.Min(Maximum, Value + step);
e.Handled = true;
}
}

private void RadialGauge_Unloaded(object sender, RoutedEventArgs e)
{
// Unregister event handlers.
KeyDown -= RadialGauge_KeyDown;
KeyboardAccelerators.Clear();
Sergio0694 marked this conversation as resolved.
Show resolved Hide resolved
ThemeListener.ThemeChanged -= ThemeListener_ThemeChanged;
PointerReleased -= RadialGauge_PointerReleased;
Unloaded -= RadialGauge_Unloaded;
Expand Down Expand Up @@ -439,10 +415,58 @@ protected override void OnApplyTemplate()
_tickBrush = ReadLocalValue(TickBrushProperty) as SolidColorBrush;
_foreground = ReadLocalValue(ForegroundProperty) as SolidColorBrush;

// Register event handlers.
// Small step
AddKeyboardAccelerator(VirtualKeyModifiers.None, VirtualKey.Left, (_, kaea) =>
{
Value = Math.Max(Minimum, Value - Math.Max(StepSize, SmallChange));
kaea.Handled = true;
});

AddKeyboardAccelerator(VirtualKeyModifiers.None, VirtualKey.Up, (_, kaea) =>
{
Value = Math.Min(Maximum, Value + Math.Max(StepSize, SmallChange));
kaea.Handled = true;
});

AddKeyboardAccelerator(VirtualKeyModifiers.None, VirtualKey.Right, (_, kaea) =>
{
Value = Math.Min(Maximum, Value + Math.Max(StepSize, SmallChange));
kaea.Handled = true;
});

AddKeyboardAccelerator(VirtualKeyModifiers.None, VirtualKey.Down, (_, kaea) =>
{
Value = Math.Max(Minimum, Value - Math.Max(StepSize, SmallChange));
kaea.Handled = true;
});

// Large step
AddKeyboardAccelerator(VirtualKeyModifiers.Control, VirtualKey.Left, (_, kaea) =>
{
Value = Math.Max(Minimum, Value - Math.Max(StepSize, LargeChange));
kaea.Handled = true;
});

AddKeyboardAccelerator(VirtualKeyModifiers.Control, VirtualKey.Up, (_, kaea) =>
{
Value = Math.Min(Maximum, Value + Math.Max(StepSize, LargeChange));
kaea.Handled = true;
});

AddKeyboardAccelerator(VirtualKeyModifiers.Control, VirtualKey.Right, (_, kaea) =>
{
Value = Math.Min(Maximum, Value + Math.Max(StepSize, LargeChange));
kaea.Handled = true;
});

AddKeyboardAccelerator(VirtualKeyModifiers.Control, VirtualKey.Down, (_, kaea) =>
{
Value = Math.Max(Minimum, Value - Math.Max(StepSize, LargeChange));
kaea.Handled = true;
});

PointerReleased += RadialGauge_PointerReleased;
ThemeListener.ThemeChanged += ThemeListener_ThemeChanged;
KeyDown += RadialGauge_KeyDown;

// Apply color scheme.
OnColorsChanged();
Expand Down Expand Up @@ -830,5 +854,19 @@ private double RoundToMultiple(double number, double multiple)

return number + modulo;
}

private void AddKeyboardAccelerator(
VirtualKeyModifiers keyModifiers,
VirtualKey key,
TypedEventHandler<KeyboardAccelerator, KeyboardAcceleratorInvokedEventArgs> handler)
{
var accelerator = new KeyboardAccelerator()
{
Modifiers = keyModifiers,
Key = key
};
accelerator.Invoked += handler;
KeyboardAccelerators.Add(accelerator);
}
}
}