Skip to content

Commit

Permalink
enhance: Optimized the ScrollViewer style.
Browse files Browse the repository at this point in the history
  • Loading branch information
NaBian committed Jul 27, 2024
1 parent 17c2b84 commit 51f0764
Show file tree
Hide file tree
Showing 16 changed files with 458 additions and 235 deletions.
2 changes: 1 addition & 1 deletion src/Avalonia/Directory.Build.props
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<Project>
<PropertyGroup>
<Nullable>enable</Nullable>
<AvaloniaVersion>11.0.6</AvaloniaVersion>
<AvaloniaVersion>11.1.1</AvaloniaVersion>
</PropertyGroup>
</Project>
2 changes: 1 addition & 1 deletion src/Avalonia/HandyControlDemo_Avalonia/Data/DemoInfo.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[
{
"title": "Styles",
"selectedIndex": -1,
"selectedIndex": 0,
"group": false,
"demoItemList": [
[ "Brush", "BrushDemoCtl", "Brush.Brush", "", "" ],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@
<DependentUpon>BorderDemoCtl.axaml</DependentUpon>
<SubType>Code</SubType>
</Compile>
<Compile Update="UserControl\Styles\RepeatButtonDemoCtl.axaml.cs">
<DependentUpon>RepeatButtonDemoCtl.axaml</DependentUpon>
<SubType>Code</SubType>
</Compile>
</ItemGroup>

<ItemGroup>
Expand Down
25 changes: 12 additions & 13 deletions src/Avalonia/HandyControlDemo_Avalonia/Service/Data/DataService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,21 @@ internal List<DemoInfoModel> GetDemoInfo()
{
var infoList = new List<DemoInfoModel>();
string jsonStr = ReadEmbeddedJsonFile("HandyControlDemo.Data.DemoInfo.json");
var jsonObj = JsonConvert.DeserializeObject<dynamic>(jsonStr);
dynamic? jsonObj = JsonConvert.DeserializeObject<dynamic>(jsonStr);
if (jsonObj is null)
{
return new List<DemoInfoModel>();
return [];
}

foreach (var item in jsonObj)
foreach (dynamic? item in jsonObj)
{
var titleKey = (string) item.title;
var title = titleKey;
string? titleKey = (string) item.title;
List<DemoItemModel> list = Convert2DemoItemList(item.demoItemList);

var demoInfoModel = new DemoInfoModel
{
Key = titleKey,
Title = title,
Title = titleKey,
DemoItemList = list,
SelectedIndex = (int) item.selectedIndex,
IsGroupEnabled = (bool) item.group
Expand All @@ -43,10 +42,10 @@ internal List<DemoInfoModel> GetDemoInfo()
return infoList;
}

private string ReadEmbeddedJsonFile(string resourceName)
private static string ReadEmbeddedJsonFile(string resourceName)
{
var assembly = Assembly.GetExecutingAssembly();
var resourceNameWithNamespace = assembly.GetManifestResourceNames()
string? resourceNameWithNamespace = assembly.GetManifestResourceNames()
.FirstOrDefault(n => n.EndsWith(resourceName, StringComparison.OrdinalIgnoreCase));

if (resourceNameWithNamespace is null)
Expand All @@ -64,17 +63,17 @@ private string ReadEmbeddedJsonFile(string resourceName)
return reader.ReadToEnd();
}

private List<DemoItemModel> Convert2DemoItemList(dynamic list)
private static List<DemoItemModel> Convert2DemoItemList(dynamic list)
{
var resultList = new List<DemoItemModel>();

foreach (var item in list)
foreach (dynamic? item in list)
{
var name = (string) item[0];
string? name = (string) item[0];
string targetCtlName = item[1];
string imageBrushName = item[2];
var isNew = !string.IsNullOrEmpty((string) item[3]);
var groupName = (string) item[4];
bool isNew = !string.IsNullOrEmpty((string) item[3]);
string? groupName = (string) item[4];
if (string.IsNullOrEmpty(groupName))
{
groupName = "Misc";
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<UserControl xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Class="HandyControlDemo.UserControl.RepeatButtonDemoCtl"
xmlns:hc="https://handyorg.github.io/handycontrol"
Background="{DynamicResource RegionBrush}">
<ScrollViewer>
<hc:UniformSpacingPanel ChildWrapping="Wrap"
Spacing="32"
Margin="32">
<hc:UniformSpacingPanel Orientation="Vertical"
Spacing="32">
<WrapPanel>
<StackPanel>
<RepeatButton Content="default"
Margin="5" />
<RepeatButton IsEnabled="False"
Content="default"
Margin="5" />
</StackPanel>
<StackPanel>
<RepeatButton Content="primary"
Margin="5"
Theme="{StaticResource RepeatButtonPrimary}" />
<RepeatButton IsEnabled="False"
Content="primary"
Margin="5"
Theme="{StaticResource RepeatButtonPrimary}" />
</StackPanel>
<StackPanel>
<RepeatButton Content="success"
Margin="5"
Theme="{StaticResource RepeatButtonSuccess}" />
<RepeatButton IsEnabled="False"
Content="success"
Margin="5"
Theme="{StaticResource RepeatButtonSuccess}" />
</StackPanel>
<StackPanel>
<RepeatButton Content="info"
Margin="5"
Theme="{StaticResource RepeatButtonInfo}" />
<RepeatButton IsEnabled="False"
Content="info"
Margin="5"
Theme="{StaticResource RepeatButtonInfo}" />
</StackPanel>
<StackPanel>
<RepeatButton Content="warning"
Margin="5"
Theme="{StaticResource RepeatButtonWarning}" />
<RepeatButton IsEnabled="False"
Content="warning"
Margin="5"
Theme="{StaticResource RepeatButtonWarning}" />
</StackPanel>
<StackPanel>
<RepeatButton Content="danger"
Margin="5"
Theme="{StaticResource RepeatButtonDanger}" />
<RepeatButton IsEnabled="False"
Content="danger"
Margin="5"
Theme="{StaticResource RepeatButtonDanger}" />
</StackPanel>
</WrapPanel>
</hc:UniformSpacingPanel>
</hc:UniformSpacingPanel>
</ScrollViewer>
</UserControl>
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace HandyControlDemo.UserControl;

public partial class RepeatButtonDemoCtl : Avalonia.Controls.UserControl
{
public RepeatButtonDemoCtl()
{
InitializeComponent();
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.ObjectModel;
using System.Linq;
using Avalonia.Threading;
using CommunityToolkit.Mvvm.Messaging;
using HandyControlDemo.Data;
Expand Down Expand Up @@ -44,10 +45,11 @@ public object? ContentTitle

private void UpdateMainContent()
{
WeakReferenceMessenger.Default.Register<DemoItemModel, string>(this, MessageToken.SwitchDemo, (_, message) =>
{
SwitchDemo(message);
});
WeakReferenceMessenger.Default.Register<DemoItemModel, string>(
recipient: this,
token: MessageToken.SwitchDemo,
handler: (_, message) => SwitchDemo(message)
);
}

private void UpdateLeftContent()
Expand Down Expand Up @@ -76,11 +78,9 @@ private void UpdateLeftContent()
DemoInfoCollection = [];
foreach (var item in _dataService.GetDemoInfo())
{
Dispatcher.UIThread.InvokeAsync(() =>
{
DemoInfoCollection.Add(item);
}, DispatcherPriority.ApplicationIdle);
Dispatcher.UIThread.InvokeAsync(() => DemoInfoCollection.Add(item));
}
Dispatcher.UIThread.InvokeAsync(() => SwitchDemo(DemoInfoCollection.First().DemoItemList.First()));
}

private void SwitchDemo(DemoItemModel item)
Expand All @@ -92,7 +92,8 @@ private void SwitchDemo(DemoItemModel item)

DemoItemCurrent = item;
ContentTitle = Lang.ResourceManager.GetString(item.Name, Lang.Culture);
object? demoControl = AssemblyHelper.ResolveByKey(item.TargetCtlName) ?? AssemblyHelper.CreateInternalInstance($"UserControl.{item.TargetCtlName}");
object? demoControl = AssemblyHelper.ResolveByKey(item.TargetCtlName) ??
AssemblyHelper.CreateInternalInstance($"UserControl.{item.TargetCtlName}");
SubContent = demoControl;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ namespace HandyControlDemo.ViewModel;

public class ViewModelLocator
{
private static readonly Lazy<ViewModelLocator> InstanceInternal = new(() => new ViewModelLocator(), isThreadSafe: true);
private static readonly Lazy<ViewModelLocator> InstanceInternal =
new(() => new ViewModelLocator(), isThreadSafe: true);

public static ViewModelLocator Instance => InstanceInternal.Value;

Expand Down
124 changes: 124 additions & 0 deletions src/Avalonia/HandyControl_Avalonia/Themes/Styles/RepeatButton.axaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
<ResourceDictionary xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<ControlTheme x:Key="RepeatButtonBaseStyle"
TargetType="RepeatButton">
<Setter Property="Height"
Value="{StaticResource DefaultControlHeight}" />
<Setter Property="Padding"
Value="{StaticResource DefaultControlPadding}" />
<Setter Property="HorizontalAlignment"
Value="Center" />
<Setter Property="VerticalAlignment"
Value="Center" />
<Setter Property="HorizontalContentAlignment"
Value="Center" />
<Setter Property="VerticalContentAlignment"
Value="Center" />
<Setter Property="Background"
Value="{DynamicResource RegionBrush}" />
<Setter Property="Foreground"
Value="{DynamicResource TextIconBrush}" />
<Setter Property="BorderBrush"
Value="{DynamicResource BorderBrush}" />
<Setter Property="BorderThickness"
Value="1" />
<Setter Property="CornerRadius"
Value="{StaticResource DefaultCornerRadius}" />
<Setter Property="Template">
<ControlTemplate>
<ContentPresenter x:Name="PART_ContentPresenter"
Background="{TemplateBinding Background}"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}"
CornerRadius="{TemplateBinding CornerRadius}"
Content="{TemplateBinding Content}"
ContentTemplate="{TemplateBinding ContentTemplate}"
Padding="{TemplateBinding Padding}"
RecognizesAccessKey="True"
HorizontalContentAlignment="{TemplateBinding HorizontalContentAlignment}"
VerticalContentAlignment="{TemplateBinding VerticalContentAlignment}" />
</ControlTemplate>
</Setter>

<Style Selector="^:pointerover /template/ ContentPresenter#PART_ContentPresenter">
<Setter Property="Opacity"
Value="0.9" />
</Style>

<Style Selector="^:pressed /template/ ContentPresenter#PART_ContentPresenter">
<Setter Property="Opacity"
Value="0.6" />
</Style>

<Style Selector="^:disabled /template/ ContentPresenter#PART_ContentPresenter">
<Setter Property="Opacity"
Value="0.4" />
</Style>
</ControlTheme>

<ControlTheme x:Key="RepeatButtonDefault"
BasedOn="{StaticResource RepeatButtonBaseStyle}"
TargetType="RepeatButton">
<Setter Property="Foreground"
Value="{DynamicResource PrimaryTextBrush}" />

<Style Selector="^:pointerover /template/ ContentPresenter#PART_ContentPresenter">
<Setter Property="Background"
Value="{DynamicResource SecondaryRegionBrush}" />
</Style>

<Style Selector="^:pressed /template/ ContentPresenter#PART_ContentPresenter">
<Setter Property="Background"
Value="{DynamicResource BorderBrush}" />
</Style>
</ControlTheme>

<ControlTheme x:Key="{x:Type RepeatButton}"
BasedOn="{StaticResource RepeatButtonDefault}"
TargetType="RepeatButton" />

<ControlTheme x:Key="RepeatButtonPrimary"
BasedOn="{StaticResource RepeatButtonBaseStyle}"
TargetType="RepeatButton">
<Setter Property="Background"
Value="{DynamicResource PrimaryBrush}" />
<Setter Property="BorderBrush"
Value="{DynamicResource PrimaryBrush}" />
</ControlTheme>

<ControlTheme x:Key="RepeatButtonSuccess"
BasedOn="{StaticResource RepeatButtonBaseStyle}"
TargetType="RepeatButton">
<Setter Property="Background"
Value="{DynamicResource SuccessBrush}" />
<Setter Property="BorderBrush"
Value="{DynamicResource SuccessBrush}" />
</ControlTheme>

<ControlTheme x:Key="RepeatButtonInfo"
BasedOn="{StaticResource RepeatButtonBaseStyle}"
TargetType="RepeatButton">
<Setter Property="Background"
Value="{DynamicResource InfoBrush}" />
<Setter Property="BorderBrush"
Value="{DynamicResource InfoBrush}" />
</ControlTheme>

<ControlTheme x:Key="RepeatButtonWarning"
BasedOn="{StaticResource RepeatButtonBaseStyle}"
TargetType="RepeatButton">
<Setter Property="Background"
Value="{DynamicResource WarningBrush}" />
<Setter Property="BorderBrush"
Value="{DynamicResource WarningBrush}" />
</ControlTheme>

<ControlTheme x:Key="RepeatButtonDanger"
BasedOn="{StaticResource RepeatButtonBaseStyle}"
TargetType="RepeatButton">
<Setter Property="Background"
Value="{DynamicResource DangerBrush}" />
<Setter Property="BorderBrush"
Value="{DynamicResource DangerBrush}" />
</ControlTheme>
</ResourceDictionary>
Loading

0 comments on commit 51f0764

Please sign in to comment.