Skip to content

Commit bb3c075

Browse files
committed
feat: add avalonia menu style (step1).
1 parent aa4fa95 commit bb3c075

File tree

11 files changed

+400
-11
lines changed

11 files changed

+400
-11
lines changed

src/Avalonia/Directory.Build.props

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<Project>
22
<PropertyGroup>
33
<Nullable>enable</Nullable>
4-
<AvaloniaVersion>11.3.2</AvaloniaVersion>
4+
<AvaloniaVersion>11.3.4</AvaloniaVersion>
55
</PropertyGroup>
66
</Project>

src/Avalonia/HandyControlDemo_Avalonia/Data/DemoInfo.json

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,19 +19,12 @@
1919
[ "Expander", "ExpanderDemo", "Brush.Expander", "", "" ],
2020
[ "ProgressBar", "NativeProgressBarDemo", "Brush.ProgressBar", "", "" ],
2121
[ "Calendar", "CalendarDemo", "Brush.Calendar", "", "" ],
22-
[ "DatePicker", "NativeDatePickerDemo", "Brush.DateTimePicker", "", "" ],
2322
[ "TabControl", "NativeTabControlDemo", "Brush.TabPage", "", "" ],
24-
[ "DataGrid", "DataGridDemo", "Brush.DataGrid", "", "" ],
2523
[ "TreeView", "TreeViewDemo", "Brush.TreeView", "", "" ],
2624
[ "ListBox", "ListBoxDemo", "Brush.ListBox", "", "" ],
27-
[ "GroupBox", "GroupBoxDemo", "Brush.GroupBox", "", "" ],
2825
[ "Menu", "MenuDemo", "Brush.ContextMenu", "", "" ],
29-
[ "ToolBar", "ToolBarDemo", "Brush.ToolBar", "", "" ],
3026
[ "Border", "BorderDemo", "Brush.BorderElement", "", "" ],
3127
[ "Label", "LabelDemo", "Brush.Label", "", "" ],
32-
[ "Frame", "FrameDemo", "Brush.FrameBorder", "", "" ],
33-
[ "Window", "NativeWindowDemo", "Brush.WindowsForm", "", "" ],
34-
[ "Geometry", "GeometryDemo", "Brush.Path", "", "" ]
3528
]
3629
},
3730
{
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<UserControl xmlns="https://github.com/avaloniaui"
2+
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
3+
xmlns:hc="https://handyorg.github.io/handycontrol"
4+
x:Class="HandyControlDemo.UserControl.MenuDemo">
5+
<hc:UniformSpacingPanel Spacing="16"
6+
Margin="32">
7+
<hc:DashedBorder Width="200"
8+
Height="112"
9+
Background="Transparent"
10+
CornerRadius="4"
11+
BorderBrush="{DynamicResource BorderBrush}"
12+
BorderThickness="1"
13+
BorderDashArray="2,2">
14+
<hc:DashedBorder.ContextMenu>
15+
<ContextMenu ItemsSource="{Binding DataList}">
16+
<ContextMenu.ItemTemplate>
17+
<TreeDataTemplate ItemsSource="{Binding DataList}">
18+
<TextBlock Text="{Binding Name}" />
19+
</TreeDataTemplate>
20+
</ContextMenu.ItemTemplate>
21+
</ContextMenu>
22+
</hc:DashedBorder.ContextMenu>
23+
<TextBlock Foreground="{DynamicResource BorderBrush}"
24+
Theme="{StaticResource TextBlockTitle}"
25+
TextAlignment="Center">
26+
<Run Text="Right click here" />
27+
<LineBreak />
28+
<Run Text="Default" />
29+
</TextBlock>
30+
</hc:DashedBorder>
31+
<hc:DashedBorder Width="200"
32+
Height="112"
33+
Background="Transparent"
34+
CornerRadius="4"
35+
BorderBrush="{DynamicResource BorderBrush}"
36+
BorderThickness="1"
37+
BorderDashArray="2,2">
38+
<hc:DashedBorder.ContextMenu>
39+
<ContextMenu ItemsSource="{Binding DataList}"
40+
Theme="{StaticResource ContextMenu.Small}">
41+
<ContextMenu.ItemTemplate>
42+
<TreeDataTemplate ItemsSource="{Binding DataList}">
43+
<TextBlock Text="{Binding Name}" />
44+
</TreeDataTemplate>
45+
</ContextMenu.ItemTemplate>
46+
</ContextMenu>
47+
</hc:DashedBorder.ContextMenu>
48+
<TextBlock Foreground="{DynamicResource BorderBrush}"
49+
Theme="{StaticResource TextBlockTitle}"
50+
TextAlignment="Center">
51+
<Run Text="Right click here" />
52+
<LineBreak />
53+
<Run Text="Small" />
54+
</TextBlock>
55+
</hc:DashedBorder>
56+
</hc:UniformSpacingPanel>
57+
</UserControl>
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
namespace HandyControlDemo.UserControl;
2+
3+
public partial class MenuDemo : Avalonia.Controls.UserControl
4+
{
5+
public MenuDemo()
6+
{
7+
InitializeComponent();
8+
}
9+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
using Avalonia;
2+
3+
namespace HandyControl.Controls;
4+
5+
public class MenuAttach
6+
{
7+
public static readonly AttachedProperty<double> PopupVerticalOffsetProperty =
8+
AvaloniaProperty.RegisterAttached<MenuAttach, AvaloniaObject, double>("PopupVerticalOffset");
9+
10+
public static void SetPopupVerticalOffset(AvaloniaObject element, double value) =>
11+
element.SetValue(PopupVerticalOffsetProperty, value);
12+
13+
public static double GetPopupVerticalOffset(AvaloniaObject element) =>
14+
element.GetValue(PopupVerticalOffsetProperty);
15+
16+
public static readonly AttachedProperty<double> PopupHorizontalOffsetProperty =
17+
AvaloniaProperty.RegisterAttached<MenuAttach, AvaloniaObject, double>("PopupHorizontalOffset");
18+
19+
public static void SetPopupHorizontalOffset(AvaloniaObject element, double value) =>
20+
element.SetValue(PopupHorizontalOffsetProperty, value);
21+
22+
public static double GetPopupHorizontalOffset(AvaloniaObject element) =>
23+
element.GetValue(PopupHorizontalOffsetProperty);
24+
25+
public static readonly AttachedProperty<Thickness> ItemPaddingProperty =
26+
AvaloniaProperty.RegisterAttached<MenuAttach, AvaloniaObject, Thickness>("ItemPadding");
27+
28+
public static void SetItemPadding(AvaloniaObject element, Thickness value) =>
29+
element.SetValue(ItemPaddingProperty, value);
30+
31+
public static Thickness GetItemPadding(AvaloniaObject element) => element.GetValue(ItemPaddingProperty);
32+
}

src/Avalonia/HandyControl_Avalonia/Themes/Basic/Converters.axaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
<hc:Double2GridLengthConverter x:Key="Double2GridLengthConverter" />
1313
<hc:ThicknessSplitConverter x:Key="ThicknessSplitConverter" />
1414
<avalonia:StringFormatConverter x:Key="StringFormatConverter" />
15+
<avalonia:PlatformKeyGestureConverter x:Key="PlatformKeyGestureConverter" />
1516
<avalonia:MarginMultiplierConverter x:Key="LeftMarginConverter"
1617
Indent="16"
1718
Left="True" />
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<ResourceDictionary xmlns="https://github.com/avaloniaui"
2+
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
3+
xmlns:hc="https://handyorg.github.io/handycontrol">
4+
5+
<ControlTheme x:Key="ContextMenuBaseStyle"
6+
TargetType="ContextMenu">
7+
<Setter Property="hc:MenuAttach.ItemPadding"
8+
Value="{StaticResource DefaultControlPadding}" />
9+
<Setter Property="hc:MenuAttach.PopupVerticalOffset"
10+
Value="-9" />
11+
<Setter Property="hc:MenuAttach.PopupHorizontalOffset"
12+
Value="10" />
13+
<Setter Property="Background"
14+
Value="{DynamicResource RegionBrush}" />
15+
<Setter Property="hc:BorderElement.CornerRadius"
16+
Value="{StaticResource DefaultCornerRadius}" />
17+
<Setter Property="CornerRadius"
18+
Value="{Binding $self.(hc:BorderElement.CornerRadius)}" />
19+
<Setter Property="Padding"
20+
Value="2,2,2,0" />
21+
<Setter Property="Template">
22+
<Setter.Value>
23+
<ControlTemplate TargetType="ContextMenu">
24+
<Border Effect="{StaticResource EffectShadow2}"
25+
Margin="8"
26+
Background="{TemplateBinding Background}"
27+
CornerRadius="{TemplateBinding CornerRadius}"
28+
MaxHeight="{TemplateBinding MaxHeight}"
29+
BorderThickness="1"
30+
BorderBrush="{DynamicResource BorderBrush}">
31+
<ScrollViewer Theme="{StaticResource ScrollViewerUpDown}"
32+
Margin="{TemplateBinding Padding}">
33+
<ItemsPresenter Name="PART_ItemsPresenter"
34+
ItemsPanel="{TemplateBinding ItemsPanel}"
35+
KeyboardNavigation.TabNavigation="Continue" />
36+
</ScrollViewer>
37+
</Border>
38+
</ControlTemplate>
39+
</Setter.Value>
40+
</Setter>
41+
</ControlTheme>
42+
43+
<ControlTheme x:Key="{x:Type ContextMenu}"
44+
BasedOn="{StaticResource ContextMenuBaseStyle}"
45+
TargetType="ContextMenu" />
46+
47+
<ControlTheme x:Key="ContextMenu.Small"
48+
BasedOn="{StaticResource ContextMenuBaseStyle}"
49+
TargetType="ContextMenu">
50+
<Setter Property="hc:MenuAttach.ItemPadding"
51+
Value="6,0" />
52+
<Setter Property="hc:MenuAttach.PopupVerticalOffset"
53+
Value="-3" />
54+
<Setter Property="hc:MenuAttach.PopupHorizontalOffset"
55+
Value="6" />
56+
</ControlTheme>
57+
58+
</ResourceDictionary>
Lines changed: 161 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
1+
<ResourceDictionary xmlns="https://github.com/avaloniaui"
2+
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
3+
xmlns:hc="https://handyorg.github.io/handycontrol"
4+
xmlns:sys="using:System">
5+
6+
<ControlTheme x:Key="MenuItemBaseStyle"
7+
TargetType="MenuItem">
8+
<Setter Property="Padding"
9+
Value="{Binding Path=(hc:MenuAttach.ItemPadding),RelativeSource={RelativeSource AncestorType=MenuBase}}" />
10+
<Setter Property="Background"
11+
Value="Transparent" />
12+
<Setter Property="BorderThickness"
13+
Value="0" />
14+
<Setter Property="Foreground"
15+
Value="{DynamicResource PrimaryTextBrush}" />
16+
<Setter Property="Margin"
17+
Value="0,0,0,2" />
18+
<Setter Property="CornerRadius"
19+
Value="{Binding $self.(hc:BorderElement.CornerRadius)}" />
20+
<Setter Property="MinWidth"
21+
Value="240" />
22+
<Setter Property="Template">
23+
<ControlTemplate>
24+
<Border Name="root"
25+
Background="{TemplateBinding Background}"
26+
BorderBrush="{TemplateBinding BorderBrush}"
27+
BorderThickness="{TemplateBinding BorderThickness}"
28+
CornerRadius="{TemplateBinding CornerRadius}">
29+
<Grid Margin="{TemplateBinding Padding}">
30+
<Grid.ColumnDefinitions>
31+
<ColumnDefinition Width="26" />
32+
<ColumnDefinition Width="*" />
33+
<ColumnDefinition Width="30" />
34+
<ColumnDefinition Width="Auto"
35+
SharedSizeGroup="MenuItemIGT" />
36+
<ColumnDefinition Width="20" />
37+
</Grid.ColumnDefinitions>
38+
<ContentControl Grid.Column="0"
39+
x:Name="PART_ToggleIconPresenter"
40+
IsVisible="False"
41+
Margin="3"
42+
Width="16"
43+
Height="16" />
44+
<ContentControl Grid.Column="0"
45+
Name="PART_IconPresenter"
46+
Width="16"
47+
Height="16"
48+
Margin="3"
49+
HorizontalAlignment="Center"
50+
VerticalAlignment="Center"
51+
Content="{TemplateBinding Icon}" />
52+
<ContentPresenter Name="PART_HeaderPresenter"
53+
Grid.Column="1"
54+
Margin="{TemplateBinding Padding}"
55+
VerticalAlignment="Center"
56+
Content="{TemplateBinding Header}"
57+
ContentTemplate="{TemplateBinding HeaderTemplate}">
58+
<ContentPresenter.DataTemplates>
59+
<DataTemplate DataType="sys:String">
60+
<AccessText Text="{Binding}" />
61+
</DataTemplate>
62+
</ContentPresenter.DataTemplates>
63+
</ContentPresenter>
64+
<TextBlock x:Name="PART_InputGestureText"
65+
Grid.Column="3"
66+
VerticalAlignment="Center"
67+
Text="{TemplateBinding InputGesture, Converter={StaticResource PlatformKeyGestureConverter}}" />
68+
<Path Name="rightArrow"
69+
Grid.Column="4"
70+
Margin="10,0,0,0"
71+
Width="10"
72+
Height="10"
73+
HorizontalAlignment="Left"
74+
VerticalAlignment="Center"
75+
Stretch="Uniform"
76+
Data="{StaticResource RightGeometry}"
77+
Fill="{TemplateBinding Foreground}" />
78+
<Popup Grid.Column="1"
79+
Name="PART_Popup"
80+
IsLightDismissEnabled="False"
81+
IsOpen="{TemplateBinding IsSubMenuOpen, Mode=TwoWay}"
82+
Placement="RightEdgeAlignedTop">
83+
<Border Background="{DynamicResource RegionBrush}"
84+
BorderBrush="{DynamicResource BorderBrush}"
85+
BorderThickness="1"
86+
CornerRadius="{Binding Path=(hc:BorderElement.CornerRadius),RelativeSource={RelativeSource TemplatedParent}}">
87+
<ScrollViewer Theme="{StaticResource ScrollViewerUpDown}"
88+
Margin="2,2,2,0">
89+
<ItemsPresenter Name="PART_ItemsPresenter"
90+
Grid.IsSharedSizeScope="True"
91+
ItemsPanel="{TemplateBinding ItemsPanel}" />
92+
</ScrollViewer>
93+
</Border>
94+
</Popup>
95+
</Grid>
96+
</Border>
97+
</ControlTemplate>
98+
</Setter>
99+
100+
<Style Selector="^:selected /template/ Border#root">
101+
<Setter Property="Background"
102+
Value="{DynamicResource SecondaryRegionBrush}" />
103+
</Style>
104+
105+
<Style Selector="^:empty /template/ Path#rightArrow">
106+
<Setter Property="IsVisible"
107+
Value="False" />
108+
</Style>
109+
</ControlTheme>
110+
111+
<ControlTheme x:Key="MenuBaseStyle"
112+
TargetType="Menu">
113+
<Setter Property="hc:MenuAttach.ItemPadding"
114+
Value="{StaticResource DefaultControlPadding}" />
115+
<Setter Property="hc:MenuAttach.PopupVerticalOffset"
116+
Value="-9" />
117+
<Setter Property="hc:MenuAttach.PopupHorizontalOffset"
118+
Value="10" />
119+
<Setter Property="Background"
120+
Value="{DynamicResource RegionBrush}" />
121+
<Setter Property="Foreground"
122+
Value="{DynamicResource PrimaryTextBrush}" />
123+
<Setter Property="hc:BorderElement.CornerRadius"
124+
Value="{StaticResource DefaultCornerRadius}" />
125+
<Setter Property="CornerRadius"
126+
Value="{Binding $self.(hc:BorderElement.CornerRadius)}" />
127+
<Setter Property="Template">
128+
<ControlTemplate>
129+
<Border Padding="{TemplateBinding Padding}"
130+
Background="{TemplateBinding Background}"
131+
BorderBrush="{TemplateBinding BorderBrush}"
132+
BorderThickness="{TemplateBinding BorderThickness}"
133+
CornerRadius="{TemplateBinding CornerRadius}">
134+
<ItemsPresenter Name="PART_ItemsPresenter"
135+
ItemsPanel="{TemplateBinding ItemsPanel}"
136+
KeyboardNavigation.TabNavigation="Continue" />
137+
</Border>
138+
</ControlTemplate>
139+
</Setter>
140+
</ControlTheme>
141+
142+
<ControlTheme x:Key="{x:Type MenuItem}"
143+
BasedOn="{StaticResource MenuItemBaseStyle}"
144+
TargetType="MenuItem" />
145+
146+
<ControlTheme x:Key="{x:Type Menu}"
147+
BasedOn="{StaticResource MenuBaseStyle}"
148+
TargetType="Menu" />
149+
150+
<ControlTheme x:Key="Menu.Small"
151+
BasedOn="{StaticResource MenuBaseStyle}"
152+
TargetType="Menu">
153+
<Setter Property="hc:MenuAttach.ItemPadding"
154+
Value="6,0" />
155+
<Setter Property="hc:MenuAttach.PopupVerticalOffset"
156+
Value="-3" />
157+
<Setter Property="hc:MenuAttach.PopupHorizontalOffset"
158+
Value="6" />
159+
</ControlTheme>
160+
161+
</ResourceDictionary>

0 commit comments

Comments
 (0)