diff --git a/.editorconfig b/.editorconfig index 6ec6e8c..7d20e56 100644 --- a/.editorconfig +++ b/.editorconfig @@ -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 @@ -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 \ No newline at end of file +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 \ No newline at end of file diff --git a/src/samples/ActiveX/Program.cs b/src/samples/ActiveX/Program.cs index d6b2c7c..d8f7f6d 100644 --- a/src/samples/ActiveX/Program.cs +++ b/src/samples/ActiveX/Program.cs @@ -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"); @@ -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) - { - } } } \ No newline at end of file diff --git a/src/thirtytwo/Controls/ActiveXControl.Container.ActiveXControlEnum.cs b/src/thirtytwo/Controls/ActiveXControl.Container.ActiveXControlEnum.cs index 5e453f7..1782f7d 100644 --- a/src/thirtytwo/Controls/ActiveXControl.Container.ActiveXControlEnum.cs +++ b/src/thirtytwo/Controls/ActiveXControl.Container.ActiveXControlEnum.cs @@ -9,15 +9,9 @@ public unsafe partial class ActiveXControl { internal sealed unsafe partial class Container { - private class ActiveXControlEnum : EnumUnknown + private class ActiveXControlEnum(IReadOnlyList? controls) : EnumUnknown(controls?.Count ?? 0) { - private readonly IReadOnlyList? _controls; - - public ActiveXControlEnum(IReadOnlyList? controls) - : base(controls?.Count ?? 0) - { - _controls = controls; - } + private readonly IReadOnlyList? _controls = controls; protected override EnumUnknown Clone() => new ActiveXControlEnum(_controls); diff --git a/src/thirtytwo/Controls/ActiveXControl.Container.cs b/src/thirtytwo/Controls/ActiveXControl.Container.cs index 3628394..2a6f006 100644 --- a/src/thirtytwo/Controls/ActiveXControl.Container.cs +++ b/src/thirtytwo/Controls/ActiveXControl.Container.cs @@ -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 { - internal Window Window { get; } + internal Window Window { get; } = containerWindow; public AgileComPointer? 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) @@ -48,7 +43,7 @@ HRESULT IOleContainer.Interface.EnumObjects(uint grfFlags, IEnumUnknown** ppenum List? 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) diff --git a/src/thirtytwo/DeviceContextExtensions.ObjectScope.cs b/src/thirtytwo/DeviceContextExtensions.ObjectScope.cs index f1eae8e..85905ce 100644 --- a/src/thirtytwo/DeviceContextExtensions.ObjectScope.cs +++ b/src/thirtytwo/DeviceContextExtensions.ObjectScope.cs @@ -10,21 +10,12 @@ public static unsafe partial class DeviceContextExtensions /// /// Scope for putting back an object selected into a device context. /// - public readonly ref struct ObjectScope where T : IHandle + [method: SetsRequiredMembers] + public readonly ref struct ObjectScope(HGDIOBJ priorObject, T deviceContext) where T : IHandle { - 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); } } \ No newline at end of file diff --git a/src/thirtytwo/DotNet/IManagedObject.cs b/src/thirtytwo/DotNet/IManagedObject.cs index d3c701a..5333924 100644 --- a/src/thirtytwo/DotNet/IManagedObject.cs +++ b/src/thirtytwo/DotNet/IManagedObject.cs @@ -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(@this, o => o.QueryInterface(riid, ppvObject)); - [UnmanagedCallersOnly(CallConvs = new[] { typeof(CallConvStdcall) })] + [UnmanagedCallersOnly(CallConvs = [typeof(CallConvStdcall)])] private static uint AddRef(IManagedObject* @this) => UnwrapAndInvoke(@this, o => o.AddRef()); - [UnmanagedCallersOnly(CallConvs = new[] { typeof(CallConvStdcall) })] + [UnmanagedCallersOnly(CallConvs = [typeof(CallConvStdcall)])] private static uint Release(IManagedObject* @this) => UnwrapAndInvoke(@this, o => o.Release()); - [UnmanagedCallersOnly(CallConvs = new[] { typeof(CallConvStdcall) })] + [UnmanagedCallersOnly(CallConvs = [typeof(CallConvStdcall)])] private static HRESULT GetSerializedBuffer(IManagedObject* @this, BSTR* pBSTR) => UnwrapAndInvoke(@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(@this, o => o.GetObjectIdentity(pBSTRGUID, AppDomainID, pCCW)); diff --git a/src/thirtytwo/Layout/FillLayout.cs b/src/thirtytwo/Layout/FillLayout.cs index eaac827..e401517 100644 --- a/src/thirtytwo/Layout/FillLayout.cs +++ b/src/thirtytwo/Layout/FillLayout.cs @@ -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); } \ No newline at end of file diff --git a/src/thirtytwo/Layout/FixedPercentLayout.cs b/src/thirtytwo/Layout/FixedPercentLayout.cs index 39a87f6..2b557e2 100644 --- a/src/thirtytwo/Layout/FixedPercentLayout.cs +++ b/src/thirtytwo/Layout/FixedPercentLayout.cs @@ -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) { diff --git a/src/thirtytwo/Layout/FixedSizeLayout.cs b/src/thirtytwo/Layout/FixedSizeLayout.cs index ce0e8b4..cb94834 100644 --- a/src/thirtytwo/Layout/FixedSizeLayout.cs +++ b/src/thirtytwo/Layout/FixedSizeLayout.cs @@ -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) { diff --git a/src/thirtytwo/Layout/PaddedLayout.cs b/src/thirtytwo/Layout/PaddedLayout.cs index a60e872..1f12ae6 100644 --- a/src/thirtytwo/Layout/PaddedLayout.cs +++ b/src/thirtytwo/Layout/PaddedLayout.cs @@ -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) { diff --git a/src/thirtytwo/Layout/Padding.cs b/src/thirtytwo/Layout/Padding.cs index c2a1585..7095897 100644 --- a/src/thirtytwo/Layout/Padding.cs +++ b/src/thirtytwo/Layout/Padding.cs @@ -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) diff --git a/src/thirtytwo/Layout/PaddingF.cs b/src/thirtytwo/Layout/PaddingF.cs index 4e57faf..aca08b7 100644 --- a/src/thirtytwo/Layout/PaddingF.cs +++ b/src/thirtytwo/Layout/PaddingF.cs @@ -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) diff --git a/src/thirtytwo/Layout/ReplaceableLayout.cs b/src/thirtytwo/Layout/ReplaceableLayout.cs index 2997fab..af20b19 100644 --- a/src/thirtytwo/Layout/ReplaceableLayout.cs +++ b/src/thirtytwo/Layout/ReplaceableLayout.cs @@ -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 { @@ -20,8 +20,6 @@ public ILayoutHandler Handler } } - public ReplaceableLayout(ILayoutHandler handler) => _layoutHandler = handler; - public void Layout(Rectangle bounds) { _lastBounds = bounds; diff --git a/src/thirtytwo/Messages/Message.Create.cs b/src/thirtytwo/Messages/Message.Create.cs index 80f9a53..9207165 100644 --- a/src/thirtytwo/Messages/Message.Create.cs +++ b/src/thirtytwo/Messages/Message.Create.cs @@ -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; diff --git a/src/thirtytwo/Messages/Message.DpiChanged.cs b/src/thirtytwo/Messages/Message.DpiChanged.cs index 529f91a..d69687e 100644 --- a/src/thirtytwo/Messages/Message.DpiChanged.cs +++ b/src/thirtytwo/Messages/Message.DpiChanged.cs @@ -7,15 +7,9 @@ namespace Windows; public static partial class Message { - public readonly ref struct DpiChanged + public unsafe readonly ref struct DpiChanged(WPARAM wParam, LPARAM lParam) { - public uint Dpi { get; } - public Rectangle SuggestedBounds { get; } - - public unsafe DpiChanged(WPARAM wParam, LPARAM lParam) - { - Dpi = wParam.HIWORD; - SuggestedBounds = *(RECT*)lParam; - } + public uint Dpi { get; } = wParam.HIWORD; + public Rectangle SuggestedBounds { get; } = *(RECT*)lParam; } } \ No newline at end of file diff --git a/src/thirtytwo/Messages/Message.DrawItem.cs b/src/thirtytwo/Messages/Message.DrawItem.cs index 968eb11..812eb9f 100644 --- a/src/thirtytwo/Messages/Message.DrawItem.cs +++ b/src/thirtytwo/Messages/Message.DrawItem.cs @@ -7,14 +7,9 @@ namespace Windows; public static partial class Message { - public readonly unsafe ref struct DrawItem + public readonly unsafe ref struct DrawItem(LPARAM lParam) { - private readonly DRAWITEMSTRUCT* _drawItemStruct; - - public DrawItem(LPARAM lParam) - { - _drawItemStruct = (DRAWITEMSTRUCT*)lParam; - } + private readonly DRAWITEMSTRUCT* _drawItemStruct = (DRAWITEMSTRUCT*)lParam; public ControlType Type => (ControlType)_drawItemStruct->CtlType; public uint ControlId => _drawItemStruct->CtlID; diff --git a/src/thirtytwo/Messages/Message.KeyUpDown.cs b/src/thirtytwo/Messages/Message.KeyUpDown.cs index e6e8073..7d34186 100644 --- a/src/thirtytwo/Messages/Message.KeyUpDown.cs +++ b/src/thirtytwo/Messages/Message.KeyUpDown.cs @@ -7,13 +7,10 @@ namespace Windows; public static partial class Message { - public readonly ref struct KeyUpDown +#pragma warning disable CS9113 // LPARAM is needed for other details, just haven't implemented yet. + public readonly ref struct KeyUpDown(WPARAM wParam, LPARAM lParam) +#pragma warning restore CS9113 { - public KeyUpDown(WPARAM wParam, LPARAM lParam) - { - Key = (VIRTUAL_KEY)(nuint)wParam; - } - - public VIRTUAL_KEY Key { get; } + public VIRTUAL_KEY Key { get; } = (VIRTUAL_KEY)(nuint)wParam; } } \ No newline at end of file diff --git a/src/thirtytwo/Messages/Message.PrintClient.cs b/src/thirtytwo/Messages/Message.PrintClient.cs index 037a3df..1346b07 100644 --- a/src/thirtytwo/Messages/Message.PrintClient.cs +++ b/src/thirtytwo/Messages/Message.PrintClient.cs @@ -5,15 +5,10 @@ namespace Windows; public static partial class Message { - public readonly ref struct PrintClient + public readonly ref struct PrintClient(WPARAM wParam) { - public HDC HDC { get; } + public HDC HDC { get; } = new((nint)wParam.Value); public DeviceContext DeviceContext => DeviceContext.Create(HDC); - - public unsafe PrintClient(WPARAM wParam) - { - HDC = new((nint)wParam.Value); - } } } \ No newline at end of file diff --git a/src/thirtytwo/Messages/Message.SetText.cs b/src/thirtytwo/Messages/Message.SetText.cs index 7f811f3..312bbe3 100644 --- a/src/thirtytwo/Messages/Message.SetText.cs +++ b/src/thirtytwo/Messages/Message.SetText.cs @@ -7,13 +7,8 @@ namespace Windows; public static partial class Message { - public readonly ref struct SetText + public unsafe readonly ref struct SetText(LPARAM lParam) { - public ReadOnlySpan Text { get; } - - public unsafe SetText(LPARAM lParam) - { - Text = Conversion.NullTerminatedStringToSpan((char*)lParam); - } + public ReadOnlySpan Text { get; } = Conversion.NullTerminatedStringToSpan((char*)lParam); } } \ No newline at end of file diff --git a/src/thirtytwo/Messages/Message.Size.cs b/src/thirtytwo/Messages/Message.Size.cs index f1c21a9..646db4b 100644 --- a/src/thirtytwo/Messages/Message.Size.cs +++ b/src/thirtytwo/Messages/Message.Size.cs @@ -5,16 +5,10 @@ namespace Windows; public static partial class Message { - public readonly ref struct Size + public readonly ref struct Size(WPARAM wParam, LPARAM lParam) { - public System.Drawing.Size NewSize { get; } - public SizeType Type { get; } - - public Size(WPARAM wParam, LPARAM lParam) - { - NewSize = new System.Drawing.Size(lParam.LOWORD, lParam.HIWORD); - Type = (SizeType)(int)wParam; - } + public System.Drawing.Size NewSize { get; } = new System.Drawing.Size(lParam.LOWORD, lParam.HIWORD); + public SizeType Type { get; } = (SizeType)(int)wParam; public enum SizeType { diff --git a/src/thirtytwo/Messages/Message.Timer.cs b/src/thirtytwo/Messages/Message.Timer.cs index 45e1456..cc19fca 100644 --- a/src/thirtytwo/Messages/Message.Timer.cs +++ b/src/thirtytwo/Messages/Message.Timer.cs @@ -5,16 +5,10 @@ namespace Windows; public static partial class Message { - public readonly ref struct Timer + public readonly ref struct Timer(WPARAM wParam, LPARAM lParam) { - private readonly WPARAM _wParam; - private readonly LPARAM _lParam; - - public Timer(WPARAM wParam, LPARAM lParam) - { - _wParam = wParam; - _lParam = lParam; - } + private readonly WPARAM _wParam = wParam; + private readonly LPARAM _lParam = lParam; public uint Id => (uint)_wParam; diff --git a/src/thirtytwo/Support/DriveLockedException.cs b/src/thirtytwo/Support/DriveLockedException.cs index 7ddc56b..bbc60aa 100644 --- a/src/thirtytwo/Support/DriveLockedException.cs +++ b/src/thirtytwo/Support/DriveLockedException.cs @@ -3,8 +3,6 @@ namespace Windows.Support; -public class DriveLockedException : ThirtyTwoIOException +public class DriveLockedException(string? message = null) : ThirtyTwoIOException(HRESULT.FVE_E_LOCKED_VOLUME, message) { - public DriveLockedException(string? message = null) - : base(HRESULT.FVE_E_LOCKED_VOLUME, message) { } } \ No newline at end of file diff --git a/src/thirtytwo/Support/DriveNotReadyException.cs b/src/thirtytwo/Support/DriveNotReadyException.cs index ea00d31..95ffaa4 100644 --- a/src/thirtytwo/Support/DriveNotReadyException.cs +++ b/src/thirtytwo/Support/DriveNotReadyException.cs @@ -3,8 +3,6 @@ namespace Windows.Support; -public class DriveNotReadyException : ThirtyTwoIOException +public class DriveNotReadyException(string? message = null) : ThirtyTwoIOException(WIN32_ERROR.ERROR_NOT_READY, message) { - public DriveNotReadyException(string? message = null) - : base(WIN32_ERROR.ERROR_NOT_READY, message) { } } \ No newline at end of file diff --git a/src/thirtytwo/Support/FileExistsException.cs b/src/thirtytwo/Support/FileExistsException.cs index 4e21f9d..b0053a5 100644 --- a/src/thirtytwo/Support/FileExistsException.cs +++ b/src/thirtytwo/Support/FileExistsException.cs @@ -3,8 +3,6 @@ namespace Windows.Support; -public class FileExistsException : ThirtyTwoIOException +public class FileExistsException(WIN32_ERROR error, string? message = null) : ThirtyTwoIOException(error, message) { - public FileExistsException(WIN32_ERROR error, string? message = null) - : base(error, message) { } } \ No newline at end of file diff --git a/src/thirtytwo/Support/HandleRef.cs b/src/thirtytwo/Support/HandleRef.cs index 87b9f5a..fe84b33 100644 --- a/src/thirtytwo/Support/HandleRef.cs +++ b/src/thirtytwo/Support/HandleRef.cs @@ -17,8 +17,8 @@ namespace Windows.Support; internal readonly struct HandleRef : IHandle, IEquatable> where THandle : unmanaged, IEquatable { - required public object? Wrapper { get; init; } - required public THandle Handle { get; init; } + public required object? Wrapper { get; init; } + public required THandle Handle { get; init; } [SetsRequiredMembers] public HandleRef(object? wrapper, THandle handle) diff --git a/src/thirtytwo/Support/SpanExtensions.cs b/src/thirtytwo/Support/SpanExtensions.cs index 4b7dc43..e6133e5 100644 --- a/src/thirtytwo/Support/SpanExtensions.cs +++ b/src/thirtytwo/Support/SpanExtensions.cs @@ -30,7 +30,7 @@ public static Span SliceAtNull(this Span span) /// public static IEnumerable Split(this ReadOnlySpan span, char delimiter, bool includeEmptyStrings = false) { - List strings = new(); + List strings = []; SpanReader reader = new(span); while (reader.TryReadTo(out var next, delimiter)) { diff --git a/src/thirtytwo/Support/SpanReader.cs b/src/thirtytwo/Support/SpanReader.cs index d88e5aa..a5bd0d5 100644 --- a/src/thirtytwo/Support/SpanReader.cs +++ b/src/thirtytwo/Support/SpanReader.cs @@ -8,13 +8,11 @@ namespace Windows.Support; /// /// Simple span reader. Follows . /// -public ref struct SpanReader where T : unmanaged, IEquatable +public ref struct SpanReader(ReadOnlySpan span) where T : unmanaged, IEquatable { - public ReadOnlySpan Span { get; } + public ReadOnlySpan Span { get; } = span; public int Index { get; private set; } - public SpanReader(ReadOnlySpan span) => Span = span; - public readonly ReadOnlySpan Remaining => Span[Index..]; /// diff --git a/src/thirtytwo/Support/ThreadModalScope.cs b/src/thirtytwo/Support/ThreadModalScope.cs index f08fb29..253873f 100644 --- a/src/thirtytwo/Support/ThreadModalScope.cs +++ b/src/thirtytwo/Support/ThreadModalScope.cs @@ -14,7 +14,7 @@ public ThreadModalScope() _focusedWindow = Interop.GetFocus(); _activeWindow = Interop.GetActiveWindow(); - List windows = new(); + List windows = []; Application.EnumerateThreadWindows((HWND hwnd) => { diff --git a/src/thirtytwo/Win32/Foundation/ATOM.cs b/src/thirtytwo/Win32/Foundation/ATOM.cs index f0fe28b..612d710 100644 --- a/src/thirtytwo/Win32/Foundation/ATOM.cs +++ b/src/thirtytwo/Win32/Foundation/ATOM.cs @@ -3,7 +3,7 @@ namespace Windows.Win32.Foundation; -public readonly struct ATOM +public readonly struct ATOM(ushort atom) { // #define MAXINTATOM 0xC000 // #define MAKEINTATOM(i) (LPTSTR)((ULONG_PTR)((WORD)(i))) @@ -12,9 +12,7 @@ public readonly struct ATOM // "Strange uses for window class atoms" // https://devblogs.microsoft.com/oldnewthing/20080501-00/?p=22503 - public ushort Value { get; } - - public ATOM(ushort atom) => Value = atom; + public ushort Value { get; } = atom; public static ATOM Null { get; } = new(0); diff --git a/src/thirtytwo/Win32/Foundation/INTRESOURCE.cs b/src/thirtytwo/Win32/Foundation/INTRESOURCE.cs index f32c371..c69feea 100644 --- a/src/thirtytwo/Win32/Foundation/INTRESOURCE.cs +++ b/src/thirtytwo/Win32/Foundation/INTRESOURCE.cs @@ -3,14 +3,12 @@ namespace Windows.Win32.Foundation; -public readonly unsafe struct INTRESOURCE +public readonly unsafe struct INTRESOURCE(ushort integer) { // How can I tell that somebody used the MAKEINTRESOURCE macro to smuggle an integer inside a pointer? // https://devblogs.microsoft.com/oldnewthing/20130925-00/?p=3123 - public nint Value { get; } - - public INTRESOURCE(ushort integer) => Value = integer; + public nint Value { get; } = integer; public static bool IsIntResource(nint value) => ((ulong)value) >> 16 == 0; public static bool IsIntResource(void* value) => ((ulong)value) >> 16 == 0; diff --git a/src/thirtytwo/Win32/System/Com/ComTypeDescriptor.cs b/src/thirtytwo/Win32/System/Com/ComTypeDescriptor.cs index a9d2738..8feb2c7 100644 --- a/src/thirtytwo/Win32/System/Com/ComTypeDescriptor.cs +++ b/src/thirtytwo/Win32/System/Com/ComTypeDescriptor.cs @@ -158,7 +158,7 @@ private void InitializePropertyDescriptors() return; } - _properties = new(); + _properties = []; using var typeInfo = GetObjectTypeInfo(preferIProvideClassInfo: false); if (typeInfo.IsNull) @@ -174,7 +174,7 @@ private void InitializePropertyDescriptors() TYPEATTR typeAttributes = *ta; typeInfo.Value->ReleaseTypeAttr(ta); - Dictionary propertyInfo = new(); + Dictionary propertyInfo = []; for (int i = 0; i < typeAttributes.cFuncs; i++) { diff --git a/src/thirtytwo/Win32/System/Com/DispatchExAdapter.cs b/src/thirtytwo/Win32/System/Com/DispatchExAdapter.cs index 7309965..bbb97b0 100644 --- a/src/thirtytwo/Win32/System/Com/DispatchExAdapter.cs +++ b/src/thirtytwo/Win32/System/Com/DispatchExAdapter.cs @@ -15,11 +15,9 @@ namespace Windows.Win32.System.Com; /// yet that validates that everything in should be available in . /// /// -public unsafe class DispatchExAdapter : IDispatchEx.Interface +public unsafe class DispatchExAdapter(IDispatch.Interface dispatch) : IDispatchEx.Interface { - private readonly IDispatch.Interface _dispatch; - - public DispatchExAdapter(IDispatch.Interface dispatch) => _dispatch = dispatch; + private readonly IDispatch.Interface _dispatch = dispatch; public virtual HRESULT GetDispID(BSTR bstrName, uint grfdex, int* pid) { diff --git a/src/thirtytwo/Win32/System/Com/IDispatch.Interface.cs b/src/thirtytwo/Win32/System/Com/IDispatch.Interface.cs index 0f195a3..5ede284 100644 --- a/src/thirtytwo/Win32/System/Com/IDispatch.Interface.cs +++ b/src/thirtytwo/Win32/System/Com/IDispatch.Interface.cs @@ -18,19 +18,19 @@ public static void PopulateVTable(Vtbl* vtable) vtable->Invoke_7 = &Invoke; } - [UnmanagedCallersOnly(CallConvs = new[] { typeof(CallConvStdcall) })] + [UnmanagedCallersOnly(CallConvs = [typeof(CallConvStdcall)])] private static HRESULT GetTypeInfoCount(IDispatch* @this, uint* pctinfo) => UnwrapAndInvoke(@this, o => o.GetTypeInfoCount(pctinfo)); - [UnmanagedCallersOnly(CallConvs = new[] { typeof(CallConvStdcall) })] + [UnmanagedCallersOnly(CallConvs = [typeof(CallConvStdcall)])] private static HRESULT GetTypeInfo(IDispatch* @this, uint iTInfo, uint lcid, ITypeInfo** ppTInfo) => UnwrapAndInvoke(@this, o => o.GetTypeInfo(iTInfo, lcid, ppTInfo)); - [UnmanagedCallersOnly(CallConvs = new[] { typeof(CallConvStdcall) })] + [UnmanagedCallersOnly(CallConvs = [typeof(CallConvStdcall)])] private static HRESULT GetIDsOfNames(IDispatch* @this, Guid* riid, PWSTR* rgszNames, uint cNames, uint lcid, int* rgDispId) => UnwrapAndInvoke(@this, o => o.GetIDsOfNames(riid, rgszNames, cNames, lcid, rgDispId)); - [UnmanagedCallersOnly(CallConvs = new[] { typeof(CallConvStdcall) })] + [UnmanagedCallersOnly(CallConvs = [typeof(CallConvStdcall)])] private static HRESULT Invoke( IDispatch* @this, int dispIdMember, diff --git a/src/thirtytwo/Win32/System/Com/IUnknown.cs b/src/thirtytwo/Win32/System/Com/IUnknown.cs index 8fb02c4..af9e69f 100644 --- a/src/thirtytwo/Win32/System/Com/IUnknown.cs +++ b/src/thirtytwo/Win32/System/Com/IUnknown.cs @@ -30,15 +30,15 @@ public static void PopulateVTable(Vtbl* vtable) vtable->Release_3 = &Release; } - [UnmanagedCallersOnly(CallConvs = new[] { typeof(CallConvStdcall) })] + [UnmanagedCallersOnly(CallConvs = [typeof(CallConvStdcall)])] private static HRESULT QueryInterface(IUnknown* @this, Guid* riid, void** ppvObject) => UnwrapAndInvoke(@this, o => o.QueryInterface(riid, ppvObject)); - [UnmanagedCallersOnly(CallConvs = new[] { typeof(CallConvStdcall) })] + [UnmanagedCallersOnly(CallConvs = [typeof(CallConvStdcall)])] private static uint AddRef(IUnknown* @this) => UnwrapAndInvoke(@this, o => o.AddRef()); - [UnmanagedCallersOnly(CallConvs = new[] { typeof(CallConvStdcall) })] + [UnmanagedCallersOnly(CallConvs = [typeof(CallConvStdcall)])] private static uint Release(IUnknown* @this) => UnwrapAndInvoke(@this, o => o.Release()); diff --git a/src/thirtytwo/Win32/System/Ole/ClassPropertyDispatchAdapter.cs b/src/thirtytwo/Win32/System/Ole/ClassPropertyDispatchAdapter.cs index 5a4e612..3b727e4 100644 --- a/src/thirtytwo/Win32/System/Ole/ClassPropertyDispatchAdapter.cs +++ b/src/thirtytwo/Win32/System/Ole/ClassPropertyDispatchAdapter.cs @@ -20,7 +20,7 @@ public unsafe class ClassPropertyDispatchAdapter private readonly WeakReference _instance; private readonly Type _type; - private readonly Dictionary _members = new(); + private readonly Dictionary _members = []; private readonly Dictionary _reverseLookup = new(StringComparer.OrdinalIgnoreCase); public ClassPropertyDispatchAdapter(object instance) @@ -148,7 +148,7 @@ public HRESULT Invoke( bindingFlags, binder: null, target, - new object?[] { value }); + [value]); } catch (Exception ex) { diff --git a/src/thirtytwo/Win32/System/Ole/IDispatchEx.cs b/src/thirtytwo/Win32/System/Ole/IDispatchEx.cs index 368c55a..08f4f19 100644 --- a/src/thirtytwo/Win32/System/Ole/IDispatchEx.cs +++ b/src/thirtytwo/Win32/System/Ole/IDispatchEx.cs @@ -61,7 +61,7 @@ public HRESULT TrySetPropertyValue( public IDictionary GetAllDispatchIds() { - Dictionary dispatchIds = new(); + Dictionary dispatchIds = []; int dispid = Interop.DISPID_STARTENUM; while (GetNextDispID(Interop.fdexEnumAll, dispid, &dispid) == HRESULT.S_OK) { diff --git a/src/thirtytwo/Win32/System/Registry/Registry.cs b/src/thirtytwo/Win32/System/Registry/Registry.cs index a432f5c..2e073c5 100644 --- a/src/thirtytwo/Win32/System/Registry/Registry.cs +++ b/src/thirtytwo/Win32/System/Registry/Registry.cs @@ -145,7 +145,7 @@ public static unsafe IEnumerable GetValueNames(HKEY key) uint maxValueNameLength; Interop.RegQueryInfoKey(key, default, lpcValues: &valueCount, lpcbMaxValueNameLen: &maxValueNameLength).ThrowIfFailed(); - List names = new(); + List names = []; // Doesn't include the terminating null. maxValueNameLength += 1; diff --git a/src/thirtytwo/Win32/UI/WindowsAndMessaging/ChildWindowEnumerator.cs b/src/thirtytwo/Win32/UI/WindowsAndMessaging/ChildWindowEnumerator.cs index c18ddcf..899cd60 100644 --- a/src/thirtytwo/Win32/UI/WindowsAndMessaging/ChildWindowEnumerator.cs +++ b/src/thirtytwo/Win32/UI/WindowsAndMessaging/ChildWindowEnumerator.cs @@ -16,7 +16,7 @@ public unsafe ChildWindowEnumerator(HWND parent, Func callback) Interop.EnumChildWindows(parent, &CallBack, (nint)_callback); } - [UnmanagedCallersOnly(CallConvs = new[] { typeof(CallConvStdcall) })] + [UnmanagedCallersOnly(CallConvs = [typeof(CallConvStdcall)])] private static BOOL CallBack(HWND hwnd, LPARAM lParam) { var callback = (Func)(GCHandle.FromIntPtr(lParam).Target!); diff --git a/src/thirtytwo/Win32/UI/WindowsAndMessaging/ThreadWindowEnumerator.cs b/src/thirtytwo/Win32/UI/WindowsAndMessaging/ThreadWindowEnumerator.cs index f3ea813..77c2b15 100644 --- a/src/thirtytwo/Win32/UI/WindowsAndMessaging/ThreadWindowEnumerator.cs +++ b/src/thirtytwo/Win32/UI/WindowsAndMessaging/ThreadWindowEnumerator.cs @@ -16,7 +16,7 @@ public unsafe ThreadWindowEnumerator(uint threadId, Func callback) Interop.EnumThreadWindows(threadId, &CallBack, (nint)_callback); } - [UnmanagedCallersOnly(CallConvs = new[] { typeof(CallConvStdcall) })] + [UnmanagedCallersOnly(CallConvs = [typeof(CallConvStdcall)])] private static BOOL CallBack(HWND hwnd, LPARAM lParam) { var callback = (Func)(GCHandle.FromIntPtr(lParam).Target!); diff --git a/src/thirtytwo/Win32/UI/WindowsAndMessaging/WNDENUMPROC.cs b/src/thirtytwo/Win32/UI/WindowsAndMessaging/WNDENUMPROC.cs index bdd55e4..ffd313f 100644 --- a/src/thirtytwo/Win32/UI/WindowsAndMessaging/WNDENUMPROC.cs +++ b/src/thirtytwo/Win32/UI/WindowsAndMessaging/WNDENUMPROC.cs @@ -3,11 +3,10 @@ namespace Windows.Win32.UI.WindowsAndMessaging; -public unsafe readonly struct WNDENUMPROC +public unsafe readonly struct WNDENUMPROC(delegate* unmanaged[Stdcall] value) { - public readonly delegate* unmanaged[Stdcall] Value; + public readonly delegate* unmanaged[Stdcall] Value = value; - public WNDENUMPROC(delegate* unmanaged[Stdcall] value) => Value = value; public bool IsNull => Value is null; public static implicit operator WNDENUMPROC(delegate* unmanaged[Stdcall] value) diff --git a/src/thirtytwo/Win32/UI/WindowsAndMessaging/WNDPROC.cs b/src/thirtytwo/Win32/UI/WindowsAndMessaging/WNDPROC.cs index 173551f..87ebd86 100644 --- a/src/thirtytwo/Win32/UI/WindowsAndMessaging/WNDPROC.cs +++ b/src/thirtytwo/Win32/UI/WindowsAndMessaging/WNDPROC.cs @@ -3,11 +3,10 @@ namespace Windows.Win32.UI.WindowsAndMessaging; -public unsafe readonly struct WNDPROC +public unsafe readonly struct WNDPROC(delegate* unmanaged[Stdcall] value) { - public readonly delegate* unmanaged[Stdcall] Value; + public readonly delegate* unmanaged[Stdcall] Value = value; - public WNDPROC(delegate* unmanaged[Stdcall] value) => Value = value; public bool IsNull => Value is null; public static implicit operator WNDPROC(delegate* unmanaged[Stdcall] value) diff --git a/src/thirtytwo/WindowClass.WindowClassInfo.cs b/src/thirtytwo/WindowClass.WindowClassInfo.cs index 4828334..69ae583 100644 --- a/src/thirtytwo/WindowClass.WindowClassInfo.cs +++ b/src/thirtytwo/WindowClass.WindowClassInfo.cs @@ -7,11 +7,11 @@ namespace Windows; public partial class WindowClass { - private unsafe partial class WindowClassInfo + private unsafe partial class WindowClassInfo(WindowProcedure windowProcedure) { public uint Size; public ClassStyle Style; - public WindowProcedure WindowProcedure; + public WindowProcedure WindowProcedure = windowProcedure; public int ClassExtraBytes; public int WindowExtraBytes; public HINSTANCE Instance; @@ -24,11 +24,6 @@ private unsafe partial class WindowClassInfo public HICON SmallIcon; public HBRUSH Background; - public WindowClassInfo(WindowProcedure windowProcedure) - { - WindowProcedure = windowProcedure; - } - public static implicit operator WindowClassInfo(WNDCLASSEXW nativeClass) { var windowClass = new WindowClassInfo( diff --git a/src/thirtytwo/thirtytwo.csproj b/src/thirtytwo/thirtytwo.csproj index dc4f864..641932b 100644 --- a/src/thirtytwo/thirtytwo.csproj +++ b/src/thirtytwo/thirtytwo.csproj @@ -11,7 +11,7 @@ all runtime; build; native; contentfiles; analyzers; buildtransitive - + all runtime; build; native; contentfiles; analyzers; buildtransitive diff --git a/src/thirtytwo_tests/Win32/System/Com/ComponentCategoriesManagerTests.cs b/src/thirtytwo_tests/Win32/System/Com/ComponentCategoriesManagerTests.cs index d266b2d..8af288b 100644 --- a/src/thirtytwo_tests/Win32/System/Com/ComponentCategoriesManagerTests.cs +++ b/src/thirtytwo_tests/Win32/System/Com/ComponentCategoriesManagerTests.cs @@ -16,7 +16,7 @@ public void EnumerateCategories() using ComScope enumCategories = new(null); hr = categoriesInfo.Value->EnumCategories(Interop.GetUserDefaultLCID(), enumCategories); - Dictionary categories = new(); + Dictionary categories = []; CATEGORYINFO categoryInfo = default; uint fetched = 0; while (enumCategories.Value->Next(1, &categoryInfo, &fetched).Succeeded && fetched == 1) @@ -38,7 +38,7 @@ public void EnumerateActiveXClasses() hr = categoriesInfo.Value->EnumClassesOfCategories(1, &control, 0, null, enumGuids); hr.Succeeded.Should().BeTrue(); - List classGuids = new(); + List classGuids = []; Guid guid = default; uint fetched = 0; while (enumGuids.Value->Next(1, &guid, &fetched).Succeeded && fetched == 1) diff --git a/src/thirtytwo_tests/Win32/System/Com/VariantTests.cs b/src/thirtytwo_tests/Win32/System/Com/VariantTests.cs index 3233177..92a174a 100644 --- a/src/thirtytwo_tests/Win32/System/Com/VariantTests.cs +++ b/src/thirtytwo_tests/Win32/System/Com/VariantTests.cs @@ -46,7 +46,7 @@ public void MarshalRectangleToVariant_ThroughLegacyCCW() [Fact] public void MarshalArrayToVariant() { - int[] array = new[] { 1, 2, 3, 4 }; + int[] array = [1, 2, 3, 4]; using VARIANT variant = default; nint address = (nint)(void*)&variant; @@ -81,7 +81,7 @@ public void MarshalArrayToVariant() [Fact] public void ArrayToVariantAfterFreeIsSometimesNewObject() { - int[] array = new[] { 1, 2, 3, 4 }; + int[] array = [1, 2, 3, 4]; SAFEARRAY* safeArray; using (VARIANT variant = default) @@ -193,15 +193,15 @@ public struct Vtbl internal delegate* unmanaged[Stdcall] SetVariantByRef_6; } - [UnmanagedCallersOnly(CallConvs = new[] { typeof(CallConvStdcall) })] + [UnmanagedCallersOnly(CallConvs = [typeof(CallConvStdcall)])] private static HRESULT GetVariant(ITestVariant* @this, VARIANT* variant) => ComHelpers.UnwrapAndInvoke(@this, o => o.GetVariant(variant)); - [UnmanagedCallersOnly(CallConvs = new[] { typeof(CallConvStdcall) })] + [UnmanagedCallersOnly(CallConvs = [typeof(CallConvStdcall)])] private static HRESULT SetVariant(ITestVariant* @this, VARIANT variant) => ComHelpers.UnwrapAndInvoke(@this, o => o.SetVariant(variant)); - [UnmanagedCallersOnly(CallConvs = new[] { typeof(CallConvStdcall) })] + [UnmanagedCallersOnly(CallConvs = [typeof(CallConvStdcall)])] private static HRESULT SetVariantByRef(ITestVariant* @this, VARIANT* variant) => ComHelpers.UnwrapAndInvoke(@this, o => o.SetVariantByRef(variant));