Skip to content

RichCommand: InstallFont #11780

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

Merged
merged 4 commits into from
Mar 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions src/Files.App/Actions/Content/InstallFontAction.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.DependencyInjection;
using Files.App.Contexts;
using Files.App.Extensions;
using Files.App.Filesystem;
using Files.App.Shell;
using Files.Backend.Helpers;
using System.Linq;
using System.Threading.Tasks;

namespace Files.App.Actions
{
internal class InstallFontAction : ObservableObject, IAction
{
private readonly IContentPageContext context = Ioc.Default.GetRequiredService<IContentPageContext>();

public string Label => "Install".GetLocalizedResource();

public bool IsExecutable => context.SelectedItems.Any() &&
context.SelectedItems.All(x => FileExtensionHelpers.IsFontFile(x.FileExtension)) &&
context.PageType is not ContentPageTypes.RecycleBin;

public InstallFontAction()
{
context.PropertyChanged += Context_PropertyChanged;
}

public Task ExecuteAsync()
{
foreach (ListedItem selectedItem in context.SelectedItems)
Win32API.InstallFont(selectedItem.ItemPath, false);

return Task.CompletedTask;
}

public void Context_PropertyChanged(object? sender, System.ComponentModel.PropertyChangedEventArgs e)
{
switch (e.PropertyName)
{
case nameof(IContentPageContext.SelectedItems):
case nameof(IContentPageContext.PageType):
OnPropertyChanged(nameof(IsExecutable));
break;
}
}
}
}
5 changes: 4 additions & 1 deletion src/Files.App/Commands/CommandCodes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,12 @@ public enum CommandCodes
GroupAscending,
GroupDescending,
ToggleGroupDirection,

// Navigation
NewTab,
DuplicateTab,

// Other
InstallFont,
}
}
4 changes: 2 additions & 2 deletions src/Files.App/Commands/Manager/CommandManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ internal class CommandManager : ICommandManager
public IRichCommand ToggleGroupDirection => commands[CommandCodes.ToggleGroupDirection];
public IRichCommand NewTab => commands[CommandCodes.NewTab];
public IRichCommand DuplicateTab => commands[CommandCodes.DuplicateTab];
public IRichCommand InstallFont => commands[CommandCodes.InstallFont];

public CommandManager()
{
Expand Down Expand Up @@ -205,11 +206,10 @@ public CommandManager()
[CommandCodes.GroupByFolderPath] = new GroupByFolderPathAction(),
[CommandCodes.GroupAscending] = new GroupAscendingAction(),
[CommandCodes.GroupDescending] = new GroupDescendingAction(),
[CommandCodes.GroupAscending] = new GroupAscendingAction(),
[CommandCodes.GroupDescending] = new GroupDescendingAction(),
[CommandCodes.ToggleGroupDirection] = new ToggleGroupDirectionAction(),
[CommandCodes.NewTab] = new NewTabAction(),
[CommandCodes.DuplicateTab] = new DuplicateTabAction(),
[CommandCodes.InstallFont] = new InstallFontAction(),
};

[DebuggerDisplay("Command None")]
Expand Down
2 changes: 2 additions & 0 deletions src/Files.App/Commands/Manager/ICommandManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -96,5 +96,7 @@ public interface ICommandManager : IEnumerable<IRichCommand>

IRichCommand NewTab { get; }
IRichCommand DuplicateTab { get; }

IRichCommand InstallFont { get; }
}
}
14 changes: 2 additions & 12 deletions src/Files.App/Helpers/ShellContextMenuHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -178,14 +178,14 @@ async void InvokeShellMenuItem(ContextMenu contextMenu, object? tag)
case "install" when isFont:
{
foreach (string path in contextMenu.ItemsPath)
InstallFont(path, false);
Win32API.InstallFont(path, false);
}
break;

case "installAllUsers" when isFont:
{
foreach (string path in contextMenu.ItemsPath)
InstallFont(path, true);
Win32API.InstallFont(path, true);
}
break;

Expand All @@ -204,16 +204,6 @@ async void InvokeShellMenuItem(ContextMenu contextMenu, object? tag)
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 Down
Original file line number Diff line number Diff line change
Expand Up @@ -407,14 +407,6 @@ public async Task InstallInfDriver()
await Win32API.InstallInf(selectedItem.ItemPath);
}

public Task InstallFont()
{
foreach (ListedItem selectedItem in SlimContentPage.SelectedItems)
Win32API.InstallFont(selectedItem.ItemPath);

return Task.CompletedTask;
}

public async Task PlayAll()
{
await NavigationHelpers.OpenSelectedItems(associatedInstance);
Expand Down
3 changes: 0 additions & 3 deletions src/Files.App/Interacts/BaseLayoutCommandsViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ private void InitializeCommands()
SearchUnindexedItems = new RelayCommand<RoutedEventArgs>(CommandsModel.SearchUnindexedItems);
CreateFolderWithSelection = new AsyncRelayCommand<RoutedEventArgs>(CommandsModel.CreateFolderWithSelection);
InstallInfDriver = new AsyncRelayCommand(CommandsModel.InstallInfDriver);
InstallFontCommand = new AsyncRelayCommand(CommandsModel.InstallFont);
PlayAllCommand = new AsyncRelayCommand(CommandsModel.PlayAll);
FormatDriveCommand = new RelayCommand<ListedItem>(CommandsModel.FormatDrive);
}
Expand Down Expand Up @@ -93,8 +92,6 @@ private void InitializeCommands()

public ICommand InstallInfDriver { get; set; }

public ICommand InstallFontCommand { get; private set; }

public ICommand PlayAllCommand { get; private set; }

public ICommand FormatDriveCommand { get; private set; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,6 @@ public interface IBaseLayoutCommandImplementationModel : IDisposable

Task InstallInfDriver();

Task InstallFont();

Task PlayAll();

void FormatDrive(ListedItem? obj);
Expand Down
15 changes: 11 additions & 4 deletions src/Files.App/Shell/Win32API.cs
Original file line number Diff line number Diff line change
Expand Up @@ -805,12 +805,19 @@ public static async Task<bool> InstallInf(string filePath)
}
}

public static void InstallFont(string fontFilePath)
public static void InstallFont(string fontFilePath, bool forAllUsers)
{
var userFontDir = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "Microsoft", "Windows", "Fonts");
var destName = Path.Combine(userFontDir, Path.GetFileName(fontFilePath));
string fontDirectory = forAllUsers
? Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Windows), "Fonts")
: Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "Microsoft", "Windows", "Fonts");

RunPowershellCommand($"-command \"Copy-Item '{fontFilePath}' '{userFontDir}'; New-ItemProperty -Name '{Path.GetFileNameWithoutExtension(fontFilePath)}' -Path 'HKCU:\\Software\\Microsoft\\Windows NT\\CurrentVersion\\Fonts' -PropertyType string -Value '{destName}'\"", false);
string registryKey = forAllUsers
? "HKLM:\\Software\\Microsoft\\Windows NT\\CurrentVersion\\Fonts"
: "HKCU:\\Software\\Microsoft\\Windows NT\\CurrentVersion\\Fonts";

var destinationPath = Path.Combine(fontDirectory, Path.GetFileName(fontFilePath));

RunPowershellCommand($"-command \"Copy-Item '{fontFilePath}' '{fontDirectory}'; New-ItemProperty -Name '{Path.GetFileNameWithoutExtension(fontFilePath)}' -Path '{registryKey}' -PropertyType string -Value '{destinationPath}'\"", forAllUsers);
}
}
}
6 changes: 3 additions & 3 deletions src/Files.App/UserControls/InnerNavigationToolbar.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -352,9 +352,9 @@
</AppBarButton>
<AppBarButton
x:Name="InstallFontButton"
x:Load="{x:Bind ViewModel.IsFont, Mode=OneWay, FallbackValue=False}"
Command="{x:Bind ViewModel.InstallFontCommand, Mode=OneWay}"
Label="{helpers:ResourceString Name=Install}"
x:Load="{x:Bind Commands.InstallFont.IsExecutable, Mode=OneWay, FallbackValue=False}"
Command="{x:Bind Commands.InstallFont, Mode=OneWay}"
Label="{x:Bind Commands.InstallFont.Label}"
LabelPosition="Default"
ToolTipService.ToolTip="{helpers:ResourceString Name=Install}">

Expand Down
2 changes: 0 additions & 2 deletions src/Files.App/ViewModels/ToolbarViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -591,8 +591,6 @@ private void SearchRegion_Escaped(object? sender, ISearchBox searchBox)

public ICommand? InstallInfCommand { get; set; }

public ICommand? InstallFontCommand { get; set; }

public ICommand? UpdateCommand { get; set; }

public ICommand? PlayAllCommand { get; set; }
Expand Down
1 change: 0 additions & 1 deletion src/Files.App/Views/BaseShellPage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -636,7 +636,6 @@ protected void InitToolbarCommands()
ToolbarViewModel.RunWithPowerShellCommand = new RelayCommand(async () => await Win32Helpers.InvokeWin32ComponentAsync("powershell", this, PathNormalization.NormalizePath(SlimContentPage?.SelectedItem.ItemPath)));
ToolbarViewModel.PropertiesCommand = new RelayCommand(() => SlimContentPage?.CommandsViewModel.ShowPropertiesCommand.Execute(null));
ToolbarViewModel.InstallInfCommand = new RelayCommand(() => SlimContentPage?.CommandsViewModel.InstallInfDriver.Execute(null));;
ToolbarViewModel.InstallFontCommand = new RelayCommand(() => SlimContentPage?.CommandsViewModel.InstallFontCommand.Execute(null));
ToolbarViewModel.UpdateCommand = new AsyncRelayCommand(async () => await updateSettingsService.DownloadUpdates());
ToolbarViewModel.PlayAllCommand = new RelayCommand(() => SlimContentPage?.CommandsViewModel.PlayAllCommand.Execute(null));
}
Expand Down