From 9167cba6367867b216c2440ef0f6c0393c1d1b8d Mon Sep 17 00:00:00 2001 From: Jack251970 <1160210343@qq.com> Date: Sat, 22 Mar 2025 23:08:47 +0800 Subject: [PATCH] Use selected item for binding preview properties & Code cleanup --- Flow.Launcher/MainWindow.xaml | 4 ++-- Flow.Launcher/ViewModel/MainViewModel.cs | 21 +++++++++++++---- Flow.Launcher/ViewModel/ResultViewModel.cs | 25 ++++++++++++++------- Flow.Launcher/ViewModel/ResultsViewModel.cs | 13 ++++++----- 4 files changed, 43 insertions(+), 20 deletions(-) diff --git a/Flow.Launcher/MainWindow.xaml b/Flow.Launcher/MainWindow.xaml index 5b63303ac..d1f6e7812 100644 --- a/Flow.Launcher/MainWindow.xaml +++ b/Flow.Launcher/MainWindow.xaml @@ -441,7 +441,7 @@ diff --git a/Flow.Launcher/ViewModel/MainViewModel.cs b/Flow.Launcher/ViewModel/MainViewModel.cs index 46970a6a1..f06b8aa81 100644 --- a/Flow.Launcher/ViewModel/MainViewModel.cs +++ b/Flow.Launcher/ViewModel/MainViewModel.cs @@ -162,6 +162,7 @@ namespace Flow.Launcher.ViewModel switch (args.PropertyName) { case nameof(Results.SelectedItem): + SelectedItem = Results.SelectedItem; UpdatePreview(); break; } @@ -786,6 +787,18 @@ namespace Flow.Launcher.ViewModel #region Preview + private ResultViewModel _selectedItem; + + public ResultViewModel SelectedItem + { + get => _selectedItem; + set + { + _selectedItem = value; + OnPropertyChanged(); + } + } + public bool InternalPreviewVisible { get @@ -884,7 +897,7 @@ namespace Flow.Launcher.ViewModel private void ShowInternalPreview() { ResultAreaColumn = ResultAreaColumnPreviewShown; - Results.SelectedItem?.LoadPreviewImage(); + SelectedItem?.LoadPreviewImage(); } private void HideInternalPreview() @@ -939,14 +952,14 @@ namespace Flow.Launcher.ViewModel case false when InternalPreviewVisible: - Results.SelectedItem?.LoadPreviewImage(); + SelectedItem?.LoadPreviewImage(); break; } } private bool CanExternalPreviewSelectedResult(out string path) { - path = Results.SelectedItem?.Result?.Preview.FilePath; + path = SelectedItem?.Result?.Preview.FilePath; return !string.IsNullOrEmpty(path); } @@ -976,7 +989,7 @@ namespace Flow.Launcher.ViewModel var query = QueryText.ToLower().Trim(); ContextMenu.Clear(); - var selected = Results.SelectedItem?.Result; + var selected = SelectedItem?.Result; if (selected != null) // SelectedItem returns null if selection is empty. { diff --git a/Flow.Launcher/ViewModel/ResultViewModel.cs b/Flow.Launcher/ViewModel/ResultViewModel.cs index 5130e7eba..172eca502 100644 --- a/Flow.Launcher/ViewModel/ResultViewModel.cs +++ b/Flow.Launcher/ViewModel/ResultViewModel.cs @@ -1,4 +1,7 @@ using System; +using System.Collections.Generic; +using System.Drawing.Text; +using System.IO; using System.Threading.Tasks; using System.Windows; using System.Windows.Media; @@ -6,16 +9,13 @@ using Flow.Launcher.Infrastructure.Image; using Flow.Launcher.Infrastructure.Logger; using Flow.Launcher.Infrastructure.UserSettings; using Flow.Launcher.Plugin; -using System.IO; -using System.Drawing.Text; -using System.Collections.Generic; namespace Flow.Launcher.ViewModel { public class ResultViewModel : BaseModel { - private static PrivateFontCollection fontCollection = new(); - private static Dictionary fonts = new(); + private static readonly PrivateFontCollection fontCollection = new(); + private static readonly Dictionary fonts = new(); public ResultViewModel(Result result, Settings settings) { @@ -39,11 +39,11 @@ namespace Flow.Launcher.ViewModel fontFamilyPath = Path.Combine(Result.PluginDirectory, fontFamilyPath); } - if (fonts.ContainsKey(fontFamilyPath)) + if (fonts.TryGetValue(fontFamilyPath, out var value)) { Glyph = glyph with { - FontFamily = fonts[fontFamilyPath] + FontFamily = value }; } else @@ -171,7 +171,16 @@ namespace Flow.Launcher.ViewModel public ImageSource PreviewImage { - get => previewImage; + get + { + if (!PreviewImageLoaded) + { + PreviewImageLoaded = true; + _ = LoadPreviewImageAsync(); + } + + return previewImage; + } private set => previewImage = value; } diff --git a/Flow.Launcher/ViewModel/ResultsViewModel.cs b/Flow.Launcher/ViewModel/ResultsViewModel.cs index 7d2b5bc93..512f5c150 100644 --- a/Flow.Launcher/ViewModel/ResultsViewModel.cs +++ b/Flow.Launcher/ViewModel/ResultsViewModel.cs @@ -1,6 +1,4 @@ using System; -using Flow.Launcher.Infrastructure.UserSettings; -using Flow.Launcher.Plugin; using System.Collections.Generic; using System.Collections.Specialized; using System.Linq; @@ -10,6 +8,8 @@ using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; +using Flow.Launcher.Infrastructure.UserSettings; +using Flow.Launcher.Plugin; namespace Flow.Launcher.ViewModel { @@ -219,7 +219,6 @@ namespace Flow.Launcher.ViewModel if (newRawResults.Count == 0) return Results; - var newResults = newRawResults.Select(r => new ResultViewModel(r, _settings)); return Results.Where(r => r.Result.PluginID != resultId) @@ -241,6 +240,7 @@ namespace Flow.Launcher.ViewModel #endregion #region FormattedText Dependency Property + public static readonly DependencyProperty FormattedTextProperty = DependencyProperty.RegisterAttached( "FormattedText", typeof(Inline), @@ -259,8 +259,7 @@ namespace Flow.Launcher.ViewModel private static void FormattedTextPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { - var textBlock = d as TextBlock; - if (textBlock == null) return; + if (d is not TextBlock textBlock) return; var inline = (Inline)e.NewValue; @@ -269,6 +268,7 @@ namespace Flow.Launcher.ViewModel textBlock.Inlines.Add(inline); } + #endregion public class ResultCollection : List, INotifyCollectionChanged @@ -279,7 +279,6 @@ namespace Flow.Launcher.ViewModel public event NotifyCollectionChangedEventHandler CollectionChanged; - protected void OnCollectionChanged(NotifyCollectionChangedEventArgs e) { CollectionChanged?.Invoke(this, e); @@ -297,6 +296,7 @@ namespace Flow.Launcher.ViewModel // wpf use DirectX / double buffered already, so just reset all won't cause ui flickering OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset)); } + private void AddAll(List Items) { for (int i = 0; i < Items.Count; i++) @@ -308,6 +308,7 @@ namespace Flow.Launcher.ViewModel OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, item, i)); } } + public void RemoveAll(int Capacity = 512) { Clear();