Skip to content

Commit 7d1be78

Browse files
authored
Code cleanup (#2313)
1 parent 3c5a024 commit 7d1be78

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

59 files changed

+1643
-1017
lines changed

Common/Extensions.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,12 @@ public static IEnumerable<TSource> ExceptBy<TSource, TKey>(
1414
{
1515
var set = new HashSet<TKey>(other.Select(keySelector));
1616
foreach (var item in source)
17+
{
1718
if (set.Add(keySelector(item)))
19+
{
1820
yield return item;
21+
}
22+
}
1923
}
2024

2125
public static IEnumerable<TSource> IntersectBy<TSource, TKey>(
@@ -26,11 +30,13 @@ public static IEnumerable<TSource> IntersectBy<TSource, TKey>(
2630
return source.Join(other.Select(keySelector), keySelector, id => id, (o, id) => o);
2731
}
2832

29-
public static TOut Get<TOut, TKey, TValue>(this IDictionary<TKey, TValue> dictionary, TKey key, TOut defaultValue = default(TOut))
33+
public static TOut Get<TOut, TKey, TValue>(this IDictionary<TKey, TValue> dictionary, TKey key, TOut defaultValue = default)
3034
{
3135
// If setting doesn't exist, create it.
3236
if (!dictionary.ContainsKey(key))
37+
{
3338
dictionary[key] = (TValue)(object)defaultValue;
39+
}
3440

3541
return (TOut)(object)dictionary[key];
3642
}
@@ -66,7 +72,7 @@ public static async Task<T> WithTimeoutAsync<T>(this Task<T> task,
6672
{
6773
return await task;
6874
}
69-
return default(T);
75+
return default;
7076
}
7177
}
7278
}

Files.Launcher/Program.cs

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -120,10 +120,12 @@ private static async void Watcher_Changed(object sender, FileSystemEventArgs e)
120120
}
121121
if (connection != null)
122122
{
123-
var response = new ValueSet() {
123+
var response = new ValueSet()
124+
{
124125
{ "FileSystem", @"Shell:RecycleBinFolder" },
125126
{ "Path", e.FullPath },
126-
{ "Type", e.ChangeType.ToString() } };
127+
{ "Type", e.ChangeType.ToString() }
128+
};
127129
if (e.ChangeType == WatcherChangeTypes.Created)
128130
{
129131
using var folderItem = new ShellItem(e.FullPath);
@@ -299,9 +301,11 @@ private static async Task ParseArgumentsAsync(AppServiceRequestReceivedEventArgs
299301
case "GetIconOverlay":
300302
var fileIconPath = (string)args.Request.Message["filePath"];
301303
var iconOverlay = Win32API.GetFileOverlayIcon(fileIconPath);
302-
await args.Request.SendResponseAsync(new ValueSet() {
304+
await args.Request.SendResponseAsync(new ValueSet()
305+
{
303306
{ "IconOverlay", iconOverlay.icon },
304-
{ "HasCustomIcon", iconOverlay.isCustom } });
307+
{ "HasCustomIcon", iconOverlay.isCustom }
308+
});
305309
break;
306310

307311
default:
@@ -364,7 +368,8 @@ private static object HandleMenuMessage(ValueSet message, Win32API.DisposableDic
364368

365369
private static Func<string, bool> FilterMenuItems(bool showOpenMenu)
366370
{
367-
var knownItems = new List<string>() {
371+
var knownItems = new List<string>()
372+
{
368373
"opennew", "openas", "opencontaining", "opennewprocess",
369374
"runas", "runasuser", "pintohome", "PinToStartScreen",
370375
"cut", "copy", "paste", "delete", "properties", "link",
@@ -433,7 +438,8 @@ await Win32API.StartSTATask(() =>
433438
if (linkPath.EndsWith(".lnk"))
434439
{
435440
using var link = new ShellLink(linkPath, LinkResolution.NoUIWithMsgPump, null, TimeSpan.FromMilliseconds(100));
436-
await args.Request.SendResponseAsync(new ValueSet() {
441+
await args.Request.SendResponseAsync(new ValueSet()
442+
{
437443
{ "TargetPath", link.TargetPath },
438444
{ "Arguments", link.Arguments },
439445
{ "WorkingDirectory", link.WorkingDirectory },
@@ -450,7 +456,8 @@ await args.Request.SendResponseAsync(new ValueSet() {
450456
ipf.GetUrl(out var retVal);
451457
return retVal;
452458
});
453-
await args.Request.SendResponseAsync(new ValueSet() {
459+
await args.Request.SendResponseAsync(new ValueSet()
460+
{
454461
{ "TargetPath", linkUrl },
455462
{ "Arguments", null },
456463
{ "WorkingDirectory", null },
@@ -463,7 +470,8 @@ await args.Request.SendResponseAsync(new ValueSet() {
463470
{
464471
// Could not parse shortcut
465472
Logger.Warn(ex, ex.Message);
466-
await args.Request.SendResponseAsync(new ValueSet() {
473+
await args.Request.SendResponseAsync(new ValueSet()
474+
{
467475
{ "TargetPath", null },
468476
{ "Arguments", null },
469477
{ "WorkingDirectory", null },

Files.Launcher/Win32API.cs

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -97,18 +97,34 @@ public static (string icon, bool isCustom) GetFileOverlayIcon(string path)
9797
{
9898
var shfi = new Shell32.SHFILEINFO();
9999
var ret = Shell32.SHGetFileInfo(path, 0, ref shfi, Shell32.SHFILEINFO.Size, Shell32.SHGFI.SHGFI_OVERLAYINDEX | Shell32.SHGFI.SHGFI_ICON | Shell32.SHGFI.SHGFI_SYSICONINDEX | Shell32.SHGFI.SHGFI_ICONLOCATION);
100-
if (ret == IntPtr.Zero) return (null, false);
100+
if (ret == IntPtr.Zero)
101+
{
102+
return (null, false);
103+
}
104+
101105
bool isCustom = !shfi.szDisplayName.StartsWith(Environment.GetFolderPath(Environment.SpecialFolder.Windows));
102106
User32.DestroyIcon(shfi.hIcon);
103107
Shell32.SHGetImageList(Shell32.SHIL.SHIL_LARGE, typeof(ComCtl32.IImageList).GUID, out var tmp);
104108
using var imageList = ComCtl32.SafeHIMAGELIST.FromIImageList(tmp);
105-
if (imageList.IsNull || imageList.IsInvalid) return (null, isCustom);
109+
if (imageList.IsNull || imageList.IsInvalid)
110+
{
111+
return (null, isCustom);
112+
}
113+
106114
var overlay_idx = shfi.iIcon >> 24;
107115
//var icon_idx = shfi.iIcon & 0xFFFFFF;
108-
if (overlay_idx == 0) return (null, isCustom);
116+
if (overlay_idx == 0)
117+
{
118+
return (null, isCustom);
119+
}
120+
109121
var overlay_image = imageList.Interface.GetOverlayImage(overlay_idx);
110122
using var hIcon = imageList.Interface.GetIcon(overlay_image, ComCtl32.IMAGELISTDRAWFLAGS.ILD_TRANSPARENT);
111-
if (hIcon.IsNull || hIcon.IsInvalid) return (null, isCustom);
123+
if (hIcon.IsNull || hIcon.IsInvalid)
124+
{
125+
return (null, isCustom);
126+
}
127+
112128
using var image = hIcon.ToIcon().ToBitmap();
113129
byte[] bitmapData = (byte[])new ImageConverter().ConvertTo(image, typeof(byte[]));
114130
return (Convert.ToBase64String(bitmapData, 0, bitmapData.Length), isCustom);

Files.Launcher/Win32API_ContextMenu.cs

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,15 +87,20 @@ public string AddValue(object obj)
8787
{
8888
string key = Guid.NewGuid().ToString();
8989
if (!_dict.TryAdd(key, obj))
90+
{
9091
throw new ArgumentException("Could not create handle: key exists");
92+
}
93+
9194
return key;
9295
}
9396

9497
public void SetValue(string key, object obj)
9598
{
9699
RemoveValue(key);
97100
if (!_dict.TryAdd(key, obj))
101+
{
98102
throw new ArgumentException("Could not create handle: key exists");
103+
}
99104
}
100105

101106
public object GetValue(string key)
@@ -142,7 +147,11 @@ public ContextMenu(Shell32.IContextMenu cMenu, User32.SafeHMENU hMenu, IEnumerab
142147

143148
public void InvokeVerb(string verb)
144149
{
145-
if (string.IsNullOrEmpty(verb)) return;
150+
if (string.IsNullOrEmpty(verb))
151+
{
152+
return;
153+
}
154+
146155
try
147156
{
148157
var pici = new Shell32.CMINVOKECOMMANDINFOEX();
@@ -161,7 +170,11 @@ ex is COMException
161170

162171
public void InvokeItem(int itemID)
163172
{
164-
if (itemID < 0) return;
173+
if (itemID < 0)
174+
{
175+
return;
176+
}
177+
165178
try
166179
{
167180
var pici = new Shell32.CMINVOKECOMMANDINFOEX();
@@ -186,7 +199,10 @@ public static ContextMenu GetContextMenuForFiles(string[] filePathList, Shell32.
186199
try
187200
{
188201
foreach (var fp in filePathList.Where(x => !string.IsNullOrEmpty(x)))
202+
{
189203
shellItems.Add(new ShellItem(fp));
204+
}
205+
190206
return GetContextMenuForFiles(shellItems.ToArray(), flags, itemFilter);
191207
}
192208
catch (ArgumentException)
@@ -197,14 +213,19 @@ public static ContextMenu GetContextMenuForFiles(string[] filePathList, Shell32.
197213
finally
198214
{
199215
foreach (var si in shellItems)
216+
{
200217
si.Dispose();
218+
}
201219
}
202220
}
203221

204222
private static ContextMenu GetContextMenuForFiles(ShellItem[] shellItems, Shell32.CMF flags, Func<string, bool> itemFilter = null)
205223
{
206224
if (shellItems == null || !shellItems.Any())
225+
{
207226
return null;
227+
}
228+
208229
using var sf = shellItems.First().Parent; // HP: the items are all in the same folder
209230
Shell32.IContextMenu menu = sf.GetChildrenUIObjects<Shell32.IContextMenu>(null, shellItems);
210231
var hMenu = User32.CreatePopupMenu();
@@ -324,10 +345,14 @@ private static Bitmap GetBitmapFromHBitmap(HBITMAP hBitmap)
324345
Bitmap bmp = hBitmap.ToBitmap();
325346

326347
if (Bitmap.GetPixelFormatSize(bmp.PixelFormat) < 32)
348+
{
327349
return bmp;
350+
}
328351

329352
if (IsAlphaBitmap(bmp, out var bmpData))
353+
{
330354
return GetAlphaBitmapFromBitmapData(bmpData);
355+
}
331356

332357
return bmp;
333358
}
@@ -386,7 +411,10 @@ protected virtual void Dispose(bool disposing)
386411
if (Items != null)
387412
{
388413
foreach (var si in Items)
414+
{
389415
(si as IDisposable)?.Dispose();
416+
}
417+
390418
Items = null;
391419
}
392420
}
@@ -451,7 +479,10 @@ public void Dispose()
451479
if (SubItems != null)
452480
{
453481
foreach (var si in SubItems)
482+
{
454483
(si as IDisposable)?.Dispose();
484+
}
485+
455486
SubItems = null;
456487
}
457488
}

Files/App.xaml.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -372,7 +372,10 @@ private static void AppUnhandledException(Exception ex)
372372

373373
public static async void CloseApp()
374374
{
375-
if (!await ApplicationView.GetForCurrentView().TryConsolidateAsync()) Application.Current.Exit();
375+
if (!await ApplicationView.GetForCurrentView().TryConsolidateAsync())
376+
{
377+
Application.Current.Exit();
378+
}
376379
}
377380
}
378381

0 commit comments

Comments
 (0)