mirror of
https://gitcode.com/gh_mirrors/se/Semi.Avalonia
synced 2026-05-01 21:41:27 +08:00
feat: enhance IconDemo.
This commit is contained in:
@@ -9,36 +9,86 @@
|
|||||||
<Design.DataContext>
|
<Design.DataContext>
|
||||||
<vm:IconDemoViewModel />
|
<vm:IconDemoViewModel />
|
||||||
</Design.DataContext>
|
</Design.DataContext>
|
||||||
<ScrollViewer>
|
<Grid RowDefinitions="Auto, *">
|
||||||
<ItemsControl ItemsSource="{Binding Icons}">
|
<TextBox
|
||||||
<ItemsControl.ItemsPanel>
|
Grid.Row="0"
|
||||||
<ItemsPanelTemplate>
|
Width="600"
|
||||||
<WrapPanel />
|
Margin="8"
|
||||||
</ItemsPanelTemplate>
|
Classes="ClearButton"
|
||||||
</ItemsControl.ItemsPanel>
|
Text="{Binding SearchText}"
|
||||||
<ItemsControl.ItemTemplate>
|
Watermark="Input Icon Name" />
|
||||||
<DataTemplate>
|
|
||||||
<Button Theme="{DynamicResource OutlineButton}"
|
<TabControl Grid.Row="1">
|
||||||
Classes="Tertiary"
|
<TabItem Header="Filled Icons">
|
||||||
Padding="0"
|
<ScrollViewer>
|
||||||
Margin="10"
|
<ItemsControl ItemsSource="{Binding FilteredFilledIcons}">
|
||||||
Width="200"
|
<ItemsControl.ItemsPanel>
|
||||||
Height="120">
|
<ItemsPanelTemplate>
|
||||||
<StackPanel>
|
<WrapPanel />
|
||||||
<PathIcon
|
</ItemsPanelTemplate>
|
||||||
Theme="{DynamicResource InnerPathIcon}"
|
</ItemsControl.ItemsPanel>
|
||||||
HorizontalAlignment="Center"
|
<ItemsControl.ItemTemplate>
|
||||||
Classes="ExtraLarge"
|
<DataTemplate>
|
||||||
Data="{Binding Geometry}" />
|
<Button Theme="{DynamicResource OutlineButton}"
|
||||||
<TextBlock
|
Classes="Tertiary"
|
||||||
HorizontalAlignment="Center"
|
Padding="0"
|
||||||
FontSize="12"
|
Margin="10"
|
||||||
FontWeight="Normal"
|
Width="200"
|
||||||
Text="{Binding ResourceKey}" />
|
Height="120"
|
||||||
</StackPanel>
|
Click="Button_Clicked">
|
||||||
</Button>
|
<StackPanel Spacing="8">
|
||||||
</DataTemplate>
|
<PathIcon
|
||||||
</ItemsControl.ItemTemplate>
|
Theme="{DynamicResource InnerPathIcon}"
|
||||||
</ItemsControl>
|
HorizontalAlignment="Center"
|
||||||
</ScrollViewer>
|
Classes="ExtraLarge"
|
||||||
|
Data="{Binding Geometry}" />
|
||||||
|
<TextBlock
|
||||||
|
HorizontalAlignment="Center"
|
||||||
|
FontSize="12"
|
||||||
|
FontWeight="Normal"
|
||||||
|
Text="{Binding ResourceKey}" />
|
||||||
|
</StackPanel>
|
||||||
|
</Button>
|
||||||
|
</DataTemplate>
|
||||||
|
</ItemsControl.ItemTemplate>
|
||||||
|
</ItemsControl>
|
||||||
|
</ScrollViewer>
|
||||||
|
</TabItem>
|
||||||
|
|
||||||
|
<TabItem Header="Stroked Icons">
|
||||||
|
<ScrollViewer>
|
||||||
|
<ItemsControl ItemsSource="{Binding FilteredStrokedIcons}">
|
||||||
|
<ItemsControl.ItemsPanel>
|
||||||
|
<ItemsPanelTemplate>
|
||||||
|
<WrapPanel />
|
||||||
|
</ItemsPanelTemplate>
|
||||||
|
</ItemsControl.ItemsPanel>
|
||||||
|
<ItemsControl.ItemTemplate>
|
||||||
|
<DataTemplate>
|
||||||
|
<Button Theme="{DynamicResource OutlineButton}"
|
||||||
|
Classes="Tertiary"
|
||||||
|
Padding="0"
|
||||||
|
Margin="10"
|
||||||
|
Width="200"
|
||||||
|
Height="120">
|
||||||
|
<StackPanel Spacing="8">
|
||||||
|
<PathIcon
|
||||||
|
Theme="{DynamicResource InnerPathIcon}"
|
||||||
|
HorizontalAlignment="Center"
|
||||||
|
Classes="ExtraLarge"
|
||||||
|
Data="{Binding Geometry}" />
|
||||||
|
<TextBlock
|
||||||
|
HorizontalAlignment="Center"
|
||||||
|
FontSize="12"
|
||||||
|
FontWeight="Normal"
|
||||||
|
Text="{Binding ResourceKey}" />
|
||||||
|
</StackPanel>
|
||||||
|
</Button>
|
||||||
|
</DataTemplate>
|
||||||
|
</ItemsControl.ItemTemplate>
|
||||||
|
</ItemsControl>
|
||||||
|
</ScrollViewer>
|
||||||
|
</TabItem>
|
||||||
|
</TabControl>
|
||||||
|
</Grid>
|
||||||
</UserControl>
|
</UserControl>
|
||||||
@@ -1,5 +1,9 @@
|
|||||||
using Avalonia.Controls;
|
using Avalonia;
|
||||||
|
using Avalonia.Controls;
|
||||||
|
using Avalonia.Controls.Notifications;
|
||||||
using Avalonia.Controls.Primitives;
|
using Avalonia.Controls.Primitives;
|
||||||
|
using Avalonia.Input.Platform;
|
||||||
|
using Avalonia.Interactivity;
|
||||||
using Avalonia.Threading;
|
using Avalonia.Threading;
|
||||||
using Semi.Avalonia.Demo.ViewModels;
|
using Semi.Avalonia.Demo.ViewModels;
|
||||||
|
|
||||||
@@ -7,6 +11,9 @@ namespace Semi.Avalonia.Demo.Pages;
|
|||||||
|
|
||||||
public partial class IconDemo : UserControl
|
public partial class IconDemo : UserControl
|
||||||
{
|
{
|
||||||
|
private IClipboard? _clipboard;
|
||||||
|
private WindowNotificationManager? _windowNotificationManager;
|
||||||
|
|
||||||
public IconDemo()
|
public IconDemo()
|
||||||
{
|
{
|
||||||
InitializeComponent();
|
InitializeComponent();
|
||||||
@@ -19,4 +26,22 @@ public partial class IconDemo : UserControl
|
|||||||
var vm = this.DataContext as IconDemoViewModel;
|
var vm = this.DataContext as IconDemoViewModel;
|
||||||
await Dispatcher.UIThread.InvokeAsync(() => { vm?.InitializeResources(); });
|
await Dispatcher.UIThread.InvokeAsync(() => { vm?.InitializeResources(); });
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected override void OnAttachedToVisualTree(VisualTreeAttachmentEventArgs e)
|
||||||
|
{
|
||||||
|
base.OnAttachedToVisualTree(e);
|
||||||
|
var topLevel = TopLevel.GetTopLevel(this);
|
||||||
|
_windowNotificationManager = new WindowNotificationManager(topLevel) { MaxItems = 3 };
|
||||||
|
_clipboard = topLevel?.Clipboard;
|
||||||
|
}
|
||||||
|
|
||||||
|
private async void Button_Clicked(object sender, RoutedEventArgs e)
|
||||||
|
{
|
||||||
|
if (_clipboard is null) return;
|
||||||
|
if (sender is not Button { DataContext: IconItem s }) return;
|
||||||
|
await _clipboard.SetTextAsync(s.ResourceKey);
|
||||||
|
_windowNotificationManager?.Show(
|
||||||
|
new Notification("Copied", s.ResourceKey),
|
||||||
|
NotificationType.Success);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -1,5 +1,7 @@
|
|||||||
using System;
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
using System.Collections.ObjectModel;
|
using System.Collections.ObjectModel;
|
||||||
|
using System.Linq;
|
||||||
using Avalonia.Controls;
|
using Avalonia.Controls;
|
||||||
using Avalonia.Markup.Xaml;
|
using Avalonia.Markup.Xaml;
|
||||||
using Avalonia.Media;
|
using Avalonia.Media;
|
||||||
@@ -12,24 +14,51 @@ public partial class IconDemoViewModel : ObservableObject
|
|||||||
private readonly IResourceDictionary? _resources =
|
private readonly IResourceDictionary? _resources =
|
||||||
AvaloniaXamlLoader.Load(new Uri("avares://Semi.Avalonia/Themes/Shared/Icon.axaml")) as ResourceDictionary;
|
AvaloniaXamlLoader.Load(new Uri("avares://Semi.Avalonia/Themes/Shared/Icon.axaml")) as ResourceDictionary;
|
||||||
|
|
||||||
public ObservableCollection<IconItem> Icons { get; set; } = [];
|
private readonly Dictionary<string, IconItem> _filledIcons = new();
|
||||||
|
private readonly Dictionary<string, IconItem> _strokedIcons = new();
|
||||||
|
|
||||||
|
[ObservableProperty] private string? _searchText;
|
||||||
|
|
||||||
|
public ObservableCollection<IconItem> FilteredFilledIcons { get; set; } = [];
|
||||||
|
public ObservableCollection<IconItem> FilteredStrokedIcons { get; set; } = [];
|
||||||
|
|
||||||
public void InitializeResources()
|
public void InitializeResources()
|
||||||
{
|
|
||||||
LoadIcons();
|
|
||||||
}
|
|
||||||
|
|
||||||
private void LoadIcons()
|
|
||||||
{
|
{
|
||||||
if (_resources is null) return;
|
if (_resources is null) return;
|
||||||
|
|
||||||
foreach (var key in _resources.Keys)
|
foreach (var key in _resources.Keys)
|
||||||
{
|
{
|
||||||
if (_resources[key] is Geometry geometry)
|
if (_resources[key] is not Geometry geometry) continue;
|
||||||
|
var icon = new IconItem { ResourceKey = key.ToString(), Geometry = geometry };
|
||||||
|
|
||||||
|
if (key.ToString().EndsWith("Stroked"))
|
||||||
{
|
{
|
||||||
var icon = new IconItem { ResourceKey = key.ToString(), Geometry = geometry };
|
_strokedIcons[key.ToString()] = icon;
|
||||||
Icons.Add(icon);
|
|
||||||
}
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
_filledIcons[key.ToString()] = icon;
|
||||||
|
}
|
||||||
|
|
||||||
|
OnSearchTextChanged(string.Empty);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
partial void OnSearchTextChanged(string? value)
|
||||||
|
{
|
||||||
|
var search = value?.ToLowerInvariant() ?? string.Empty;
|
||||||
|
|
||||||
|
FilteredFilledIcons.Clear();
|
||||||
|
foreach (var icon in _filledIcons.Values.Where(i => i.ResourceKey?.ToLowerInvariant().Contains(search) == true))
|
||||||
|
{
|
||||||
|
FilteredFilledIcons.Add(icon);
|
||||||
|
}
|
||||||
|
|
||||||
|
FilteredStrokedIcons.Clear();
|
||||||
|
foreach (var icon in
|
||||||
|
_strokedIcons.Values.Where(i => i.ResourceKey?.ToLowerInvariant().Contains(search) == true))
|
||||||
|
{
|
||||||
|
FilteredStrokedIcons.Add(icon);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user