Minor clean

This commit is contained in:
Kurt 2026-03-19 20:35:58 -05:00
parent 0e097b1fc6
commit 48938c5e14
2 changed files with 25 additions and 34 deletions

View File

@ -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")]

View File

@ -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
{