From 48938c5e14981eb2d9e7954421496ec6ea3bbd8d Mon Sep 17 00:00:00 2001 From: Kurt Date: Thu, 19 Mar 2026 20:35:58 -0500 Subject: [PATCH] Minor clean --- PKHeX.WinForms/Controls/Slots/PokePreview.cs | 4 +- .../Controls/Slots/SummaryPreviewer.cs | 55 ++++++++----------- 2 files changed, 25 insertions(+), 34 deletions(-) diff --git a/PKHeX.WinForms/Controls/Slots/PokePreview.cs b/PKHeX.WinForms/Controls/Slots/PokePreview.cs index f9f26bbdb..0eb3e5b60 100644 --- a/PKHeX.WinForms/Controls/Slots/PokePreview.cs +++ b/PKHeX.WinForms/Controls/Slots/PokePreview.cs @@ -467,7 +467,9 @@ public void MoveForm(int x, int y) const uint SWP_ASYNCWINDOWPOS = 0x4000; const uint flags = SWP_NOZORDER | SWP_NOSIZE | SWP_NOREDRAW | SWP_NOACTIVATE | SWP_NOSENDCHANGING | SWP_ASYNCWINDOWPOS; - SetWindowPos(Handle, 0, x, y, 0, 0, flags); + + const int HWND_TOPMOST = -1; + SetWindowPos(Handle, HWND_TOPMOST, x, y, 0, 0, flags); return; [System.Runtime.InteropServices.DllImport("user32.dll")] diff --git a/PKHeX.WinForms/Controls/Slots/SummaryPreviewer.cs b/PKHeX.WinForms/Controls/Slots/SummaryPreviewer.cs index 7388c5f90..2e6660a9c 100644 --- a/PKHeX.WinForms/Controls/Slots/SummaryPreviewer.cs +++ b/PKHeX.WinForms/Controls/Slots/SummaryPreviewer.cs @@ -57,40 +57,25 @@ private void UpdatePreview(Control pb, PKM pk, in BattleTemplateExportSettings s _source.Cancel(); _source.Dispose(); // Properly dispose the previous CancellationTokenSource _source = new(); - var wasVisible = Previewer.Visible; UpdatePreviewPosition(new()); Previewer.Populate(pk, settings, ctx); - if (!wasVisible) - ShowInactiveTopmost(Previewer); + + SetWindowState(Previewer, true); + bool showFirst = !_isFirstShown; + if (showFirst) + _isFirstShown = true; } - private const int SW_SHOWNOACTIVATE = 4; - private const int HWND_TOPMOST = -1; - private const uint SWP_NOACTIVATE = 0x0010; - - #pragma warning disable SYSLIB1054 // Use 'LibraryImportAttribute' instead of 'DllImportAttribute' to generate P/Invoke marshalling code at compile time - [System.Runtime.InteropServices.DllImport("user32.dll", EntryPoint = "SetWindowPos")] - private static extern bool SetWindowPos( - int hWnd, // Window handle - int hWndInsertAfter, // Placement-order handle - int X, // Horizontal position - int Y, // Vertical position - int cx, // Width - int cy, // Height - uint uFlags); // Window positioning flags - - [System.Runtime.InteropServices.DllImport("user32.dll")] - private static extern bool ShowWindow(nint hWnd, int nCmdShow); - #pragma warning restore SYSLIB1054 // Use 'LibraryImportAttribute' instead of 'DllImportAttribute' to generate P/Invoke marshalling code at compile time - - public static void ShowInactiveTopmost(Form frm) + private static void SetWindowState(Form frm, bool visible) { try { - ShowWindow(frm.Handle, SW_SHOWNOACTIVATE); - SetWindowPos(frm.Handle.ToInt32(), HWND_TOPMOST, - frm.Left, frm.Top, frm.Width, frm.Height, - SWP_NOACTIVATE); + const int SW_SHOWNOACTIVATE = 4; + var state = visible ? SW_SHOWNOACTIVATE : 0; + ShowWindowAsync(frm.Handle, state); + + [System.Runtime.InteropServices.DllImport("user32.dll")] + static extern bool ShowWindowAsync(nint hWnd, int nCmdShow); } catch { @@ -98,6 +83,8 @@ public static void ShowInactiveTopmost(Form frm) } } + private bool _isFirstShown; + public void UpdatePreviewPosition(Point location) { var cLoc = Cursor.Position; @@ -124,17 +111,19 @@ public void Clear() { try { - var token = _source.Token; + var token = _source.Token; // did the user move to another slot in time? + var noToken = CancellationToken.None; // don't throw task canceled exceptions Task.Run(async () => { - if (!Previewer.IsHandleCreated) + if (!Previewer.IsHandleCreated || !_isFirstShown) return; // not shown ever - // Give a little bit of fade-out delay - await Task.Delay(50, CancellationToken.None).ConfigureAwait(false); + // Give a little bit of delay before hiding, assuming user is moving between slots. If they enter another, we'll cancel. + + await Task.Delay(50, noToken).ConfigureAwait(false); if (!token.IsCancellationRequested) - await Previewer.InvokeAsync(Previewer.Hide, CancellationToken.None).ConfigureAwait(false); - }, CancellationToken.None).ConfigureAwait(false); + await Previewer.InvokeAsync(() => SetWindowState(Previewer, false), noToken); // hide + }, noToken).ConfigureAwait(false); } catch {