mirror of
https://github.com/microsoft/PowerToys.git
synced 2026-04-09 12:46:47 +02:00
Added localization to default sizes (#8928)
This commit is contained in:
@@ -11,6 +11,9 @@ namespace Microsoft.PowerToys.Settings.UI.Library
|
||||
T GetSettings<T>(string powertoy = "", string fileName = "settings.json")
|
||||
where T : ISettingsConfig, new();
|
||||
|
||||
T GetSettingsOrDefault<T>(string powertoy = "", string fileName = "settings.json")
|
||||
where T : ISettingsConfig, new();
|
||||
|
||||
void SaveSettings(string jsonSettings, string powertoy = "", string fileName = "settings.json");
|
||||
|
||||
bool SettingsExists(string powertoy = "", string fileName = "settings.json");
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
// The Microsoft Corporation licenses this file to you under the MIT license.
|
||||
// See the LICENSE file in the project root for more information.
|
||||
|
||||
using System;
|
||||
using System.Collections.ObjectModel;
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Serialization;
|
||||
@@ -20,20 +21,29 @@ namespace Microsoft.PowerToys.Settings.UI.Library
|
||||
ImageresizerPngInterlaceOption = new IntProperty();
|
||||
ImageresizerTiffCompressOption = new IntProperty();
|
||||
ImageresizerFileName = new StringProperty("%1 (%2)");
|
||||
|
||||
ImageresizerSizes = new ImageResizerSizes(new ObservableCollection<ImageSize>()
|
||||
{
|
||||
new ImageSize(0, "Small", ResizeFit.Fit, 854, 480, ResizeUnit.Pixel),
|
||||
new ImageSize(1, "Medium", ResizeFit.Fit, 1366, 768, ResizeUnit.Pixel),
|
||||
new ImageSize(2, "Large", ResizeFit.Fit, 1920, 1080, ResizeUnit.Pixel),
|
||||
new ImageSize(3, "Phone", ResizeFit.Fit, 320, 568, ResizeUnit.Pixel),
|
||||
});
|
||||
|
||||
ImageresizerSizes = new ImageResizerSizes();
|
||||
ImageresizerKeepDateModified = new BoolProperty();
|
||||
ImageresizerFallbackEncoder = new StringProperty(new System.Guid("19e4a5aa-5662-4fc5-a0c0-1758028e1057").ToString());
|
||||
ImageresizerCustomSize = new ImageResizerCustomSizeProperty(new ImageSize(4, "custom", ResizeFit.Fit, 1024, 640, ResizeUnit.Pixel));
|
||||
}
|
||||
|
||||
public ImageResizerProperties(Func<string, string> resourceLoader)
|
||||
: this()
|
||||
{
|
||||
if (resourceLoader == null)
|
||||
{
|
||||
throw new NullReferenceException("Resource loader is null");
|
||||
}
|
||||
|
||||
ImageresizerSizes = new ImageResizerSizes(new ObservableCollection<ImageSize>()
|
||||
{
|
||||
new ImageSize(0, resourceLoader("ImageResizer_DefaultSize_Small"), ResizeFit.Fit, 854, 480, ResizeUnit.Pixel),
|
||||
new ImageSize(1, resourceLoader("ImageResizer_DefaultSize_Medium"), ResizeFit.Fit, 1366, 768, ResizeUnit.Pixel),
|
||||
new ImageSize(2, resourceLoader("ImageResizer_DefaultSize_Large"), ResizeFit.Fit, 1920, 1080, ResizeUnit.Pixel),
|
||||
new ImageSize(3, resourceLoader("ImageResizer_DefaultSize_Phone"), ResizeFit.Fit, 320, 568, ResizeUnit.Pixel),
|
||||
});
|
||||
}
|
||||
|
||||
[JsonPropertyName("imageresizer_selectedSizeIndex")]
|
||||
public IntProperty ImageresizerSelectedSizeIndex { get; set; }
|
||||
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
// The Microsoft Corporation licenses this file to you under the MIT license.
|
||||
// See the LICENSE file in the project root for more information.
|
||||
|
||||
using System;
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Serialization;
|
||||
using Microsoft.PowerToys.Settings.UI.Library.Interfaces;
|
||||
@@ -22,6 +23,12 @@ namespace Microsoft.PowerToys.Settings.UI.Library
|
||||
Properties = new ImageResizerProperties();
|
||||
}
|
||||
|
||||
public ImageResizerSettings(Func<string, string> resourceLoader)
|
||||
: this()
|
||||
{
|
||||
Properties = new ImageResizerProperties(resourceLoader);
|
||||
}
|
||||
|
||||
public override string ToJsonString()
|
||||
{
|
||||
var options = new JsonSerializerOptions
|
||||
|
||||
@@ -52,7 +52,7 @@ namespace Microsoft.PowerToys.Settings.UI.Library
|
||||
if (settingsConfig == null)
|
||||
{
|
||||
T settingsItem = new T();
|
||||
settingsConfig = _settingsUtils.GetSettings<T>(settingsItem.GetModuleName());
|
||||
settingsConfig = _settingsUtils.GetSettingsOrDefault<T>(settingsItem.GetModuleName());
|
||||
}
|
||||
|
||||
return settingsConfig;
|
||||
|
||||
@@ -46,37 +46,49 @@ namespace Microsoft.PowerToys.Settings.UI.Library
|
||||
_settingsPath.DeleteSettings(powertoy);
|
||||
}
|
||||
|
||||
public T GetSettings<T>(string powertoy = DefaultModuleName, string fileName = DefaultFileName)
|
||||
where T : ISettingsConfig, new()
|
||||
{
|
||||
if (!SettingsExists(powertoy, fileName))
|
||||
{
|
||||
throw new FileNotFoundException();
|
||||
}
|
||||
|
||||
// Given the file already exists, to deserialize the file and read it's content.
|
||||
T deserializedSettings = GetFile<T>(powertoy, fileName);
|
||||
|
||||
// If the file needs to be modified, to save the new configurations accordingly.
|
||||
if (deserializedSettings.UpgradeSettingsConfiguration())
|
||||
{
|
||||
SaveSettings(deserializedSettings.ToJsonString(), powertoy, fileName);
|
||||
}
|
||||
|
||||
return deserializedSettings;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get a Deserialized object of the json settings string.
|
||||
/// This function creates a file in the powertoy folder if it does not exist and returns an object with default properties.
|
||||
/// </summary>
|
||||
/// <returns>Deserialized json settings object.</returns>
|
||||
public T GetSettings<T>(string powertoy = DefaultModuleName, string fileName = DefaultFileName)
|
||||
public T GetSettingsOrDefault<T>(string powertoy = DefaultModuleName, string fileName = DefaultFileName)
|
||||
where T : ISettingsConfig, new()
|
||||
{
|
||||
if (SettingsExists(powertoy, fileName))
|
||||
try
|
||||
{
|
||||
try
|
||||
{
|
||||
// Given the file already exists, to deserialize the file and read it's content.
|
||||
T deserializedSettings = GetFile<T>(powertoy, fileName);
|
||||
return GetSettings<T>(powertoy, fileName);
|
||||
}
|
||||
|
||||
// If the file needs to be modified, to save the new configurations accordingly.
|
||||
if (deserializedSettings.UpgradeSettingsConfiguration())
|
||||
{
|
||||
SaveSettings(deserializedSettings.ToJsonString(), powertoy, fileName);
|
||||
}
|
||||
|
||||
return deserializedSettings;
|
||||
}
|
||||
|
||||
// Catch json deserialization exceptions when the file is corrupt and has an invalid json.
|
||||
// If there are any deserialization issues like in https://github.com/microsoft/PowerToys/issues/7500, log the error and create a new settings.json file.
|
||||
// This is different from the case where we have trailing zeros following a valid json file, which we have handled by trimming the trailing zeros.
|
||||
catch (JsonException ex)
|
||||
{
|
||||
Logger.LogError($"Exception encountered while loading {powertoy} settings.", ex);
|
||||
}
|
||||
// Catch json deserialization exceptions when the file is corrupt and has an invalid json.
|
||||
// If there are any deserialization issues like in https://github.com/microsoft/PowerToys/issues/7500, log the error and create a new settings.json file.
|
||||
// This is different from the case where we have trailing zeros following a valid json file, which we have handled by trimming the trailing zeros.
|
||||
catch (JsonException ex)
|
||||
{
|
||||
Logger.LogError($"Exception encountered while loading {powertoy} settings.", ex);
|
||||
}
|
||||
catch (FileNotFoundException)
|
||||
{
|
||||
Logger.LogInfo($"Settings file {fileName} for {powertoy} was not found.");
|
||||
}
|
||||
|
||||
// If the settings file does not exist or if the file is corrupt, to create a new object with default parameters and save it to a newly created settings file.
|
||||
|
||||
@@ -60,7 +60,7 @@ namespace Microsoft.PowerToys.Settings.UI.Library.ViewModels
|
||||
_settingsUtils = settingsUtils ?? throw new ArgumentNullException(nameof(settingsUtils));
|
||||
if (_settingsUtils.SettingsExists(ColorPickerSettings.ModuleName))
|
||||
{
|
||||
_colorPickerSettings = _settingsUtils.GetSettings<ColorPickerSettings>(ColorPickerSettings.ModuleName);
|
||||
_colorPickerSettings = _settingsUtils.GetSettingsOrDefault<ColorPickerSettings>(ColorPickerSettings.ModuleName);
|
||||
}
|
||||
else
|
||||
{
|
||||
|
||||
@@ -27,7 +27,7 @@ namespace Microsoft.PowerToys.Settings.UI.Library.ViewModels
|
||||
private Func<string, int> SendConfigMSG { get; }
|
||||
|
||||
[SuppressMessage("Design", "CA1031:Do not catch general exception types", Justification = "Exceptions should not crash the program but will be logged until we can understand common exception scenarios")]
|
||||
public ImageResizerViewModel(ISettingsUtils settingsUtils, ISettingsRepository<GeneralSettings> settingsRepository, Func<string, int> ipcMSGCallBackFunc)
|
||||
public ImageResizerViewModel(ISettingsUtils settingsUtils, ISettingsRepository<GeneralSettings> settingsRepository, Func<string, int> ipcMSGCallBackFunc, Func<string, string> resourceLoader)
|
||||
{
|
||||
_settingsUtils = settingsUtils ?? throw new ArgumentNullException(nameof(settingsUtils));
|
||||
|
||||
@@ -52,7 +52,7 @@ namespace Microsoft.PowerToys.Settings.UI.Library.ViewModels
|
||||
throw;
|
||||
}
|
||||
#endif
|
||||
Settings = new ImageResizerSettings();
|
||||
Settings = new ImageResizerSettings(resourceLoader);
|
||||
_settingsUtils.SaveSettings(Settings.ToJsonString(), ModuleName);
|
||||
}
|
||||
|
||||
|
||||
@@ -62,7 +62,7 @@ namespace Microsoft.PowerToys.Settings.UI.Library.ViewModels
|
||||
{
|
||||
try
|
||||
{
|
||||
Settings = _settingsUtils.GetSettings<KeyboardManagerSettings>(PowerToyName);
|
||||
Settings = _settingsUtils.GetSettingsOrDefault<KeyboardManagerSettings>(PowerToyName);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
@@ -214,7 +214,7 @@ namespace Microsoft.PowerToys.Settings.UI.Library.ViewModels
|
||||
|
||||
if (_settingsUtils.SettingsExists(PowerToyName, fileName))
|
||||
{
|
||||
_profile = _settingsUtils.GetSettings<KeyboardManagerProfile>(PowerToyName, fileName);
|
||||
_profile = _settingsUtils.GetSettingsOrDefault<KeyboardManagerProfile>(PowerToyName, fileName);
|
||||
}
|
||||
else
|
||||
{
|
||||
|
||||
@@ -58,7 +58,7 @@ namespace Microsoft.PowerToys.Settings.UI.Library.ViewModels
|
||||
|
||||
if (_settingsUtils.SettingsExists(PowerLauncherSettings.ModuleName))
|
||||
{
|
||||
settings = _settingsUtils.GetSettings<PowerLauncherSettings>(PowerLauncherSettings.ModuleName);
|
||||
settings = _settingsUtils.GetSettingsOrDefault<PowerLauncherSettings>(PowerLauncherSettings.ModuleName);
|
||||
}
|
||||
else
|
||||
{
|
||||
|
||||
@@ -42,7 +42,7 @@ namespace Microsoft.PowerToys.Settings.UI.Library.ViewModels
|
||||
|
||||
try
|
||||
{
|
||||
PowerRenameLocalProperties localSettings = _settingsUtils.GetSettings<PowerRenameLocalProperties>(GetSettingsSubPath(), "power-rename-settings.json");
|
||||
PowerRenameLocalProperties localSettings = _settingsUtils.GetSettingsOrDefault<PowerRenameLocalProperties>(GetSettingsSubPath(), "power-rename-settings.json");
|
||||
Settings = new PowerRenameSettings(localSettings);
|
||||
}
|
||||
catch (Exception e)
|
||||
|
||||
Reference in New Issue
Block a user