[Feature] Show link overlay for files like lnk & URL (#166), version 1.0.17.26

This commit is contained in:
Markus Hofknecht 2021-05-02 10:05:33 +02:00
parent c824c7a10b
commit a005582d5e
3 changed files with 59 additions and 20 deletions

View file

@ -122,6 +122,7 @@ namespace SystemTrayMenu.DataClasses
else else
{ {
bool handled = false; bool handled = false;
bool showOverlay = false;
string fileExtension = Path.GetExtension(TargetFilePath); string fileExtension = Path.GetExtension(TargetFilePath);
if (fileExtension == ".lnk") if (fileExtension == ".lnk")
@ -129,10 +130,12 @@ namespace SystemTrayMenu.DataClasses
handled = SetLnk( handled = SetLnk(
ref isLnkDirectory, ref isLnkDirectory,
ref resolvedLnkPath); ref resolvedLnkPath);
showOverlay = true;
} }
else if (fileExtension == ".url") else if (fileExtension == ".url")
{ {
handled = SetUrl(); handled = SetUrl();
showOverlay = true;
} }
else if (fileExtension == ".sln") else if (fileExtension == ".sln")
{ {
@ -143,20 +146,8 @@ namespace SystemTrayMenu.DataClasses
{ {
try try
{ {
icon = IconReader.GetFileIconWithCache(TargetFilePath, false); icon = IconReader.GetFileIconWithCache(TargetFilePath, showOverlay, out bool toDispose);
diposeIcon = false; diposeIcon = toDispose;
// other project -> fails sometimes
// icon = IconHelper.ExtractIcon(TargetFilePath, 0);
// standard way -> fails sometimes
// icon = Icon.ExtractAssociatedIcon(filePath);
// API Code Pack -> fails sometimes
// ShellFile shellFile = ShellFile.FromFilePath(filePath);
// Bitmap shellThumb = shellFile.Thumbnail.ExtraLargeBitmap;
// IShellItemImageFactory GetImage works, but missing link overlay there #149
} }
catch (Exception ex) catch (Exception ex)
{ {
@ -279,6 +270,15 @@ namespace SystemTrayMenu.DataClasses
{ {
bool handled = false; bool handled = false;
resolvedLnkPath = FileLnk.GetResolvedFileName(TargetFilePath); resolvedLnkPath = FileLnk.GetResolvedFileName(TargetFilePath);
//if (FileLnk.IsNetworkPath(resolvedLnkPath))
//{
// string nameOrAdress = resolvedLnkPath.Split(@"\\")[1].Split(@"\").First();
// if (!FileLnk.PingHost(nameOrAdress))
// {
// return handled;
// }
//}
if (FileLnk.IsDirectory(resolvedLnkPath)) if (FileLnk.IsDirectory(resolvedLnkPath))
{ {
icon = IconReader.GetFolderIconSTA(TargetFilePath, IconReader.FolderType.Open, true); icon = IconReader.GetFolderIconSTA(TargetFilePath, IconReader.FolderType.Open, true);
@ -308,7 +308,8 @@ namespace SystemTrayMenu.DataClasses
{ {
try try
{ {
icon = Icon.ExtractAssociatedIcon(iconLocation); icon = IconReader.GetFileIconWithCache(iconLocation, true, out bool toDispose);
diposeIcon = toDispose;
handled = true; handled = true;
} }
catch (ArgumentException ex) catch (ArgumentException ex)
@ -344,14 +345,15 @@ namespace SystemTrayMenu.DataClasses
} }
else else
{ {
icon = IconReader.GetFileIconWithCache(browserPath, false); icon = IconReader.GetFileIconWithCache(browserPath, true, out bool toDispose);
diposeIcon = false; diposeIcon = toDispose;
handled = true; handled = true;
} }
} }
else if (System.IO.File.Exists(iconFile)) else if (System.IO.File.Exists(iconFile))
{ {
icon = Icon.ExtractAssociatedIcon(iconFile); icon = IconReader.GetFileIconWithCache(iconFile, true, out bool toDispose);
diposeIcon = toDispose;
handled = true; handled = true;
} }
else else

View file

@ -6,6 +6,7 @@ namespace SystemTrayMenu.Utilities
{ {
using System; using System;
using System.IO; using System.IO;
using System.Net.NetworkInformation;
using System.Runtime.InteropServices; using System.Runtime.InteropServices;
using System.Threading; using System.Threading;
using Shell32; using Shell32;
@ -96,5 +97,37 @@ namespace SystemTrayMenu.Utilities
return resolvedFilename; return resolvedFilename;
} }
public static bool IsNetworkPath(string path)
{
return path.StartsWith(@"\\", StringComparison.InvariantCulture) &&
!path.StartsWith(@"\\?\", StringComparison.InvariantCulture);
}
public static bool PingHost(string nameOrAddress)
{
bool pingable = false;
Ping pinger = null;
try
{
pinger = new Ping();
PingReply reply = pinger.Send(nameOrAddress);
pingable = reply.Status == IPStatus.Success;
}
catch (PingException ex)
{
Log.Warn($"Ping {nameOrAddress} failed", ex);
}
finally
{
if (pinger != null)
{
pinger.Dispose();
}
}
return pingable;
}
} }
} }

View file

@ -47,22 +47,26 @@ namespace SystemTrayMenu.Utilities
} }
} }
public static Icon GetFileIconWithCache(string filePath, bool linkOverlay, IconSize size = IconSize.Small) public static Icon GetFileIconWithCache(string filePath, bool linkOverlay, out bool toDispose)
{ {
Icon icon = null; Icon icon = null;
string extension = Path.GetExtension(filePath); string extension = Path.GetExtension(filePath);
IconSize size = IconSize.Small;
if (IsExtensionWitSameIcon(extension)) if (IsExtensionWitSameIcon(extension))
{ {
icon = DictIconCache.GetOrAdd(extension, GetIcon); icon = DictIconCache.GetOrAdd(extension + linkOverlay, GetIcon);
Icon GetIcon(string keyExtension) Icon GetIcon(string keyExtension)
{ {
return GetFileIconSTA(filePath, linkOverlay, size); return GetFileIconSTA(filePath, linkOverlay, size);
} }
toDispose = false;
} }
else else
{ {
icon = GetFileIconSTA(filePath, linkOverlay, size); icon = GetFileIconSTA(filePath, linkOverlay, size);
toDispose = true;
} }
return icon; return icon;