Skip to content

Code Qaulity: Improved compatibility mode classes #14473

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 1 commit into from
Feb 28, 2024
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
26 changes: 26 additions & 0 deletions src/Files.App/Data/Contracts/IWindowsCompatibilityService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Copyright (c) 2023 Files Community
// Licensed under the MIT License. See the LICENSE.

namespace Files.App.Data.Contracts
{
/// <summary>
/// Represents contract for compatibility mode service for Windows.
/// </summary>
public interface IWindowsCompatibilityService
{
/// <summary>
/// Gets compatibility options for path.
/// </summary>
/// <param name="filePath">The path to get options.</param>
/// <returns>Returns an instance of<see cref="WindowsCompatibilityOptions"/> contains options for the path.</returns>
public WindowsCompatibilityOptions GetCompatibilityOptionsForPath(string filePath);

/// <summary>
/// Sets compatibility options for path.
/// </summary>
/// <param name="filePath">The path to set options.</param>
/// <param name="options">The options to set.</param>
/// <returns>Returns true if succeed; otherwise, false.</returns>
public bool SetCompatibilityOptionsForPath(string filePath, WindowsCompatibilityOptions options);
}
}
1 change: 1 addition & 0 deletions src/Files.App/Helpers/Application/AppLifecycleHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,7 @@ public static IHost ConfigureHost()
.AddSingleton<IRemovableDrivesService, RemovableDrivesService>()
.AddSingleton<INetworkDrivesService, NetworkDrivesService>()
.AddSingleton<IStartMenuService, StartMenuService>()
.AddSingleton<IWindowsCompatibilityService, WindowsCompatibilityService>()
// ViewModels
.AddSingleton<MainPageViewModel>()
.AddSingleton<InfoPaneViewModel>()
Expand Down
33 changes: 0 additions & 33 deletions src/Files.App/Helpers/LocalizedEnumHelper.cs

This file was deleted.

53 changes: 53 additions & 0 deletions src/Files.App/Services/WindowsCompatibilityService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// Copyright (c) 2023 Files Community
// Licensed under the MIT License. See the LICENSE.

using Microsoft.Win32;

namespace Files.App.Services
{
/// <inheritdoc cref="IWindowsCompatibilityService"/>
public class WindowsCompatibilityService : IWindowsCompatibilityService
{
private readonly string _registrySubPath = "SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\AppCompatFlags\\Layers";

/// <inheritdoc/>
public WindowsCompatibilityOptions GetCompatibilityOptionsForPath(string filePath)
{
try
{
// Get the key
using var compatKey = Registry.CurrentUser.OpenSubKey(_registrySubPath);
if (compatKey is null)
return new();

// Get the value for the specified path
var stringOptions = (string?)compatKey.GetValue(filePath, null);

return WindowsCompatibilityOptions.FromString(stringOptions);
}
catch (Exception)
{
return new();
}
}

/// <inheritdoc/>
public bool SetCompatibilityOptionsForPath(string filePath, WindowsCompatibilityOptions options)
{
var stringOptions = options.ToString();

// Remove old one if new one is valid
if (string.IsNullOrEmpty(stringOptions) || stringOptions == "~")
{
return Win32API.RunPowershellCommand(
@$"Remove-ItemProperty -Path 'HKCU:\{_registrySubPath}' -Name '{filePath}' | Out-Null",
true);
}

// Set the new one
return Win32API.RunPowershellCommand(
@$"New-ItemProperty -Path 'HKCU:\{_registrySubPath}' -Name '{filePath}' -Value '{options}' -PropertyType String -Force | Out-Null",
true);
}
}
}
74 changes: 34 additions & 40 deletions src/Files.App/Strings/en-US/Resources.resw
Original file line number Diff line number Diff line change
Expand Up @@ -1797,66 +1797,60 @@
<data name="None" xml:space="preserve">
<value>None</value>
</data>
<data name="HighDpiOption_UseDPIOnLogin" xml:space="preserve">
<data name="CompatibilityOnWindowsLogin" xml:space="preserve">
<value>On Windows login</value>
</data>
<data name="HighDpiOption_UseDPIOnProgramStart" xml:space="preserve">
<value>On program start</value>
<data name="CompatibilityOnProgramStart" xml:space="preserve">
<value>On this program start</value>
</data>
<data name="HighDpiOverride_System" xml:space="preserve">
<data name="System" xml:space="preserve">
<value>System</value>
</data>
<data name="HighDpiOverride_SystemAdvanced" xml:space="preserve">
<value>System (advanced)</value>
<data name="CompatibilitySystemEnhanced" xml:space="preserve">
<value>System (Enhanced)</value>
</data>
<data name="OSCompatibility_Windows7" xml:space="preserve">
<value>Windows 7</value>
<data name="CompatibilityReducedColorModeColor16bit" xml:space="preserve">
<value>16-bit (65536) color</value>
</data>
<data name="OSCompatibility_Windows8" xml:space="preserve">
<value>Windows 8</value>
<data name="CompatibilityReducedColorModeColor8bit" xml:space="preserve">
<value>8-bit (256) color</value>
</data>
<data name="OSCompatibility_WindowsVista" xml:space="preserve">
<value>Windows Vista</value>
<data name="CompatibilityDisableFullscreenOptimizations" xml:space="preserve">
<value>Disable full-screen optimizations</value>
</data>
<data name="OSCompatibility_WindowsVistaSP1" xml:space="preserve">
<value>Windows Vista SP1</value>
<data name="CompatibilityRunIn640x480Resolution" xml:space="preserve">
<value>Run in 640 x 480 screen resolution</value>
</data>
<data name="OSCompatibility_WindowsVistaSP2" xml:space="preserve">
<value>Windows Vista SP2</value>
<data name="CompatibilityOverrideHighDPIBehavior" xml:space="preserve">
<value>Override high DPI scaling behavior</value>
</data>
<data name="ReducedColorMode_Color16Bit" xml:space="preserve">
<value>16 Bit Colors (65536)</value>
</data>
<data name="ReducedColorMode_Color8Bit" xml:space="preserve">
<value>8 Bit Colors (256)</value>
</data>
<data name="CompatibilityMode" xml:space="preserve">
<value>Compatibility mode</value>
</data>
<data name="DisableFullscreenOptimizations" xml:space="preserve">
<value>Disable fullscreen optimizations</value>
</data>
<data name="ExecuteAt640X480" xml:space="preserve">
<value>Run at 640 x 480 screen resolution</value>
</data>
<data name="OverrideDPIScalingBehaviour" xml:space="preserve">
<value>Override high DPI scaling behaviour</value>
</data>
<data name="ReducedColorMode" xml:space="preserve">
<data name="CompatibilityReducedColorMode" xml:space="preserve">
<value>Reduced color mode</value>
</data>
<data name="RegisterForRestart" xml:space="preserve">
<value>Register for restart</value>
<data name="CompatibilityRegisterThisProgramForRestart" xml:space="preserve">
<value>Register this program for restart</value>
</data>
<data name="RunAsAdministrator" xml:space="preserve">
<value>Run as administrator</value>
</data>
<data name="RunCompatibilityTroubleShooter" xml:space="preserve">
<data name="CompatibilityRunTroubleshooter" xml:space="preserve">
<value>Run compatibility troubleshooter</value>
</data>
<data name="UseMainMonitorDPISettings" xml:space="preserve">
<data name="CompatibilityUseDPISettings" xml:space="preserve">
<value>Use DPI settings of the main monitor</value>
</data>
<data name="CompatibilityDoNotAdjustDPI" xml:space="preserve">
<value>Do not adjust DPI</value>
</data>
<data name="CompatibilityDoNotOverrideDPI" xml:space="preserve">
<value>Do not override DPI</value>
</data>
<data name="CompatibilityMode" xml:space="preserve">
<value>Compatibility mode</value>
</data>
<data name="CompatibilityNoReducedColor" xml:space="preserve">
<value>No reduced color</value>
</data>
<data name="ThirdPartyLicenses" xml:space="preserve">
<value>Third party licenses</value>
</data>
Expand Down Expand Up @@ -3722,4 +3716,4 @@
<value>Extra large</value>
<comment>Used to describe layout sizes</comment>
</data>
</root>
</root>
21 changes: 0 additions & 21 deletions src/Files.App/Utils/Storage/Operations/FileOperationsHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -816,27 +816,6 @@ public static bool SetLinkIcon(string filePath, string iconFile, int iconIndex)
});
}

public static string? ReadCompatOptions(string filePath)
=> SafetyExtensions.IgnoreExceptions(() =>
{
using var compatKey = Registry.CurrentUser.OpenSubKey(@"SOFTWARE\Microsoft\Windows NT\CurrentVersion\AppCompatFlags\Layers");
if (compatKey is null)
{
return null;
}
return (string?)compatKey.GetValue(filePath, null);
}, App.Logger);

public static bool SetCompatOptions(string filePath, string options)
{
if (string.IsNullOrEmpty(options) || options == "~")
{
return Win32API.RunPowershellCommand(@$"Remove-ItemProperty -Path 'HKCU:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\AppCompatFlags\Layers' -Name '{filePath}' | Out-Null", true);
}

return Win32API.RunPowershellCommand(@$"New-ItemProperty -Path 'HKCU:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\AppCompatFlags\Layers' -Name '{filePath}' -Value '{options}' -PropertyType String -Force | Out-Null", true);
}

private static ShellItem? GetFirstFile(ShellItem shi)
{
if (!shi.IsFolder || shi.Attributes.HasFlag(ShellItemAttribute.Stream))
Expand Down
Loading