mirror of
https://github.com/microsoft/PowerToys.git
synced 2025-01-19 15:03:36 +08:00
[Hosts]Add setting to select the file encoding (#26495)
This commit is contained in:
parent
177c58f494
commit
9960d2d536
@ -168,7 +168,7 @@ namespace Hosts.Tests
|
||||
|
||||
var fileSystem = new CustomMockFileSystem();
|
||||
var userSettings = new Mock<IUserSettings>();
|
||||
userSettings.Setup(m => m.AdditionalLinesPosition).Returns(AdditionalLinesPosition.Top);
|
||||
userSettings.Setup(m => m.AdditionalLinesPosition).Returns(HostsAdditionalLinesPosition.Top);
|
||||
var service = new HostsService(fileSystem, userSettings.Object, _elevationHelper.Object);
|
||||
fileSystem.AddFile(service.HostsFilePath, new MockFileData(content));
|
||||
|
||||
@ -200,7 +200,7 @@ namespace Hosts.Tests
|
||||
|
||||
var fileSystem = new CustomMockFileSystem();
|
||||
var userSettings = new Mock<IUserSettings>();
|
||||
userSettings.Setup(m => m.AdditionalLinesPosition).Returns(AdditionalLinesPosition.Bottom);
|
||||
userSettings.Setup(m => m.AdditionalLinesPosition).Returns(HostsAdditionalLinesPosition.Bottom);
|
||||
|
||||
var service = new HostsService(fileSystem, userSettings.Object, _elevationHelper.Object);
|
||||
fileSystem.AddFile(service.HostsFilePath, new MockFileData(content));
|
||||
|
@ -38,6 +38,8 @@ namespace Hosts.Helpers
|
||||
|
||||
public event EventHandler FileChanged;
|
||||
|
||||
public Encoding Encoding => _userSettings.Encoding == HostsEncoding.Utf8 ? new UTF8Encoding(false) : new UTF8Encoding(true);
|
||||
|
||||
public HostsService(
|
||||
IFileSystem fileSystem,
|
||||
IUserSettings userSettings,
|
||||
@ -72,7 +74,7 @@ namespace Hosts.Helpers
|
||||
return (unparsedBuilder.ToString(), entries);
|
||||
}
|
||||
|
||||
var lines = await _fileSystem.File.ReadAllLinesAsync(HostsFilePath);
|
||||
var lines = await _fileSystem.File.ReadAllLinesAsync(HostsFilePath, Encoding);
|
||||
|
||||
for (var i = 0; i < lines.Length; i++)
|
||||
{
|
||||
@ -153,11 +155,11 @@ namespace Hosts.Helpers
|
||||
|
||||
if (!string.IsNullOrWhiteSpace(additionalLines))
|
||||
{
|
||||
if (_userSettings.AdditionalLinesPosition == AdditionalLinesPosition.Top)
|
||||
if (_userSettings.AdditionalLinesPosition == HostsAdditionalLinesPosition.Top)
|
||||
{
|
||||
lines.Insert(0, additionalLines);
|
||||
}
|
||||
else if (_userSettings.AdditionalLinesPosition == AdditionalLinesPosition.Bottom)
|
||||
else if (_userSettings.AdditionalLinesPosition == HostsAdditionalLinesPosition.Bottom)
|
||||
{
|
||||
lines.Add(additionalLines);
|
||||
}
|
||||
@ -174,7 +176,7 @@ namespace Hosts.Helpers
|
||||
_backupDone = true;
|
||||
}
|
||||
|
||||
await _fileSystem.File.WriteAllLinesAsync(HostsFilePath, lines);
|
||||
await _fileSystem.File.WriteAllLinesAsync(HostsFilePath, lines, Encoding);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
|
@ -13,7 +13,9 @@ namespace Hosts.Settings
|
||||
|
||||
public bool LoopbackDuplicates { get; }
|
||||
|
||||
public AdditionalLinesPosition AdditionalLinesPosition { get; }
|
||||
public HostsAdditionalLinesPosition AdditionalLinesPosition { get; }
|
||||
|
||||
public HostsEncoding Encoding { get; }
|
||||
|
||||
event EventHandler LoopbackDuplicatesChanged;
|
||||
}
|
||||
|
@ -38,14 +38,17 @@ namespace Hosts.Settings
|
||||
}
|
||||
}
|
||||
|
||||
public AdditionalLinesPosition AdditionalLinesPosition { get; private set; }
|
||||
public HostsAdditionalLinesPosition AdditionalLinesPosition { get; private set; }
|
||||
|
||||
public HostsEncoding Encoding { get; set; }
|
||||
|
||||
public UserSettings()
|
||||
{
|
||||
_settingsUtils = new SettingsUtils();
|
||||
ShowStartupWarning = true;
|
||||
LoopbackDuplicates = false;
|
||||
AdditionalLinesPosition = AdditionalLinesPosition.Top;
|
||||
AdditionalLinesPosition = HostsAdditionalLinesPosition.Top;
|
||||
Encoding = HostsEncoding.Utf8;
|
||||
|
||||
LoadSettingsFromJson();
|
||||
|
||||
@ -79,6 +82,7 @@ namespace Hosts.Settings
|
||||
{
|
||||
ShowStartupWarning = settings.Properties.ShowStartupWarning;
|
||||
AdditionalLinesPosition = settings.Properties.AdditionalLinesPosition;
|
||||
Encoding = settings.Properties.Encoding;
|
||||
LoopbackDuplicates = settings.Properties.LoopbackDuplicates;
|
||||
}
|
||||
|
||||
|
@ -4,7 +4,7 @@
|
||||
|
||||
namespace Settings.UI.Library.Enumerations
|
||||
{
|
||||
public enum AdditionalLinesPosition
|
||||
public enum HostsAdditionalLinesPosition
|
||||
{
|
||||
Top = 0,
|
||||
Bottom = 1,
|
@ -0,0 +1,12 @@
|
||||
// Copyright (c) Microsoft Corporation
|
||||
// The Microsoft Corporation licenses this file to you under the MIT license.
|
||||
// See the LICENSE file in the project root for more information.
|
||||
|
||||
namespace Settings.UI.Library.Enumerations
|
||||
{
|
||||
public enum HostsEncoding
|
||||
{
|
||||
Utf8 = 0,
|
||||
Utf8Bom = 1,
|
||||
}
|
||||
}
|
@ -18,14 +18,17 @@ namespace Microsoft.PowerToys.Settings.UI.Library
|
||||
[JsonConverter(typeof(BoolPropertyJsonConverter))]
|
||||
public bool LoopbackDuplicates { get; set; }
|
||||
|
||||
public AdditionalLinesPosition AdditionalLinesPosition { get; set; }
|
||||
public HostsAdditionalLinesPosition AdditionalLinesPosition { get; set; }
|
||||
|
||||
public HostsEncoding Encoding { get; set; }
|
||||
|
||||
public HostsProperties()
|
||||
{
|
||||
ShowStartupWarning = true;
|
||||
LaunchAdministrator = true;
|
||||
LoopbackDuplicates = false;
|
||||
AdditionalLinesPosition = AdditionalLinesPosition.Top;
|
||||
AdditionalLinesPosition = HostsAdditionalLinesPosition.Top;
|
||||
Encoding = HostsEncoding.Utf8;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -3537,4 +3537,17 @@ Activate by holding the key for the character you want to add an accent to, then
|
||||
<data name="MWB_SecurityKeyLabel.PlaceholderText" xml:space="preserve">
|
||||
<value>Security key</value>
|
||||
</data>
|
||||
<data name="Hosts_Encoding.Description" xml:space="preserve">
|
||||
<value>Chose the encoding of the hosts file</value>
|
||||
<comment>"Hosts" refers to the system hosts file, do not loc</comment>
|
||||
</data>
|
||||
<data name="Hosts_Encoding.Header" xml:space="preserve">
|
||||
<value>Encoding</value>
|
||||
</data>
|
||||
<data name="Hosts_Encoding_Utf8.Content" xml:space="preserve">
|
||||
<value>UTF-8</value>
|
||||
</data>
|
||||
<data name="Hosts_Encoding_Utf8Bom.Content" xml:space="preserve">
|
||||
<value>UTF-8 with BOM</value>
|
||||
</data>
|
||||
</root>
|
@ -109,7 +109,20 @@ namespace Microsoft.PowerToys.Settings.UI.ViewModels
|
||||
{
|
||||
if (value != (int)Settings.Properties.AdditionalLinesPosition)
|
||||
{
|
||||
Settings.Properties.AdditionalLinesPosition = (AdditionalLinesPosition)value;
|
||||
Settings.Properties.AdditionalLinesPosition = (HostsAdditionalLinesPosition)value;
|
||||
NotifyPropertyChanged();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public int Encoding
|
||||
{
|
||||
get => (int)Settings.Properties.Encoding;
|
||||
set
|
||||
{
|
||||
if (value != (int)Settings.Properties.Encoding)
|
||||
{
|
||||
Settings.Properties.Encoding = (HostsEncoding)value;
|
||||
NotifyPropertyChanged();
|
||||
}
|
||||
}
|
||||
|
@ -60,6 +60,12 @@
|
||||
x:Uid="ToggleSwitch"
|
||||
IsOn="{x:Bind Mode=TwoWay, Path=ViewModel.LoopbackDuplicates}" />
|
||||
</labs:SettingsCard>
|
||||
<labs:SettingsCard x:Uid="Hosts_Encoding">
|
||||
<ComboBox MinWidth="{StaticResource SettingActionControlMinWidth}" SelectedIndex="{x:Bind Path=ViewModel.Encoding, Mode=TwoWay}">
|
||||
<ComboBoxItem x:Uid="Hosts_Encoding_Utf8" />
|
||||
<ComboBoxItem x:Uid="Hosts_Encoding_Utf8Bom" />
|
||||
</ComboBox>
|
||||
</labs:SettingsCard>
|
||||
</controls:SettingsGroup>
|
||||
</StackPanel>
|
||||
</controls:SettingsPageControl.ModuleContent>
|
||||
|
Loading…
Reference in New Issue
Block a user