Browse Source

Merge branch 'master' into feature/flowDirectionImpl

pull/7810/head
Benedikt Stebner 4 years ago
committed by GitHub
parent
commit
c98571764d
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 32
      samples/ControlCatalog/Pages/ButtonsPage.xaml
  2. 4
      src/Avalonia.Controls/ApiCompatBaseline.txt
  3. 123
      src/Avalonia.Controls/Button.cs
  4. 28
      src/Avalonia.Controls/DropDown.cs
  5. 15
      src/Avalonia.Controls/DropDownButton.cs
  6. 67
      src/Avalonia.Controls/HotkeyManager.cs
  7. 10
      src/Avalonia.Controls/MenuItem.cs
  8. 5
      src/Avalonia.Controls/Primitives/TemplatedControl.cs
  9. 32
      src/Avalonia.Controls/SplitButton/SplitButton.cs
  10. 18
      src/Avalonia.Input/IClickableControl.cs
  11. 4
      src/Avalonia.Input/Properties/AssemblyInfo.cs
  12. 67
      src/Avalonia.Themes.Default/Controls/DropDownButton.xaml
  13. 4
      src/Avalonia.Themes.Default/Controls/SplitButton.xaml
  14. 1
      src/Avalonia.Themes.Default/DefaultTheme.xaml
  15. 14
      src/Avalonia.Themes.Fluent/Controls/Button.xaml
  16. 1
      src/Avalonia.Themes.Fluent/Controls/ComboBox.xaml
  17. 103
      src/Avalonia.Themes.Fluent/Controls/DropDownButton.xaml
  18. 1
      src/Avalonia.Themes.Fluent/Controls/FluentControls.xaml
  19. 4
      src/Avalonia.Themes.Fluent/Controls/SplitButton.xaml
  20. 149
      tests/Avalonia.Controls.UnitTests/Utils/HotKeyManagerTests.cs

32
samples/ControlCatalog/Pages/ButtonsPage.xaml

@ -147,6 +147,35 @@
</StackPanel>
</Border>
<!-- DropDownButton -->
<Border Classes="header-border">
<StackPanel Orientation="Vertical"
Spacing="4">
<TextBlock Text="DropDownButton"
Classes="header" />
<TextBlock TextWrapping="Wrap">A button with an added drop-down chevron to visually indicate it has a flyout with additional actions.</TextBlock>
</StackPanel>
</Border>
<Border Classes="thin"
Padding="15">
<StackPanel Orientation="Vertical"
Spacing="8">
<DropDownButton Flyout="{StaticResource SharedMenuFlyout}">
<TextBlock Text="Drop Down Button" />
</DropDownButton>
<DropDownButton Padding="0,0,8,0">
<Border Background="Teal"
HorizontalAlignment="Stretch"
VerticalAlignment="Stretch"
Height="32"
Width="32" />
</DropDownButton>
<DropDownButton IsEnabled="False">Disabled</DropDownButton>
<DropDownButton />
</StackPanel>
</Border>
<!-- SplitButton -->
<Border Classes="header-border">
<StackPanel Orientation="Vertical"
@ -169,7 +198,8 @@
<TextBlock Text="Disabled" />
</SplitButton>
<SplitButton Flyout="{StaticResource SharedMenuFlyout}"
Content="Re-themed">
Content="Re-themed"
Foreground="White">
<SplitButton.Styles>
<Style>
<Style.Resources>

4
src/Avalonia.Controls/ApiCompatBaseline.txt

@ -1,4 +1,6 @@
Compat issues with assembly Avalonia.Controls:
TypesMustExist : Type 'Avalonia.Controls.DropDown' does not exist in the implementation but it does exist in the contract.
TypesMustExist : Type 'Avalonia.Controls.DropDownItem' does not exist in the implementation but it does exist in the contract.
InterfacesShouldHaveSameMembers : Interface member 'public System.Boolean Avalonia.Controls.IMenuItem.StaysOpenOnClick' is present in the implementation but not in the contract.
InterfacesShouldHaveSameMembers : Interface member 'public System.Boolean Avalonia.Controls.IMenuItem.StaysOpenOnClick.get()' is present in the implementation but not in the contract.
InterfacesShouldHaveSameMembers : Interface member 'public void Avalonia.Controls.IMenuItem.StaysOpenOnClick.set(System.Boolean)' is present in the implementation but not in the contract.
@ -87,4 +89,4 @@ InterfacesShouldHaveSameMembers : Interface member 'public void Avalonia.Platfor
MembersMustExist : Member 'public void Avalonia.Platform.IWindowImpl.Resize(Avalonia.Size)' does not exist in the implementation but it does exist in the contract.
InterfacesShouldHaveSameMembers : Interface member 'public void Avalonia.Platform.IWindowImpl.Resize(Avalonia.Size, Avalonia.Platform.PlatformResizeReason)' is present in the implementation but not in the contract.
InterfacesShouldHaveSameMembers : Interface member 'public Avalonia.Platform.ITrayIconImpl Avalonia.Platform.IWindowingPlatform.CreateTrayIcon()' is present in the implementation but not in the contract.
Total Issues: 88
Total Issues: 90

123
src/Avalonia.Controls/Button.cs

@ -29,11 +29,14 @@ namespace Avalonia.Controls
}
/// <summary>
/// A button control.
/// A standard button control.
/// </summary>
[PseudoClasses(":pressed")]
public class Button : ContentControl, ICommandSource
[PseudoClasses(pcFlyoutOpen, pcPressed)]
public class Button : ContentControl, ICommandSource, IClickableControl
{
protected const string pcPressed = ":pressed";
protected const string pcFlyoutOpen = ":flyout-open";
/// <summary>
/// Defines the <see cref="ClickMode"/> property.
/// </summary>
@ -92,6 +95,7 @@ namespace Avalonia.Controls
private ICommand? _command;
private bool _commandCanExecute = true;
private KeyGesture? _hotkey;
private bool _isFlyoutOpen = false;
/// <summary>
/// Initializes static members of the <see cref="Button"/> class.
@ -107,7 +111,6 @@ namespace Avalonia.Controls
/// </summary>
public Button()
{
UpdatePseudoClasses(IsPressed);
}
/// <summary>
@ -328,11 +331,30 @@ namespace Avalonia.Controls
}
}
/// <summary>
/// Opens the button's flyout.
/// </summary>
protected virtual void OpenFlyout()
{
Flyout?.ShowAt(this);
}
/// <summary>
/// Invoked when the button's flyout is opened.
/// </summary>
protected virtual void OnFlyoutOpened()
{
// Available for derived types
}
/// <summary>
/// Invoked when the button's flyout is closed.
/// </summary>
protected virtual void OnFlyoutClosed()
{
// Available for derived types
}
/// <inheritdoc/>
protected override void OnPointerPressed(PointerPressedEventArgs e)
{
@ -382,6 +404,14 @@ namespace Avalonia.Controls
IsPressed = false;
}
/// <inheritdoc/>
protected override void OnApplyTemplate(TemplateAppliedEventArgs e)
{
UnregisterFlyoutEvents(Flyout);
RegisterFlyoutEvents(Flyout);
UpdatePseudoClasses();
}
/// <inheritdoc/>
protected override void OnPropertyChanged<T>(AvaloniaPropertyChangedEventArgs<T> change)
{
@ -442,17 +472,26 @@ namespace Avalonia.Controls
}
else if (change.Property == IsPressedProperty)
{
UpdatePseudoClasses(change.NewValue.GetValueOrDefault<bool>());
UpdatePseudoClasses();
}
else if (change.Property == FlyoutProperty)
{
var oldFlyout = change.OldValue.GetValueOrDefault() as FlyoutBase;
var newFlyout = change.NewValue.GetValueOrDefault() as FlyoutBase;
// If flyout is changed while one is already open, make sure we
// close the old one first
if (change.OldValue.GetValueOrDefault() is FlyoutBase oldFlyout &&
if (oldFlyout != null &&
oldFlyout.IsOpen)
{
oldFlyout.Hide();
}
// Must unregister events here while a reference to the old flyout still exists
UnregisterFlyoutEvents(oldFlyout);
RegisterFlyoutEvents(newFlyout);
UpdatePseudoClasses();
}
}
@ -493,6 +532,32 @@ namespace Avalonia.Controls
}
}
/// <summary>
/// Registers all flyout events.
/// </summary>
/// <param name="flyout">The flyout to connect events to.</param>
private void RegisterFlyoutEvents(FlyoutBase? flyout)
{
if (flyout != null)
{
flyout.Opened += Flyout_Opened;
flyout.Closed += Flyout_Closed;
}
}
/// <summary>
/// Explicitly unregisters all flyout events.
/// </summary>
/// <param name="flyout">The flyout to disconnect events from.</param>
private void UnregisterFlyoutEvents(FlyoutBase? flyout)
{
if (flyout != null)
{
flyout.Opened -= Flyout_Opened;
flyout.Closed -= Flyout_Closed;
}
}
/// <summary>
/// Starts listening for the Enter key when the button <see cref="IsDefault"/>.
/// </summary>
@ -560,11 +625,53 @@ namespace Avalonia.Controls
/// <summary>
/// Updates the visual state of the control by applying latest PseudoClasses.
/// </summary>
private void UpdatePseudoClasses(bool isPressed)
private void UpdatePseudoClasses()
{
PseudoClasses.Set(":pressed", isPressed);
PseudoClasses.Set(pcFlyoutOpen, _isFlyoutOpen);
PseudoClasses.Set(pcPressed, IsPressed);
}
void ICommandSource.CanExecuteChanged(object sender, EventArgs e) => this.CanExecuteChanged(sender, e);
void IClickableControl.RaiseClick() => OnClick();
/// <summary>
/// Event handler for when the button's flyout is opened.
/// </summary>
private void Flyout_Opened(object? sender, EventArgs e)
{
var flyout = sender as FlyoutBase;
// It is possible to share flyouts among multiple controls including Button.
// This can cause a problem here since all controls that share a flyout receive
// the same Opened/Closed events at the same time.
// For Button that means they all would be updating their pseudoclasses accordingly.
// In other words, all Buttons with a shared Flyout would have the backgrounds changed together.
// To fix this, only continue here if the Flyout target matches this Button instance.
if (object.ReferenceEquals(flyout?.Target, this))
{
_isFlyoutOpen = true;
UpdatePseudoClasses();
OnFlyoutOpened();
}
}
/// <summary>
/// Event handler for when the button's flyout is closed.
/// </summary>
private void Flyout_Closed(object? sender, EventArgs e)
{
var flyout = sender as FlyoutBase;
// See comments in Flyout_Opened
if (object.ReferenceEquals(flyout?.Target, this))
{
_isFlyoutOpen = false;
UpdatePseudoClasses();
OnFlyoutClosed();
}
}
}
}

28
src/Avalonia.Controls/DropDown.cs

@ -1,28 +0,0 @@
using System;
using Avalonia.Logging;
using Avalonia.Styling;
namespace Avalonia.Controls
{
[Obsolete("Use ComboBox")]
public class DropDown : ComboBox, IStyleable
{
public DropDown()
{
Logger.TryGet(LogEventLevel.Warning, LogArea.Control)?.Log(this, "DropDown is deprecated: Use ComboBox");
}
Type IStyleable.StyleKey => typeof(ComboBox);
}
[Obsolete("Use ComboBoxItem")]
public class DropDownItem : ComboBoxItem, IStyleable
{
public DropDownItem()
{
Logger.TryGet(LogEventLevel.Warning, LogArea.Control)?.Log(this, "DropDownItem is deprecated: Use ComboBoxItem");
}
Type IStyleable.StyleKey => typeof(ComboBoxItem);
}
}

15
src/Avalonia.Controls/DropDownButton.cs

@ -0,0 +1,15 @@
namespace Avalonia.Controls
{
/// <summary>
/// A button with an added drop-down chevron to visually indicate it has a flyout with additional actions.
/// </summary>
public class DropDownButton : Button
{
/// <summary>
/// Initializes a new instance of the <see cref="DropDownButton"/> class.
/// </summary>
public DropDownButton()
{
}
}
}

67
src/Avalonia.Controls/HotkeyManager.cs

@ -12,21 +12,61 @@ namespace Avalonia.Controls
class HotkeyCommandWrapper : ICommand
{
public HotkeyCommandWrapper(ICommandSource? control)
readonly WeakReference reference;
public HotkeyCommandWrapper(IControl control)
{
CommandSource = control;
reference = new WeakReference(control);
}
public readonly ICommandSource? CommandSource;
public ICommand? GetCommand()
{
if (reference.Target is { } target)
{
if (target is ICommandSource commandSource && commandSource.Command is { } command)
{
return command;
}
else if (target is IClickableControl { })
{
return this;
}
}
return null;
}
private ICommand? GetCommand() => CommandSource?.Command;
public bool CanExecute(object? parameter)
{
if (reference.Target is { } target)
{
if (target is ICommandSource commandSource && commandSource.Command is { } command)
{
return commandSource.IsEffectivelyEnabled
&& command.CanExecute(commandSource.CommandParameter) == true;
}
else if (target is IClickableControl clickable)
{
return clickable.IsEffectivelyEnabled;
}
}
return false;
}
public bool CanExecute(object? parameter) =>
CommandSource?.Command?.CanExecute(CommandSource.CommandParameter) == true
&& CommandSource.IsEffectivelyEnabled;
public void Execute(object? parameter)
{
if (reference.Target is { } target)
{
if (target is ICommandSource commandSource && commandSource.Command is { } command)
{
command.Execute(commandSource.CommandParameter);
}
else if (target is IClickableControl { IsEffectivelyEnabled: true } clickable)
{
clickable.RaiseClick();
}
}
}
public void Execute(object? parameter) =>
GetCommand()?.Execute(CommandSource?.CommandParameter);
#pragma warning disable 67 // Event not used
public event EventHandler? CanExecuteChanged;
@ -47,7 +87,7 @@ namespace Avalonia.Controls
public Manager(IControl control)
{
_control = control;
_wrapper = new HotkeyCommandWrapper(_control as ICommandSource);
_wrapper = new HotkeyCommandWrapper(_control);
}
public void Init()
@ -104,13 +144,14 @@ namespace Avalonia.Controls
{
HotKeyProperty.Changed.Subscribe(args =>
{
if (args.NewValue.Value is null) return;
if (args.NewValue.Value is null)
return;
var control = args.Sender as IControl;
if (control is not ICommandSource)
if (control is not IClickableControl)
{
Logging.Logger.TryGet(Logging.LogEventLevel.Warning, Logging.LogArea.Control)?.Log(control,
$"The element {args.Sender.GetType().Name} does not implement ICommandSource and does not support binding a HotKey ({args.NewValue}).");
$"The element {args.Sender.GetType().Name} does not implement IClickableControl and does not support binding a HotKey ({args.NewValue}).");
return;
}

10
src/Avalonia.Controls/MenuItem.cs

@ -22,7 +22,7 @@ namespace Avalonia.Controls
/// </summary>
[TemplatePart("PART_Popup", typeof(Popup))]
[PseudoClasses(":separator", ":icon", ":open", ":pressed", ":selected")]
public class MenuItem : HeaderedSelectingItemsControl, IMenuItem, ISelectable, ICommandSource
public class MenuItem : HeaderedSelectingItemsControl, IMenuItem, ISelectable, ICommandSource, IClickableControl
{
/// <summary>
/// Defines the <see cref="Command"/> property.
@ -705,6 +705,14 @@ namespace Avalonia.Controls
void ICommandSource.CanExecuteChanged(object sender, EventArgs e) => this.CanExecuteChanged(sender, e);
void IClickableControl.RaiseClick()
{
if (IsEffectivelyEnabled)
{
RaiseEvent(new RoutedEventArgs(ClickEvent));
}
}
/// <summary>
/// A dependency resolver which returns a <see cref="MenuItemAccessKeyHandler"/>.
/// </summary>

5
src/Avalonia.Controls/Primitives/TemplatedControl.cs

@ -356,6 +356,11 @@ namespace Avalonia.Controls.Primitives
base.OnDetachedFromLogicalTree(e);
}
/// <summary>
/// Called when the control's template is applied.
/// In simple terms, this means the method is called just before the control is displayed.
/// </summary>
/// <param name="e">The event args.</param>
protected virtual void OnApplyTemplate(TemplateAppliedEventArgs e)
{
}

32
src/Avalonia.Controls/SplitButton/SplitButton.cs

@ -1,7 +1,6 @@
using System;
using System.Windows.Input;
using Avalonia.Controls.Metadata;
using Avalonia.Controls.Mixins;
using Avalonia.Controls.Primitives;
using Avalonia.Input;
using Avalonia.Interactivity;
@ -316,16 +315,9 @@ namespace Avalonia.Controls
}
// Must unregister events here while a reference to the old flyout still exists
if (oldFlyout != null)
{
UnregisterFlyoutEvents(oldFlyout);
}
if (newFlyout != null)
{
RegisterFlyoutEvents(newFlyout);
}
UnregisterFlyoutEvents(oldFlyout);
RegisterFlyoutEvents(newFlyout);
UpdatePseudoClasses();
}
@ -417,6 +409,22 @@ namespace Avalonia.Controls
}
}
/// <summary>
/// Invoked when the split button's flyout is opened.
/// </summary>
protected virtual void OnFlyoutOpened()
{
// Available for derived types
}
/// <summary>
/// Invoked when the split button's flyout is closed.
/// </summary>
protected virtual void OnFlyoutClosed()
{
// Available for derived types
}
////////////////////////////////////////////////////////////////////////
// Event Handling
////////////////////////////////////////////////////////////////////////
@ -466,6 +474,8 @@ namespace Avalonia.Controls
{
_isFlyoutOpen = true;
UpdatePseudoClasses();
OnFlyoutOpened();
}
}
@ -481,6 +491,8 @@ namespace Avalonia.Controls
{
_isFlyoutOpen = false;
UpdatePseudoClasses();
OnFlyoutClosed();
}
}
}

18
src/Avalonia.Input/IClickableControl.cs

@ -0,0 +1,18 @@
using System;
using Avalonia.Interactivity;
namespace Avalonia.Input
{
/// <summary>
///
/// </summary>
internal interface IClickableControl
{
event EventHandler<RoutedEventArgs> Click;
void RaiseClick();
/// <summary>
/// Gets a value indicating whether this control and all its parents are enabled.
/// </summary>
bool IsEffectivelyEnabled { get; }
}
}

4
src/Avalonia.Input/Properties/AssemblyInfo.cs

@ -1,6 +1,10 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using Avalonia.Metadata;
[assembly: XmlnsDefinition("https://github.com/avaloniaui", "Avalonia.Input")]
[assembly: XmlnsDefinition("https://github.com/avaloniaui", "Avalonia.Input.TextInput")]
[assembly: XmlnsDefinition("https://github.com/avaloniaui", "Avalonia.Input.GestureRecognizers")]
[assembly: InternalsVisibleTo("Avalonia.Controls, PublicKey=0024000004800000940000000602000000240000525341310004000001000100c1bba1142285fe0419326fb25866ba62c47e6c2b5c1ab0c95b46413fad375471232cb81706932e1cef38781b9ebd39d5100401bacb651c6c5bbf59e571e81b3bc08d2a622004e08b1a6ece82a7e0b9857525c86d2b95fab4bc3dce148558d7f3ae61aa3a234086902aeface87d9dfdd32b9d2fe3c6dd4055b5ab4b104998bd87")]
[assembly: InternalsVisibleTo("Avalonia.Controls.UnitTests, PublicKey=0024000004800000940000000602000000240000525341310004000001000100c1bba1142285fe0419326fb25866ba62c47e6c2b5c1ab0c95b46413fad375471232cb81706932e1cef38781b9ebd39d5100401bacb651c6c5bbf59e571e81b3bc08d2a622004e08b1a6ece82a7e0b9857525c86d2b95fab4bc3dce148558d7f3ae61aa3a234086902aeface87d9dfdd32b9d2fe3c6dd4055b5ab4b104998bd87")]

67
src/Avalonia.Themes.Default/Controls/DropDownButton.xaml

@ -0,0 +1,67 @@
<Styles xmlns="https://github.com/avaloniaui">
<Style Selector="DropDownButton">
<Setter Property="Background" Value="{DynamicResource ThemeControlMidBrush}"/>
<Setter Property="BorderBrush" Value="{DynamicResource ThemeBorderLowBrush}"/>
<Setter Property="BorderThickness" Value="{DynamicResource ThemeBorderThickness}"/>
<Setter Property="Foreground" Value="{DynamicResource ThemeForegroundBrush}"/>
<Setter Property="HorizontalContentAlignment" Value="Center"/>
<Setter Property="VerticalContentAlignment" Value="Center"/>
<Setter Property="Padding" Value="4"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate>
<Border Name="RootBorder"
Background="{TemplateBinding Background}"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}"
CornerRadius="{TemplateBinding CornerRadius}"
ClipToBounds="True">
<Grid Name="InnerGrid">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<ContentPresenter Name="PART_ContentPresenter"
Grid.Column="0"
Content="{TemplateBinding Content}"
ContentTemplate="{TemplateBinding ContentTemplate}"
Padding="{TemplateBinding Padding}"
RecognizesAccessKey="True"
HorizontalContentAlignment="{TemplateBinding HorizontalContentAlignment}"
VerticalContentAlignment="{TemplateBinding VerticalContentAlignment}" />
<PathIcon Name="DropDownGlyph"
Grid.Column="1"
UseLayoutRounding="False"
IsHitTestVisible="False"
Height="12"
Width="12"
Margin="0,0,10,0"
HorizontalAlignment="Right"
VerticalAlignment="Center" />
</Grid>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<Style Selector="DropDownButton /template/ PathIcon#DropDownGlyph">
<Setter Property="Foreground" Value="{DynamicResource ThemeForegroundBrush}" />
<Setter Property="Data" Value="M1939 486L2029 576L1024 1581L19 576L109 486L1024 1401L1939 486Z" />
</Style>
<Style Selector="DropDownButton:pointerover /template/ Border#RootBorder">
<Setter Property="BorderBrush" Value="{DynamicResource ThemeBorderMidBrush}"/>
</Style>
<Style Selector="DropDownButton:pressed /template/ Border#RootBorder">
<Setter Property="Background" Value="{DynamicResource ThemeControlHighBrush}"/>
</Style>
<Style Selector="DropDownButton:disabled">
<Setter Property="Opacity" Value="{DynamicResource ThemeDisabledOpacity}"/>
</Style>
</Styles>

4
src/Avalonia.Themes.Default/Controls/SplitButton.xaml

@ -16,6 +16,7 @@
<x:Double x:Key="SplitButtonPrimaryButtonSize">24</x:Double>
<x:Double x:Key="SplitButtonSecondaryButtonSize">24</x:Double>
<x:Double x:Key="SplitButtonSeparatorWidth">1</x:Double>
<x:Double x:Key="SplitButtonMinHeight">24</x:Double>
<Thickness x:Key="SplitButtonBorderThemeThickness">1</Thickness>
<converters:MarginMultiplierConverter x:Key="PrimaryButtonBorderMultiplier" Left="True" Top="True" Bottom="True" Indent="1" />
@ -59,8 +60,11 @@
<Setter Property="Foreground" Value="{DynamicResource SplitButtonForeground}" />
<Setter Property="BorderBrush" Value="{DynamicResource SplitButtonBorderBrush}" />
<Setter Property="BorderThickness" Value="{DynamicResource SplitButtonBorderThemeThickness}" />
<Setter Property="MinHeight" Value="{DynamicResource SplitButtonMinHeight}" />
<Setter Property="HorizontalAlignment" Value="Left" />
<Setter Property="VerticalAlignment" Value="Center" />
<Setter Property="HorizontalContentAlignment" Value="Stretch" />
<Setter Property="VerticalContentAlignment" Value="Center" />
<!--<Setter Property="UseSystemFocusVisuals" Value="True" />
<Setter Property="FocusVisualMargin" Value="-3" />-->
<Setter Property="KeyboardNavigation.IsTabStop" Value="True" />

1
src/Avalonia.Themes.Default/DefaultTheme.xaml

@ -14,6 +14,7 @@
<StyleInclude Source="avares://Avalonia.Themes.Default/Controls/ComboBox.xaml"/>
<StyleInclude Source="avares://Avalonia.Themes.Default/Controls/ComboBoxItem.xaml"/>
<StyleInclude Source="avares://Avalonia.Themes.Default/Controls/ContentControl.xaml"/>
<StyleInclude Source="avares://Avalonia.Themes.Default/Controls/DropDownButton.xaml"/>
<StyleInclude Source="avares://Avalonia.Themes.Default/Controls/Label.xaml"/>
<StyleInclude Source="avares://Avalonia.Themes.Default/Controls/GridSplitter.xaml"/>
<StyleInclude Source="avares://Avalonia.Themes.Default/Controls/ItemsControl.xaml"/>

14
src/Avalonia.Themes.Fluent/Controls/Button.xaml

@ -4,7 +4,7 @@
<StackPanel Spacing="20">
<Button Content="Click Me!" />
<Button Classes="accent" Content="Click Me!" />
</StackPanel>
</StackPanel>
</Border>
</Design.PreviewWith>
<Styles.Resources>
@ -19,9 +19,7 @@
<Setter Property="CornerRadius" Value="{DynamicResource ControlCornerRadius}" />
<Setter Property="Padding" Value="{DynamicResource ButtonPadding}" />
<Setter Property="HorizontalAlignment" Value="Left" />
<Setter Property="VerticalAlignment" Value="Center" />
<Setter Property="FontWeight" Value="Normal" />
<Setter Property="FontSize" Value="{DynamicResource ControlContentThemeFontSize}" />
<Setter Property="VerticalAlignment" Value="Center" />
<!--<Setter Property="UseSystemFocusVisuals" Value="{StaticResource UseSystemFocusVisuals}" />
<Setter Property="FocusVisualMargin" Value="-3" />-->
<Setter Property="Template">
@ -47,13 +45,13 @@
<Setter Property="BorderBrush" Value="{DynamicResource ButtonBorderBrushPointerOver}" />
<Setter Property="TextElement.Foreground" Value="{DynamicResource ButtonForegroundPointerOver}" />
</Style>
<Style Selector="Button:pressed /template/ ContentPresenter#PART_ContentPresenter">
<Setter Property="Background" Value="{DynamicResource ButtonBackgroundPressed}" />
<Setter Property="BorderBrush" Value="{DynamicResource ButtonBorderBrushPressed}" />
<Setter Property="TextElement.Foreground" Value="{DynamicResource ButtonForegroundPressed}" />
</Style>
<Style Selector="Button:disabled /template/ ContentPresenter#PART_ContentPresenter">
<Setter Property="Background" Value="{DynamicResource ButtonBackgroundDisabled}" />
<Setter Property="BorderBrush" Value="{DynamicResource ButtonBorderBrushDisabled}" />
@ -78,7 +76,7 @@
<Setter Property="TextElement.Foreground" Value="{DynamicResource AccentButtonForegroundPressed}" />
</Style>
<Style Selector="Button, RepeatButton, ToggleButton">
<Style Selector="Button, RepeatButton, ToggleButton, DropDownButton">
<Setter Property="RenderTransform" Value="none" />
<Setter Property="Transitions">
<Transitions>
@ -87,7 +85,7 @@
</Setter>
</Style>
<Style Selector="Button:pressed, RepeatButton:pressed, ToggleButton:pressed">
<Style Selector="Button:pressed, RepeatButton:pressed, ToggleButton:pressed, DropDownButton:pressed">
<Setter Property="RenderTransform" Value="scale(0.98)" />
</Style>

1
src/Avalonia.Themes.Fluent/Controls/ComboBox.xaml

@ -37,6 +37,7 @@
<Setter Property="CornerRadius" Value="{DynamicResource ControlCornerRadius}" />
<Setter Property="ScrollViewer.HorizontalScrollBarVisibility" Value="Disabled" />
<Setter Property="ScrollViewer.VerticalScrollBarVisibility" Value="Auto" />
<Setter Property="MinHeight" Value="{DynamicResource ComboBoxMinHeight}" />
<Setter Property="HorizontalContentAlignment" Value="Stretch" />
<Setter Property="VerticalContentAlignment" Value="Center" />
<Setter Property="HorizontalAlignment" Value="Left" />

103
src/Avalonia.Themes.Fluent/Controls/DropDownButton.xaml

@ -0,0 +1,103 @@
<Styles xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:CompileBindings="True">
<Design.PreviewWith>
<Border Padding="20">
<StackPanel Spacing="20">
<DropDownButton Content="Click Me!" />
<DropDownButton Content="Disabled" IsEnabled="False" />
</StackPanel>
</Border>
</Design.PreviewWith>
<Styles.Resources>
<x:Double x:Key="DropDownButtonMinHeight">32</x:Double>
</Styles.Resources>
<Style Selector="DropDownButton">
<Setter Property="Background" Value="{DynamicResource ButtonBackground}" />
<Setter Property="Foreground" Value="{DynamicResource ButtonForeground}" />
<Setter Property="BorderBrush" Value="{DynamicResource ButtonBorderBrush}" />
<Setter Property="BorderThickness" Value="{DynamicResource ButtonBorderThemeThickness}" />
<Setter Property="Padding" Value="{DynamicResource ButtonPadding}" />
<Setter Property="MinHeight" Value="{DynamicResource DropDownButtonMinHeight}" />
<Setter Property="HorizontalAlignment" Value="Left" />
<Setter Property="VerticalAlignment" Value="Center" />
<Setter Property="HorizontalContentAlignment" Value="Stretch" />
<Setter Property="VerticalContentAlignment" Value="Center" />
<!--<Setter Property="UseSystemFocusVisuals" Value="{StaticResource UseSystemFocusVisuals}" />
<Setter Property="FocusVisualMargin" Value="-3" />-->
<Setter Property="CornerRadius" Value="{DynamicResource ControlCornerRadius}" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate>
<Border x:Name="RootBorder"
Background="{TemplateBinding Background}"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}"
CornerRadius="{TemplateBinding CornerRadius}"
ClipToBounds="True">
<Grid x:Name="InnerGrid">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<ContentPresenter x:Name="PART_ContentPresenter"
Grid.Column="0"
Content="{TemplateBinding Content}"
ContentTemplate="{TemplateBinding ContentTemplate}"
Padding="{TemplateBinding Padding}"
RecognizesAccessKey="True"
HorizontalContentAlignment="{TemplateBinding HorizontalContentAlignment}"
VerticalContentAlignment="{TemplateBinding VerticalContentAlignment}" />
<PathIcon x:Name="DropDownGlyph"
Grid.Column="1"
UseLayoutRounding="False"
IsHitTestVisible="False"
Height="12"
Width="12"
Margin="0,0,10,0"
HorizontalAlignment="Right"
VerticalAlignment="Center" />
</Grid>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<!-- Normal State -->
<Style Selector="DropDownButton /template/ PathIcon#DropDownGlyph">
<Setter Property="Foreground" Value="{DynamicResource ComboBoxDropDownGlyphForeground}" />
<Setter Property="Data" Value="M1939 486L2029 576L1024 1581L19 576L109 486L1024 1401L1939 486Z" />
</Style>
<!-- PointerOver State -->
<Style Selector="DropDownButton:pointerover /template/ Border#RootBorder">
<Setter Property="Background" Value="{DynamicResource ButtonBackgroundPointerOver}" />
<Setter Property="BorderBrush" Value="{DynamicResource ButtonBorderBrushPointerOver}" />
<Setter Property="TextElement.Foreground" Value="{DynamicResource ButtonForegroundPointerOver}" />
</Style>
<!-- Pressed State -->
<Style Selector="DropDownButton:pressed /template/ Border#RootBorder">
<Setter Property="Background" Value="{DynamicResource ButtonBackgroundPressed}" />
<Setter Property="BorderBrush" Value="{DynamicResource ButtonBorderBrushPressed}" />
<Setter Property="TextElement.Foreground" Value="{DynamicResource ButtonForegroundPressed}" />
</Style>
<!-- Disabled State -->
<Style Selector="DropDownButton:disabled /template/ Border#RootBorder">
<Setter Property="Background" Value="{DynamicResource ButtonBackgroundDisabled}" />
<Setter Property="BorderBrush" Value="{DynamicResource ButtonBorderBrushDisabled}" />
<Setter Property="TextElement.Foreground" Value="{DynamicResource ButtonForegroundDisabled}" />
</Style>
<Style Selector="DropDownButton:disabled /template/ PathIcon#DropDownGlyph">
<Setter Property="Foreground" Value="{DynamicResource ButtonForegroundDisabled}" />
</Style>
</Styles>

1
src/Avalonia.Themes.Fluent/Controls/FluentControls.xaml

@ -14,6 +14,7 @@
<StyleInclude Source="avares://Avalonia.Themes.Fluent/Controls/ComboBox.xaml"/>
<StyleInclude Source="avares://Avalonia.Themes.Fluent/Controls/ComboBoxItem.xaml"/>
<StyleInclude Source="avares://Avalonia.Themes.Fluent/Controls/ContentControl.xaml"/>
<StyleInclude Source="avares://Avalonia.Themes.Fluent/Controls/DropDownButton.xaml"/>
<StyleInclude Source="avares://Avalonia.Themes.Fluent/Controls/Label.xaml"/>
<StyleInclude Source="avares://Avalonia.Themes.Fluent/Controls/GridSplitter.xaml"/>
<StyleInclude Source="avares://Avalonia.Themes.Fluent/Controls/ItemsControl.xaml"/>

4
src/Avalonia.Themes.Fluent/Controls/SplitButton.xaml

@ -24,6 +24,7 @@
<x:Double x:Key="SplitButtonPrimaryButtonSize">32</x:Double>
<x:Double x:Key="SplitButtonSecondaryButtonSize">32</x:Double>
<x:Double x:Key="SplitButtonSeparatorWidth">1</x:Double>
<x:Double x:Key="SplitButtonMinHeight">32</x:Double>
<converters:MarginMultiplierConverter x:Key="PrimaryButtonBorderMultiplier" Left="True" Top="True" Bottom="True" Indent="1" />
<converters:MarginMultiplierConverter x:Key="SecondaryButtonBorderMultiplier" Right="True" Top="True" Bottom="True" Indent="1" />
@ -35,8 +36,11 @@
<Setter Property="Foreground" Value="{DynamicResource SplitButtonForeground}" />
<Setter Property="BorderBrush" Value="{DynamicResource SplitButtonBorderBrush}" />
<Setter Property="BorderThickness" Value="{DynamicResource SplitButtonBorderThemeThickness}" />
<Setter Property="MinHeight" Value="{DynamicResource SplitButtonMinHeight}" />
<Setter Property="HorizontalAlignment" Value="Left" />
<Setter Property="VerticalAlignment" Value="Center" />
<Setter Property="HorizontalContentAlignment" Value="Stretch" />
<Setter Property="VerticalContentAlignment" Value="Center" />
<!--<Setter Property="UseSystemFocusVisuals" Value="True" />
<Setter Property="FocusVisualMargin" Value="-3" />-->
<Setter Property="KeyboardNavigation.IsTabStop" Value="True" />

149
tests/Avalonia.Controls.UnitTests/Utils/HotKeyManagerTests.cs

@ -170,7 +170,7 @@ namespace Avalonia.Controls.UnitTests.Utils
}
[Theory]
[MemberData(nameof(ElementsFactory))]
[MemberData(nameof(ElementsFactory), parameters: true)]
public void HotKeyManager_Should_Use_CommandParameter(string factoryName, Factory factory)
{
using (AvaloniaLocator.EnterScope())
@ -215,7 +215,7 @@ namespace Avalonia.Controls.UnitTests.Utils
[Theory]
[MemberData(nameof(ElementsFactory))]
[MemberData(nameof(ElementsFactory), parameters: true)]
public void HotKeyManager_Should_Do_Not_Executed_When_IsEnabled_False(string factoryName, Factory factory)
{
using (AvaloniaLocator.EnterScope())
@ -256,11 +256,127 @@ namespace Avalonia.Controls.UnitTests.Utils
}
}
public static TheoryData<string, Factory> ElementsFactory =>
[Theory]
[MemberData(nameof(ElementsFactory), parameters:false)]
public void HotKeyManager_Should_Invoke_Event_Click_When_Command_Is_Null(string factoryName, Factory factory)
{
using (AvaloniaLocator.EnterScope())
{
var styler = new Mock<Styler>();
var target = new KeyboardDevice();
var clickExecutedCount = 0;
AvaloniaLocator.CurrentMutable
.Bind<IWindowingPlatform>().ToConstant(new WindowingPlatformMock())
.Bind<IStyler>().ToConstant(styler.Object);
var gesture = new KeyGesture(Key.A, KeyModifiers.Control);
void Clickable_Click(object sender, Interactivity.RoutedEventArgs e)
{
clickExecutedCount++;
}
var root = new Window();
var element = factory(0, default, root) as InputElement;
if (element is IClickableControl clickable)
{
clickable.Click += Clickable_Click;
}
root.Template = CreateWindowTemplate();
root.ApplyTemplate();
root.Presenter.ApplyTemplate();
HotKeyManager.SetHotKey(element, gesture);
target.ProcessRawEvent(new RawKeyEventArgs(target,
0,
root,
RawKeyEventType.KeyDown,
Key.A,
RawInputModifiers.Control));
element.IsEnabled = false;
target.ProcessRawEvent(new RawKeyEventArgs(target,
0,
root,
RawKeyEventType.KeyDown,
Key.A,
RawInputModifiers.Control));
Assert.True(clickExecutedCount == 1, $"{factoryName} Execution raised when IsEnabled is false.");
}
}
[Theory]
[MemberData(nameof(ElementsFactory), parameters: true)]
public void HotKeyManager_Should_Not_Invoke_Event_Click_When_Command_Is_Not_Null(string factoryName, Factory factory)
{
using (AvaloniaLocator.EnterScope())
{
var styler = new Mock<Styler>();
var target = new KeyboardDevice();
var clickExecutedCount = 0;
var commandExecutedCount = 0;
AvaloniaLocator.CurrentMutable
.Bind<IWindowingPlatform>().ToConstant(new WindowingPlatformMock())
.Bind<IStyler>().ToConstant(styler.Object);
var gesture = new KeyGesture(Key.A, KeyModifiers.Control);
void DoExecute(object parameter)
{
commandExecutedCount++;
}
void Clickable_Click(object sender, Interactivity.RoutedEventArgs e)
{
clickExecutedCount++;
}
var root = new Window();
var element = factory(0, DoExecute, root) as InputElement;
if (element is IClickableControl clickable)
{
clickable.Click += Clickable_Click;
}
root.Template = CreateWindowTemplate();
root.ApplyTemplate();
root.Presenter.ApplyTemplate();
HotKeyManager.SetHotKey(element, gesture);
target.ProcessRawEvent(new RawKeyEventArgs(target,
0,
root,
RawKeyEventType.KeyDown,
Key.A,
RawInputModifiers.Control));
element.IsEnabled = false;
target.ProcessRawEvent(new RawKeyEventArgs(target,
0,
root,
RawKeyEventType.KeyDown,
Key.A,
RawInputModifiers.Control));
Assert.True(commandExecutedCount == 1, $"{factoryName} Execution raised when IsEnabled is false.");
Assert.True(clickExecutedCount == 0, $"{factoryName} Execution raised event Click.");
}
}
public static TheoryData<string, Factory> ElementsFactory(bool withCommand) =>
new TheoryData<string, Factory>()
{
{nameof(Button), MakeButton},
{nameof(MenuItem),MakeMenu},
{nameof(Button), withCommand ? MakeButton : MakeButtonWithoutCommand},
{nameof(MenuItem),withCommand ? MakeMenu : MakeMenuWithoutCommand},
};
private static AvaloniaObject MakeMenu(int expectedParameter, Action<object> action, Window root)
@ -290,6 +406,29 @@ namespace Avalonia.Controls.UnitTests.Utils
return button;
}
private static AvaloniaObject MakeMenuWithoutCommand(int expectedParameter, Action<object> action, Window root)
{
var menuitem = new MenuItem()
{
};
var rootMenu = new Menu();
rootMenu.Items = new[] { menuitem };
root.Content = rootMenu;
return menuitem;
}
private static AvaloniaObject MakeButtonWithoutCommand(int expectedParameter, Action<object> action, Window root)
{
var button = new Button()
{
};
root.Content = button;
return button;
}
private FuncControlTemplate CreateWindowTemplate()
{
return new FuncControlTemplate<Window>((parent, scope) =>

Loading…
Cancel
Save