Skip to content

Commit 318bbb3

Browse files
authored
Code Quality: Switch DirectN to CsWin32 (#15385)
1 parent 8dab192 commit 318bbb3

File tree

3 files changed

+28
-17
lines changed

3 files changed

+28
-17
lines changed

src/Files.App/Files.App.csproj

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,6 @@
6969
<ItemGroup>
7070
<PackageReference Include="ByteSize" Version="2.1.2" />
7171
<PackageReference Include="CommunityToolkit.Mvvm" Version="8.2.2" />
72-
<PackageReference Include="DirectNCore" Version="1.16.0" />
7372
<PackageReference Include="DiscUtils.Udf" Version="0.16.13" />
7473
<PackageReference Include="FluentFTP" Version="43.0.1" />
7574
<PackageReference Include="ini-parser-netstandard" Version="2.5.2" />

src/Files.App/NativeMethods.txt

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,4 +45,12 @@ MoveFileFromApp
4545
DeleteFileFromApp
4646
RemoveDirectoryFromApp
4747
GetKeyState
48-
CreateDirectoryFromApp
48+
CreateDirectoryFromApp
49+
D3D_DRIVER_TYPE
50+
D3D_FEATURE_LEVEL
51+
ID3D11Device
52+
ID3D11DeviceContext
53+
D3D11CreateDevice
54+
IDXGIDevice
55+
DCompositionCreateDevice
56+
IDCompositionDevice

src/Files.App/ViewModels/UserControls/Previews/ShellPreviewViewModel.cs

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
1-
using DirectN;
2-
using Files.App.ViewModels.Properties;
1+
using Files.App.ViewModels.Properties;
32
using Microsoft.UI.Content;
43
using Microsoft.UI.Xaml;
54
using Microsoft.UI.Xaml.Hosting;
65
using System.Runtime.InteropServices;
76
using System.Text;
87
using Vanara.PInvoke;
8+
using Windows.Win32;
9+
using Windows.Win32.Graphics.Direct3D;
10+
using Windows.Win32.Graphics.Direct3D11;
11+
using Windows.Win32.Graphics.Dxgi;
12+
using Windows.Win32.Graphics.DirectComposition;
913
using WinRT;
1014
using static Vanara.PInvoke.ShlwApi;
1115
using static Vanara.PInvoke.User32;
@@ -112,7 +116,7 @@ public void LoadPreview(UIElement presenter)
112116
_ = ChildWindowToXaml(parent, presenter);
113117
}
114118

115-
private bool ChildWindowToXaml(IntPtr parent, UIElement presenter)
119+
private unsafe bool ChildWindowToXaml(IntPtr parent, UIElement presenter)
116120
{
117121
D3D_DRIVER_TYPE[] driverTypes =
118122
[
@@ -122,36 +126,36 @@ private bool ChildWindowToXaml(IntPtr parent, UIElement presenter)
122126

123127
ID3D11Device? d3d11Device = null;
124128
ID3D11DeviceContext? d3d11DeviceContext = null;
125-
D3D_FEATURE_LEVEL featureLevelSupported;
126129

127130
foreach (var driveType in driverTypes)
128131
{
129-
var hr = D3D11Functions.D3D11CreateDevice(
132+
var hr = PInvoke.D3D11CreateDevice(
130133
null,
131134
driveType,
132-
IntPtr.Zero,
133-
(uint)D3D11_CREATE_DEVICE_FLAG.D3D11_CREATE_DEVICE_BGRA_SUPPORT,
135+
new(IntPtr.Zero),
136+
D3D11_CREATE_DEVICE_FLAG.D3D11_CREATE_DEVICE_BGRA_SUPPORT,
134137
null,
135138
0,
136139
7,
137140
out d3d11Device,
138-
out featureLevelSupported,
141+
null,
139142
out d3d11DeviceContext);
140143

141-
if (hr.IsSuccess)
144+
if (hr.Succeeded)
142145
break;
143146
}
144147

145148
if (d3d11Device is null)
146149
return false;
147150
IDXGIDevice dxgiDevice = (IDXGIDevice)d3d11Device;
148-
if (Functions.DCompositionCreateDevice(dxgiDevice, typeof(IDCompositionDevice).GUID, out var compDevicePtr).IsError)
151+
if (PInvoke.DCompositionCreateDevice(dxgiDevice, typeof(IDCompositionDevice).GUID, out var compDevicePtr).Failed)
149152
return false;
150-
IDCompositionDevice compDevice = (IDCompositionDevice)Marshal.GetObjectForIUnknown(compDevicePtr);
153+
IDCompositionDevice compDevice = (IDCompositionDevice)compDevicePtr;
151154

152-
if (compDevice.CreateVisual(out var childVisual).IsError ||
153-
compDevice.CreateSurfaceFromHwnd(hwnd.DangerousGetHandle(), out var controlSurface).IsError ||
154-
childVisual.SetContent(controlSurface).IsError)
155+
compDevice.CreateVisual(out var childVisual);
156+
compDevice.CreateSurfaceFromHwnd(new(hwnd.DangerousGetHandle()), out var controlSurface);
157+
childVisual.SetContent(controlSurface);
158+
if (childVisual is null || controlSurface is null)
155159
return false;
156160

157161
var compositor = ElementCompositionPreview.GetElementVisual(presenter).Compositor;
@@ -169,7 +173,7 @@ private bool ChildWindowToXaml(IntPtr parent, UIElement presenter)
169173
Marshal.ReleaseComObject(childVisual);
170174
Marshal.ReleaseComObject(controlSurface);
171175
Marshal.ReleaseComObject(compDevice);
172-
Marshal.Release(compDevicePtr);
176+
Marshal.ReleaseComObject(compDevicePtr);
173177
Marshal.ReleaseComObject(dxgiDevice);
174178
Marshal.ReleaseComObject(d3d11Device);
175179
Marshal.ReleaseComObject(d3d11DeviceContext);

0 commit comments

Comments
 (0)