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

@@ -37,8 +37,8 @@
<GradientStop Offset="0.5" Color="#93a1a1" />
<GradientStop Offset="1" Color="#0093a1a1" />
</LinearGradientBrush>
<SolidColorBrush
x:Key="AppBackgroundBrush"
@@ -105,8 +105,8 @@
<converters:CompareConverter x:Key="EqualityConverter"/>
<converters:CompareConverter x:Key="NotEqualsConverter" ComparisonCondition="{x:Static converters:ComparisonCondition.NotEqual}"/>
<converters:SplitStringConverter x:Key="SplitStringConverter" />
<converters:ItemViewModeToBrushConverter
x:Key="ItemViewModeToForegroundConverter"
<converters:ItemViewModeToBrushConverter
x:Key="ItemViewModeToForegroundConverter"
DefaultBrush="{StaticResource ForegroundBrush}"
AlternativeBrush="{StaticResource AlternativeItemForegroundBrush}"
SelectedBrush="{StaticResource SelectedItemForegroundBrush}"
@@ -182,9 +182,19 @@
<Style Selector="Grid.PlacesItem:pointerover">
<Setter Property="Background" Value="{DynamicResource AppBackgroundColor}"/>
</Style>
<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);
}