[Peek] Fix PreviewHandler previewer not visible on first activation (#34991)

ensure containerHwnd is created before usage
This commit is contained in:
Davide Giacometti 2024-09-23 14:58:08 +02:00 committed by GitHub
parent 7e4baa14bb
commit 056fba0dcf
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 19 additions and 3 deletions

View File

@ -13,5 +13,4 @@
GotFocus="UserControl_GotFocus"
IsEnabled="False"
IsTabStop="True"
Loaded="UserControl_Loaded"
mc:Ignorable="d" />

View File

@ -61,6 +61,8 @@ namespace Peek.FilePreviewer.Controls
partial void OnSourceChanged(IPreviewHandler? value)
{
EnsureContainerHwndCreated();
if (Source != null)
{
UpdatePreviewerTheme();
@ -82,6 +84,8 @@ namespace Peek.FilePreviewer.Controls
private void OnHandlerVisibilityChanged()
{
EnsureContainerHwndCreated();
if (HandlerVisibility == Visibility.Visible)
{
PInvoke.ShowWindow(containerHwnd, SHOW_WINDOW_CMD.SW_SHOW);
@ -138,8 +142,19 @@ namespace Peek.FilePreviewer.Controls
return PInvoke.DefWindowProc(hWnd, msg, wParam, lParam);
}
private void UserControl_Loaded(object sender, RoutedEventArgs e)
private void EnsureContainerHwndCreated()
{
if (!containerHwnd.IsNull)
{
return;
}
var peekWindow = new HWND(Win32Interop.GetWindowFromWindowId(XamlRoot?.ContentIslandEnvironment?.AppWindowId ?? default));
if (peekWindow.IsNull)
{
return;
}
fixed (char* pContainerClassName = "PeekShellPreviewHandlerContainer")
{
PInvoke.RegisterClass(new WNDCLASSW()
@ -158,7 +173,7 @@ namespace Peek.FilePreviewer.Controls
0, // Y
0, // Width
0, // Height
(HWND)Win32Interop.GetWindowFromWindowId(XamlRoot.ContentIslandEnvironment.AppWindowId), // Peek UI window
peekWindow,
HMENU.Null,
HINSTANCE.Null);
@ -169,6 +184,8 @@ namespace Peek.FilePreviewer.Controls
private void UserControl_EffectiveViewportChanged(FrameworkElement sender, EffectiveViewportChangedEventArgs args)
{
EnsureContainerHwndCreated();
var dpi = (float)PInvoke.GetDpiForWindow(containerHwnd) / 96;
// Resize the container window