Browse Source

feat: add :dropdownopen and :pressed pseudoclass for combobox.

simplify fluent theme combobox default template.
update unit test.
pull/9084/head
rabbitism 4 years ago
committed by Dong Bin
parent
commit
79cc37e372
  1. 28
      src/Avalonia.Controls/ComboBox.cs
  2. 19
      src/Avalonia.Themes.Fluent/Controls/ComboBox.xaml
  3. 25
      tests/Avalonia.Controls.UnitTests/ComboBoxTests.cs

28
src/Avalonia.Controls/ComboBox.cs

@ -21,8 +21,11 @@ namespace Avalonia.Controls
/// A drop-down list control.
/// </summary>
[TemplatePart("PART_Popup", typeof(Popup))]
[PseudoClasses(pcDropdownOpen, pcPressed)]
public class ComboBox : SelectingItemsControl
{
public const string pcDropdownOpen = ":dropdownopen";
public const string pcPressed = ":pressed";
/// <summary>
/// The default value for the <see cref="ItemsControl.ItemsPanel"/> property.
/// </summary>
@ -95,6 +98,7 @@ namespace Avalonia.Controls
SelectedItemProperty.Changed.AddClassHandler<ComboBox>((x, e) => x.SelectedItemChanged(e));
KeyDownEvent.AddClassHandler<ComboBox>((x, e) => x.OnKeyDown(e), Interactivity.RoutingStrategies.Tunnel);
IsTextSearchEnabledProperty.OverrideDefaultValue<ComboBox>(true);
IsDropDownOpenProperty.Changed.AddClassHandler<ComboBox>((x, e) => x.DropdownChanged(e));
}
/// <summary>
@ -267,6 +271,20 @@ namespace Avalonia.Controls
}
}
/// <inheritdoc/>
protected override void OnPointerPressed(PointerPressedEventArgs e)
{
base.OnPointerPressed(e);
if(!e.Handled && e.Source is IVisual source)
{
if (_popup?.IsInsidePopup(source) == true)
{
return;
}
}
PseudoClasses.Set(pcPressed, true);
}
/// <inheritdoc/>
protected override void OnPointerReleased(PointerReleasedEventArgs e)
{
@ -286,10 +304,12 @@ namespace Avalonia.Controls
e.Handled = true;
}
}
PseudoClasses.Set(pcPressed, false);
base.OnPointerReleased(e);
}
/// <inheritdoc/>
protected override void OnApplyTemplate(TemplateAppliedEventArgs e)
{
@ -470,5 +490,11 @@ namespace Avalonia.Controls
MoveSelection(NavigationDirection.Previous, WrapSelection);
}
}
private void DropdownChanged(AvaloniaPropertyChangedEventArgs e)
{
bool newValue = e.GetNewValue<bool>();
PseudoClasses.Set(pcDropdownOpen, newValue);
}
}
}

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

@ -57,17 +57,8 @@
<Setter Property="Template">
<ControlTemplate>
<DataValidationErrors>
<Grid RowDefinitions="Auto, *, Auto" ColumnDefinitions="*,32">
<ContentPresenter x:Name="HeaderContentPresenter"
Grid.Row="0"
Grid.Column="0"
Grid.ColumnSpan="2"
IsVisible="False"
FontWeight="{DynamicResource ComboBoxHeaderThemeFontWeight}"
Margin="{DynamicResource ComboBoxTopHeaderMargin}"
VerticalAlignment="Top" />
<Grid ColumnDefinitions="*,32">
<Border x:Name="Background"
Grid.Row="1"
Grid.Column="0"
Grid.ColumnSpan="2"
Background="{TemplateBinding Background}"
@ -76,7 +67,6 @@
CornerRadius="{TemplateBinding CornerRadius}"
MinWidth="{DynamicResource ComboBoxThemeMinWidth}" />
<Border x:Name="HighlightBackground"
Grid.Row="1"
Grid.Column="0"
Grid.ColumnSpan="2"
Background="{DynamicResource ComboBoxBackgroundUnfocused}"
@ -85,7 +75,6 @@
CornerRadius="{TemplateBinding CornerRadius}"
IsVisible="False"/>
<TextBlock x:Name="PlaceholderTextBlock"
Grid.Row="1"
Grid.Column="0"
HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
@ -96,14 +85,12 @@
<ContentControl x:Name="ContentPresenter"
Content="{TemplateBinding SelectionBoxItem}"
ContentTemplate="{TemplateBinding ItemTemplate}"
Grid.Row="1"
Grid.Column="0"
Margin="{TemplateBinding Padding}"
HorizontalContentAlignment="{TemplateBinding HorizontalContentAlignment}"
VerticalContentAlignment="{TemplateBinding VerticalContentAlignment}" />
<Border x:Name="DropDownOverlay"
Grid.Row="1"
Grid.Column="1"
Background="Transparent"
Margin="0,1,1,1"
@ -112,7 +99,6 @@
HorizontalAlignment="Right" />
<PathIcon x:Name="DropDownGlyph"
Grid.Row="1"
Grid.Column="1"
UseLayoutRounding="False"
IsHitTestVisible="False"
@ -208,9 +194,6 @@
<Setter Property="Background" Value="{DynamicResource ComboBoxBackgroundDisabled}" />
<Setter Property="BorderBrush" Value="{DynamicResource ComboBoxBorderBrushDisabled}" />
</Style>
<Style Selector="^ /template/ ContentPresenter#HeaderContentPresenter">
<Setter Property="Foreground" Value="{DynamicResource ComboBoxForegroundDisabled}" />
</Style>
<Style Selector="^ /template/ ContentControl#ContentPresenter">
<Setter Property="Foreground" Value="{DynamicResource ComboBoxForegroundDisabled}" />
</Style>

25
tests/Avalonia.Controls.UnitTests/ComboBoxTests.cs

@ -29,11 +29,36 @@ namespace Avalonia.Controls.UnitTests
_helper.Down(target);
_helper.Up(target);
Assert.True(target.IsDropDownOpen);
Assert.True(target.Classes.Contains(ComboBox.pcDropdownOpen));
_helper.Down(target);
_helper.Up(target);
Assert.False(target.IsDropDownOpen);
Assert.True(!target.Classes.Contains(ComboBox.pcDropdownOpen));
}
[Fact]
public void Clicking_On_Control_PseudoClass()
{
var target = new ComboBox
{
Items = new[] { "Foo", "Bar" },
};
_helper.Down(target);
Assert.True(target.Classes.Contains(ComboBox.pcPressed));
_helper.Up(target);
Assert.True(!target.Classes.Contains(ComboBox.pcPressed));
Assert.True(target.Classes.Contains(ComboBox.pcDropdownOpen));
_helper.Down(target);
Assert.True(target.Classes.Contains(ComboBox.pcPressed));
_helper.Up(target);
Assert.True(!target.Classes.Contains(ComboBox.pcPressed));
Assert.False(target.IsDropDownOpen);
Assert.True(!target.Classes.Contains(ComboBox.pcDropdownOpen));
}
[Fact]

Loading…
Cancel
Save