Skip to content

Commit

Permalink
Add easier hotkey shortcuts for buttons
Browse files Browse the repository at this point in the history
  • Loading branch information
Mattias1 committed May 2, 2024
1 parent 9cd47e5 commit 4f940f3
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
13 changes: 13 additions & 0 deletions AvaloniaExtensions/CanvasControlExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Avalonia.Controls;
using Avalonia.Input;
using Avalonia.Markup.Declarative;
using System;

Expand Down Expand Up @@ -30,6 +31,18 @@ public static T WithInitialFocus<T>(this T control) where T : Control {
return CanvasComponentBase.FindCanvasComponent(control).SetInitialControlToFocus(control);
}

public static T HotKeyCtrl<T>(this T control, Key key) where T : Button => control.HotKey(key, KeyModifiers.Control);
public static T HotKeyAlt<T>(this T control, Key key) where T : Button => control.HotKey(key, KeyModifiers.Alt);
public static T HotKey<T>(this T control, Key key, KeyModifiers modifiers) where T : Button {
control.HotKey(new KeyGesture(key, modifiers));
return control;
}

public static T HotKey<T>(this T control, string hotkey) where T : Button {
control.HotKey(KeyGesture.Parse(hotkey));
return control;
}

// --- Basic position and size ---
public static double GetX(this Control control) => Canvas.GetLeft(control);
public static double GetY(this Control control) => Canvas.GetTop(control);
Expand Down
7 changes: 4 additions & 3 deletions ExampleApp/SettingsComponent.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Avalonia.Controls;
using Avalonia.Input;
using Avalonia.Interactivity;
using AvaloniaExtensions;

Expand All @@ -23,9 +24,9 @@ protected override void InitializeControls() {
InsertLabelLeftOf("Checkbox:", _cbExampleToggle, LABEL_WIDTH);
InsertLabelLeftOf("Text:", _tbExampleString, LABEL_WIDTH);

AddButton("Reset defaults", OnResetSettingsClick).BottomLeftInPanel();
var btnOk = AddButton("Ok", OnSaveClick);
AddButton("Cancel", OnCancelClick).BottomRightInPanel();
AddButton("Reset defaults", OnResetSettingsClick).HotKeyAlt(Key.R).BottomLeftInPanel();
var btnOk = AddButton("Ok", OnSaveClick).HotKeyCtrl(Key.Enter);
AddButton("Cancel", OnCancelClick).HotKey("Ctrl+Escape").BottomRightInPanel();
btnOk.LeftOf();

_last = _tbExampleString;
Expand Down

0 comments on commit 4f940f3

Please sign in to comment.