Skip to content

Commit 4aff4a2

Browse files
committed
More code cleanup & improvements
1 parent bb03dfe commit 4aff4a2

4 files changed

Lines changed: 27 additions & 52 deletions

File tree

Flow.Launcher.Infrastructure/Image/ImageLoader.cs

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ public static class ImageLoader
3434
public const int FullImageSize = 320;
3535

3636
private static readonly string[] ImageExtensions = [".png", ".jpg", ".jpeg", ".gif", ".bmp", ".tiff", ".ico"];
37-
private static readonly string SvgExtension = ".svg";
3837

3938
public static async Task InitializeAsync()
4039
{
@@ -93,16 +92,6 @@ private static async ValueTask<ImageResult> LoadInternalAsync(string path, bool
9392
}
9493
}
9594

96-
if (Uri.TryCreate(path, UriKind.RelativeOrAbsolute, out var uriResult)
97-
&& (uriResult.Scheme == Uri.UriSchemeHttp || uriResult.Scheme == Uri.UriSchemeHttps))
98-
{
99-
Log.Error(ClassName, $"Failed to load image from path {path}: Remote images are not supported.");
100-
101-
ImageSource image = MissingImage;
102-
ImageCache[path, false] = image;
103-
imageResult = new ImageResult(image, ImageType.Error);
104-
}
105-
10695
if (path.StartsWith("data:image", StringComparison.OrdinalIgnoreCase))
10796
{
10897
throw new NotSupportedException();
@@ -180,12 +169,6 @@ private static ImageResult GetThumbnailResult(ref string path, bool loadFullImag
180169
image = GetThumbnail(path, ThumbnailOptions.ThumbnailOnly);
181170
}
182171
}
183-
else if (extension == SvgExtension)
184-
{
185-
Log.Error(ClassName, $"Failed to load SVG image from path {path}: SVG images aren't supported.");
186-
image = Image;
187-
type = ImageType.Error;
188-
}
189172
else
190173
{
191174
type = ImageType.File;

Flow.Launcher/ViewModel/MainViewModel.cs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.Collections.Generic;
33
using System.ComponentModel;
4+
using System.Diagnostics.CodeAnalysis;
45
using System.Linq;
56
using System.Text;
67
using System.Threading;
@@ -695,16 +696,16 @@ public double ResultSubItemFontSize
695696
set => Settings.ResultSubItemFontSize = value;
696697
}
697698

698-
public ImageSource PluginIconSource { get; private set; } = null;
699+
public ImageSource? PluginIconSource { get; private set; } = null;
699700

700-
public string PluginIconPath { get; set; } = null;
701+
public string? PluginIconPath { get; set; } = null;
701702

702703
private static string VerifyOrSetDefaultHotkey(string hotkey, string defaultHotkey)
703704
{
704705
try
705706
{
706707
var converter = new KeyGestureConverter();
707-
var key = (KeyGesture)converter.ConvertFromString(hotkey);
708+
var key = (KeyGesture?)converter.ConvertFromString(hotkey);
708709
}
709710
catch (Exception e) when (e is NotSupportedException || e is InvalidEnumArgumentException)
710711
{
@@ -763,7 +764,7 @@ public bool InternalPreviewVisible
763764
}
764765
}
765766

766-
public Control PreviewContent
767+
public Control? PreviewContent
767768
{
768769
get
769770
{
@@ -918,9 +919,9 @@ when CanExternalPreviewSelectedResult(out var path):
918919
}
919920
}
920921

921-
private bool CanExternalPreviewSelectedResult(out string path)
922+
private bool CanExternalPreviewSelectedResult([NotNullWhen(true)] out string? path)
922923
{
923-
path = QueryResultsPreviewed() ? _results.SelectedItem?.Result?.Preview.FilePath : string.Empty;
924+
path = QueryResultsPreviewed() ? _results.SelectedItem?.Result.Preview.FilePath : string.Empty;
924925
return !string.IsNullOrEmpty(path);
925926
}
926927

Flow.Launcher/ViewModel/ResultViewModel.cs

Lines changed: 15 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,6 @@ public class ResultViewModel : BaseModel
2121
public ResultViewModel(Result result, Settings settings)
2222
{
2323
Settings = settings;
24-
25-
if (result == null) return;
26-
2724
Result = result;
2825

2926
if (Result.Glyph is { FontFamily: not null } glyph)
@@ -163,9 +160,9 @@ public ImageSource PreviewImage
163160
/// </summary>
164161
public bool UseBigThumbnail => Result.Preview.IsMedia;
165162

166-
public GlyphInfo Glyph { get; set; }
163+
public GlyphInfo? Glyph { get; set; }
167164

168-
private async Task<ImageSource> LoadImageInternalAsync(string imagePath, Result.IconDelegate icon, bool loadFullImage)
165+
private async Task<ImageSource> LoadImageInternalAsync(string? imagePath, Result.IconDelegate? icon, bool loadFullImage)
169166
{
170167
if (string.IsNullOrEmpty(imagePath) && icon != null)
171168
{
@@ -181,6 +178,7 @@ private async Task<ImageSource> LoadImageInternalAsync(string imagePath, Result.
181178
}
182179
}
183180

181+
imagePath ??= string.Empty;
184182
return await App.API.LoadImageAsync(imagePath, loadFullImage).ConfigureAwait(false);
185183
}
186184

@@ -189,33 +187,29 @@ private async Task LoadImageAsync()
189187
var imagePath = Result.IcoPath;
190188
var iconDelegate = Result.Icon;
191189

192-
if (imagePath is null && iconDelegate is null)
193-
return;
194-
195-
if (ImageLoader.TryGetValue(imagePath, false, out var img))
190+
if (imagePath is not null && ImageLoader.TryGetValue(imagePath, false, out var img))
196191
{
197192
_image = img;
193+
return;
198194
}
199-
else
200-
{
201-
// We need to modify the property not field here to trigger the OnPropertyChanged event
202-
Image = await LoadImageInternalAsync(imagePath, iconDelegate, false).ConfigureAwait(false);
203-
}
195+
196+
// We need to modify the property not field here to trigger the OnPropertyChanged event
197+
Image = await LoadImageInternalAsync(imagePath, iconDelegate, false).ConfigureAwait(false);
204198
}
205199

206200
private async Task LoadPreviewImageAsync()
207201
{
208202
var imagePath = Result.Preview.PreviewImagePath ?? Result.IcoPath;
209203
var iconDelegate = Result.Preview.PreviewDelegate ?? Result.Icon;
210-
if (ImageLoader.TryGetValue(imagePath, true, out var img))
204+
205+
if (imagePath is not null && ImageLoader.TryGetValue(imagePath, true, out var img))
211206
{
212207
_previewImage = img;
208+
return;
213209
}
214-
else
215-
{
216-
// We need to modify the property not field here to trigger the OnPropertyChanged event
217-
PreviewImage = await LoadImageInternalAsync(imagePath, iconDelegate, true).ConfigureAwait(false);
218-
}
210+
211+
// We need to modify the property not field here to trigger the OnPropertyChanged event
212+
PreviewImage = await LoadImageInternalAsync(imagePath, iconDelegate, true).ConfigureAwait(false);
219213
}
220214

221215
public void LoadPreviewImage()
@@ -229,7 +223,7 @@ public void LoadPreviewImage()
229223

230224
public Result Result { get; }
231225

232-
public override bool Equals(object obj)
226+
public override bool Equals(object? obj)
233227
{
234228
return obj is ResultViewModel r && Result.Equals(r.Result);
235229
}

Flow.Launcher/ViewModel/ResultsViewModel.cs

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,11 @@ public class ResultsViewModel : BaseModel
2222
private readonly MainViewModel _mainVM;
2323
private int MaxResults => _settings?.MaxResultsToShow ?? 6;
2424

25-
public ResultsViewModel()
25+
public ResultsViewModel(Settings settings, MainViewModel mainVM)
2626
{
2727
Results = [];
2828
BindingOperations.EnableCollectionSynchronization(Results, _collectionLock);
29-
}
3029

31-
public ResultsViewModel(Settings settings, MainViewModel mainVM) : this()
32-
{
3330
_settings = settings;
3431
_mainVM = mainVM;
3532
_settings.PropertyChanged += (s, e) =>
@@ -74,12 +71,12 @@ public double ItemHeightSize
7471

7572
public int SelectedIndex { get; set; }
7673

77-
public ResultViewModel SelectedItem { get; set; }
74+
public ResultViewModel? SelectedItem { get; set; }
7875
public Thickness Margin { get; set; }
7976
public Visibility Visibility { get; set; } = Visibility.Collapsed;
8077

81-
public ICommand RightClickResultCommand { get; init; }
82-
public ICommand LeftClickResultCommand { get; init; }
78+
public required ICommand RightClickResultCommand { get; init; }
79+
public required ICommand LeftClickResultCommand { get; init; }
8380

8481
private int NewIndex(int i)
8582
{
@@ -221,7 +218,7 @@ public class ResultCollection : List<ResultViewModel>, INotifyCollectionChanged
221218
{
222219
private long editTime = 0;
223220

224-
public event NotifyCollectionChangedEventHandler CollectionChanged;
221+
public event NotifyCollectionChangedEventHandler? CollectionChanged;
225222

226223
protected void OnCollectionChanged(NotifyCollectionChangedEventArgs e)
227224
{

0 commit comments

Comments
 (0)