Skip to content

Commit

Permalink
Cleanup code using latest C# (#255)
Browse files Browse the repository at this point in the history
  • Loading branch information
shibayan authored Mar 17, 2024
1 parent 7a8bf20 commit 765a15a
Show file tree
Hide file tree
Showing 11 changed files with 21 additions and 47 deletions.
3 changes: 3 additions & 0 deletions Directory.Build.props
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
<Project>
<PropertyGroup>
<TargetFramework>net8.0-windows10.0.22621.0</TargetFramework>
<Platforms>x64;arm64</Platforms>
<Nullable>enable</Nullable>
<WarningsAsErrors>nullable</WarningsAsErrors>
</PropertyGroup>
</Project>
6 changes: 3 additions & 3 deletions WinQuickLook.App/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ public static void Main(string[] args)

private static void ConfigureService(IServiceCollection services)
{
services.TryAddEnumerable(new[]
{
services.TryAddEnumerable(
[
ServiceDescriptor.Singleton<IFileSystemPreviewHandler, CodeFilePreviewHandler>(),
ServiceDescriptor.Singleton<IFileSystemPreviewHandler, GenericDirectoryPreviewHandler>(),
ServiceDescriptor.Singleton<IFileSystemPreviewHandler, GenericFilePreviewHandler>(),
Expand All @@ -51,7 +51,7 @@ private static void ConfigureService(IServiceCollection services)
ServiceDescriptor.Singleton<IFileSystemPreviewHandler, ShellFilePreviewHandler>(),
ServiceDescriptor.Singleton<IFileSystemPreviewHandler, SvgFilePreviewHandler>(),
ServiceDescriptor.Singleton<IFileSystemPreviewHandler, TextFilePreviewHandler>()
});
]);

services.AddSingleton<ShellAssociationProvider>();
services.AddSingleton<ShellThumbnailProvider>();
Expand Down
3 changes: 0 additions & 3 deletions WinQuickLook.App/WinQuickLook.App.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,9 @@

<PropertyGroup>
<OutputType>WinExe</OutputType>
<Nullable>enable</Nullable>
<UseWPF>true</UseWPF>
<ApplicationIcon>Icon.ico</ApplicationIcon>
<ApplicationManifest>app.manifest</ApplicationManifest>
<Platforms>x64;arm64</Platforms>
<WarningsAsErrors>nullable</WarningsAsErrors>
<RuntimeIdentifiers>win-x64;win-arm64</RuntimeIdentifiers>
<PublishProfile>win-$(Platform).pubxml</PublishProfile>
<GenerateSatelliteAssembliesForCore>true</GenerateSatelliteAssembliesForCore>
Expand Down
5 changes: 1 addition & 4 deletions WinQuickLook.Core.Tests/WinQuickLook.Core.Tests.csproj
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<Nullable>enable</Nullable>
<IsPackable>false</IsPackable>
<RootNamespace>WinQuickLook.Tests</RootNamespace>
<Platforms>x64;arm64</Platforms>
<WarningsAsErrors>nullable</WarningsAsErrors>
</PropertyGroup>

<ItemGroup>
Expand All @@ -26,7 +23,7 @@
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="coverlet.collector" Version="6.0.1">
<PackageReference Include="coverlet.collector" Version="6.0.2">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
Expand Down
4 changes: 3 additions & 1 deletion WinQuickLook.Core/Controls/ShellFileControl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@ protected override HandleRef BuildWindowCore(HandleRef hwndParent)
WINDOW_STYLE.WS_CHILD | WINDOW_STYLE.WS_VISIBLE | WINDOW_STYLE.WS_CLIPCHILDREN,
0, 0,
(int)ActualWidth, (int)ActualHeight,
new HWND(hwndParent.Handle));
new HWND(hwndParent.Handle),
null,
null);

return new HandleRef(this, hwndHost);
}
Expand Down
7 changes: 1 addition & 6 deletions WinQuickLook.Core/Messaging/LowLevelKeyboardHook.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,8 @@

namespace WinQuickLook.Messaging;

public class LowLevelKeyboardHook : WindowsHook
public class LowLevelKeyboardHook() : WindowsHook(WINDOWS_HOOK_ID.WH_KEYBOARD_LL)
{
public LowLevelKeyboardHook()
: base(WINDOWS_HOOK_ID.WH_KEYBOARD_LL)
{
}

public Action<VIRTUAL_KEY>? PerformKeyDown { get; set; }

protected override LRESULT HookProc(int code, WPARAM wParam, LPARAM lParam)
Expand Down
7 changes: 1 addition & 6 deletions WinQuickLook.Core/Messaging/LowLevelMouseHook.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,8 @@

namespace WinQuickLook.Messaging;

public class LowLevelMouseHook : WindowsHook
public class LowLevelMouseHook() : WindowsHook(WINDOWS_HOOK_ID.WH_MOUSE_LL)
{
public LowLevelMouseHook()
: base(WINDOWS_HOOK_ID.WH_MOUSE_LL)
{
}

protected override LRESULT HookProc(int code, WPARAM wParam, LPARAM lParam)
{
if (code == PInvoke.HC_ACTION && wParam == PInvoke.WM_LBUTTONDOWN)
Expand Down
16 changes: 3 additions & 13 deletions WinQuickLook.Core/Providers/ShellAssociationProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,14 @@ public IReadOnlyList<Entry> GetRecommends(FileInfo fileInfo)
{
if (PInvoke.SHAssocEnumHandlers(fileInfo.Extension, ASSOC_FILTER.ASSOC_FILTER_RECOMMENDED, out var enumAssocHandlers).Failed)
{
return Array.Empty<Entry>();
return [];
}

try
{
var recommends = new List<Entry>();

var assocHandlers = new IAssocHandler?[8];
var assocHandlers = new IAssocHandler[8];

while (enumAssocHandlers.Next(assocHandlers, out var fetched).Succeeded)
{
Expand All @@ -69,11 +69,6 @@ public IReadOnlyList<Entry> GetRecommends(FileInfo fileInfo)
{
var assocHandler = assocHandlers[i];

if (assocHandler is null)
{
continue;
}

try
{
assocHandler.GetUIName(out var pUiName);
Expand Down Expand Up @@ -115,7 +110,7 @@ public void Invoke(string appName, FileInfo fileInfo)

try
{
var assocHandlers = new IAssocHandler?[8];
var assocHandlers = new IAssocHandler[8];

while (enumAssocHandlers.Next(assocHandlers, out var fetched).Succeeded)
{
Expand All @@ -128,11 +123,6 @@ public void Invoke(string appName, FileInfo fileInfo)
{
var assocHandler = assocHandlers[i];

if (assocHandler is null)
{
continue;
}

try
{
assocHandler.GetUIName(out var pUiName);
Expand Down
7 changes: 2 additions & 5 deletions WinQuickLook.Core/WinQuickLook.Core.csproj
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<Nullable>enable</Nullable>
<UseWPF>true</UseWPF>
<RootNamespace>WinQuickLook</RootNamespace>
<Platforms>x64;arm64</Platforms>
<WarningsAsErrors>nullable</WarningsAsErrors>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="AvalonEdit" Version="6.3.0.90" />
<PackageReference Include="Cylinder.WPF" Version="1.0.0-preview.2" />
<PackageReference Include="Markdig" Version="0.35.0" />
<PackageReference Include="Markdig" Version="0.36.2" />
<PackageReference Include="Microsoft.Web.WebView2" Version="1.0.2365.46" />
</ItemGroup>

Expand Down
8 changes: 4 additions & 4 deletions WinQuickLook.CsWin32/PInvoke.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,30 +20,30 @@ public static partial class PInvoke
public const uint MF_SOURCE_READER_FIRST_VIDEO_STREAM = 0xFFFFFFFCU;
public const uint MF_SOURCE_READER_FIRST_AUDIO_STREAM = 0xFFFFFFFDU;

public static HRESULT SHCreateItemFromParsingName<T>(string pszPath, System.Com.IBindCtx pbc, out T ppv)
public static HRESULT SHCreateItemFromParsingName<T>(string pszPath, System.Com.IBindCtx? pbc, out T ppv)
{
var hr = SHCreateItemFromParsingName(pszPath, pbc, typeof(T).GUID, out var o);
ppv = (T)o;
return hr;
}

public static HRESULT SHGetPropertyStoreFromParsingName<T>(string pszPath, System.Com.IBindCtx pbc, GETPROPERTYSTOREFLAGS flags, out T ppv)
public static HRESULT SHGetPropertyStoreFromParsingName<T>(string pszPath, System.Com.IBindCtx? pbc, GETPROPERTYSTOREFLAGS flags, out T ppv)
{
var hr = SHGetPropertyStoreFromParsingName(pszPath, pbc, flags, typeof(T).GUID, out var o);
ppv = (T)o;
return hr;
}

/// <inheritdoc cref="AssocQueryString(ASSOCF, ASSOCSTR, PCWSTR, PCWSTR, PWSTR, uint*)"/>
public static unsafe HRESULT AssocQueryString(ASSOCF flags, ASSOCSTR str, string pszAssoc, string pszExtra, Span<char> pszOut, ref uint pcchOut)
public static unsafe HRESULT AssocQueryString(ASSOCF flags, ASSOCSTR str, string pszAssoc, string? pszExtra, Span<char> pszOut, ref uint pcchOut)
{
fixed (char* pszOutLocal = pszOut)
{
return AssocQueryString(flags, str, pszAssoc, pszExtra, new PWSTR(pszOutLocal), ref pcchOut);
}
}

public static unsafe HWND CreateWindowEx(WINDOW_EX_STYLE dwExStyle, string lpClassName, string lpWindowName, WINDOW_STYLE dwStyle, int X, int Y, int nWidth, int nHeight, HWND hWndParent, SafeHandle hMenu = default, SafeHandle hInstance = default)
public static unsafe HWND CreateWindowEx(WINDOW_EX_STYLE dwExStyle, string lpClassName, string lpWindowName, WINDOW_STYLE dwStyle, int X, int Y, int nWidth, int nHeight, HWND hWndParent, SafeHandle? hMenu, SafeHandle? hInstance)
{
return CreateWindowEx(dwExStyle, lpClassName, lpWindowName, dwStyle, X, Y, nWidth, nHeight, hWndParent, hMenu, hInstance, null);
}
Expand Down
2 changes: 0 additions & 2 deletions WinQuickLook.CsWin32/WinQuickLook.CsWin32.csproj
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<Platforms>x64;arm64</Platforms>
<NoWarn>CS0109</NoWarn>
<RootNamespace>Windows.Win32</RootNamespace>
<WarningsAsErrors>nullable</WarningsAsErrors>
</PropertyGroup>

<ItemGroup>
Expand Down

0 comments on commit 765a15a

Please sign in to comment.