Remove windows desktop runtime dependency from chardonnay

This commit is contained in:
Mbucari 2023-02-27 15:08:37 -07:00
parent d787843fd2
commit 2a22d05f37
8 changed files with 123 additions and 23 deletions

View File

@ -101,8 +101,6 @@
<PackageReference Include="Avalonia.Diagnostics" Version="11.0.0-preview4 " /> <PackageReference Include="Avalonia.Diagnostics" Version="11.0.0-preview4 " />
<PackageReference Include="Avalonia.ReactiveUI" Version="11.0.0-preview4" /> <PackageReference Include="Avalonia.ReactiveUI" Version="11.0.0-preview4" />
<PackageReference Include="Avalonia.Themes.Fluent" Version="11.0.0-preview4" /> <PackageReference Include="Avalonia.Themes.Fluent" Version="11.0.0-preview4" />
<PackageReference Include="SixLabors.ImageSharp" Version="2.1.3" />
<PackageReference Include="XamlNameReferenceGenerator" Version="1.6.1" /> <PackageReference Include="XamlNameReferenceGenerator" Version="1.6.1" />
</ItemGroup> </ItemGroup>

View File

@ -8,6 +8,10 @@
<AppendRuntimeIdentifierToOutputPath>false</AppendRuntimeIdentifierToOutputPath> <AppendRuntimeIdentifierToOutputPath>false</AppendRuntimeIdentifierToOutputPath>
</PropertyGroup> </PropertyGroup>
<ItemGroup>
<PackageReference Include="SixLabors.ImageSharp" Version="2.1.3" />
</ItemGroup>
<ItemGroup> <ItemGroup>
<ProjectReference Include="..\ApplicationServices\ApplicationServices.csproj" /> <ProjectReference Include="..\ApplicationServices\ApplicationServices.csproj" />
<ProjectReference Include="..\AppScaffolding\AppScaffolding.csproj" /> <ProjectReference Include="..\AppScaffolding\AppScaffolding.csproj" />

View File

@ -1,6 +1,7 @@
using System.Drawing; using System.Drawing;
using System.Linq; using System.Linq;
using System.Windows.Forms; using System.Windows.Forms;
using Dinah.Core.WindowsDesktop;
using LibationFileManager; using LibationFileManager;
namespace LibationWinForms namespace LibationWinForms

View File

@ -25,7 +25,7 @@
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<ProjectReference Include="..\..\AppScaffolding\AppScaffolding.csproj" /> <ProjectReference Include="..\..\LibationUiBase\LibationUiBase.csproj" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>

View File

@ -25,7 +25,7 @@
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<ProjectReference Include="..\..\AppScaffolding\AppScaffolding.csproj" /> <ProjectReference Include="..\..\LibationUiBase\LibationUiBase.csproj" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>

View File

@ -0,0 +1,106 @@
using SixLabors.ImageSharp;
using SixLabors.ImageSharp.Formats.Png;
using System;
using System.IO;
using System.Runtime.InteropServices;
namespace WindowsConfigApp
{
internal static partial class FolderIcon
{
// https://stackoverflow.com/a/21389253
public static byte[] ToIcon(this Image img)
{
using var ms = new MemoryStream();
using var bw = new BinaryWriter(ms);
// Header
bw.Write((short)0); // 0-1 : reserved
bw.Write((short)1); // 2-3 : 1=ico, 2=cur
bw.Write((short)1); // 4-5 : number of images
// Image directory
var w = img.Width;
if (w >= 256) w = 0;
bw.Write((byte)w); // 0 : width of image
var h = img.Height;
if (h >= 256) h = 0;
bw.Write((byte)h); // 1 : height of image
bw.Write((byte)0); // 2 : number of colors in palette
bw.Write((byte)0); // 3 : reserved
bw.Write((short)0); // 4 : number of color planes
bw.Write((short)0); // 6 : bits per pixel
var sizeHere = ms.Position;
bw.Write((int)0); // 8 : image size
var start = (int)ms.Position + 4;
bw.Write(start); // 12: offset of image data
// Image data
img.Save(ms, new PngEncoder());
var imageSize = (int)ms.Position - start;
ms.Seek(sizeHere, SeekOrigin.Begin);
bw.Write(imageSize);
ms.Seek(0, SeekOrigin.Begin);
// And load it
return ms.ToArray();
}
public static void DeleteIcon(this DirectoryInfo directoryInfo) => DeleteIcon(directoryInfo.FullName);
public static void DeleteIcon(string dir)
{
string[] array = new string[3] { "desktop.ini", "Icon.ico", ".hidden" };
foreach (string path in array)
{
string text = Path.Combine(dir, path);
if (File.Exists(text))
{
File.SetAttributes(text, File.GetAttributes(text) | FileAttributes.Normal);
new FileInfo(text).IsReadOnly = false;
File.Delete(text);
}
}
refresh();
}
// https://github.com/dimuththarindu/FIC-Folder-Icon-Changer/blob/master/project/FIC/Classes/IconCustomizer.cs
public static void SetIcon(this DirectoryInfo directoryInfo, string icoPath, string folderType)
=> SetIcon(directoryInfo.FullName, icoPath, folderType);
public static void SetIcon(string dir, string icoPath, string folderType)
{
var desktop_ini = Path.Combine(dir, "desktop.ini");
var Icon_ico = Path.Combine(dir, "Icon.ico");
var hidden = Path.Combine(dir, ".hidden");
//deleting existing files
DeleteIcon(dir);
//copying Icon file //overwriting
File.Copy(icoPath, Icon_ico, true);
//writing configuration file
string[] desktopLines = { "[.ShellClassInfo]", "IconResource=Icon.ico,0", "[ViewState]", "Mode=", "Vid=", $"FolderType={folderType}" };
File.WriteAllLines(desktop_ini, desktopLines);
//configure file 2
string[] hiddenLines = { "desktop.ini", "Icon.ico" };
File.WriteAllLines(hidden, hiddenLines);
//making system files
File.SetAttributes(desktop_ini, File.GetAttributes(desktop_ini) | FileAttributes.Hidden | FileAttributes.System | FileAttributes.ReadOnly);
File.SetAttributes(Icon_ico, File.GetAttributes(Icon_ico) | FileAttributes.Hidden | FileAttributes.System | FileAttributes.ReadOnly);
File.SetAttributes(hidden, File.GetAttributes(hidden) | FileAttributes.Hidden | FileAttributes.System | FileAttributes.ReadOnly);
// this strangely completes the process. also hides these 3 hidden system files, even if "show hidden items" is checked
File.SetAttributes(dir, File.GetAttributes(dir) | FileAttributes.ReadOnly);
refresh();
}
private static void refresh() => SHChangeNotify(0x08000000, 0x0000, IntPtr.Zero, IntPtr.Zero); //SHCNE_ASSOCCHANGED SHCNF_IDLIST
[DllImport("shell32.dll", SetLastError = true)]
private static extern void SHChangeNotify(int wEventId, int uFlags, IntPtr dwItem1, IntPtr dwItem2);
}
}

View File

@ -1,12 +1,8 @@
using System; using SixLabors.ImageSharp;
using System.Collections.Generic;
using System.Diagnostics; using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Dinah.Core.WindowsDesktop;
using Dinah.Core.WindowsDesktop.Drawing;
using LibationFileManager; using LibationFileManager;
using System.IO;
using System;
namespace WindowsConfigApp namespace WindowsConfigApp
{ {
@ -21,11 +17,11 @@ namespace WindowsConfigApp
try try
{ {
var icon = ImageReader.ToIcon(image); var icon = Image.Load(File.ReadAllBytes(image)).ToIcon();
iconPath = Path.Combine(directory, $"{Guid.NewGuid()}.ico"); iconPath = Path.Combine(directory, $"{Guid.NewGuid()}.ico");
icon.Save(iconPath); File.WriteAllBytes(iconPath, icon);
new DirectoryInfo(directory).SetIcon(iconPath, Directories.FolderTypes.Music); new DirectoryInfo(directory)?.SetIcon(iconPath, "Music");
} }
finally finally
{ {
@ -36,6 +32,7 @@ namespace WindowsConfigApp
public void DeleteFolderIcon(string directory) public void DeleteFolderIcon(string directory)
=> new DirectoryInfo(directory)?.DeleteIcon(); => new DirectoryInfo(directory)?.DeleteIcon();
public bool CanUpdate => true; public bool CanUpdate => true;
public void InstallUpdate(string updateBundle) public void InstallUpdate(string updateBundle)
{ {

View File

@ -2,10 +2,8 @@
<PropertyGroup> <PropertyGroup>
<OutputType>WinExe</OutputType> <OutputType>WinExe</OutputType>
<TargetFramework>net7.0-windows</TargetFramework> <TargetFramework>net7.0</TargetFramework>
<EnableWindowsTargeting>true</EnableWindowsTargeting> <EnableWindowsTargeting>true</EnableWindowsTargeting>
<UseWindowsForms>true</UseWindowsForms>
<ImplicitUsings>enable</ImplicitUsings>
<PublishReadyToRun>true</PublishReadyToRun> <PublishReadyToRun>true</PublishReadyToRun>
<AppendTargetFrameworkToOutputPath>false</AppendTargetFrameworkToOutputPath> <AppendTargetFrameworkToOutputPath>false</AppendTargetFrameworkToOutputPath>
<AppendRuntimeIdentifierToOutputPath>false</AppendRuntimeIdentifierToOutputPath> <AppendRuntimeIdentifierToOutputPath>false</AppendRuntimeIdentifierToOutputPath>
@ -16,7 +14,7 @@
</PropertyGroup> </PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'"> <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
<OutputPath>..\..\bin\Debug</OutputPath> <OutputPath>..\..\bin\Avalonia\Debug</OutputPath>
<DebugType>embedded</DebugType> <DebugType>embedded</DebugType>
</PropertyGroup> </PropertyGroup>
@ -26,11 +24,7 @@
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="Dinah.Core.WindowsDesktop" Version="7.2.2.1" /> <ProjectReference Include="..\..\LibationUiBase\LibationUiBase.csproj" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\AppScaffolding\AppScaffolding.csproj" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>