Skip to content

Commit

Permalink
Fix: Backported recent fixes into the servicing branch (#12338)
Browse files Browse the repository at this point in the history
  • Loading branch information
yaira2 committed May 12, 2023
2 parents d31a0bd + 305b1b9 commit 4863423
Show file tree
Hide file tree
Showing 10 changed files with 83 additions and 35 deletions.
2 changes: 1 addition & 1 deletion src/Files.App/BaseLayout.cs
Expand Up @@ -218,7 +218,7 @@ internal set
//if (!(value?.All(x => selectedItems?.Contains(x) ?? false) ?? value == selectedItems))
if (value != selectedItems)
{
if (value?.FirstOrDefault() != selectedItems?.FirstOrDefault())
if (value?.FirstOrDefault() != PreviewPaneViewModel.SelectedItem)
{
// Update preview pane properties
PreviewPaneViewModel.IsItemSelected = value?.Count > 0;
Expand Down
4 changes: 3 additions & 1 deletion src/Files.App/DataModels/SidebarPinnedModel.cs
Expand Up @@ -115,7 +115,9 @@ public async Task<LocationItem> CreateLocationItemFromPathAsync(string path)
{
var iconData = await FileThumbnailHelper.LoadIconFromStorageItemAsync(res.Result, 96u, ThumbnailMode.ListView);
locationItem.IconData = iconData;
locationItem.Icon = await App.Window.DispatcherQueue.EnqueueOrInvokeAsync(() => locationItem.IconData.ToBitmapAsync());

if (locationItem.IconData is not null)
locationItem.Icon = await App.Window.DispatcherQueue.EnqueueOrInvokeAsync(() => locationItem.IconData.ToBitmapAsync());
}

if (locationItem.IconData is null)
Expand Down
21 changes: 14 additions & 7 deletions src/Files.App/Helpers/BitmapHelper.cs
Expand Up @@ -19,15 +19,22 @@ public static async Task<BitmapImage> ToBitmapAsync(this byte[]? data, int decod
return null;
}

using var ms = new MemoryStream(data);
var image = new BitmapImage();
if (decodeSize > 0)
try
{
using var ms = new MemoryStream(data);
var image = new BitmapImage();
if (decodeSize > 0)
{
image.DecodePixelWidth = decodeSize;
image.DecodePixelHeight = decodeSize;
}
await image.SetSourceAsync(ms.AsRandomAccessStream());
return image;
}
catch (Exception)
{
image.DecodePixelWidth = decodeSize;
image.DecodePixelHeight = decodeSize;
return null;
}
await image.SetSourceAsync(ms.AsRandomAccessStream());
return image;
}

/// <summary>
Expand Down
2 changes: 1 addition & 1 deletion src/Files.App/Helpers/FileOperationsHelpers.cs
Expand Up @@ -522,7 +522,7 @@ public static void TryCancelOperation(string operationId)
{
Name = x.ProcessName,
Pid = x.Id,
FileName = x.MainModule?.FileName,
FileName = SafetyExtensions.IgnoreExceptions(() => x.MainModule?.FileName),
AppName = SafetyExtensions.IgnoreExceptions(() => x.MainModule?.FileVersionInfo?.FileDescription)
}).ToList();
processes.ForEach(x => x.Dispose());
Expand Down
Expand Up @@ -332,6 +332,7 @@
"SectionResource": "Document",
"Property": "System.Document.TotalEditingTime",
"IsReadOnly": true,
"DisplayFunctionName": "FormatDuration",
"ID": null
},
{
Expand Down
1 change: 1 addition & 0 deletions src/Files.App/Resources/PropertiesInformation.json
Expand Up @@ -434,6 +434,7 @@
"SectionResource": "Document",
"Property": "System.Document.TotalEditingTime",
"IsReadOnly": true,
"DisplayFunctionName": "FormatDuration",
"ID": null
},
{
Expand Down
14 changes: 11 additions & 3 deletions src/Files.App/Shell/ContextMenu.cs
Expand Up @@ -52,6 +52,11 @@ public async Task<bool> InvokeVerb(string? verb)
if (string.IsNullOrEmpty(verb))
return false;

var item = Items.Where(x => x.CommandString == verb).FirstOrDefault();
if (item is not null && item.ID >= 0)
// Prefer invocation by ID
return await InvokeItem(item.ID);

try
{
var currentWindows = Win32API.GetDesktopWindows();
Expand All @@ -76,10 +81,10 @@ public async Task<bool> InvokeVerb(string? verb)
return false;
}

public async Task InvokeItem(int itemID)
public async Task<bool> InvokeItem(int itemID)
{
if (itemID < 0)
return;
return false;

try
{
Expand All @@ -93,13 +98,16 @@ public async Task InvokeItem(int itemID)
pici.cbSize = (uint)Marshal.SizeOf(pici);

await owningThread.PostMethod(() => cMenu.InvokeCommand(pici));

Win32API.BringToForeground(currentWindows);

return true;
}
catch (Exception ex) when (ex is COMException or UnauthorizedAccessException)
{
Debug.WriteLine(ex);
}

return false;
}

#region FactoryMethods
Expand Down
14 changes: 7 additions & 7 deletions src/Files.App/ViewModels/ItemViewModel.cs
Expand Up @@ -104,7 +104,7 @@ public ListedItem CurrentFolder

public string WorkingDirectory { get; private set; }

private StorageFolderWithPath currentStorageFolder;
private StorageFolderWithPath? currentStorageFolder;
private StorageFolderWithPath workingRoot;

public delegate void WorkingDirectoryModifiedEventHandler(object sender, WorkingDirectoryModifiedEventArgs e);
Expand Down Expand Up @@ -1400,7 +1400,7 @@ public async Task EnumerateItemsFromSpecialFolderAsync(string path)
{
var isFtp = FtpHelpers.IsFtpPath(path);

CurrentFolder = new ListedItem(null)
CurrentFolder = new ListedItem(null!)
{
PrimaryItemAttribute = StorageItemTypes.Folder,
ItemPropertiesInitialized = true,
Expand Down Expand Up @@ -1569,22 +1569,22 @@ public async Task<int> EnumerateItemsFromStandardFolderAsync(string path, Cancel
if (enumFromStorageFolder)
{
var basicProps = await rootFolder?.GetBasicPropertiesAsync();
var currentFolder = library ?? new ListedItem(rootFolder.FolderRelativeId)
var currentFolder = library ?? new ListedItem(rootFolder?.FolderRelativeId ?? string.Empty)
{
PrimaryItemAttribute = StorageItemTypes.Folder,
ItemPropertiesInitialized = true,
ItemNameRaw = rootFolder.DisplayName,
ItemNameRaw = rootFolder?.DisplayName ?? string.Empty,
ItemDateModifiedReal = basicProps.DateModified,
ItemType = rootFolder.DisplayType,
ItemType = rootFolder?.DisplayType ?? string.Empty,
FileImage = null,
LoadFileIcon = false,
ItemPath = string.IsNullOrEmpty(rootFolder.Path) ? currentStorageFolder.Path : rootFolder.Path,
ItemPath = string.IsNullOrEmpty(rootFolder?.Path) ? currentStorageFolder?.Path ?? string.Empty : rootFolder.Path,
FileSize = null,
FileSizeBytes = 0,
};

if (library is null)
currentFolder.ItemDateCreatedReal = rootFolder.DateCreated;
currentFolder.ItemDateCreatedReal = rootFolder?.DateCreated ?? DateTimeOffset.Now;

CurrentFolder = currentFolder;
await EnumFromStorageFolderAsync(path, rootFolder, currentStorageFolder, cancellationToken);
Expand Down
5 changes: 4 additions & 1 deletion src/Files.App/ViewModels/Properties/FileProperty.cs
Expand Up @@ -324,12 +324,15 @@ public async static Task<List<FileProperty>> RetrieveAndInitializePropertiesAsyn
private static readonly Dictionary<string, Func<object, string>> DisplayFuncs = new Dictionary<string, Func<object, string>>()
{
{ "DivideBy1000", input => (((uint) input)/1000).ToString() },
{ "FormatDuration", input => new TimeSpan(Convert.ToInt64(input)).ToString("hh':'mm':'ss")},
{ "FormatDuration", input => TimeSpanToString(new TimeSpan(Convert.ToInt64(input)))},
{ "Fraction" , input => ((double)input).ToFractions(2000)},
{ "AddF" , input => $"f/{(double)input}"},
{ "AddISO" , input => $"ISO-{(UInt16)input}"},
{ "RoundDouble" , input => $"{Math.Round((double)input)}"},
{ "UnitMM" , input => $"{(double)input} mm"},
};

private static string TimeSpanToString(TimeSpan t)
=> t.Days > 0 ? (t.Days * 24 + t.Hours) + t.ToString("':'mm':'ss") : t.ToString("hh':'mm':'ss");
}
}
54 changes: 40 additions & 14 deletions src/Files.Shared/Extensions/LinqExtensions.cs
Expand Up @@ -22,17 +22,33 @@ public static class LinqExtensions
/// <returns></returns>
public static bool IsEmpty<T>(this IEnumerable<T> enumerable) => enumerable is null || !enumerable.Any();

public static TOut? Get<TOut, TKey, TValue>(this IDictionary<TKey, TValue> dictionary, TKey key, TOut? defaultValue = default)
public static TOut? Get<TOut, TKey, TValue>(this IDictionary<TKey, TValue> dictionary, TKey key, TOut? defaultValue = default) where TKey : notnull
{
if (dictionary is null || key is null)
return defaultValue;

if (!dictionary.ContainsKey(key))
if (dictionary is ConcurrentDictionary<TKey, TValue> cDict)
{
if (defaultValue is TValue value)
dictionary.Add(key, value);
if (!cDict.ContainsKey(key))
{
if (defaultValue is TValue value)
cDict.TryAdd(key, value);

return defaultValue;
return defaultValue;
}
}
else
{
lock (dictionary)
{
if (!dictionary.ContainsKey(key))
{
if (defaultValue is TValue value)
dictionary.Add(key, value);

return defaultValue;
}
}
}

if (dictionary[key] is TOut o)
Expand All @@ -46,22 +62,32 @@ public static class LinqExtensions
if (dictionary is null || key is null)
return defaultValueFunc();

if (!dictionary.ContainsKey(key))
if (dictionary is ConcurrentDictionary<TKey, Task<TValue?>> cDict)
{
var defaultValue = defaultValueFunc();
if (defaultValue is Task<TValue?> value)
if (!cDict.ContainsKey(key))
{
if (dictionary is ConcurrentDictionary<TKey, Task<TValue?>> cDict)
{
var defaultValue = defaultValueFunc();
if (defaultValue is Task<TValue?> value)
cDict.TryAdd(key, value);
}
else

return defaultValue;
}
}
else
{
lock (dictionary)
{
if (!dictionary.ContainsKey(key))
{
dictionary.Add(key, value);
var defaultValue = defaultValueFunc();
if (defaultValue is Task<TValue?> value)
dictionary.Add(key, value);

return defaultValue;
}
}
return defaultValue;
}

return dictionary[key];
}

Expand Down

0 comments on commit 4863423

Please sign in to comment.