IconProvider, container exceptions, refactor
This commit is contained in:
@@ -1,9 +1,11 @@
|
||||
using FileTime.Avalonia.Models;
|
||||
using FileTime.Core.Models;
|
||||
|
||||
namespace FileTime.Avalonia.IconProviders
|
||||
{
|
||||
public interface IIconProvider
|
||||
{
|
||||
string GetImage(IItem item);
|
||||
ImagePath GetImage(IItem item);
|
||||
bool EnableAdvancedIcons { get; set; }
|
||||
}
|
||||
}
|
||||
@@ -1,33 +1,75 @@
|
||||
using Avalonia.Media.Imaging;
|
||||
using FileTime.Avalonia.Misc;
|
||||
using FileTime.Avalonia.Models;
|
||||
using FileTime.Core.Models;
|
||||
using FileTime.Providers.Local;
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace FileTime.Avalonia.IconProviders
|
||||
{
|
||||
public class MaterialIconProvider : IIconProvider
|
||||
{
|
||||
public string GetImage(IItem item)
|
||||
public bool EnableAdvancedIcons { get; set; } = true;
|
||||
|
||||
public ImagePath GetImage(IItem item)
|
||||
{
|
||||
var icon = "file.svg";
|
||||
if (item is IContainer)
|
||||
var icon = item is IContainer ? "folder.svg" : "file.svg";
|
||||
|
||||
if (EnableAdvancedIcons)
|
||||
{
|
||||
icon = "folder.svg";
|
||||
}
|
||||
else if (item is IElement element)
|
||||
{
|
||||
if(element is LocalFile localFile && element.FullName.EndsWith(".svg"))
|
||||
if (item is IElement element)
|
||||
{
|
||||
return localFile.File.FullName;
|
||||
}
|
||||
icon = !element.Name.Contains('.')
|
||||
? icon
|
||||
: element.Name.Split('.').Last() switch
|
||||
if (element is LocalFile localFile && (element.FullName?.EndsWith(".svg") ?? false))
|
||||
{
|
||||
"cs" => "csharp.svg",
|
||||
_ => icon
|
||||
};
|
||||
return new ImagePath(ImagePathType.Absolute, localFile.File.FullName);
|
||||
}
|
||||
icon = !element.Name.Contains('.')
|
||||
? icon
|
||||
: element.Name.Split('.').Last() switch
|
||||
{
|
||||
"cs" => "csharp.svg",
|
||||
_ => icon
|
||||
};
|
||||
}
|
||||
/*else if (item is LocalFolder folder && RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
|
||||
{
|
||||
var file = new FileInfo(Path.Combine(folder.FullName, "desktop.ini"));
|
||||
if (file.Exists)
|
||||
{
|
||||
var lines = File.ReadAllLines(file.FullName);
|
||||
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 "/Assets/material/" + icon;
|
||||
return new ImagePath(ImagePathType.Asset, "/Assets/material/" + icon);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,29 @@
|
||||
using System.IO;
|
||||
using Avalonia.Media.Imaging;
|
||||
using FileTime.Avalonia.Misc;
|
||||
using FileTime.Avalonia.Models;
|
||||
using FileTime.Core.Models;
|
||||
using FileTime.Providers.Local;
|
||||
|
||||
namespace FileTime.Avalonia.IconProviders
|
||||
{
|
||||
public class SystemIconProvider : IIconProvider
|
||||
{
|
||||
public bool EnableAdvancedIcons { get; set; }
|
||||
|
||||
public ImagePath GetImage(IItem item)
|
||||
{
|
||||
if (item is LocalFile file)
|
||||
{
|
||||
var extractedIconAsStream = new MemoryStream();
|
||||
var extractedIcon = System.Drawing.Icon.ExtractAssociatedIcon(file.File.FullName);
|
||||
extractedIcon.Save(extractedIconAsStream);
|
||||
extractedIconAsStream.Position = 0;
|
||||
return new ImagePath(ImagePathType.Raw, new Bitmap(extractedIconAsStream));
|
||||
}
|
||||
|
||||
var icon = item is IContainer ? "folder.svg" : "file.svg";
|
||||
return new ImagePath(ImagePathType.Asset, "/Assets/material/" + icon);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user