[fxcop] Settings UI (#7559)

* Remove redundant default initializations

* Implement IDisposable in HotkeySettingsControl

* Mark classes and methods as static

* Move Interop.ShowWindow to NativeMethods class

* Fix string-related warnings

* Remove unused argument for KeyEventHandler

* Log caught general exceptions

* Use safe navigation operator for null argument checks

* Suppress CA2007 warnings and enable FxCop

* Suppress warning for unused event handler params

* Use TryParse in ImageResizerPage

* Use ConfigureAwait(false) for CA2007
This commit is contained in:
Luthfi Mawarid
2020-10-29 14:24:16 -07:00
committed by GitHub
parent 7ec2ff5513
commit 215c353dee
18 changed files with 120 additions and 65 deletions

View File

@@ -3,6 +3,8 @@
// See the LICENSE file in the project root for more information.
using System;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using Microsoft.PowerToys.Settings.UI.Library;
using Microsoft.PowerToys.Settings.UI.Library.Utilities;
using Microsoft.PowerToys.Settings.UI.Library.ViewModels;
@@ -27,6 +29,7 @@ namespace Microsoft.PowerToys.Settings.UI.Views
/// Initializes a new instance of the <see cref="GeneralPage"/> class.
/// General Settings page constructor.
/// </summary>
[SuppressMessage("Design", "CA1031:Do not catch general exception types", Justification = "Exceptions from the IPC response handler should be caught and logged.")]
public GeneralPage()
{
InitializeComponent();
@@ -58,28 +61,30 @@ namespace Microsoft.PowerToys.Settings.UI.Views
{
str = ResourceLoader.GetForCurrentView().GetString("GeneralSettings_VersionIsLatest");
}
else if (version != string.Empty)
else if (!string.IsNullOrEmpty(version))
{
str = ResourceLoader.GetForCurrentView().GetString("GeneralSettings_NewVersionIsAvailable");
if (str != string.Empty)
if (!string.IsNullOrEmpty(str))
{
str += ": " + version;
}
}
ViewModel.LatestAvailableVersion = string.Format(str);
// Using CurrentCulture since this is user-facing
ViewModel.LatestAvailableVersion = string.Format(CultureInfo.CurrentCulture, str);
}
catch (Exception)
catch (Exception e)
{
Logger.LogError("Exception encountered when reading the version.", e);
}
});
DataContext = ViewModel;
}
public int UpdateUIThemeMethod(string themeName)
public static int UpdateUIThemeMethod(string themeName)
{
switch (themeName.ToUpperInvariant())
switch (themeName?.ToUpperInvariant())
{
case "LIGHT":
ShellPage.ShellHandler.RequestedTheme = ElementTheme.Light;
@@ -90,6 +95,9 @@ namespace Microsoft.PowerToys.Settings.UI.Views
case "SYSTEM":
ShellPage.ShellHandler.RequestedTheme = ElementTheme.Default;
break;
default:
Logger.LogError($"Unexpected theme name: {themeName}");
break;
}
return 0;

View File

@@ -2,6 +2,9 @@
// 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.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.Linq;
using Microsoft.PowerToys.Settings.UI.Library;
using Microsoft.PowerToys.Settings.UI.Library.Utilities;
@@ -25,28 +28,34 @@ namespace Microsoft.PowerToys.Settings.UI.Views
public void DeleteCustomSize(object sender, RoutedEventArgs e)
{
try
Button deleteRowButton = (Button)sender;
// Using InvariantCulture since this is internal and expected to be numerical
bool success = int.TryParse(deleteRowButton?.CommandParameter?.ToString(), NumberStyles.Integer, CultureInfo.InvariantCulture, out int rowNum);
if (success)
{
Button deleteRowButton = (Button)sender;
int rowNum = int.Parse(deleteRowButton.CommandParameter.ToString());
ViewModel.DeleteImageSize(rowNum);
}
catch
else
{
Logger.LogError("Failed to delete custom image size.");
}
}
[SuppressMessage("Design", "CA1031:Do not catch general exception types", Justification = "JSON exceptions from saving new settings should be caught and logged.")]
private void AddSizeButton_Click(object sender, RoutedEventArgs e)
{
try
{
ViewModel.AddRow();
}
catch
catch (Exception ex)
{
Logger.LogError("Exception encountered when adding a new image size.", ex);
}
}
[SuppressMessage("Usage", "CA1801:Review unused parameters", Justification = "Params are required for event handler signature requirements.")]
private void ImagesSizesListView_ContainerContentChanging(ListViewBase sender, ContainerContentChangingEventArgs args)
{
if (ViewModel.IsListViewFocusRequested)

View File

@@ -4,6 +4,7 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using Microsoft.PowerToys.Settings.UI.Library;
using Microsoft.PowerToys.Settings.UI.Library.Utilities;
@@ -56,17 +57,18 @@ namespace Microsoft.PowerToys.Settings.UI.Views
}
}
private void CombineRemappings(List<KeysDataModel> remapKeysList, uint leftKey, uint rightKey, uint combinedKey)
private static void CombineRemappings(List<KeysDataModel> remapKeysList, uint leftKey, uint rightKey, uint combinedKey)
{
KeysDataModel firstRemap = remapKeysList.Find(x => uint.Parse(x.OriginalKeys) == leftKey);
KeysDataModel secondRemap = remapKeysList.Find(x => uint.Parse(x.OriginalKeys) == rightKey);
// Using InvariantCulture for keys as they are internally represented as numerical values
KeysDataModel firstRemap = remapKeysList.Find(x => uint.Parse(x.OriginalKeys, CultureInfo.InvariantCulture) == leftKey);
KeysDataModel secondRemap = remapKeysList.Find(x => uint.Parse(x.OriginalKeys, CultureInfo.InvariantCulture) == rightKey);
if (firstRemap != null && secondRemap != null)
{
if (firstRemap.NewRemapKeys == secondRemap.NewRemapKeys)
{
KeysDataModel combinedRemap = new KeysDataModel
{
OriginalKeys = combinedKey.ToString(),
OriginalKeys = combinedKey.ToString(CultureInfo.InvariantCulture),
NewRemapKeys = firstRemap.NewRemapKeys,
};
remapKeysList.Insert(remapKeysList.IndexOf(firstRemap), combinedRemap);

View File

@@ -92,7 +92,7 @@ namespace Microsoft.PowerToys.Settings.UI.Views
/// Set Default IPC Message callback function.
/// </summary>
/// <param name="implementation">delegate function implementation.</param>
public void SetDefaultSndMessageCallback(IPCMessageCallback implementation)
public static void SetDefaultSndMessageCallback(IPCMessageCallback implementation)
{
DefaultSndMSGCallback = implementation;
}
@@ -101,7 +101,7 @@ namespace Microsoft.PowerToys.Settings.UI.Views
/// Set restart as admin IPC callback function.
/// </summary>
/// <param name="implementation">delegate function implementation.</param>
public void SetRestartAdminSndMessageCallback(IPCMessageCallback implementation)
public static void SetRestartAdminSndMessageCallback(IPCMessageCallback implementation)
{
SndRestartAsAdminMsgCallback = implementation;
}
@@ -110,17 +110,17 @@ namespace Microsoft.PowerToys.Settings.UI.Views
/// Set check for updates IPC callback function.
/// </summary>
/// <param name="implementation">delegate function implementation.</param>
public void SetCheckForUpdatesMessageCallback(IPCMessageCallback implementation)
public static void SetCheckForUpdatesMessageCallback(IPCMessageCallback implementation)
{
CheckForUpdatesMsgCallback = implementation;
}
public void SetElevationStatus(bool isElevated)
public static void SetElevationStatus(bool isElevated)
{
IsElevated = isElevated;
}
public void SetIsUserAnAdmin(bool isAdmin)
public static void SetIsUserAnAdmin(bool isAdmin)
{
IsUserAnAdmin = isAdmin;
}

View File

@@ -13,7 +13,7 @@ namespace Microsoft.PowerToys.Settings.UI.Views
{
public object Convert(object value, Type targetType, object parameter, string language)
{
return (value as IList).Count == 0 ? Visibility.Collapsed : Visibility.Visible;
return (value == null) || (value as IList).Count == 0 ? Visibility.Collapsed : Visibility.Visible;
}
public object ConvertBack(object value, Type targetType, object parameter, string language)