committed by
GitHub
10 changed files with 263 additions and 47 deletions
@ -0,0 +1,37 @@ |
|||
using Avalonia.Interactivity; |
|||
using Avalonia.Input; |
|||
|
|||
namespace Avalonia.Controls.Mixins |
|||
{ |
|||
/// <summary>
|
|||
/// Adds pressed functionality to control classes.
|
|||
///
|
|||
/// Adds the ':pressed' class when the item is pressed.
|
|||
/// </summary>
|
|||
public static class PressedMixin |
|||
{ |
|||
/// <summary>
|
|||
/// Initializes a new instance of the <see cref="PressedMixin"/> class.
|
|||
/// </summary>
|
|||
/// <typeparam name="TControl">The control type.</typeparam>
|
|||
public static void Attach<TControl>() where TControl : Control |
|||
{ |
|||
InputElement.PointerPressedEvent.AddClassHandler<TControl>((x, e) => HandlePointerPressed(x, e), RoutingStrategies.Tunnel); |
|||
InputElement.PointerReleasedEvent.AddClassHandler<TControl>((x, e) => HandlePointerReleased(x), RoutingStrategies.Tunnel); |
|||
InputElement.PointerCaptureLostEvent.AddClassHandler<TControl>((x, e) => HandlePointerReleased(x), RoutingStrategies.Tunnel); |
|||
} |
|||
|
|||
private static void HandlePointerPressed<TControl>(TControl sender, PointerPressedEventArgs e) where TControl : Control |
|||
{ |
|||
if (e.GetCurrentPoint(sender).Properties.IsLeftButtonPressed) |
|||
{ |
|||
((IPseudoClasses)sender.Classes).Set(":pressed", true); |
|||
} |
|||
} |
|||
|
|||
private static void HandlePointerReleased<TControl>(TControl sender) where TControl : Control |
|||
{ |
|||
((IPseudoClasses)sender.Classes).Set(":pressed", false); |
|||
} |
|||
} |
|||
} |
|||
@ -1,31 +1,43 @@ |
|||
<Styles xmlns="https://github.com/avaloniaui" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> |
|||
<Style Selector="ListBox"> |
|||
<Setter Property="Background" Value="{DynamicResource ThemeBackgroundBrush}"/> |
|||
<Setter Property="BorderBrush" Value="{DynamicResource ThemeBorderMidBrush}"/> |
|||
<Setter Property="BorderThickness" Value="{DynamicResource ThemeBorderThickness}"/> |
|||
<Setter Property="Padding" Value="4"/> |
|||
<Setter Property="ScrollViewer.HorizontalScrollBarVisibility" Value="Auto"/> |
|||
<Setter Property="ScrollViewer.VerticalScrollBarVisibility" Value="Auto"/> |
|||
<Setter Property="Template"> |
|||
<ControlTemplate> |
|||
<Border Name="border" BorderBrush="{TemplateBinding BorderBrush}" |
|||
BorderThickness="{TemplateBinding BorderThickness}"> |
|||
<ScrollViewer Name="PART_ScrollViewer" |
|||
Background="{TemplateBinding Background}" |
|||
HorizontalScrollBarVisibility="{TemplateBinding (ScrollViewer.HorizontalScrollBarVisibility)}" |
|||
VerticalScrollBarVisibility="{TemplateBinding (ScrollViewer.VerticalScrollBarVisibility)}"> |
|||
<ItemsPresenter Name="PART_ItemsPresenter" |
|||
Items="{TemplateBinding Items}" |
|||
ItemsPanel="{TemplateBinding ItemsPanel}" |
|||
ItemTemplate="{TemplateBinding ItemTemplate}" |
|||
Margin="{TemplateBinding Padding}" |
|||
VirtualizationMode="{TemplateBinding VirtualizationMode}"/> |
|||
</ScrollViewer> |
|||
</Border> |
|||
</ControlTemplate> |
|||
</Setter> |
|||
</Style> |
|||
<Style Selector="ListBox:disabled /template/ Border#border"> |
|||
<Setter Property="Opacity" Value="{DynamicResource ThemeDisabledOpacity}" /> |
|||
</Style> |
|||
<Design.PreviewWith> |
|||
<Border Padding="20"> |
|||
<ListBox> |
|||
<ListBoxItem>Test</ListBoxItem> |
|||
<ListBoxItem>Test</ListBoxItem> |
|||
<ListBoxItem>Test</ListBoxItem> |
|||
<ListBoxItem>Test</ListBoxItem> |
|||
</ListBox> |
|||
</Border> |
|||
</Design.PreviewWith> |
|||
<Style Selector="ListBox"> |
|||
<Setter Property="TextBlock.Foreground" Value="{DynamicResource SystemControlForegroundBaseHighBrush}" /> |
|||
<Setter Property="Background" Value="{DynamicResource SystemControlBackgroundChromeMediumLowBrush}" /> |
|||
<Setter Property="BorderBrush" Value="{DynamicResource SystemControlForegroundBaseHighBrush}" /> |
|||
<Setter Property="BorderThickness" Value="{DynamicResource ListBoxBorderThemeThickness}" /> |
|||
<Setter Property="ScrollViewer.HorizontalScrollBarVisibility" Value="Disabled" /> |
|||
<Setter Property="ScrollViewer.VerticalScrollBarVisibility" Value="Auto" /> |
|||
<Setter Property="FontFamily" Value="{DynamicResource ContentControlThemeFontFamily}" /> |
|||
<Setter Property="FontSize" Value="{DynamicResource ControlContentThemeFontSize}" /> |
|||
<Setter Property="Template"> |
|||
<ControlTemplate> |
|||
<Border Name="border" BorderBrush="{TemplateBinding BorderBrush}" |
|||
BorderThickness="{TemplateBinding BorderThickness}"> |
|||
<ScrollViewer Name="PART_ScrollViewer" |
|||
Background="{TemplateBinding Background}" |
|||
HorizontalScrollBarVisibility="{TemplateBinding (ScrollViewer.HorizontalScrollBarVisibility)}" |
|||
VerticalScrollBarVisibility="{TemplateBinding (ScrollViewer.VerticalScrollBarVisibility)}"> |
|||
<ItemsPresenter Name="PART_ItemsPresenter" |
|||
Items="{TemplateBinding Items}" |
|||
ItemsPanel="{TemplateBinding ItemsPanel}" |
|||
ItemTemplate="{TemplateBinding ItemTemplate}" |
|||
Margin="{TemplateBinding Padding}" |
|||
VirtualizationMode="{TemplateBinding VirtualizationMode}"/> |
|||
</ScrollViewer> |
|||
</Border> |
|||
</ControlTemplate> |
|||
</Setter> |
|||
</Style> |
|||
<Style Selector="ListBox:disabled /template/ Border#border"> |
|||
<Setter Property="Opacity" Value="{DynamicResource ThemeDisabledOpacity}" /> |
|||
</Style> |
|||
</Styles> |
|||
|
|||
@ -0,0 +1,37 @@ |
|||
using Avalonia.Controls.Mixins; |
|||
using Avalonia.UnitTests; |
|||
using Xunit; |
|||
|
|||
namespace Avalonia.Controls.UnitTests.Mixins |
|||
{ |
|||
public class PressedMixinTests |
|||
{ |
|||
private MouseTestHelper _mouse = new MouseTestHelper(); |
|||
|
|||
[Fact] |
|||
public void Selected_Class_Should_Not_Initially_Be_Added() |
|||
{ |
|||
var target = new TestControl(); |
|||
|
|||
Assert.Empty(target.Classes); |
|||
} |
|||
|
|||
[Fact] |
|||
public void Setting_IsSelected_Should_Add_Selected_Class() |
|||
{ |
|||
var target = new TestControl(); |
|||
|
|||
_mouse.Down(target); |
|||
|
|||
Assert.Equal(new[] { ":pressed" }, target.Classes); |
|||
} |
|||
|
|||
private class TestControl : Control |
|||
{ |
|||
static TestControl() |
|||
{ |
|||
PressedMixin.Attach<TestControl>(); |
|||
} |
|||
} |
|||
} |
|||
} |
|||
Loading…
Reference in new issue