Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: Added support for installing multiple fonts at the same time #11029

Merged
merged 14 commits into from
Jan 18, 2023
Merged
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;
}
}
}
}