Skip to content

Commit

Permalink
Merge pull request #6784 from AvaloniaUI/fixes/button-access-key
Browse files Browse the repository at this point in the history
Add support for access keys to Button.
# Conflicts:
#	src/Avalonia.Controls/Primitives/AccessText.cs
  • Loading branch information
Takoooooo authored and danwalmsley committed Oct 26, 2021
1 parent 9da1793 commit bcdfa7d
Show file tree
Hide file tree
Showing 18 changed files with 51 additions and 24 deletions.
8 changes: 4 additions & 4 deletions samples/ControlCatalog/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,11 @@
<Panel Margin="{Binding #MainWindow.OffScreenMargin}">
<DockPanel LastChildFill="True" Margin="{Binding #MainWindow.WindowDecorationMargin}">
<Menu Name="MainMenu" DockPanel.Dock="Top">
<MenuItem Header="File">
<MenuItem Header="Exit" Command="{Binding ExitCommand}" />
<MenuItem Header="_File">
<MenuItem Header="E_xit" Command="{Binding ExitCommand}" />
</MenuItem>
<MenuItem Header="Help">
<MenuItem Header="About" Command="{Binding AboutCommand}" />
<MenuItem Header="_Help">
<MenuItem Header="_About" Command="{Binding AboutCommand}" />
</MenuItem>
</Menu>
<local:MainView />
Expand Down
2 changes: 1 addition & 1 deletion samples/ControlCatalog/Pages/ButtonPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
HorizontalAlignment="Center"
Spacing="16">
<StackPanel Orientation="Vertical" Spacing="8" Width="200">
<Button>Standard XAML Button</Button>
<Button>Standard _XAML Button</Button>
<Button Foreground="White">Foreground</Button>
<Button Background="{DynamicResource SystemAccentColor}">Background</Button>
<Button IsEnabled="False">Disabled</Button>
Expand Down
6 changes: 3 additions & 3 deletions samples/ControlCatalog/Pages/CheckBoxPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@
Spacing="16">
<StackPanel Orientation="Vertical"
Spacing="16">
<CheckBox>Unchecked</CheckBox>
<CheckBox IsChecked="True">Checked</CheckBox>
<CheckBox IsChecked="{x:Null}">Indeterminate</CheckBox>
<CheckBox>_Unchecked</CheckBox>
<CheckBox IsChecked="True">_Checked</CheckBox>
<CheckBox IsChecked="{x:Null}">_Indeterminate</CheckBox>
<CheckBox IsChecked="True" IsEnabled="False">Disabled</CheckBox>
</StackPanel>
<StackPanel Orientation="Vertical"
Expand Down
20 changes: 10 additions & 10 deletions samples/ControlCatalog/Pages/DialogsPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@
x:Class="ControlCatalog.Pages.DialogsPage">
<StackPanel Orientation="Vertical" Spacing="4" Margin="4">
<CheckBox Name="UseFilters">Use filters</CheckBox>
<Button Name="OpenFile">Open File</Button>
<Button Name="SaveFile">Save File</Button>
<Button Name="SelectFolder">Select Folder</Button>
<Button Name="OpenBoth">Select Both</Button>
<Button Name="DecoratedWindow">Decorated window</Button>
<Button Name="DecoratedWindowDialog">Decorated window (dialog)</Button>
<Button Name="Dialog">Dialog</Button>
<Button Name="DialogNoTaskbar">Dialog (No taskbar icon)</Button>
<Button Name="OwnedWindow">Owned window</Button>
<Button Name="OwnedWindowNoTaskbar">Owned window (No taskbar icon)</Button>
<Button Name="OpenFile">_Open File</Button>
<Button Name="SaveFile">_Save File</Button>
<Button Name="SelectFolder">Select Fo_lder</Button>
<Button Name="OpenBoth">Select _Both</Button>
<Button Name="DecoratedWindow">Decorated _window</Button>
<Button Name="DecoratedWindowDialog">Decorated w_indow (dialog)</Button>
<Button Name="Dialog">_Dialog</Button>
<Button Name="DialogNoTaskbar">Dialog (_No taskbar icon)</Button>
<Button Name="OwnedWindow">Own_ed window</Button>
<Button Name="OwnedWindowNoTaskbar">Owned window (No tas_kbar icon)</Button>
</StackPanel>
</UserControl>
6 changes: 3 additions & 3 deletions samples/ControlCatalog/Pages/RadioButtonPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@
Spacing="16">
<StackPanel Orientation="Vertical"
Spacing="16">
<RadioButton IsChecked="True">Option 1</RadioButton>
<RadioButton>Option 2</RadioButton>
<RadioButton IsChecked="{x:Null}">Option 3</RadioButton>
<RadioButton IsChecked="True">_Option 1</RadioButton>
<RadioButton>O_ption 2</RadioButton>
<RadioButton IsChecked="{x:Null}">Op_tion 3</RadioButton>
<RadioButton IsEnabled="False">Disabled</RadioButton>
</StackPanel>
<StackPanel Orientation="Vertical"
Expand Down
6 changes: 3 additions & 3 deletions samples/ControlCatalog/Pages/ToggleSwitchPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

<Border Classes="Thin">
<StackPanel>
<ToggleSwitch Content="headered" IsChecked="true" Margin="10"/>
<ToggleSwitch Content="h_eadered" IsChecked="true" Margin="10"/>
<TextBox Classes="CodeBox"
Text="&lt;ToggleSwitch&gt;headered&lt;/ToggleSwitch&gt;"/>
</StackPanel>
Expand All @@ -24,7 +24,7 @@

<Border Classes="Thin">
<StackPanel>
<ToggleSwitch Content="Custom"
<ToggleSwitch Content="_Custom"
OnContent="On"
OffContent="Off"
Margin="10"/>
Expand All @@ -40,7 +40,7 @@ ContentOff=&quot;Off&quot; /&gt;"

<Border Classes="Thin">
<StackPanel>
<ToggleSwitch Content="Just Click!" Margin="10">
<ToggleSwitch Content="_Just Click!" Margin="10">
<ToggleSwitch.OnContent>
<Image Source="/Assets/hirsch-899118_640.jpg" Height="32"/>
</ToggleSwitch.OnContent>
Expand Down
3 changes: 3 additions & 0 deletions src/Avalonia.Controls/Button.cs
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ static Button()
CommandParameterProperty.Changed.Subscribe(CommandParameterChanged);
IsDefaultProperty.Changed.Subscribe(IsDefaultChanged);
IsCancelProperty.Changed.Subscribe(IsCancelChanged);
AccessKeyHandler.AccessKeyPressedEvent.AddClassHandler<Button>((lbl, args) => lbl.OnAccessKey(args));
}

public Button()
Expand Down Expand Up @@ -256,6 +257,8 @@ protected override void OnDetachedFromLogicalTree(LogicalTreeAttachmentEventArgs
}
}

protected virtual void OnAccessKey(RoutedEventArgs e) => OnClick();

/// <inheritdoc/>
protected override void OnKeyDown(KeyEventArgs e)
{
Expand Down
1 change: 1 addition & 0 deletions src/Avalonia.Themes.Default/Button.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
ContentTemplate="{TemplateBinding ContentTemplate}"
Content="{TemplateBinding Content}"
Padding="{TemplateBinding Padding}"
RecognizesAccessKey="True"
TextBlock.Foreground="{TemplateBinding Foreground}"
HorizontalContentAlignment="{TemplateBinding HorizontalContentAlignment}"
VerticalContentAlignment="{TemplateBinding VerticalContentAlignment}"/>
Expand Down
1 change: 1 addition & 0 deletions src/Avalonia.Themes.Default/CheckBox.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
ContentTemplate="{TemplateBinding ContentTemplate}"
Content="{TemplateBinding Content}"
Margin="{TemplateBinding Padding}"
RecognizesAccessKey="True"
VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
IsVisible="{TemplateBinding Content, Converter={x:Static ObjectConverters.IsNotNull}}"
Expand Down
1 change: 1 addition & 0 deletions src/Avalonia.Themes.Default/RadioButton.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
ContentTemplate="{TemplateBinding ContentTemplate}"
Content="{TemplateBinding Content}"
Margin="4,0,0,0"
RecognizesAccessKey="True"
VerticalAlignment="Center"
Grid.Column="1"/>
</Grid>
Expand Down
1 change: 1 addition & 0 deletions src/Avalonia.Themes.Default/ToggleButton.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
ContentTemplate="{TemplateBinding ContentTemplate}"
Content="{TemplateBinding Content}"
Padding="{TemplateBinding Padding}"
RecognizesAccessKey="True"
TextBlock.Foreground="{TemplateBinding Foreground}"
HorizontalContentAlignment="{TemplateBinding HorizontalContentAlignment}"
VerticalContentAlignment="{TemplateBinding VerticalContentAlignment}"/>
Expand Down
1 change: 1 addition & 0 deletions src/Avalonia.Themes.Default/ToggleSwitch.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@
Grid.Row="0"
Content="{TemplateBinding Content}"
ContentTemplate="{TemplateBinding ContentTemplate}"
RecognizesAccessKey="True"
VerticalAlignment="Top"/>

<Grid Grid.Row="1"
Expand Down
1 change: 1 addition & 0 deletions src/Avalonia.Themes.Fluent/Controls/Button.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
Content="{TemplateBinding Content}"
ContentTemplate="{TemplateBinding ContentTemplate}"
Padding="{TemplateBinding Padding}"
RecognizesAccessKey="True"
HorizontalContentAlignment="{TemplateBinding HorizontalContentAlignment}"
VerticalContentAlignment="{TemplateBinding VerticalContentAlignment}" />
</ControlTemplate>
Expand Down
1 change: 1 addition & 0 deletions src/Avalonia.Themes.Fluent/Controls/CheckBox.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
ContentTemplate="{TemplateBinding ContentTemplate}"
Content="{TemplateBinding Content}"
Margin="{TemplateBinding Padding}"
RecognizesAccessKey="True"
HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
Grid.Column="1" />
Expand Down
1 change: 1 addition & 0 deletions src/Avalonia.Themes.Fluent/Controls/RadioButton.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
ContentTemplate="{TemplateBinding ContentTemplate}"
TextBlock.Foreground="{TemplateBinding Foreground}"
Margin="{TemplateBinding Padding}"
RecognizesAccessKey="True"
HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
Grid.Column="1" />
Expand Down
1 change: 1 addition & 0 deletions src/Avalonia.Themes.Fluent/Controls/ToggleButton.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
Content="{TemplateBinding Content}"
ContentTemplate="{TemplateBinding ContentTemplate}"
Padding="{TemplateBinding Padding}"
RecognizesAccessKey="True"
HorizontalContentAlignment="{TemplateBinding HorizontalContentAlignment}"
VerticalContentAlignment="{TemplateBinding VerticalContentAlignment}" />
</ControlTemplate>
Expand Down
1 change: 1 addition & 0 deletions src/Avalonia.Themes.Fluent/Controls/ToggleSwitch.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
Grid.Row="0"
Content="{TemplateBinding Content}"
ContentTemplate="{TemplateBinding ContentTemplate}"
RecognizesAccessKey="True"
VerticalAlignment="Top"/>

<Grid Grid.Row="1"
Expand Down
14 changes: 14 additions & 0 deletions tests/Avalonia.Controls.UnitTests/ButtonTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Windows.Input;
using Avalonia.Data;
using Avalonia.Input;
using Avalonia.Interactivity;
using Avalonia.Media;
using Avalonia.Platform;
using Avalonia.Rendering;
Expand Down Expand Up @@ -271,6 +272,19 @@ public void Button_Unsubscribes_From_Command_CanExecuteChanged_When_Removed_From

[Fact]
public void Button_Invokes_CanExecute_When_CommandParameter_Changed()
{
var target = new Button();
var raised = 0;

target.Click += (s, e) => ++raised;

target.RaiseEvent(new RoutedEventArgs(AccessKeyHandler.AccessKeyPressedEvent));

Assert.Equal(1, raised);
}

[Fact]
public void Raises_Click_When_AccessKey_Raised()
{
var command = new TestCommand(p => p is bool value && value);
var target = new Button { Command = command };
Expand Down

0 comments on commit bcdfa7d

Please sign in to comment.