Files
LehrerApp/LehrerApp.Desktop/Views/Groups/SeatAssessmentDialog.axaml.cs
T

81 lines
3.0 KiB
C#

using Avalonia.Controls;
using Avalonia.Input;
using Avalonia.Interactivity;
using LehrerApp.Desktop.ViewModels.Groups;
namespace LehrerApp.Desktop.Views.Groups;
public partial class SeatAssessmentDialog : Window
{
public SeatAssessmentDialog()
{
InitializeComponent();
AddHandler(KeyDownEvent, OnPreviewKeyDown, RoutingStrategies.Tunnel, handledEventsToo: true);
}
protected override void OnOpened(EventArgs e)
{
base.OnOpened(e);
Focus();
}
private void OnPreviewKeyDown(object? sender, KeyEventArgs e)
{
if (DataContext is not SeatAssessmentViewModel vm) return;
var digit = DigitFromEvent(e);
var clear = e.Key == Key.X || e.PhysicalKey == PhysicalKey.X;
if (e.KeyModifiers.HasFlag(KeyModifiers.Control))
{
if (clear || digit is not null) vm.ApplyAttendanceShortcut(digit, clear);
e.Handled = clear || digit is not null;
return;
}
if (e.KeyModifiers.HasFlag(KeyModifiers.Alt))
{
if (clear || digit is not null) vm.ApplyHomeworkShortcut(digit, clear);
e.Handled = clear || digit is not null;
return;
}
if (digit is not null)
{
vm.SetRatingByNumber(digit.Value);
e.Handled = true;
return;
}
switch (e.Key)
{
case Key.Q: vm.SelectAspect(0); e.Handled = true; break;
case Key.W: vm.SelectAspect(1); e.Handled = true; break;
case Key.E: vm.SelectAspect(2); e.Handled = true; break;
case Key.R: vm.SelectAspect(3); e.Handled = true; break;
case Key.T: vm.SelectAspect(4); e.Handled = true; break;
case Key.Left: vm.MoveAspect(-1); e.Handled = true; break;
case Key.Right: vm.MoveAspect(1); e.Handled = true; break;
case Key.OemPlus or Key.Add: vm.AdjustCurrentRating(1); e.Handled = true; break;
case Key.OemMinus or Key.Subtract: vm.AdjustCurrentRating(-1); e.Handled = true; break;
case Key.Back: vm.ClearCurrentRating(); e.Handled = true; break;
case Key.Escape: Close(); e.Handled = true; break;
}
}
private static int? DigitFromEvent(KeyEventArgs e) => e.PhysicalKey switch
{
PhysicalKey.Digit0 => 0, PhysicalKey.Digit1 => 1, PhysicalKey.Digit2 => 2,
PhysicalKey.Digit3 => 3, PhysicalKey.Digit4 => 4, PhysicalKey.Digit5 => 5,
PhysicalKey.Digit6 => 6, PhysicalKey.Digit7 => 7, PhysicalKey.Digit8 => 8,
PhysicalKey.Digit9 => 9,
_ => e.Key switch
{
Key.D0 or Key.NumPad0 => 0, Key.D1 or Key.NumPad1 => 1,
Key.D2 or Key.NumPad2 => 2, Key.D3 or Key.NumPad3 => 3,
Key.D4 or Key.NumPad4 => 4, Key.D5 or Key.NumPad5 => 5,
Key.D6 or Key.NumPad6 => 6, Key.D7 or Key.NumPad7 => 7,
Key.D8 or Key.NumPad8 => 8, Key.D9 or Key.NumPad9 => 9,
_ => null,
},
};
private void OnClose(object? sender, RoutedEventArgs e) => Close();
}