Skip to content

Commit

Permalink
Resolve Roslyn warnings, switch to latest C# features (#968)
Browse files Browse the repository at this point in the history
This PR addresses or suppresses all (but one) of the warnings. Some existed prior to .NET 8 in #966, but the majority were introduced in #966.

| Code       | Description                                                                                                                                                                               | Outcome                                               |
| ---------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ----------------------------------------------------- |
| CA1724     | Type names should not match namespaces                                                                                                                                                    | Suppressed in limited cases for `Pickers` and `Store` |
| CA1859     | Use concrete types when possible for improved performance                                                                                                                                 | Fixed in production code, suppressed for tests        |
| CA1861     | Avoid constant arrays as arguments                                                                                                                                                        | Suppressed for tests                                  |
| CA1862     | Use the 'StringComparison' method overloads to perform case-insensitive string comparisons                                                                                                | Fixed                                                 |
| CA2000     | Dispose objects before losing scope                                                                                                                                                       | Fixed                                                 |
| CA2213     | Disposable fields should be disposed                                                                                                                                                      | Fixed, suppressed for most tests                      |
| CA5392     | Use DefaultDllImportSearchPaths attribute for P/Invokes                                                                                                                                   | Fixed                                                 |
| CS0108     | 'TestProxyLayoutEngine.InnerLayoutEngine' hides inherited member 'BaseProxyLayoutEngine.InnerLayoutEngine'. Use the new keyword if hiding was intended.                                   | Fixed                                                 |
| IDE0005    | Remove unnecessary using directives                                                                                                                                                       | Fixed                                                 |
| IDE0028    | Use collection initializers or expressions                                                                                                                                                | Fixed                                                 |
| IDE0290    | Use primary constructor                                                                                                                                                                   | Fixed                                                 |
| IDE0300    | Use collection expression for array                                                                                                                                                       | Fixed                                                 |
| IDE0301    | Use collection expression for empty                                                                                                                                                       | Fixed                                                 |
| IDE0303    | Use collection expression for Create()                                                                                                                                                    | Fixed                                                 |
| IDE0305    | Use collection expression for fluent                                                                                                                                                      | Fixed                                                 |
| SYSLIB0051 | Legacy serialization support APIs are obsolete                                                                                                                                            | Fixed                                                 |
| xUnit1048  | Avoid using 'async void' for test methods as it is deprecated in xUnit.net v3                                                                                                             | Fixed                                                 |
| -          | Set MSBuild property 'GenerateDocumentationFile' to 'true' in project file to enable IDE0005 (Remove unnecessary usings/imports) on build (dotnet/roslyn#41640) | Fixed                                                 |

The only remaining warning is:

```log
NETSDK1206 Found version-specific or distribution-specific runtime identifier(s): win10-arm64, win10-x64, win10-x86.
Affected libraries: Microsoft.WindowsAppSDK. In .NET 8.0 and higher, assets for version-specific and distribution-specific
runtime identifiers will not be found by default. See https://aka.ms/dotnet/rid-usage for details.
```

I also cheekily decreased the default focus indicator size in this PR.
  • Loading branch information
dalyIsaac authored Aug 6, 2024
1 parent 0480abf commit 02d88de
Show file tree
Hide file tree
Showing 205 changed files with 1,102 additions and 1,460 deletions.
7 changes: 1 addition & 6 deletions src/Whim.Bar.Tests/BarConfigTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,7 @@ public class BarConfigTests
public void Height_PropertyChanged()
{
// Given
BarConfig config =
new(
leftComponents: new List<BarComponent>(),
centerComponents: new List<BarComponent>(),
rightComponents: new List<BarComponent>()
);
BarConfig config = new(leftComponents: [], centerComponents: [], rightComponents: []);

// When
// Then
Expand Down
15 changes: 4 additions & 11 deletions src/Whim.Bar.Tests/BarLayoutEngineTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,7 @@ public class BarLayoutEngineTests
{
private static BarLayoutEngine CreateSut(ILayoutEngine innerLayoutEngine) =>
new(
new BarConfig(
leftComponents: new List<BarComponent>(),
centerComponents: new List<BarComponent>(),
rightComponents: new List<BarComponent>()
)
{
Height = 30
},
new BarConfig(leftComponents: [], centerComponents: [], rightComponents: []) { Height = 30 },
innerLayoutEngine
);

Expand Down Expand Up @@ -278,8 +271,8 @@ public void DoLayout(ILayoutEngine innerLayoutEngine, IWindow window1, IWindow w
monitor.ScaleFactor.Returns(100);
BarLayoutEngine engine = CreateSut(innerLayoutEngine);

IWindowState[] expectedWindowStates = new[]
{
IWindowState[] expectedWindowStates =
[
new WindowState()
{
Window = window1,
Expand All @@ -303,7 +296,7 @@ public void DoLayout(ILayoutEngine innerLayoutEngine, IWindow window1, IWindow w
},
WindowSize = WindowSize.Normal
}
};
];

Rectangle<int> expectedGivenRect =
new()
Expand Down
8 changes: 4 additions & 4 deletions src/Whim.Bar.Tests/BarPluginTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public class BarPluginTests
public void MonitorManager_MonitorsChanged_RemovedMonitors(IContext context, IMonitor monitor)
{
// Given
BarConfig barConfig = new(new List<BarComponent>(), new List<BarComponent>(), new List<BarComponent>());
BarConfig barConfig = new([], [], []);
BarPlugin barPlugin = new(context, barConfig);
NativeManagerUtils.SetupTryEnqueue(context);

Expand All @@ -20,9 +20,9 @@ public void MonitorManager_MonitorsChanged_RemovedMonitors(IContext context, IMo
context.MonitorManager.MonitorsChanged += Raise.EventWith(
new MonitorsChangedEventArgs()
{
AddedMonitors = Array.Empty<IMonitor>(),
UnchangedMonitors = Array.Empty<IMonitor>(),
RemovedMonitors = new[] { monitor }
AddedMonitors = [],
UnchangedMonitors = [],
RemovedMonitors = [monitor]
}
);

Expand Down
10 changes: 1 addition & 9 deletions src/Whim.Bar.Tests/BaseBarLayoutEngineTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,7 @@ public class BaseBarLayoutEngineTests : ProxyLayoutEngineBaseTests
public override Func<ILayoutEngine, BaseProxyLayoutEngine> CreateLayoutEngine =>
(inner) =>
{
BarConfig config =
new(
leftComponents: new List<BarComponent>(),
centerComponents: new List<BarComponent>(),
rightComponents: new List<BarComponent>()
)
{
Height = 30
};
BarConfig config = new(leftComponents: [], centerComponents: [], rightComponents: []) { Height = 30 };

return new BarLayoutEngine(config, inner);
};
Expand Down
3 changes: 2 additions & 1 deletion src/Whim.Bar.Tests/Directory.Build.props
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<Project>
<PropertyGroup>
<GenerateDocumentationFile>false</GenerateDocumentationFile>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<NoWarn>$(NoWarn);CS1591</NoWarn>
</PropertyGroup>
</Project>
4 changes: 3 additions & 1 deletion src/Whim.Bar.Tests/GlobalSuppressions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,6 @@
[assembly: SuppressMessage("Style", "IDE0042:Variable declaration can be deconstructed")]
[assembly: SuppressMessage("Style", "IDE0058:Expression value is never used")]
[assembly: SuppressMessage("Style", "IDE0022:Use expression body for methods")]
[assembly: SuppressMessage("Performance", "CA1813:Avoid unsealed attributes", Justification = "It's fine for tests")]
[assembly: SuppressMessage("Performance", "CA1813:Avoid unsealed attributes")]
[assembly: SuppressMessage("Performance", "CA1852:Seal internal types")]
[assembly: SuppressMessage("Performance", "CA1861:Avoid constant arrays as arguments")]
25 changes: 12 additions & 13 deletions src/Whim.Bar/ActiveLayout/NextLayoutEngineCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,22 @@ namespace Whim.Bar;
/// <summary>
/// Command to switch to the next layout engine.
/// </summary>
internal class NextLayoutEngineCommand : System.Windows.Input.ICommand
/// <remarks>
/// Creates a new instance of <see cref="NextLayoutEngineCommand"/>.
/// </remarks>
/// <param name="context"></param>
/// <param name="viewModel"></param>
internal class NextLayoutEngineCommand(IContext context, ActiveLayoutWidgetViewModel viewModel)
: System.Windows.Input.ICommand
{
private readonly IContext _context;
private readonly ActiveLayoutWidgetViewModel _viewModel;
private readonly IContext _context = context;
private readonly ActiveLayoutWidgetViewModel _viewModel = viewModel;

/// <inheritdoc/>
public event EventHandler? CanExecuteChanged;

/// <summary>
/// Creates a new instance of <see cref="NextLayoutEngineCommand"/>.
/// </summary>
/// <param name="context"></param>
/// <param name="viewModel"></param>
public NextLayoutEngineCommand(IContext context, ActiveLayoutWidgetViewModel viewModel)
public event EventHandler? CanExecuteChanged
{
_context = context;
_viewModel = viewModel;
add { }
remove { }
}

/// <inheritdoc/>
Expand Down
35 changes: 14 additions & 21 deletions src/Whim.Bar/BarConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,25 +18,35 @@ namespace Whim.Bar;
/// <remarks>
/// The components lists can be changed up until when Whim is initialized.
/// </remarks>
public class BarConfig : INotifyPropertyChanged
/// <remarks>
/// Creates a new bar configuration.
/// </remarks>
/// <param name="leftComponents">The components to display on the left side of the bar.</param>
/// <param name="centerComponents">The components to display in the center of the bar.</param>
/// <param name="rightComponents">The components to display on the right side of the bar.</param>
public class BarConfig(
IList<BarComponent> leftComponents,
IList<BarComponent> centerComponents,
IList<BarComponent> rightComponents
) : INotifyPropertyChanged
{
/// <inheritdoc/>
public event PropertyChangedEventHandler? PropertyChanged;

/// <summary>
/// The components to display on the left side of the bar.
/// </summary>
internal IList<BarComponent> LeftComponents;
internal IList<BarComponent> LeftComponents = leftComponents;

/// <summary>
/// The components to display in the center of the bar.
/// </summary>
internal IList<BarComponent> CenterComponents;
internal IList<BarComponent> CenterComponents = centerComponents;

/// <summary>
/// The components to display on the right side of the bar.
/// </summary>
internal IList<BarComponent> RightComponents;
internal IList<BarComponent> RightComponents = rightComponents;

private int _height = GetHeightFromResourceDictionary() ?? 30;

Expand Down Expand Up @@ -75,23 +85,6 @@ protected virtual void OnPropertyChanged(string propertyName)
/// </remarks>
public WindowBackdropConfig Backdrop { get; set; } = new(BackdropType.Mica, AlwaysShowBackdrop: true);

/// <summary>
/// Creates a new bar configuration.
/// </summary>
/// <param name="leftComponents">The components to display on the left side of the bar.</param>
/// <param name="centerComponents">The components to display in the center of the bar.</param>
/// <param name="rightComponents">The components to display on the right side of the bar.</param>
public BarConfig(
IList<BarComponent> leftComponents,
IList<BarComponent> centerComponents,
IList<BarComponent> rightComponents
)
{
LeftComponents = leftComponents;
CenterComponents = centerComponents;
RightComponents = rightComponents;
}

private static int? GetHeightFromResourceDictionary()
{
try
Expand Down
11 changes: 0 additions & 11 deletions src/Whim.Bar/BarException.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,4 @@ public BarException(string message)
/// <param name="inner">The exception that is the cause of the current exception.</param>
public BarException(string message, System.Exception inner)
: base(message, inner) { }

/// <summary>
/// Constructs a new BarException.
/// </summary>
/// <param name="info">The <see cref="System.Runtime.Serialization.SerializationInfo"/> that holds the serialized object data about the exception being thrown.</param>
/// <param name="context">The <see cref="System.Runtime.Serialization.StreamingContext"/> that contains contextual information about the source or destination.</param>
protected BarException(
System.Runtime.Serialization.SerializationInfo info,
System.Runtime.Serialization.StreamingContext context
)
: base(info, context) { }
}
24 changes: 9 additions & 15 deletions src/Whim.Bar/BarPlugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,30 +6,24 @@
namespace Whim.Bar;

/// <inheritdoc/>
public class BarPlugin : IBarPlugin
/// <summary>
/// Create the bar plugin.
/// </summary>
/// <param name="context"></param>
/// <param name="barConfig"></param>
public class BarPlugin(IContext context, BarConfig barConfig) : IBarPlugin
{
private readonly IContext _context;
private readonly BarConfig _barConfig;
private readonly IContext _context = context;
private readonly BarConfig _barConfig = barConfig;

private readonly Dictionary<IMonitor, BarWindow> _monitorBarMap = new();
private readonly Dictionary<IMonitor, BarWindow> _monitorBarMap = [];
private bool _disposedValue;

/// <summary>
/// <c>whim.bar</c>
/// </summary>
public string Name => "whim.bar";

/// <summary>
/// Create the bar plugin.
/// </summary>
/// <param name="context"></param>
/// <param name="barConfig"></param>
public BarPlugin(IContext context, BarConfig barConfig)
{
_context = context;
_barConfig = barConfig;
}

/// <inheritdoc />
public void PreInitialize()
{
Expand Down
2 changes: 1 addition & 1 deletion src/Whim.Bar/FocusedWindow/FocusedWindowWidget.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace Whim.Bar;
/// </summary>
public partial class FocusedWindowWidget : UserControl
{
private static readonly char[] _titleSeparators = new[] { '-', '—', '|' };
private static readonly char[] _titleSeparators = ['-', '—', '|'];

/// <summary>
/// The focused window view model.
Expand Down
2 changes: 1 addition & 1 deletion src/Whim.Bar/Workspace/WorkspaceWidgetViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ internal class WorkspaceWidgetViewModel : IDisposable
/// <summary>
/// The workspaces for the monitor.
/// </summary>
public VeryObservableCollection<WorkspaceModel> Workspaces { get; } = new();
public VeryObservableCollection<WorkspaceModel> Workspaces { get; } = [];

/// <summary>
/// Creates a new instance of <see cref="WorkspaceWidgetViewModel"/>.
Expand Down
45 changes: 22 additions & 23 deletions src/Whim.CommandPalette.Tests/CommandPaletteCommandsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ public Wrapper()
Windows[2] = Substitute.For<IWindow>();
Windows[2].Title.Returns("Window 2");

Workspace.Windows.Returns((_) => new List<IWindow>() { Windows[0], Windows[1] });
OtherWorkspace.Windows.Returns((_) => new List<IWindow>() { Windows[2] });
Workspace.Windows.Returns((_) => [Windows[0], Windows[1]]);
OtherWorkspace.Windows.Returns((_) => [Windows[2]]);

Commands = new(Context, Plugin);
}
Expand Down Expand Up @@ -210,34 +210,33 @@ public void MoveMultipleWindowsToWorkspaceCallback()
// Given
Wrapper wrapper = new();
List<SelectOption> options =
new()
[
new SelectOption()
{
new SelectOption()
{
Id = "0",
Title = "Window 0",
IsSelected = true
},
new SelectOption()
{
Id = "1",
Title = "Window 1",
IsSelected = false
},
new SelectOption()
{
Id = "2",
Title = "Window 2",
IsSelected = true
},
};
Id = "0",
Title = "Window 0",
IsSelected = true
},
new SelectOption()
{
Id = "1",
Title = "Window 1",
IsSelected = false
},
new SelectOption()
{
Id = "2",
Title = "Window 2",
IsSelected = true
},
];

// When
CommandPaletteCommands commands = new(wrapper.Context, wrapper.Plugin);
commands.MoveMultipleWindowsToWorkspaceCallback(options);

// Then
string[] expectedWorkspaces = new string[] { "Workspace", "Other workspace" };
string[] expectedWorkspaces = ["Workspace", "Other workspace"];
wrapper
.Plugin.Received(1)
.Activate(
Expand Down
Loading

0 comments on commit 02d88de

Please sign in to comment.