Dispose -> Destroy, windows icon color fix

This commit is contained in:
2022-02-08 11:26:35 +01:00
parent 755793c85c
commit 552362c6b3
24 changed files with 153 additions and 126 deletions

View File

@@ -287,7 +287,7 @@ namespace FileTime.Core.Components
foreach (var lastLocationItem in currentLocationItems.OfType<IContainer>())
{
lastLocationItem.Dispose();
lastLocationItem.Destroy();
}
}
}

View File

@@ -2,15 +2,16 @@ using FileTime.Core.Providers;
namespace FileTime.Core.Models
{
public interface IItem : IDisposable
public interface IItem
{
string Name { get; }
string? FullName { get; }
bool IsHidden { get; }
bool IsDisposed { get; }
bool IsDestroyed { get; }
SupportsDelete CanDelete { get; }
bool CanRename { get; }
IContentProvider Provider { get; }
void Destroy();
Task Delete(bool hardDelete = false);
Task Rename(string newName);
IContainer? GetParent();

View File

@@ -35,7 +35,7 @@ namespace FileTime.Core.Models
public AsyncEventHandler Refreshed { get; }
public bool IsDisposed => BaseContainer.IsDisposed;
public bool IsDestroyed => BaseContainer.IsDestroyed;
private void RefreshAddBase(Func<object?, AsyncEventArgs, CancellationToken, Task> handler)
{
@@ -171,9 +171,9 @@ namespace FileTime.Core.Models
public async Task Rename(string newName) => await BaseContainer.Rename(newName);
public async Task<bool> CanOpen() => await BaseContainer.CanOpen();
public void Dispose()
public void Destroy()
{
BaseContainer.Dispose();
BaseContainer.Destroy();
}
public void Unload()

View File

@@ -33,7 +33,7 @@ namespace FileTime.Core.Providers
public bool SupportsDirectoryLevelSoftDelete => false;
public bool IsDisposed => false;
public bool IsDestroyed => false;
public TopContainer(IEnumerable<IContentProvider> contentProviders)
{
@@ -71,7 +71,7 @@ namespace FileTime.Core.Providers
public Task<bool> CanOpen() => Task.FromResult(true);
public void Dispose() { }
public void Destroy() { }
public void Unload() { }
}

View File

@@ -29,7 +29,7 @@ namespace FileTime.Core.Timeline
public bool SupportsDirectoryLevelSoftDelete => false;
public bool IsDisposed { get; private set; }
public bool IsDestroyed { get; private set; }
public TimeContainer(string name, IContainer parent, IContentProvider contentProvider, IContentProvider virtualContentProvider, PointInTime pointInTime)
{
@@ -124,7 +124,7 @@ namespace FileTime.Core.Timeline
}
public Task<bool> CanOpen() => Task.FromResult(true);
public void Dispose() => IsDisposed = true;
public void Destroy() => IsDestroyed = true;
public void Unload() { }
}
}

View File

@@ -31,7 +31,7 @@ namespace FileTime.Core.Timeline
public IContentProvider Provider { get; }
public IContentProvider VirtualProvider { get; }
public bool IsDisposed { get; private set; }
public bool IsDestroyed { get; private set; }
public Task Delete(bool hardDelete = false) => Task.CompletedTask;
@@ -44,6 +44,6 @@ namespace FileTime.Core.Timeline
public Task<string> GetContent(CancellationToken token = default) => Task.FromResult("");
public Task<long> GetElementSize(CancellationToken token = default) => Task.FromResult(-1L);
public void Dispose() => IsDisposed = true;
public void Destroy() => IsDestroyed = true;
}
}

View File

@@ -28,7 +28,7 @@ namespace FileTime.Core.Timeline
public bool SupportsDirectoryLevelSoftDelete => false;
public bool IsDisposed => false;
public bool IsDestroyed => false;
public TimeProvider(PointInTime pointInTime)
{
@@ -93,7 +93,7 @@ namespace FileTime.Core.Timeline
public void SetParent(IContainer container) { }
public Task<bool> CanOpen() => Task.FromResult(true);
public void Dispose() { }
public void Destroy() { }
public void Unload() { }
}

View File

@@ -186,5 +186,15 @@
<Style Selector="Border.SelectedTimelineCommand">
<Setter Property="BorderBrush" Value="{DynamicResource ForegroundBrush}"/>
</Style>
<!--Style Selector="MenuItem">
<Setter Property="Template">
<DataTemplate>
<Grid ColumnDefinitions="20,*">
<TextBlock Grid.Column="1" Text="{Binding Header}" />
</Grid>
</DataTemplate>
</Setter>
</Style-->
</Application.Styles>
</Application>

View File

@@ -33,6 +33,10 @@
<PackageReference Include="Avalonia.ReactiveUI" Version="0.10.12" />
<PackageReference Include="Avalonia.Svg.Skia" Version="0.10.12" />
<PackageReference Include="Avalonia.Xaml.Behaviors" Version="0.10.12" />
<PackageReference Include="IDisposableAnalyzers" Version="4.0.1">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.Extensions.Configuration" Version="6.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="6.0.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="6.0.0" />

View File

@@ -1,4 +1,5 @@
using System;
using System.Drawing.Imaging;
using System.IO;
using Avalonia.Media.Imaging;
using FileTime.Avalonia.Misc;
@@ -18,32 +19,64 @@ namespace FileTime.Avalonia.IconProviders
if (Array.Find(lines, l => l.StartsWith("iconresource", StringComparison.OrdinalIgnoreCase)) is string iconLine)
{
var nameLineValue = string.Join('=', iconLine.Split('=')[1..]);
var environemntVariables = Environment.GetEnvironmentVariables();
foreach (var keyo in environemntVariables.Keys)
{
if (keyo is string key && environemntVariables[key] is string value)
{
nameLineValue = nameLineValue.Replace($"%{key}%", value);
}
}
var parts = nameLineValue.Split(',');
if (parts.Length >= 2 && long.TryParse(parts[^1], out var parsedResourceId))
{
if (parsedResourceId < 0) parsedResourceId *= -1;
var extractedIcon = NativeMethodHelpers.GetIconResource(string.Join(',', parts[..^1]), (uint)parsedResourceId);
var extractedIconAsStream = new MemoryStream();
extractedIcon.Save(extractedIconAsStream);
extractedIconAsStream.Position = 0;
return new ImagePath(ImagePathType.Raw, new Bitmap(extractedIconAsStream));
}
return GetImagePathByIconPath(nameLineValue);
}
}
return null;
}
public static ImagePath GetImagePathByIconPath(string path)
{
var environemntVariables = Environment.GetEnvironmentVariables();
foreach (var keyo in environemntVariables.Keys)
{
if (keyo is string key && environemntVariables[key] is string value)
{
path = path.Replace($"%{key}%", value);
}
}
var parts = path.Split(',');
(var parsedResourceId, var path2) = parts.Length >= 2 && long.TryParse(parts[^1], out var id)
? (id, NormalizePath(string.Join(',', parts[..^1])))
: (0, NormalizePath(path));
if (parsedResourceId == 0)
{
using var extractedIconAsStream = new MemoryStream();
using var extractedIcon = System.Drawing.Icon.ExtractAssociatedIcon(path2).ToBitmap();
extractedIcon.Save(extractedIconAsStream, ImageFormat.Png);
extractedIconAsStream.Position = 0;
#pragma warning disable IDISP004 // Don't ignore created IDisposable
return new ImagePath(ImagePathType.Raw, new Bitmap(extractedIconAsStream));
#pragma warning restore IDISP004 // Don't ignore created IDisposable
}
else
{
if (parsedResourceId < 0) parsedResourceId *= -1;
using var extractedIcon = NativeMethodHelpers.GetIconResource(path2, (uint)parsedResourceId).ToBitmap();
using var extractedIconAsStream = new MemoryStream();
extractedIcon.Save(extractedIconAsStream, ImageFormat.Png);
extractedIconAsStream.Position = 0;
#pragma warning disable IDISP004 // Don't ignore created IDisposable
return new ImagePath(ImagePathType.Raw, new Bitmap(extractedIconAsStream));
#pragma warning restore IDISP004 // Don't ignore created IDisposable
}
}
private static string NormalizePath(string path)
{
if (path.StartsWith('\"') && path.EndsWith('\"'))
{
return path[1..^1];
}
return path;
}
}
}

View File

@@ -61,10 +61,7 @@ namespace FileTime.Avalonia
BuildAvaloniaApp().StartWithClassicDesktopLifetime(args);
}
#if DEBUG
catch
{
throw;
}
finally { }
#else
catch (Exception e)
{

View File

@@ -6,10 +6,12 @@ using FileTime.Providers.Local;
using System.Runtime.InteropServices;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;
using System.IO;
using FileTime.Avalonia.Misc;
using FileTime.Avalonia.IconProviders;
using Avalonia.Svg.Skia;
using Avalonia.Media;
#pragma warning disable CA1416
namespace FileTime.Avalonia.Services
@@ -24,15 +26,16 @@ namespace FileTime.Avalonia.Services
if (container is LocalFolder localFolder)
{
ProcessKey(Registry.ClassesRoot.OpenSubKey("Directory"), menuItems, localFolder.Directory.FullName);
using var directoryKey = Registry.ClassesRoot.OpenSubKey("Directory");
ProcessRegistryKey(directoryKey, menuItems, localFolder.Directory.FullName);
}
return menuItems;
}
private void ProcessKey(RegistryKey? contextMenuContainer, List<object> menuItems, string folderPath)
private void ProcessRegistryKey(RegistryKey? contextMenuContainer, List<object> menuItems, string folderPath)
{
var shell = contextMenuContainer?.OpenSubKey("shell");
using var shell = contextMenuContainer?.OpenSubKey("shell");
if (shell == null) return;
var shellSubKeys = shell.GetSubKeyNames();
@@ -63,18 +66,36 @@ namespace FileTime.Avalonia.Services
{
text = text.Replace("&", "");
if (shellKey.GetSubKeyNames().Contains("command") && shellKey.OpenSubKey("command")?.GetValue(null) is string commandString)
object? image = null;
try
{
var item = new MenuItem() { Header = text };
if (shellKey.GetValue("Icon") is string iconPath)
{
var imagePath = WindowsSystemIconHelper.GetImagePathByIconPath(iconPath);
if (imagePath.Type == Models.ImagePathType.Raw)
{
image = new Image()
{
Source = (IImage)imagePath.Image!
};
}
}
}
catch { }
using var commandKey = shellKey.OpenSubKey("command");
if (shellKey.GetSubKeyNames().Contains("command") && commandKey?.GetValue(null) is string commandString)
{
var item = new MenuItem() { Header = text, Icon = image };
item.Click += (o, e) => MenuItemClick(folderPath, commandString);
menuItems.Add(item);
}
else if (shellKey.GetValue("ExtendedSubCommandsKey") is string extendedCommands)
{
var rootMenu = new MenuItem() { Header = text };
var rootMenu = new MenuItem() { Header = text, Icon = image };
var rootMenuItems = new List<object>();
ProcessKey(Registry.ClassesRoot.OpenSubKey(extendedCommands), rootMenuItems, folderPath);
ProcessRegistryKey(Registry.ClassesRoot.OpenSubKey(extendedCommands), rootMenuItems, folderPath);
rootMenu.Items = rootMenuItems.ToArray();
menuItems.Add(rootMenu);
@@ -104,10 +125,6 @@ namespace FileTime.Avalonia.Services
{
for (var i2 = 0; i2 < commandParts[i].Count; i2++)
{
/*var commandPart = commandParts[i][i2];
if (commandPart == "%1" || commandPart == "%V") commandParts[i][i2] = folderPath;*/
commandParts[i][i2] = commandParts[i][i2].Replace("%1", folderPath).Replace("%V", folderPath);
}
}
@@ -145,7 +162,7 @@ namespace FileTime.Avalonia.Services
try
{
var process = new Process();
using var process = new Process();
process.StartInfo.FileName = commandPartsWithoutEmpty[0];
process.StartInfo.Arguments = arguments;
process.Start();
@@ -170,7 +187,7 @@ namespace FileTime.Avalonia.Services
var (paramStartX, paramStartY) = GetCoordinatesFrom(commandParts, 1, 0, lastExecutablePart);
arguments = SumList(commandParts, paramStartX, paramStartY);
var process = new Process();
using var process = new Process();
process.StartInfo.FileName = executable;
process.StartInfo.Arguments = arguments;
process.Start();

View File

@@ -16,7 +16,7 @@ namespace FileTime.Avalonia.ViewModels
{
[ViewModel]
[Inject(typeof(ItemNameConverterService))]
public partial class ContainerViewModel : IItemViewModel, IDisposable
public partial class ContainerViewModel : IItemViewModel
{
private bool _disposed;
private bool _isRefreshing;
@@ -321,20 +321,9 @@ namespace FileTime.Avalonia.ViewModels
return _items;
}
~ContainerViewModel()
private void Dispose()
{
Dispose(false);
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
private void Dispose(bool disposing)
{
if (!_disposed && disposing)
if (!_disposed)
{
Container.Refreshed.Remove(Container_Refreshed);
}

View File

@@ -236,7 +236,7 @@ namespace FileTime.Avalonia.ViewModels
for (var i = 0; i < itemsToRemove.Count; i++)
{
itemsToRemove[i].Dispose();
itemsToRemove[i].Destroy();
TimelineCommands.Remove(itemsToRemove[i]);
}
@@ -262,7 +262,7 @@ namespace FileTime.Avalonia.ViewModels
for (var i = 0; i < commandVMsToRemove.Count; i++)
{
commandVMsToRemove[i].Dispose();
commandVMsToRemove[i].Destroy();
parallelCommandsVM.ParallelCommands.Remove(commandVMsToRemove[i]);
}
}

View File

@@ -1,6 +1,4 @@
using System.Linq;
using System.Collections.Generic;
using System;
using FileTime.Core.Timeline;
using MvvmGen;
using System.Collections.ObjectModel;
@@ -8,7 +6,7 @@ using System.Collections.ObjectModel;
namespace FileTime.Avalonia.ViewModels
{
[ViewModel]
public partial class ParallelCommandsViewModel : IDisposable
public partial class ParallelCommandsViewModel
{
private bool _disposed;
@@ -22,24 +20,13 @@ namespace FileTime.Avalonia.ViewModels
Id = parallelCommands.Id;
}
~ParallelCommandsViewModel()
public void Destroy()
{
Dispose(false);
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
private void Dispose(bool disposing)
{
if (!_disposed && disposing)
if (!_disposed)
{
foreach(var commandVm in ParallelCommands)
{
commandVm.Dispose();
commandVm.Destroy();
}
}
_disposed = true;

View File

@@ -9,7 +9,7 @@ using MvvmGen;
namespace FileTime.Avalonia.ViewModels
{
[ViewModel]
public partial class ParallelCommandViewModel : IDisposable
public partial class ParallelCommandViewModel
{
private bool _disposed;
@@ -39,20 +39,9 @@ namespace FileTime.Avalonia.ViewModels
return Task.CompletedTask;
}
~ParallelCommandViewModel()
public void Destroy()
{
Dispose(false);
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
private void Dispose(bool disposing)
{
if (!_disposed && disposing)
if (!_disposed)
{
_commandTimeState.Command.ProgressChanged.Remove(HandleProgressChange);
}

View File

@@ -33,7 +33,7 @@ namespace FileTime.Providers.Local
public bool SupportsDirectoryLevelSoftDelete => false;
public bool IsDisposed => false;
public bool IsDestroyed => false;
public LocalContentProvider(ILogger<LocalContentProvider> logger)
{
@@ -103,7 +103,7 @@ namespace FileTime.Providers.Local
public Task Rename(string newName) => throw new NotSupportedException();
public Task<bool> CanOpen() => Task.FromResult(true);
public void Dispose()
public void Destroy()
{
foreach (var c in _rootContainers)
{

View File

@@ -30,7 +30,7 @@ namespace FileTime.Providers.Local
private readonly LocalFolder _parent;
public bool IsDisposed { get; private set; }
public bool IsDestroyed { get; private set; }
public LocalFile(FileInfo file, LocalFolder parent, IContentProvider contentProvider)
{
@@ -86,6 +86,6 @@ namespace FileTime.Providers.Local
public async Task<string> GetContent(CancellationToken token = default) => await System.IO.File.ReadAllTextAsync(File.FullName, token);
public Task<long> GetElementSize(CancellationToken token = default) => Task.FromResult(File.Length);
public void Dispose() => IsDisposed = true;
public void Destroy() => IsDestroyed = true;
}
}

View File

@@ -34,7 +34,7 @@ namespace FileTime.Providers.Local
public DateTime CreatedAt => Directory.CreationTime;
public IReadOnlyList<Exception> Exceptions { get; }
public bool IsDisposed { get; private set; }
public bool IsDestroyed { get; private set; }
public bool SupportsDirectoryLevelSoftDelete { get; }
@@ -67,7 +67,7 @@ namespace FileTime.Providers.Local
{
foreach (var item in _items)
{
item.Dispose();
item.Destroy();
}
}
@@ -185,12 +185,12 @@ namespace FileTime.Providers.Local
}
public Task<bool> CanOpen() => Task.FromResult(true);
public void Dispose()
public void Destroy()
{
_items = null;
_containers = null;
_elements = null;
IsDisposed = true;
IsDestroyed = true;
Refreshed = new AsyncEventHandler();
}

View File

@@ -31,7 +31,7 @@ namespace FileTime.Providers.Smb
public bool SupportsDirectoryLevelSoftDelete => false;
public bool IsDisposed => false;
public bool IsDestroyed => false;
public SmbContentProvider(IInputInterface inputInterface)
{
@@ -105,7 +105,7 @@ namespace FileTime.Providers.Smb
public Task Rename(string newName) => throw new NotSupportedException();
public Task<bool> CanOpen() => Task.FromResult(true);
public void Dispose() { }
public void Destroy() { }
public void Unload() { }
}

View File

@@ -19,7 +19,7 @@ namespace FileTime.Providers.Smb
public IContentProvider Provider { get; }
private IContainer _parent;
public bool IsDisposed { get; private set; }
public bool IsDestroyed { get; private set; }
public SmbFile(string name, SmbContentProvider provider, IContainer parent)
{
@@ -48,6 +48,6 @@ namespace FileTime.Providers.Smb
public Task<string> GetContent(CancellationToken token = default) => Task.FromResult("NotImplemented");
public Task<long> GetElementSize(CancellationToken token = default) => Task.FromResult(-1L);
public void Dispose() => IsDisposed = true;
public void Destroy() => IsDestroyed = true;
}
}

View File

@@ -29,7 +29,7 @@ namespace FileTime.Providers.Smb
public AsyncEventHandler Refreshed { get; } = new();
public IReadOnlyList<Exception> Exceptions { get; } = new List<Exception>().AsReadOnly();
public bool IsDisposed { get; private set; }
public bool IsDestroyed { get; private set; }
public bool SupportsDirectoryLevelSoftDelete => false;
@@ -109,7 +109,7 @@ namespace FileTime.Providers.Smb
{
foreach (var item in _items)
{
item.Dispose();
item.Destroy();
}
}
@@ -134,7 +134,7 @@ namespace FileTime.Providers.Smb
}
public Task<bool> CanOpen() => Task.FromResult(true);
public void Dispose() => IsDisposed = true;
public void Destroy() => IsDestroyed = true;
public void Unload()
{

View File

@@ -40,7 +40,7 @@ namespace FileTime.Providers.Smb
public bool SupportsDirectoryLevelSoftDelete => false;
public bool IsDisposed => false;
public bool IsDestroyed => false;
public SmbServer(string path, SmbContentProvider contentProvider, IInputInterface inputInterface)
{
@@ -193,7 +193,7 @@ namespace FileTime.Providers.Smb
public Task Rename(string newName) => throw new NotSupportedException();
public Task<bool> CanOpen() => Task.FromResult(true);
public void Dispose() { }
public void Destroy() { }
public void Unload() { }
}

View File

@@ -29,7 +29,7 @@ namespace FileTime.Providers.Smb
public AsyncEventHandler Refreshed { get; } = new();
public IReadOnlyList<Exception> Exceptions { get; } = new List<Exception>().AsReadOnly();
public bool IsDisposed => false;
public bool IsDestroyed => false;
public bool SupportsDirectoryLevelSoftDelete => false;
@@ -117,7 +117,7 @@ namespace FileTime.Providers.Smb
{
foreach (var item in _items)
{
item.Dispose();
item.Destroy();
}
}
@@ -171,7 +171,7 @@ namespace FileTime.Providers.Smb
public Task Rename(string newName) => throw new NotSupportedException();
public Task<bool> CanOpen() => Task.FromResult(true);
public void Dispose() { }
public void Destroy() { }
public void Unload()
{