Skip to content

Commit 618c35a

Browse files
committed
Update
1 parent 1661e0e commit 618c35a

File tree

2 files changed

+57
-42
lines changed

2 files changed

+57
-42
lines changed

src/Files.App/Utils/Shell/PreviewHandler.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ namespace Files.App.Utils.Shell
1616
/// </summary>
1717
public sealed class PreviewHandler : IDisposable
1818
{
19+
// Fields
20+
21+
private static readonly Guid IPreviewHandlerIid = Guid.ParseExact("8895b1c6-b41f-4c1c-a562-0d564250836f", "d");
22+
1923
#region IPreviewHandlerFrame support
2024

2125
[StructLayout(LayoutKind.Sequential)]
@@ -125,13 +129,16 @@ interface IObjectWithSite
125129
IPreviewHandlerVisuals visuals;
126130
nint pPreviewHandler;
127131

132+
// Initializer
133+
128134
public PreviewHandler(Guid clsid, nint frame)
129135
{
130136
disposed = true;
131137
init = false;
132138
shown = false;
133139
comSite = new PreviewHandlerFrame(frame);
134140
hwnd = frame;
141+
135142
try
136143
{
137144
SetupHandler(clsid);
@@ -151,7 +158,7 @@ public PreviewHandler(Guid clsid, nint frame)
151158
}
152159
}
153160

154-
static readonly Guid IPreviewHandlerIid = Guid.ParseExact("8895b1c6-b41f-4c1c-a562-0d564250836f", "d");
161+
// Methods
155162

156163
unsafe void SetupHandler(Guid clsid)
157164
{

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

Lines changed: 49 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -24,17 +24,23 @@ namespace Files.App.ViewModels.Previews
2424
{
2525
public sealed class ShellPreviewViewModel : BasePreviewModel
2626
{
27-
PreviewHandler? currentHandler;
28-
ContentExternalOutputLink? outputLink;
27+
// Fields
28+
29+
PreviewHandler? _currentPreviewHandler;
30+
ContentExternalOutputLink? _contentExternalOutputLink;
2931
WNDCLASSEXW _windowClass;
30-
WNDPROC _windProc = default;
31-
HWND hwnd = HWND.Null;
32-
bool isOfficePreview = false;
32+
WNDPROC _windProc = null!;
33+
HWND _hWnd = HWND.Null;
34+
bool _isOfficePreview = false;
35+
36+
// Initializer
3337

3438
public ShellPreviewViewModel(ListedItem item) : base(item)
3539
{
3640
}
3741

42+
// Methods
43+
3844
public async override Task<List<FileProperty>> LoadPreviewAndDetailsAsync()
3945
=> [];
4046

@@ -70,13 +76,13 @@ public async override Task<List<FileProperty>> LoadPreviewAndDetailsAsync()
7076

7177
public void SizeChanged(RECT size)
7278
{
73-
if (hwnd != HWND.Null)
74-
PInvoke.SetWindowPos(hwnd, (HWND)0, size.left, size.top, size.Width, size.Height, SET_WINDOW_POS_FLAGS.SWP_NOACTIVATE);
79+
if (_hWnd != HWND.Null)
80+
PInvoke.SetWindowPos(_hWnd, (HWND)0, size.left, size.top, size.Width, size.Height, SET_WINDOW_POS_FLAGS.SWP_NOACTIVATE);
7581

76-
currentHandler?.ResetBounds(new(0, 0, size.Width, size.Height));
82+
_currentPreviewHandler?.ResetBounds(new(0, 0, size.Width, size.Height));
7783

78-
if (outputLink is not null)
79-
outputLink.PlacementVisual.Size = new(size.Width, size.Height);
84+
if (_contentExternalOutputLink is not null)
85+
_contentExternalOutputLink.PlacementVisual.Size = new(size.Width, size.Height);
8086
}
8187

8288
private unsafe LRESULT WndProc(HWND hwnd, uint msg, WPARAM wParam, LPARAM lParam)
@@ -85,7 +91,7 @@ private unsafe LRESULT WndProc(HWND hwnd, uint msg, WPARAM wParam, LPARAM lParam
8591
{
8692
var clsid = FindPreviewHandlerFor(Item.FileExtension, hwnd);
8793

88-
isOfficePreview = new Guid?[]
94+
_isOfficePreview = new Guid?[]
8995
{
9096
Guid.Parse("84F66100-FF7C-4fb4-B0C0-02CD7FB668FE"), // preview handler for Word files
9197
Guid.Parse("65235197-874B-4A07-BDC5-E65EA825B718"), // preview handler for PowerPoint files
@@ -94,9 +100,9 @@ private unsafe LRESULT WndProc(HWND hwnd, uint msg, WPARAM wParam, LPARAM lParam
94100

95101
try
96102
{
97-
currentHandler = new PreviewHandler(clsid.Value, hwnd.Value);
98-
currentHandler.InitWithFileWithEveryWay(Item.ItemPath);
99-
currentHandler.DoPreview();
103+
_currentPreviewHandler = new PreviewHandler(clsid.Value, hwnd.Value);
104+
_currentPreviewHandler.InitWithFileWithEveryWay(Item.ItemPath);
105+
_currentPreviewHandler.DoPreview();
100106
}
101107
catch
102108
{
@@ -105,10 +111,10 @@ private unsafe LRESULT WndProc(HWND hwnd, uint msg, WPARAM wParam, LPARAM lParam
105111
}
106112
else if (msg == 0x0002 /*WM_DESTROY*/)
107113
{
108-
if (currentHandler is not null)
114+
if (_currentPreviewHandler is not null)
109115
{
110-
currentHandler.UnloadPreview();
111-
currentHandler = null;
116+
_currentPreviewHandler.UnloadPreview();
117+
_currentPreviewHandler = null;
112118
}
113119
}
114120

@@ -148,7 +154,7 @@ public unsafe void LoadPreview(UIElement presenter)
148154

149155
fixed (char* pszWindowName = szWindowName)
150156
{
151-
hwnd = PInvoke.CreateWindowEx(
157+
_hWnd = PInvoke.CreateWindowEx(
152158
WINDOW_EX_STYLE.WS_EX_LAYERED | WINDOW_EX_STYLE.WS_EX_COMPOSITED,
153159
pszClassName,
154160
pszWindowName,
@@ -204,20 +210,20 @@ private unsafe bool ChildWindowToXaml(nint parent, UIElement presenter)
204210
IUnknown* pControlSurface = default;
205211

206212
pDCompositionDevice->CreateVisual(&pChildVisual);
207-
pDCompositionDevice->CreateSurfaceFromHwnd(hwnd, &pControlSurface);
213+
pDCompositionDevice->CreateSurfaceFromHwnd(_hWnd, &pControlSurface);
208214
pChildVisual->SetContent(pControlSurface);
209215
if (pChildVisual is null || pControlSurface is null)
210216
return false;
211217

212218
var compositor = ElementCompositionPreview.GetElementVisual(presenter).Compositor;
213-
outputLink = ContentExternalOutputLink.Create(compositor);
219+
_contentExternalOutputLink = ContentExternalOutputLink.Create(compositor);
214220

215-
var target = outputLink.As<IDCompositionTarget.Interface>();
221+
var target = _contentExternalOutputLink.As<IDCompositionTarget.Interface>();
216222
target.SetRoot(pChildVisual);
217223

218-
outputLink.PlacementVisual.Size = new(0, 0);
219-
outputLink.PlacementVisual.Scale = new(1/(float)presenter.XamlRoot.RasterizationScale);
220-
ElementCompositionPreview.SetElementChildVisual(presenter, outputLink.PlacementVisual);
224+
_contentExternalOutputLink.PlacementVisual.Size = new(0, 0);
225+
_contentExternalOutputLink.PlacementVisual.Scale = new(1/(float)presenter.XamlRoot.RasterizationScale);
226+
ElementCompositionPreview.SetElementChildVisual(presenter, _contentExternalOutputLink.PlacementVisual);
221227

222228
pDCompositionDevice->Commit();
223229

@@ -233,55 +239,57 @@ private unsafe bool ChildWindowToXaml(nint parent, UIElement presenter)
233239

234240
return
235241
PInvoke.DwmSetWindowAttribute(
236-
new((nint)hwnd),
242+
new((nint)_hWnd),
237243
DWMWINDOWATTRIBUTE.DWMWA_CLOAK,
238244
&dwAttrib,
239245
(uint)Marshal.SizeOf(dwAttrib))
240246
.Succeeded;
241247
}
242248

243-
public void UnloadPreview()
244-
{
245-
if (hwnd != HWND.Null)
246-
PInvoke.DestroyWindow(hwnd);
247-
248-
//outputLink?.Dispose();
249-
outputLink = null;
250-
251-
PInvoke.UnregisterClass(_windowClass.lpszClassName, PInvoke.GetModuleHandle(default(PWSTR)));
252-
}
253-
254249
public unsafe void PointerEntered(bool onPreview)
255250
{
256251
if (onPreview)
257252
{
258253
var dwAttrib = Convert.ToUInt32(false);
259254

260255
PInvoke.DwmSetWindowAttribute(
261-
new((nint)hwnd),
256+
new((nint)_hWnd),
262257
DWMWINDOWATTRIBUTE.DWMWA_CLOAK,
263258
&dwAttrib,
264259
(uint)Marshal.SizeOf(dwAttrib));
265260

266-
if (isOfficePreview)
267-
PInvoke.SetWindowLong(hwnd, WINDOW_LONG_PTR_INDEX.GWL_EXSTYLE, 0);
261+
if (_isOfficePreview)
262+
PInvoke.SetWindowLong(_hWnd, WINDOW_LONG_PTR_INDEX.GWL_EXSTYLE, 0);
268263
}
269264
else
270265
{
271266
PInvoke.SetWindowLong(
272-
hwnd,
267+
_hWnd,
273268
WINDOW_LONG_PTR_INDEX.GWL_EXSTYLE,
274269
(int)(WINDOW_EX_STYLE.WS_EX_LAYERED | WINDOW_EX_STYLE.WS_EX_COMPOSITED));
275270

276271
var dwAttrib = Convert.ToUInt32(true);
277272

278273
PInvoke.DwmSetWindowAttribute(
279-
new((nint)hwnd),
274+
new((nint)_hWnd),
280275
DWMWINDOWATTRIBUTE.DWMWA_CLOAK,
281276
&dwAttrib,
282277
(uint)Marshal.SizeOf(dwAttrib));
283278
}
284279
}
280+
281+
// Disposer
282+
283+
public void UnloadPreview()
284+
{
285+
if (_hWnd != HWND.Null)
286+
PInvoke.DestroyWindow(_hWnd);
287+
288+
//outputLink?.Dispose();
289+
_contentExternalOutputLink = null;
290+
291+
PInvoke.UnregisterClass(_windowClass.lpszClassName, PInvoke.GetModuleHandle(default(PWSTR)));
292+
}
285293
}
286294
}
287295

0 commit comments

Comments
 (0)