Use selected item for binding preview properties & Code cleanup

This commit is contained in:
Jack251970 2025-03-22 23:08:47 +08:00
parent dda008f80b
commit 9167cba636
4 changed files with 43 additions and 20 deletions

View file

@ -441,7 +441,7 @@
<Border
MinHeight="380"
d:DataContext="{d:DesignInstance vm:ResultViewModel}"
DataContext="{Binding SelectedItem, ElementName=ResultListBox}"
DataContext="{Binding SelectedItem, Mode=OneWay}"
Visibility="{Binding ShowDefaultPreview}">
<Grid
Margin="0 0 10 5"
@ -518,7 +518,7 @@
MaxHeight="{Binding ElementName=ResultListBox, Path=ActualHeight}"
Padding="0 0 10 10"
d:DataContext="{d:DesignInstance vm:ResultViewModel}"
DataContext="{Binding SelectedItem, ElementName=ResultListBox}"
DataContext="{Binding SelectedItem, Mode=OneWay}"
Visibility="{Binding ShowCustomizedPreview}">
<ContentControl Content="{Binding Result.PreviewPanel.Value}" />
</Border>

View file

@ -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.
{

View file

@ -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<string, string> fonts = new();
private static readonly PrivateFontCollection fontCollection = new();
private static readonly Dictionary<string, string> 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;
}

View file

@ -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<ResultViewModel>, 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<ResultViewModel> 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();