Skip to content

Commit 9167cba

Browse files
committed
Use selected item for binding preview properties & Code cleanup
1 parent dda008f commit 9167cba

File tree

4 files changed

+43
-20
lines changed

4 files changed

+43
-20
lines changed

Flow.Launcher/MainWindow.xaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -441,7 +441,7 @@
441441
<Border
442442
MinHeight="380"
443443
d:DataContext="{d:DesignInstance vm:ResultViewModel}"
444-
DataContext="{Binding SelectedItem, ElementName=ResultListBox}"
444+
DataContext="{Binding SelectedItem, Mode=OneWay}"
445445
Visibility="{Binding ShowDefaultPreview}">
446446
<Grid
447447
Margin="0 0 10 5"
@@ -518,7 +518,7 @@
518518
MaxHeight="{Binding ElementName=ResultListBox, Path=ActualHeight}"
519519
Padding="0 0 10 10"
520520
d:DataContext="{d:DesignInstance vm:ResultViewModel}"
521-
DataContext="{Binding SelectedItem, ElementName=ResultListBox}"
521+
DataContext="{Binding SelectedItem, Mode=OneWay}"
522522
Visibility="{Binding ShowCustomizedPreview}">
523523
<ContentControl Content="{Binding Result.PreviewPanel.Value}" />
524524
</Border>

Flow.Launcher/ViewModel/MainViewModel.cs

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,7 @@ public MainViewModel()
162162
switch (args.PropertyName)
163163
{
164164
case nameof(Results.SelectedItem):
165+
SelectedItem = Results.SelectedItem;
165166
UpdatePreview();
166167
break;
167168
}
@@ -786,6 +787,18 @@ private static string VerifyOrSetDefaultHotkey(string hotkey, string defaultHotk
786787

787788
#region Preview
788789

790+
private ResultViewModel _selectedItem;
791+
792+
public ResultViewModel SelectedItem
793+
{
794+
get => _selectedItem;
795+
set
796+
{
797+
_selectedItem = value;
798+
OnPropertyChanged();
799+
}
800+
}
801+
789802
public bool InternalPreviewVisible
790803
{
791804
get
@@ -884,7 +897,7 @@ private static void SwitchExternalPreview(string path, bool sendFailToast = true
884897
private void ShowInternalPreview()
885898
{
886899
ResultAreaColumn = ResultAreaColumnPreviewShown;
887-
Results.SelectedItem?.LoadPreviewImage();
900+
SelectedItem?.LoadPreviewImage();
888901
}
889902

890903
private void HideInternalPreview()
@@ -939,14 +952,14 @@ when CanExternalPreviewSelectedResult(out var path):
939952

940953
case false
941954
when InternalPreviewVisible:
942-
Results.SelectedItem?.LoadPreviewImage();
955+
SelectedItem?.LoadPreviewImage();
943956
break;
944957
}
945958
}
946959

947960
private bool CanExternalPreviewSelectedResult(out string path)
948961
{
949-
path = Results.SelectedItem?.Result?.Preview.FilePath;
962+
path = SelectedItem?.Result?.Preview.FilePath;
950963
return !string.IsNullOrEmpty(path);
951964
}
952965

@@ -976,7 +989,7 @@ private void QueryContextMenu()
976989
var query = QueryText.ToLower().Trim();
977990
ContextMenu.Clear();
978991

979-
var selected = Results.SelectedItem?.Result;
992+
var selected = SelectedItem?.Result;
980993

981994
if (selected != null) // SelectedItem returns null if selection is empty.
982995
{

Flow.Launcher/ViewModel/ResultViewModel.cs

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
11
using System;
2+
using System.Collections.Generic;
3+
using System.Drawing.Text;
4+
using System.IO;
25
using System.Threading.Tasks;
36
using System.Windows;
47
using System.Windows.Media;
58
using Flow.Launcher.Infrastructure.Image;
69
using Flow.Launcher.Infrastructure.Logger;
710
using Flow.Launcher.Infrastructure.UserSettings;
811
using Flow.Launcher.Plugin;
9-
using System.IO;
10-
using System.Drawing.Text;
11-
using System.Collections.Generic;
1212

1313
namespace Flow.Launcher.ViewModel
1414
{
1515
public class ResultViewModel : BaseModel
1616
{
17-
private static PrivateFontCollection fontCollection = new();
18-
private static Dictionary<string, string> fonts = new();
17+
private static readonly PrivateFontCollection fontCollection = new();
18+
private static readonly Dictionary<string, string> fonts = new();
1919

2020
public ResultViewModel(Result result, Settings settings)
2121
{
@@ -39,11 +39,11 @@ public ResultViewModel(Result result, Settings settings)
3939
fontFamilyPath = Path.Combine(Result.PluginDirectory, fontFamilyPath);
4040
}
4141

42-
if (fonts.ContainsKey(fontFamilyPath))
42+
if (fonts.TryGetValue(fontFamilyPath, out var value))
4343
{
4444
Glyph = glyph with
4545
{
46-
FontFamily = fonts[fontFamilyPath]
46+
FontFamily = value
4747
};
4848
}
4949
else
@@ -171,7 +171,16 @@ public ImageSource Image
171171

172172
public ImageSource PreviewImage
173173
{
174-
get => previewImage;
174+
get
175+
{
176+
if (!PreviewImageLoaded)
177+
{
178+
PreviewImageLoaded = true;
179+
_ = LoadPreviewImageAsync();
180+
}
181+
182+
return previewImage;
183+
}
175184
private set => previewImage = value;
176185
}
177186

Flow.Launcher/ViewModel/ResultsViewModel.cs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
using System;
2-
using Flow.Launcher.Infrastructure.UserSettings;
3-
using Flow.Launcher.Plugin;
42
using System.Collections.Generic;
53
using System.Collections.Specialized;
64
using System.Linq;
@@ -10,6 +8,8 @@
108
using System.Windows.Data;
119
using System.Windows.Documents;
1210
using System.Windows.Input;
11+
using Flow.Launcher.Infrastructure.UserSettings;
12+
using Flow.Launcher.Plugin;
1313

1414
namespace Flow.Launcher.ViewModel
1515
{
@@ -219,7 +219,6 @@ private List<ResultViewModel> NewResults(List<Result> newRawResults, string resu
219219
if (newRawResults.Count == 0)
220220
return Results;
221221

222-
223222
var newResults = newRawResults.Select(r => new ResultViewModel(r, _settings));
224223

225224
return Results.Where(r => r.Result.PluginID != resultId)
@@ -241,6 +240,7 @@ private List<ResultViewModel> NewResults(ICollection<ResultsForUpdate> resultsFo
241240
#endregion
242241

243242
#region FormattedText Dependency Property
243+
244244
public static readonly DependencyProperty FormattedTextProperty = DependencyProperty.RegisterAttached(
245245
"FormattedText",
246246
typeof(Inline),
@@ -259,8 +259,7 @@ public static Inline GetFormattedText(DependencyObject textBlock)
259259

260260
private static void FormattedTextPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
261261
{
262-
var textBlock = d as TextBlock;
263-
if (textBlock == null) return;
262+
if (d is not TextBlock textBlock) return;
264263

265264
var inline = (Inline)e.NewValue;
266265

@@ -269,6 +268,7 @@ private static void FormattedTextPropertyChanged(DependencyObject d, DependencyP
269268

270269
textBlock.Inlines.Add(inline);
271270
}
271+
272272
#endregion
273273

274274
public class ResultCollection : List<ResultViewModel>, INotifyCollectionChanged
@@ -279,7 +279,6 @@ public class ResultCollection : List<ResultViewModel>, INotifyCollectionChanged
279279

280280
public event NotifyCollectionChangedEventHandler CollectionChanged;
281281

282-
283282
protected void OnCollectionChanged(NotifyCollectionChangedEventArgs e)
284283
{
285284
CollectionChanged?.Invoke(this, e);
@@ -297,6 +296,7 @@ public void BulkAddAll(List<ResultViewModel> resultViews)
297296
// wpf use DirectX / double buffered already, so just reset all won't cause ui flickering
298297
OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));
299298
}
299+
300300
private void AddAll(List<ResultViewModel> Items)
301301
{
302302
for (int i = 0; i < Items.Count; i++)
@@ -308,6 +308,7 @@ private void AddAll(List<ResultViewModel> Items)
308308
OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, item, i));
309309
}
310310
}
311+
311312
public void RemoveAll(int Capacity = 512)
312313
{
313314
Clear();

0 commit comments

Comments
 (0)