Skip to content

Commit

Permalink
Feature: Added support for installing multiple fonts at the same time (
Browse files Browse the repository at this point in the history
  • Loading branch information
hecksmosis authored Jan 18, 2023
1 parent ca98008 commit 7cd9e84
Showing 1 changed file with 15 additions and 6 deletions.
21 changes: 15 additions & 6 deletions src/Files.App/Helpers/ShellContextMenuHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -172,16 +172,15 @@ async void InvokeShellMenuItem(ContextMenu contextMenu, object? tag)
{
case "install" when isFont:
{
var userFontDir = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "Microsoft", "Windows", "Fonts");
var destName = Path.Combine(userFontDir, Path.GetFileName(contextMenu.ItemsPath[0]));
Win32API.RunPowershellCommand($"-command \"Copy-Item '{contextMenu.ItemsPath[0]}' '{userFontDir}'; New-ItemProperty -Name '{Path.GetFileNameWithoutExtension(contextMenu.ItemsPath[0])}' -Path 'HKCU:\\Software\\Microsoft\\Windows NT\\CurrentVersion\\Fonts' -PropertyType string -Value '{destName}'\"", false);
foreach (string path in contextMenu.ItemsPath)
InstallFont(path, false);
}
break;

case "installAllUsers" when isFont:
{
var winFontDir = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Windows), "Fonts");
Win32API.RunPowershellCommand($"-command \"Copy-Item '{contextMenu.ItemsPath[0]}' '{winFontDir}'; New-ItemProperty -Name '{Path.GetFileNameWithoutExtension(contextMenu.ItemsPath[0])}' -Path 'HKLM:\\Software\\Microsoft\\Windows NT\\CurrentVersion\\Fonts' -PropertyType string -Value '{Path.GetFileName(contextMenu.ItemsPath[0])}'\"", true);
foreach (string path in contextMenu.ItemsPath)
InstallFont(path, true);
}
break;

Expand All @@ -199,7 +198,17 @@ async void InvokeShellMenuItem(ContextMenu contextMenu, object? tag)
await contextMenu.InvokeItem(menuId);
break;
}

void InstallFont(string path, bool asAdmin)
{
string dir = asAdmin ? Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Windows), "Fonts")
: Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "Microsoft", "Windows", "Fonts");

string registryKey = asAdmin ? "HKLM:\\Software\\Microsoft\\Windows NT\\CurrentVersion\\Fonts"
: "HKCU:\\Software\\Microsoft\\Windows NT\\CurrentVersion\\Fonts";

Win32API.RunPowershellCommand($"-command \"Copy-Item '{path}' '{dir}'; New-ItemProperty -Name '{Path.GetFileNameWithoutExtension(path)}' -Path '{registryKey}' -PropertyType string -Value '{dir}'\"", asAdmin);
}
//contextMenu.Dispose(); // Prevents some menu items from working (TBC)
}
}
Expand All @@ -211,4 +220,4 @@ public static List<ContextMenuFlyoutItemViewModel> GetOpenWithItems(List<Context
return item?.Items;
}
}
}
}

0 comments on commit 7cd9e84

Please sign in to comment.