From e61c07f3525e4868f02a2fa66e3c54a008544102 Mon Sep 17 00:00:00 2001 From: Zhang Dian <54255897+zdpcdt@users.noreply.github.com> Date: Fri, 20 Dec 2024 23:38:53 +0800 Subject: [PATCH] feat: enhance IconDemo. --- demo/Semi.Avalonia.Demo/Pages/IconDemo.axaml | 114 +++++++++++++----- .../Pages/IconDemo.axaml.cs | 27 ++++- .../ViewModels/IconDemoViewModel.cs | 47 ++++++-- 3 files changed, 146 insertions(+), 42 deletions(-) diff --git a/demo/Semi.Avalonia.Demo/Pages/IconDemo.axaml b/demo/Semi.Avalonia.Demo/Pages/IconDemo.axaml index b9f61cf..4545386 100644 --- a/demo/Semi.Avalonia.Demo/Pages/IconDemo.axaml +++ b/demo/Semi.Avalonia.Demo/Pages/IconDemo.axaml @@ -9,36 +9,86 @@ - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/demo/Semi.Avalonia.Demo/Pages/IconDemo.axaml.cs b/demo/Semi.Avalonia.Demo/Pages/IconDemo.axaml.cs index ebc7fef..8a5a032 100644 --- a/demo/Semi.Avalonia.Demo/Pages/IconDemo.axaml.cs +++ b/demo/Semi.Avalonia.Demo/Pages/IconDemo.axaml.cs @@ -1,5 +1,9 @@ -using Avalonia.Controls; +using Avalonia; +using Avalonia.Controls; +using Avalonia.Controls.Notifications; using Avalonia.Controls.Primitives; +using Avalonia.Input.Platform; +using Avalonia.Interactivity; using Avalonia.Threading; using Semi.Avalonia.Demo.ViewModels; @@ -7,6 +11,9 @@ namespace Semi.Avalonia.Demo.Pages; public partial class IconDemo : UserControl { + private IClipboard? _clipboard; + private WindowNotificationManager? _windowNotificationManager; + public IconDemo() { InitializeComponent(); @@ -19,4 +26,22 @@ public partial class IconDemo : UserControl var vm = this.DataContext as IconDemoViewModel; 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); + } } \ No newline at end of file diff --git a/demo/Semi.Avalonia.Demo/ViewModels/IconDemoViewModel.cs b/demo/Semi.Avalonia.Demo/ViewModels/IconDemoViewModel.cs index cef93e1..09e8fc8 100644 --- a/demo/Semi.Avalonia.Demo/ViewModels/IconDemoViewModel.cs +++ b/demo/Semi.Avalonia.Demo/ViewModels/IconDemoViewModel.cs @@ -1,5 +1,7 @@ using System; +using System.Collections.Generic; using System.Collections.ObjectModel; +using System.Linq; using Avalonia.Controls; using Avalonia.Markup.Xaml; using Avalonia.Media; @@ -12,24 +14,51 @@ public partial class IconDemoViewModel : ObservableObject private readonly IResourceDictionary? _resources = AvaloniaXamlLoader.Load(new Uri("avares://Semi.Avalonia/Themes/Shared/Icon.axaml")) as ResourceDictionary; - public ObservableCollection Icons { get; set; } = []; + private readonly Dictionary _filledIcons = new(); + private readonly Dictionary _strokedIcons = new(); + + [ObservableProperty] private string? _searchText; + + public ObservableCollection FilteredFilledIcons { get; set; } = []; + public ObservableCollection FilteredStrokedIcons { get; set; } = []; public void InitializeResources() - { - LoadIcons(); - } - - private void LoadIcons() { if (_resources is null) return; 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 }; - Icons.Add(icon); + _strokedIcons[key.ToString()] = 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); } } }