using System; using Avalonia.Input; using Avalonia.Interactivity; using OpenNest.Avalonia.Controls; namespace OpenNest.Avalonia { /// /// View > Zoom To (Area, Fit, Plate, Selected), Zoom In (Ctrl+Plus) and Zoom Out /// (Ctrl+Minus); Nest > First, Last, Next (Ctrl+Right) and Previous (Ctrl+Left) Plate; and /// Tools > Measure Area, as the WinForms main form's menus. /// public partial class MainWindow { private void AddViewKeyBindings() { foreach (var (key, action) in new (Key, Action)[] { (Key.OemPlus, () => OnZoomIn(null, null)), (Key.OemMinus, () => OnZoomOut(null, null)), (Key.Right, () => StepPlate(1)), (Key.Left, () => StepPlate(-1)), }) { KeyBindings.Add(new KeyBinding { Gesture = new KeyGesture(key, KeyModifiers.Control), Command = new Command(action), }); } } private void OnZoomIn(object sender, RoutedEventArgs e) => ActiveView?.PlateView.ZoomIn(); private void OnZoomOut(object sender, RoutedEventArgs e) => ActiveView?.PlateView.ZoomOut(); private void OnZoomToPlate(object sender, RoutedEventArgs e) => ActiveView?.PlateView.ZoomToPlate(); private void OnZoomToSelected(object sender, RoutedEventArgs e) => ActiveView?.PlateView.ZoomToSelected(); private void OnZoomToArea(object sender, RoutedEventArgs e) => StartTool(canvas => canvas.StartZoomWindow()); private void OnMeasureArea(object sender, RoutedEventArgs e) => StartTool(canvas => canvas.StartMeasureArea()); // The view takes the keyboard, so Escape (and Space for Measure Area) reach the tool after // a menu click. private void StartTool(Action start) { if (ActiveView is not { PlateView: { } canvas } view) return; start(canvas); // Focusing the view would otherwise place a drawing picked up in the list. view.CancelPickup(); canvas.Focus(); } private void OnFirstPlate(object sender, RoutedEventArgs e) => ActiveView?.ShowPlate(0); private void OnLastPlate(object sender, RoutedEventArgs e) { if (ActiveView is { } view) view.ShowPlate(view.PlateCount - 1); } } }