Added Tests and Refactored code (#2129)

* Added Tests and Refactored code

* removed un-used file

* delete test files when test completes

* removed extra build configs

* added clean-up method

* removed unused variable

* re-added removed attributtion

* added error handling and move strings to string resource

* added error handling to file explorer view model

* moved varible assignment to if statement block

* removed savin of settings file from the UI

* re-added open source notice

* added missing controls for powerrename and fancy zones

* removed dead coded

* remove un-used configuration

* added error handling for file saving and updated powerreanme constructor

* removed added configurations

* added settings state
This commit is contained in:
Lavius Motileng
2020-04-17 15:25:08 -07:00
committed by GitHub
parent 7e1f554c4e
commit 3fc738b53a
37 changed files with 1538 additions and 504 deletions

View File

@@ -22,6 +22,8 @@ namespace Microsoft.PowerToys.Settings.UI.Lib
this.FancyzonesHighlightOpacity = new IntProperty();
this.FancyzonesEditorHotkey = new KeyBoardKeysProperty();
this.FancyzonesExcludedApps = new StringProperty();
this.FancyzonesInActiveColor = new StringProperty();
this.FancyzonesBorderColor = new StringProperty();
}
[JsonPropertyName("fancyzones_shiftDrag")]
@@ -62,5 +64,11 @@ namespace Microsoft.PowerToys.Settings.UI.Lib
[JsonPropertyName("fancyzones_excluded_apps")]
public StringProperty FancyzonesExcludedApps { get; set; }
[JsonPropertyName("fancyzones_zoneBorderColor")]
public StringProperty FancyzonesBorderColor { get; set; }
[JsonPropertyName("fancyzones_zoneColor")]
public StringProperty FancyzonesInActiveColor { get; set; }
}
}

View File

@@ -0,0 +1,53 @@
// 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.
using System.Text.Json;
using System.Text.Json.Serialization;
namespace Microsoft.PowerToys.Settings.UI.Lib
{
public class PowerRenameLocalProperties
{
public PowerRenameLocalProperties()
{
PersistState = false;
MRUEnabled = false;
MaxMRUSize = 0;
ShowIcon = false;
ExtendedContextMenuOnly = false;
}
private int _maxSize;
public bool PersistState { get; set; }
public bool MRUEnabled { get; set; }
public int MaxMRUSize
{
get
{
return _maxSize;
}
set
{
if (value < 0)
{
_maxSize = 0;
}
}
}
public bool ShowIcon { get; set; }
public bool ExtendedContextMenuOnly { get; set; }
public string ToJsonString()
{
return JsonSerializer.Serialize(this);
}
}
}

View File

@@ -10,26 +10,26 @@ namespace Microsoft.PowerToys.Settings.UI.Lib
{
public PowerRenameProperties()
{
PersistInput = new BoolProperty();
MruEnabled = new BoolProperty();
MaxMruSize = new IntProperty();
ShowIconInMenu = new BoolProperty();
ShowExtendedMenu = new BoolProperty();
PersistState = new BoolProperty();
MRUEnabled = new BoolProperty();
MaxMRUSize = new IntProperty();
ShowIcon = new BoolProperty();
ExtendedContextMenuOnly = new BoolProperty();
}
[JsonPropertyName("bool_persist_input")]
public BoolProperty PersistInput { get; set; }
public BoolProperty PersistState { get; set; }
[JsonPropertyName("bool_mru_enabled")]
public BoolProperty MruEnabled { get; set; }
public BoolProperty MRUEnabled { get; set; }
[JsonPropertyName("int_max_mru_size")]
public IntProperty MaxMruSize { get; set; }
public IntProperty MaxMRUSize { get; set; }
[JsonPropertyName("bool_show_icon_on_menu")]
public BoolProperty ShowIconInMenu { get; set; }
public BoolProperty ShowIcon { get; set; }
[JsonPropertyName("bool_show_extended_menu")]
public BoolProperty ShowExtendedMenu { get; set; }
public BoolProperty ExtendedContextMenuOnly { get; set; }
}
}

View File

@@ -15,7 +15,20 @@ namespace Microsoft.PowerToys.Settings.UI.Lib
{
properties = new PowerRenameProperties();
version = "1";
name = "_unset_";
name = "PowerRename";
}
public PowerRenameSettings(PowerRenameLocalProperties localProperties)
{
properties = new PowerRenameProperties();
properties.PersistState.Value = localProperties.PersistState;
properties.MRUEnabled.Value = localProperties.MRUEnabled;
properties.MaxMRUSize.Value = localProperties.MaxMRUSize;
properties.ShowIcon.Value = localProperties.ShowIcon;
properties.ExtendedContextMenuOnly.Value = localProperties.ExtendedContextMenuOnly;
version = "1";
name = "PowerRename";
}
public PowerRenameSettings(string ptName)

View File

@@ -10,6 +10,8 @@ namespace Microsoft.PowerToys.Settings.UI.Lib
{
public static class SettingsUtils
{
private const string DefaultFileName = "settings.json";
public static bool SettingsFolderExists(string powertoy)
{
return Directory.Exists(Path.Combine(LocalApplicationDataFolder(), $"Microsoft\\PowerToys\\{powertoy}"));
@@ -24,50 +26,56 @@ namespace Microsoft.PowerToys.Settings.UI.Lib
/// Get path to the json settings file.
/// </summary>
/// <returns>string path.</returns>
public static string GetSettingsPath(string powertoy)
public static string GetSettingsPath(string powertoy, string fileName = DefaultFileName)
{
if (string.IsNullOrWhiteSpace(powertoy))
{
return Path.Combine(
LocalApplicationDataFolder(),
$"Microsoft\\PowerToys\\settings.json");
$"Microsoft\\PowerToys\\{fileName}");
}
return Path.Combine(
LocalApplicationDataFolder(),
$"Microsoft\\PowerToys\\{powertoy}\\settings.json");
$"Microsoft\\PowerToys\\{powertoy}\\{fileName}");
}
public static bool SettingsExists(string powertoy)
public static bool SettingsExists(string powertoy, string fileName = DefaultFileName)
{
return File.Exists(GetSettingsPath(powertoy));
return File.Exists(GetSettingsPath(powertoy, fileName));
}
/// <summary>
/// Get a Deserialized object of the json settings string.
/// </summary>
/// <returns>Deserialized json settings object.</returns>
public static T GetSettings<T>(string powertoy)
public static T GetSettings<T>(string powertoy, string fileName = DefaultFileName)
{
var jsonSettingsString = File.ReadAllText(GetSettingsPath(powertoy));
var jsonSettingsString = File.ReadAllText(GetSettingsPath(powertoy, fileName));
return JsonSerializer.Deserialize<T>(jsonSettingsString);
}
// Save settings to a json file.
public static void SaveSettings(string jsonSettings, string powertoy)
public static void SaveSettings(string jsonSettings, string powertoy, string fileName = DefaultFileName)
{
if (jsonSettings != null)
try
{
if (!SettingsFolderExists(powertoy))
if (jsonSettings != null)
{
CreateSettingsFolder(powertoy);
}
if (!SettingsFolderExists(powertoy))
{
CreateSettingsFolder(powertoy);
}
File.WriteAllText(GetSettingsPath(powertoy), jsonSettings);
File.WriteAllText(GetSettingsPath(powertoy, fileName), jsonSettings);
}
}
catch
{
}
}
private static string LocalApplicationDataFolder()
public static string LocalApplicationDataFolder()
{
return Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
}