Skip to content

Commit

Permalink
.NET 8 updates
Browse files Browse the repository at this point in the history
- Update StyleCop version
- Tweak editor config
- Use collection initializer syntax
- Use default constructors where useful
  • Loading branch information
JeremyKuhne committed Nov 14, 2023
1 parent 2f7ca83 commit 3885518
Show file tree
Hide file tree
Showing 45 changed files with 153 additions and 254 deletions.
33 changes: 32 additions & 1 deletion .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,22 @@ dotnet_diagnostic.IDE0005.severity = error

# IDE0046: 'if' statement can be simplified.
dotnet_diagnostic.IDE0046.severity = silent
csharp_style_prefer_primary_constructors = true:silent
dotnet_diagnostic.SA1010.severity = none
csharp_style_prefer_null_check_over_type_check = true:suggestion
csharp_style_implicit_object_creation_when_type_is_apparent = true:suggestion
csharp_style_prefer_tuple_swap = true:suggestion
csharp_style_prefer_utf8_string_literals = true:suggestion
csharp_style_prefer_readonly_struct_member = true:suggestion
csharp_style_prefer_readonly_struct = true:suggestion
csharp_style_allow_blank_line_after_token_in_arrow_expression_clause_experimental = true:silent
csharp_style_allow_blank_line_after_token_in_conditional_expression_experimental = true:silent
csharp_style_allow_blank_line_after_colon_in_constructor_initializer_experimental = true:silent
csharp_style_allow_blank_lines_between_consecutive_braces_experimental = true:silent
csharp_style_allow_embedded_statements_on_same_line_experimental = true:silent
csharp_style_prefer_pattern_matching = true:silent
csharp_style_prefer_extended_property_pattern = true:suggestion
csharp_style_prefer_not_pattern = true:suggestion

[*.{cs,vb}]
dotnet_style_coalesce_expression = true:suggestion
Expand All @@ -392,4 +408,19 @@ end_of_line = crlf
dotnet_style_prefer_compound_assignment = true:suggestion
dotnet_code_quality_unused_parameters = all:suggestion
dotnet_style_prefer_simplified_interpolation = true:suggestion
dotnet_style_namespace_match_folder = true:suggestion
dotnet_style_namespace_match_folder = true:suggestion
dotnet_style_prefer_collection_expression = true:suggestion
dotnet_style_readonly_field = true:suggestion
dotnet_style_predefined_type_for_locals_parameters_members = true:silent
dotnet_style_predefined_type_for_member_access = true:silent
dotnet_style_require_accessibility_modifiers = for_non_interface_members:silent
dotnet_style_allow_multiple_blank_lines_experimental = true:silent
dotnet_style_allow_statement_immediately_after_block_experimental = true:silent
dotnet_style_parentheses_in_arithmetic_binary_operators = always_for_clarity:silent
dotnet_style_parentheses_in_other_binary_operators = always_for_clarity:silent
dotnet_style_parentheses_in_relational_binary_operators = always_for_clarity:silent
dotnet_style_parentheses_in_other_operators = never_if_unnecessary:silent
dotnet_style_qualification_for_property = false:silent
dotnet_style_qualification_for_field = false:silent
dotnet_style_qualification_for_event = false:silent
dotnet_style_qualification_for_method = false:silent
16 changes: 4 additions & 12 deletions src/samples/ActiveX/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,9 @@ public MainWindow(string title) : base(
}
}

private class MediaPlayer : ActiveXControl
private class MediaPlayer(Rectangle bounds, Window parentWindow, nint parameters = 0)
: ActiveXControl(CLSID.WindowsMediaPlayer, bounds, parentWindow, parameters)
{
public MediaPlayer(Rectangle bounds, Window parentWindow, nint parameters = 0)
: base(CLSID.WindowsMediaPlayer, bounds, parentWindow, parameters)
{
}

public string? URL
{
get => (string?)GetComProperty("URL");
Expand All @@ -63,13 +59,9 @@ public bool StretchToFit
}
}

private class SystemMonitor : ActiveXControl
private class SystemMonitor(Rectangle bounds, Window parentWindow, nint parameters = 0)
: ActiveXControl(s_systemMonitorClassId, bounds, parentWindow, parameters)
{
private static readonly Guid s_systemMonitorClassId = new("C4D2D8E0-D1DD-11CE-940F-008029004347");

public SystemMonitor(Rectangle bounds, Window parentWindow, nint parameters = 0)
: base(s_systemMonitorClassId, bounds, parentWindow, parameters)
{
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,9 @@ public unsafe partial class ActiveXControl
{
internal sealed unsafe partial class Container
{
private class ActiveXControlEnum : EnumUnknown
private class ActiveXControlEnum(IReadOnlyList<ActiveXControl>? controls) : EnumUnknown(controls?.Count ?? 0)
{
private readonly IReadOnlyList<ActiveXControl>? _controls;

public ActiveXControlEnum(IReadOnlyList<ActiveXControl>? controls)
: base(controls?.Count ?? 0)
{
_controls = controls;
}
private readonly IReadOnlyList<ActiveXControl>? _controls = controls;

protected override EnumUnknown Clone() => new ActiveXControlEnum(_controls);

Expand Down
11 changes: 3 additions & 8 deletions src/thirtytwo/Controls/ActiveXControl.Container.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,14 @@ namespace Windows;

public unsafe partial class ActiveXControl
{
internal sealed unsafe partial class Container : IOleContainer.Interface, IOleInPlaceFrame.Interface, IDisposable,
internal sealed unsafe partial class Container(Window containerWindow) : IOleContainer.Interface, IOleInPlaceFrame.Interface, IDisposable,
// IOleContainer : IParseDisplayName - IOleInPlaceFrame : IOleInPlaceUIWindow : IOleWindow
IManagedWrapper<IOleContainer, IParseDisplayName, IOleInPlaceFrame, IOleInPlaceUIWindow, IOleWindow>
{
internal Window Window { get; }
internal Window Window { get; } = containerWindow;

public AgileComPointer<IOleInPlaceActiveObject>? ActiveObject { get; private set; }

public Container(Window containerWindow)
{
Window = containerWindow;
}

HRESULT IOleContainer.Interface.ParseDisplayName(IBindCtx* pbc, PWSTR pszDisplayName, uint* pchEaten, IMoniker** ppmkOut)
{
if (ppmkOut is not null)
Expand Down Expand Up @@ -48,7 +43,7 @@ HRESULT IOleContainer.Interface.EnumObjects(uint grfFlags, IEnumUnknown** ppenum
List<ActiveXControl>? activeXControls = null;
if (((OLECONTF)grfFlags).HasFlag(OLECONTF.OLECONTF_EMBEDDINGS) && Window is not null)
{
activeXControls = new();
activeXControls = [];
Window.EnumerateChildWindows((HWND child) =>
{
if (FromHandle(child) is ActiveXControl control)
Expand Down
19 changes: 5 additions & 14 deletions src/thirtytwo/DeviceContextExtensions.ObjectScope.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,12 @@ public static unsafe partial class DeviceContextExtensions
/// <summary>
/// Scope for putting back an object selected into a device context.
/// </summary>
public readonly ref struct ObjectScope<T> where T : IHandle<HDC>
[method: SetsRequiredMembers]
public readonly ref struct ObjectScope<T>(HGDIOBJ priorObject, T deviceContext) where T : IHandle<HDC>
{
required public HGDIOBJ PriorObject { get; init; }
required public T DeviceContext { get; init; }
public required HGDIOBJ PriorObject { get; init; } = priorObject;
public required T DeviceContext { get; init; } = deviceContext;

[SetsRequiredMembers]
public ObjectScope(HGDIOBJ priorObject, T deviceContext)
{
PriorObject = priorObject;
DeviceContext = deviceContext;
}

public void Dispose()
{
DeviceContext.SelectObject(PriorObject);
}
public void Dispose() => DeviceContext.SelectObject(PriorObject);
}
}
10 changes: 5 additions & 5 deletions src/thirtytwo/DotNet/IManagedObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,23 +89,23 @@ public static void PopulateVTable(Vtbl* vtable)
vtable->GetObjectIdentity_5 = &GetObjectIdentity;
}

[UnmanagedCallersOnly(CallConvs = new[] { typeof(CallConvStdcall) })]
[UnmanagedCallersOnly(CallConvs = [typeof(CallConvStdcall)])]
private static HRESULT QueryInterface(IManagedObject* @this, Guid* riid, void** ppvObject)
=> UnwrapAndInvoke<IManagedObject, Interface>(@this, o => o.QueryInterface(riid, ppvObject));

[UnmanagedCallersOnly(CallConvs = new[] { typeof(CallConvStdcall) })]
[UnmanagedCallersOnly(CallConvs = [typeof(CallConvStdcall)])]
private static uint AddRef(IManagedObject* @this)
=> UnwrapAndInvoke<IManagedObject, Interface, uint>(@this, o => o.AddRef());

[UnmanagedCallersOnly(CallConvs = new[] { typeof(CallConvStdcall) })]
[UnmanagedCallersOnly(CallConvs = [typeof(CallConvStdcall)])]
private static uint Release(IManagedObject* @this)
=> UnwrapAndInvoke<IManagedObject, Interface, uint>(@this, o => o.Release());

[UnmanagedCallersOnly(CallConvs = new[] { typeof(CallConvStdcall) })]
[UnmanagedCallersOnly(CallConvs = [typeof(CallConvStdcall)])]
private static HRESULT GetSerializedBuffer(IManagedObject* @this, BSTR* pBSTR)
=> UnwrapAndInvoke<IManagedObject, Interface>(@this, o => o.GetSerializedBuffer(pBSTR));

[UnmanagedCallersOnly(CallConvs = new[] { typeof(CallConvStdcall) })]
[UnmanagedCallersOnly(CallConvs = [typeof(CallConvStdcall)])]
private static HRESULT GetObjectIdentity(IManagedObject* @this, BSTR* pBSTRGUID, int* AppDomainID, int* pCCW)
=> UnwrapAndInvoke<IManagedObject, Interface>(@this, o => o.GetObjectIdentity(pBSTRGUID, AppDomainID, pCCW));

Expand Down
6 changes: 2 additions & 4 deletions src/thirtytwo/Layout/FillLayout.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,9 @@

namespace Windows;

public class FillLayout : ILayoutHandler
public class FillLayout(ILayoutHandler handler) : ILayoutHandler
{
private readonly ILayoutHandler _handler;

public FillLayout(ILayoutHandler handler) => _handler = handler;
private readonly ILayoutHandler _handler = handler;

public void Layout(Rectangle bounds) => _handler.Layout(bounds);
}
26 changes: 9 additions & 17 deletions src/thirtytwo/Layout/FixedPercentLayout.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,16 @@

namespace Windows;

public class FixedPercentLayout : ILayoutHandler
public class FixedPercentLayout(
ILayoutHandler handler,
SizeF percent,
VerticalAlignment verticalAlignment = VerticalAlignment.Center,
HorizontalAlignment horizontalAlignment = HorizontalAlignment.Center) : ILayoutHandler
{
private readonly ILayoutHandler _handler;
private readonly SizeF _percent;
private readonly VerticalAlignment _verticalAlignment;
private readonly HorizontalAlignment _horizontalAlignment;

public FixedPercentLayout(
ILayoutHandler handler,
SizeF percent,
VerticalAlignment verticalAlignment = VerticalAlignment.Center,
HorizontalAlignment horizontalAlignment = HorizontalAlignment.Center)
{
_handler = handler;
_percent = percent;
_verticalAlignment = verticalAlignment;
_horizontalAlignment = horizontalAlignment;
}
private readonly ILayoutHandler _handler = handler;
private readonly SizeF _percent = percent;
private readonly VerticalAlignment _verticalAlignment = verticalAlignment;
private readonly HorizontalAlignment _horizontalAlignment = horizontalAlignment;

public void Layout(Rectangle bounds)
{
Expand Down
26 changes: 9 additions & 17 deletions src/thirtytwo/Layout/FixedSizeLayout.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,16 @@

namespace Windows;

public class FixedSizeLayout : ILayoutHandler
public class FixedSizeLayout(
ILayoutHandler handler,
Size size,
VerticalAlignment verticalAlignment = VerticalAlignment.Center,
HorizontalAlignment horizontalAlignment = HorizontalAlignment.Center) : ILayoutHandler
{
private readonly ILayoutHandler _handler;
private readonly Size _size;
private readonly VerticalAlignment _verticalAlignment;
private readonly HorizontalAlignment _horizontalAlignment;

public FixedSizeLayout(
ILayoutHandler handler,
Size size,
VerticalAlignment verticalAlignment = VerticalAlignment.Center,
HorizontalAlignment horizontalAlignment = HorizontalAlignment.Center)
{
_handler = handler;
_size = size;
_verticalAlignment = verticalAlignment;
_horizontalAlignment = horizontalAlignment;
}
private readonly ILayoutHandler _handler = handler;
private readonly Size _size = size;
private readonly VerticalAlignment _verticalAlignment = verticalAlignment;
private readonly HorizontalAlignment _horizontalAlignment = horizontalAlignment;

public void Layout(Rectangle bounds)
{
Expand Down
16 changes: 5 additions & 11 deletions src/thirtytwo/Layout/PaddedLayout.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,12 @@

namespace Windows;

public class PaddedLayout : ILayoutHandler
public class PaddedLayout(
Padding margin,
ILayoutHandler handler) : ILayoutHandler
{
private readonly ILayoutHandler _handler;
private readonly Padding _margin;

public PaddedLayout(
Padding margin,
ILayoutHandler handler)
{
_handler = handler;
_margin = margin;
}
private readonly ILayoutHandler _handler = handler;
private readonly Padding _margin = margin;

public void Layout(Rectangle bounds)
{
Expand Down
18 changes: 5 additions & 13 deletions src/thirtytwo/Layout/Padding.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,12 @@

namespace Windows;

public readonly struct Padding
public readonly struct Padding(int left, int top, int right, int bottom)
{
public readonly int Left;
public readonly int Top;
public readonly int Right;
public readonly int Bottom;

public Padding(int left, int top, int right, int bottom)
{
Left = left;
Top = top;
Right = right;
Bottom = bottom;
}
public readonly int Left = left;
public readonly int Top = top;
public readonly int Right = right;
public readonly int Bottom = bottom;

public static implicit operator Padding(int padding) => new(padding, padding, padding, padding);
public static implicit operator Padding((int Left, int Top, int Right, int Bottom) padding)
Expand Down
18 changes: 5 additions & 13 deletions src/thirtytwo/Layout/PaddingF.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,12 @@

namespace Windows;

public readonly struct PaddingF
public readonly struct PaddingF(float left, float top, float right, float bottom)
{
public readonly float Left;
public readonly float Top;
public readonly float Right;
public readonly float Bottom;

public PaddingF(float left, float top, float right, float bottom)
{
Left = left;
Top = top;
Right = right;
Bottom = bottom;
}
public readonly float Left = left;
public readonly float Top = top;
public readonly float Right = right;
public readonly float Bottom = bottom;

public static implicit operator PaddingF(float padding) => new(padding, padding, padding, padding);
public static implicit operator PaddingF((float Left, float Top, float Right, float Bottom) padding)
Expand Down
6 changes: 2 additions & 4 deletions src/thirtytwo/Layout/ReplaceableLayout.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@

namespace Windows;

public class ReplaceableLayout : ILayoutHandler
public class ReplaceableLayout(ILayoutHandler handler) : ILayoutHandler
{
public Rectangle _lastBounds;
private ILayoutHandler _layoutHandler;
private ILayoutHandler _layoutHandler = handler;

public ILayoutHandler Handler
{
Expand All @@ -20,8 +20,6 @@ public ILayoutHandler Handler
}
}

public ReplaceableLayout(ILayoutHandler handler) => _layoutHandler = handler;

public void Layout(Rectangle bounds)
{
_lastBounds = bounds;
Expand Down
9 changes: 2 additions & 7 deletions src/thirtytwo/Messages/Message.Create.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,9 @@ namespace Windows;

public static partial class Message
{
public readonly unsafe ref struct Create
public readonly unsafe ref struct Create(LPARAM lParam)
{
private readonly CREATESTRUCTW* _createStruct;

public Create(LPARAM lParam)
{
_createStruct = (CREATESTRUCTW*)(nint)lParam;
}
private readonly CREATESTRUCTW* _createStruct = (CREATESTRUCTW*)(nint)lParam;

public HINSTANCE Instance => _createStruct->hInstance;
public HMENU MenuHandle => _createStruct->hMenu;
Expand Down
Loading

0 comments on commit 3885518

Please sign in to comment.