Skip to content

Commit

Permalink
#28, #29 Icons for .url files
Browse files Browse the repository at this point in the history
  • Loading branch information
brondavies committed Nov 11, 2024
1 parent 78e6a60 commit 903c729
Show file tree
Hide file tree
Showing 7 changed files with 95 additions and 11 deletions.
1 change: 1 addition & 0 deletions src/TrayToolbar/ConfigHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ internal class ConfigHelper
internal static string ProfileFolder => Path.Combine(LocalAppData, "TrayToolbar");
internal static string ConfigurationFile = Path.Combine(ProfileFolder, "TrayToolbarConfig.json");
internal static string LegacyConfigurationFile = Path.Combine(ApplicationRoot, "TrayToolbar.json");
internal static int WindowsMajorVersion = Environment.OSVersion.Version.Build >= 22000 ? 11 : 10;

internal static bool GetStartupKey()
{
Expand Down
16 changes: 15 additions & 1 deletion src/TrayToolbar/Extensions/ExtensionMethods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,15 @@ public static bool IsMatch(this string? value, string pattern)
return Regex.IsMatch(value, pattern, RegexOptions.IgnoreCase | RegexOptions.CultureInvariant);
}

public static bool IsOneOf(this string? value, params string?[] compare)
{
foreach(var item in compare)
{
if (value.Is(item)) return true;
}
return false;
}

public static string Join(this string[] value, string separator = ", ")
{
return string.Join(separator, value);
Expand All @@ -60,7 +69,12 @@ public static string Join(this string[] value, string separator = ", ")

public static string ToLocalPath(this string value)
{
return Environment.ExpandEnvironmentVariables(value);
var path = Environment.ExpandEnvironmentVariables(value);
if (path.StartsWith("file://"))
{
path = new Uri(path).LocalPath;
}
return path;
}

public static void ShowContextMenu(this NotifyIcon notifyIcon)
Expand Down
54 changes: 51 additions & 3 deletions src/TrayToolbar/Extensions/ShellIcons.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,59 @@ public static unsafe Bitmap FetchIconAsBitmap(string path, bool large)

public static unsafe Icon FetchIcon(string path, bool large = false)
{
var icon = ExtractFromPath(path, large);
return icon;
if (Path.GetExtension(path).Is(".url"))
{
return GetIconFromUrlFile(path, large);
}
return ExtractFromPath(path, large);
}

private static Icon GetIconFromUrlFile(string path, bool large)
{
var fi = new FileInfo(path);
if (fi.Exists && fi.Length < 1_000_000) // limit to files under 1MB to guard against potential memory overuse
{
var dict = new Dictionary<string, string>();
foreach (var line in File.ReadAllLines(path))
{
var parts = line.Split('=', 2);
if (parts.Length == 2)
{
dict.Add(parts[0].ToLowerInvariant(), parts[1]);
}
}
dict.TryGetValue("url", out var url);
dict.TryGetValue("iconfile", out var iconFile);
dict.TryGetValue("iconindex", out var iconIndex);
if (iconFile.HasValue() && File.Exists(iconFile.ToLocalPath()))
{
_ = int.TryParse(iconIndex, out int id); // will be 0 if parse fails
var icon = Icon.ExtractIcon(iconFile, id, !large);
if (icon != null) return icon;
}
//no iconfile was set or it didn't contain an icon, check the url
if (url.HasValue())
{
//special case urls
if (url.StartsWith("ms-settings:"))
{
//TODO: https://github.com/dotnet/winforms/issues/12447
//return SystemIcons.GetStockIcon(StockIconId.Settings, large ? StockIconOptions.Default : StockIconOptions.SmallIcon);
return Icon.ExtractIcon(Shell32Dll, SETTINGS_ICON_INDEX, !large) ?? SystemIcons.Application;
}
//try getting an icon from the target if it's a local path
if (File.Exists(url.ToLocalPath()))
{
return ExtractFromPath(url, large);
}
}
}
return ExtractFromPath(path, large);
}

static uint SizeOfSHGetFileInfo = (uint)Unsafe.SizeOf<SHFILEINFOW>();
static readonly int SETTINGS_ICON_INDEX = ConfigHelper.WindowsMajorVersion == 11 ? 314 : 316;
static readonly string Shell32Dll = Path.Combine(Environment.SystemDirectory, "SHELL32.dll");
static readonly uint SizeOfSHGetFileInfo = (uint)Unsafe.SizeOf<SHFILEINFOW>();
private static unsafe Icon ExtractFromPath(string path, bool large = false)
{
var shinfo = new SHFILEINFOW();
Expand Down
1 change: 1 addition & 0 deletions src/TrayToolbar/NativeMethods.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ RegCloseKey
RegNotifyChangeKeyValue
RegOpenKeyEx
SHGetFileInfo
SHGetStockIconInfo
SHGFI_FLAGS
WM_MOUSE*
WM_PAINT
Expand Down
11 changes: 4 additions & 7 deletions src/TrayToolbar/SettingsForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,16 +53,13 @@ private void LoadResources()
SaveButton.Text = R.Save;
CancelBtn.Text = R.Cancel;
AddFolderButton.Text = R.Add_Folder;
Text = R.TrayToolbar_Settings + " (" + ConfigHelper.ApplicationVersion + ")";
Text = $"{R.TrayToolbar_Settings} ({ConfigHelper.ApplicationVersion})";
NewVersionLabel.Text = R.A_new_version_is_available;
ConfigHelper.CheckForUpdate().ContinueWith(r =>
{
if (r.Result?.Name != null)
if (r.Result?.Name != null && r.Result.Name != "v" + ConfigHelper.ApplicationVersion)
{
if (r.Result.Name != "v" + ConfigHelper.ApplicationVersion)
{
ShowUpdateAvailable(r.Result.UpdateUrl);
}
ShowUpdateAvailable(r.Result.UpdateUrl);
}
});

Expand Down Expand Up @@ -280,7 +277,7 @@ private void ReloadMenuItems(FolderConfig folder, CancellationToken token)
submenu = menu.CreateFolder(Path.GetRelativePath(folder.Name, parentPath), LeftClickMenu_ItemClicked, LeftClickMenuEntry_MouseDown);
}
var menuText = Path.GetFileName(file);
if (Configuration.HideFileExtensions || file.FileExtension().Is(".lnk"))
if (Configuration.HideFileExtensions || file.FileExtension().IsOneOf(".lnk", ".url"))
{
menuText = Path.GetFileNameWithoutExtension(file);
}
Expand Down
1 change: 1 addition & 0 deletions src/TrayToolbar/TrayToolbar.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<GenerateSatelliteAssembliesForCore>true</GenerateSatelliteAssembliesForCore>
<Platforms>AnyCPU;x64</Platforms>
<ApplicationManifest>app.manifest</ApplicationManifest>
</PropertyGroup>

<ItemGroup>
Expand Down
22 changes: 22 additions & 0 deletions src/TrayToolbar/app.manifest
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?xml version="1.0" encoding="utf-8"?>
<assembly manifestVersion="1.0" xmlns="urn:schemas-microsoft-com:asm.v1">
<assemblyIdentity version="1.0.0.0" name="MyApplication.app"/>
<trustInfo xmlns="urn:schemas-microsoft-com:asm.v2">
<security>
<requestedPrivileges xmlns="urn:schemas-microsoft-com:asm.v3">
<requestedExecutionLevel level="asInvoker" uiAccess="false" />
</requestedPrivileges>
</security>
</trustInfo>

<compatibility xmlns="urn:schemas-microsoft-com:compatibility.v1">
<application>
<!-- Windows 8 -->
<supportedOS Id="{4a2f28e3-53b9-4441-ba9c-d69d4a4a6e38}" />
<!-- Windows 8.1 -->
<supportedOS Id="{1f676c76-80e1-4239-95bb-83d0f6d0da78}" />
<!-- Windows 10 -->
<supportedOS Id="{8e0f7a12-bfb3-4fe8-b9a5-48fd50a15a9a}" />
</application>
</compatibility>
</assembly>

0 comments on commit 903c729

Please sign in to comment.