Form: handle filtering inside RefreshCurrentProfile, handle backspace

Fixes momentary flicker when saving profile, also seemed to help with
the collapsible groups perf issue, so removed the 250ms wait before
applying filter

Updated with NVDRS_APPLICATION_V4 from nvapi r575
This commit is contained in:
emoose
2025-07-28 11:04:35 +01:00
parent 8e0360149c
commit de9f86e878
4 changed files with 69 additions and 66 deletions

View File

@@ -549,7 +549,7 @@ namespace nspector.Common
});
}
private string GetApplicationFingerprint(NVDRS_APPLICATION_V3 application)
private string GetApplicationFingerprint(NVDRS_APPLICATION_V4 application)
{
return $"{application.appName}|{application.fileInFolder}|{application.userFriendlyName}|{application.launcher}";
}

View File

@@ -215,9 +215,9 @@ namespace nspector.Common
protected void AddApplication(IntPtr hSession, IntPtr hProfile, string applicationName)
{
var newApp = new NVDRS_APPLICATION_V3()
var newApp = new NVDRS_APPLICATION_V4()
{
version = nvw.NVDRS_APPLICATION_VER_V3,
version = nvw.NVDRS_APPLICATION_VER_V4,
appName = applicationName,
};
@@ -227,7 +227,7 @@ namespace nspector.Common
}
protected void DeleteApplication(IntPtr hSession, IntPtr hProfile, NVDRS_APPLICATION_V3 application)
protected void DeleteApplication(IntPtr hSession, IntPtr hProfile, NVDRS_APPLICATION_V4 application)
{
var caRes = nvw.DRS_DeleteApplicationEx(hSession, hProfile, ref application);
if (caRes != NvAPI_Status.NVAPI_OK)
@@ -285,16 +285,16 @@ namespace nspector.Common
return settings.ToList();
}
protected List<NVDRS_APPLICATION_V3> GetProfileApplications(IntPtr hSession, IntPtr hProfile)
protected List<NVDRS_APPLICATION_V4> GetProfileApplications(IntPtr hSession, IntPtr hProfile)
{
uint appCount = 512;
var apps = new NVDRS_APPLICATION_V3[512];
apps[0].version = NvapiDrsWrapper.NVDRS_APPLICATION_VER_V3;
var apps = new NVDRS_APPLICATION_V4[512];
apps[0].version = NvapiDrsWrapper.NVDRS_APPLICATION_VER_V4;
var esRes = NvapiDrsWrapper.DRS_EnumApplications(hSession, hProfile, 0, ref appCount, ref apps);
if (esRes == NvAPI_Status.NVAPI_END_ENUMERATION)
return new List<NVDRS_APPLICATION_V3>();
return new List<NVDRS_APPLICATION_V4>();
if (esRes != NvAPI_Status.NVAPI_OK)
throw new NvapiException("DRS_EnumApplications", esRes);

View File

@@ -344,7 +344,8 @@ namespace nspector.Native.NVAPI2
[StructLayout(LayoutKind.Sequential, Pack = 8, CharSet = CharSet.Unicode)]
internal struct NVDRS_APPLICATION_V3
{
public uint isMetro { get { return ((uint)((bitvector1 & 1))); } set { bitvector1 = ((uint)((value | bitvector1))); } }
public bool isMetro { get { return (bitvector1 & 1) != 0; } set { if (value) bitvector1 |= 1; else bitvector1 &= ~1u; } }
public bool isCommandLine { get { return (bitvector1 & 2) != 0; } set { if (value) bitvector1 |= 2; else bitvector1 &= ~2u; } }
public uint version;
public uint isPredefined;
@@ -359,6 +360,27 @@ namespace nspector.Native.NVAPI2
private uint bitvector1;
}
[StructLayout(LayoutKind.Sequential, Pack = 8, CharSet = CharSet.Unicode)]
internal struct NVDRS_APPLICATION_V4
{
public bool isMetro { get { return (bitvector1 & 1) != 0; } set { if (value) bitvector1 |= 1; else bitvector1 &= ~1u; } }
public bool isCommandLine { get { return (bitvector1 & 2) != 0; } set { if (value) bitvector1 |= 2; else bitvector1 &= ~2u; } }
public uint version;
public uint isPredefined;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = (int)NvapiDrsWrapper.NVAPI_UNICODE_STRING_MAX)]
public string appName;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = (int)NvapiDrsWrapper.NVAPI_UNICODE_STRING_MAX)]
public string userFriendlyName;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = (int)NvapiDrsWrapper.NVAPI_UNICODE_STRING_MAX)]
public string launcher;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = (int)NvapiDrsWrapper.NVAPI_UNICODE_STRING_MAX)]
public string fileInFolder;
private uint bitvector1;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = (int)NvapiDrsWrapper.NVAPI_UNICODE_STRING_MAX)]
public string commandLine;
}
[StructLayout(LayoutKind.Sequential, Pack = 8, CharSet = CharSet.Unicode)]
internal struct NVDRS_PROFILE
{
@@ -388,7 +410,8 @@ namespace nspector.Native.NVAPI2
public static uint NVDRS_APPLICATION_VER_V1 = MAKE_NVAPI_VERSION<NVDRS_APPLICATION_V1>(1);
public static uint NVDRS_APPLICATION_VER_V2 = MAKE_NVAPI_VERSION<NVDRS_APPLICATION_V2>(2);
public static uint NVDRS_APPLICATION_VER_V3 = MAKE_NVAPI_VERSION<NVDRS_APPLICATION_V3>(3);
public static uint NVDRS_APPLICATION_VER = NVDRS_APPLICATION_VER_V3;
public static uint NVDRS_APPLICATION_VER_V4 = MAKE_NVAPI_VERSION<NVDRS_APPLICATION_V4>(4);
public static uint NVDRS_APPLICATION_VER = NVDRS_APPLICATION_VER_V4;
public static uint NVDRS_PROFILE_VER = MAKE_NVAPI_VERSION<NVDRS_PROFILE>(1);
public const uint OGL_IMPLICIT_GPU_AFFINITY_NUM_VALUES = 1;
@@ -549,11 +572,11 @@ namespace nspector.Native.NVAPI2
public static readonly DRS_GetNumProfilesDelegate DRS_GetNumProfiles;
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
public delegate NvAPI_Status DRS_CreateApplicationDelegate(IntPtr hSession, IntPtr hProfile, ref NVDRS_APPLICATION_V3 pApplication);
public delegate NvAPI_Status DRS_CreateApplicationDelegate(IntPtr hSession, IntPtr hProfile, ref NVDRS_APPLICATION_V4 pApplication);
public static readonly DRS_CreateApplicationDelegate DRS_CreateApplication;
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
public delegate NvAPI_Status DRS_DeleteApplicationExDelegate(IntPtr hSession, IntPtr hProfile, ref NVDRS_APPLICATION_V3 pApp);
public delegate NvAPI_Status DRS_DeleteApplicationExDelegate(IntPtr hSession, IntPtr hProfile, ref NVDRS_APPLICATION_V4 pApp);
public static readonly DRS_DeleteApplicationExDelegate DRS_DeleteApplicationEx;
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
@@ -561,7 +584,7 @@ namespace nspector.Native.NVAPI2
public static readonly DRS_DeleteApplicationDelegate DRS_DeleteApplication;
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
public delegate NvAPI_Status DRS_GetApplicationInfoDelegate(IntPtr hSession, IntPtr hProfile, [MarshalAs(UnmanagedType.LPWStr, SizeConst = (int)NvapiDrsWrapper.NVAPI_UNICODE_STRING_MAX)]StringBuilder appName, ref NVDRS_APPLICATION_V3 pApplication);
public delegate NvAPI_Status DRS_GetApplicationInfoDelegate(IntPtr hSession, IntPtr hProfile, [MarshalAs(UnmanagedType.LPWStr, SizeConst = (int)NvapiDrsWrapper.NVAPI_UNICODE_STRING_MAX)]StringBuilder appName, ref NVDRS_APPLICATION_V4 pApplication);
public static readonly DRS_GetApplicationInfoDelegate DRS_GetApplicationInfo;
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
@@ -587,7 +610,7 @@ namespace nspector.Native.NVAPI2
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
public delegate NvAPI_Status DRS_FindApplicationByNameDelegate(IntPtr hSession, [MarshalAs(UnmanagedType.LPWStr, SizeConst = (int)NvapiDrsWrapper.NVAPI_UNICODE_STRING_MAX)]StringBuilder appName, ref IntPtr phProfile, ref NVDRS_APPLICATION_V3 pApplication);
public delegate NvAPI_Status DRS_FindApplicationByNameDelegate(IntPtr hSession, [MarshalAs(UnmanagedType.LPWStr, SizeConst = (int)NvapiDrsWrapper.NVAPI_UNICODE_STRING_MAX)]StringBuilder appName, ref IntPtr phProfile, ref NVDRS_APPLICATION_V4 pApplication);
public static readonly DRS_FindApplicationByNameDelegate DRS_FindApplicationByName;
public static NvAPI_Status DRS_SetSetting(IntPtr hSession, IntPtr hProfile, ref NVDRS_SETTING pSetting)

View File

@@ -42,6 +42,8 @@ namespace nspector
private bool isDevMode = false;
private Dictionary<string, bool> _groupCollapsedStates = new();
protected override void WndProc(ref Message m)
{
switch (m.Msg)
@@ -190,6 +192,8 @@ namespace nspector
}
}
ApplySearchFilter();
lvSettings.EndUpdate();
GC.Collect();
@@ -209,7 +213,6 @@ namespace nspector
}
}
}
}
private void RefreshProfilesCombo()
@@ -550,8 +553,6 @@ namespace nspector
LoadGroupStates(Path.Combine(AppContext.BaseDirectory, "HiddenGroups.ini"));
}
private Dictionary<string, bool> _groupCollapsedStates = new();
private void lvSettings_GroupStateChanged(object sender, GroupStateChangedEventArgs e)
{
_groupCollapsedStates[e.Group.Header] = e.IsCollapsed;
@@ -580,11 +581,11 @@ namespace nspector
private bool LoadGroupStates(string filePath)
{
_groupCollapsedStates.Clear();
if (!File.Exists(filePath))
return false;
_groupCollapsedStates.Clear();
try
{
foreach (var line in File.ReadAllLines(filePath))
@@ -707,8 +708,6 @@ namespace nspector
DeleteSelectedValue();
else
ResetSelectedValue();
ApplySearchFilter();
}
ToolTip appPathsTooltip = new ToolTip() { InitialDelay = 250 };
@@ -921,8 +920,6 @@ namespace nspector
catch { }
StoreChangesOfProfileToDriver();
ApplySearchFilter();
}
private void tsbBitValueEditor_Click(object sender, EventArgs e)
@@ -1402,69 +1399,52 @@ namespace nspector
else if (!e.Control && (e.KeyCode >= Keys.A && e.KeyCode <= Keys.Z ||
e.KeyCode >= Keys.D0 && e.KeyCode <= Keys.D9 ||
e.KeyCode >= Keys.NumPad0 && e.KeyCode <= Keys.NumPad9 ||
e.KeyCode == Keys.Space || e.KeyCode == Keys.OemPeriod))
e.KeyCode == Keys.Space || e.KeyCode == Keys.OemPeriod ||
e.KeyCode == Keys.Back))
{
txtFilter.Visible = true;
txtFilter.Focus();
txtFilter.Text += e.Shift ? e.KeyCode.ToString() : e.KeyCode.ToString().ToLower();
if (e.KeyCode == Keys.Back)
{
if (txtFilter.Text.Length > 0)
{
txtFilter.Text = txtFilter.Text.Substring(0, txtFilter.Text.Length - 1);
}
}
else
{
txtFilter.Text += e.Shift ? e.KeyCode.ToString() : e.KeyCode.ToString().ToLower();
}
txtFilter.SelectionStart = txtFilter.Text.Length;
e.SuppressKeyPress = true;
e.Handled = true;
}
}
private CancellationTokenSource cts;
private void ApplySearchFilter(CancellationTokenSource cts = null)
private void ApplySearchFilter()
{
var lowerInput = txtFilter.Text.Trim().ToLowerInvariant();
if (cts != null && cts.Token.IsCancellationRequested) return;
Invoke(new Action(() =>
if (string.IsNullOrEmpty(lowerInput))
{
RefreshCurrentProfile();
return;
}
if (string.IsNullOrEmpty(lowerInput))
lvSettings.BeginUpdate();
foreach (ListViewItem itm in lvSettings.Items)
{
if (!itm.Text.ToLowerInvariant().Contains(lowerInput))
{
return;
itm.Remove();
}
lvSettings.BeginUpdate();
foreach (ListViewItem itm in lvSettings.Items)
{
if (!itm.Text.ToLowerInvariant().Contains(lowerInput))
{
itm.Remove();
}
}
lvSettings.EndUpdate();
txtFilter.Focus(); // Setting listbox sometimes steals focus away
}));
}
lvSettings.EndUpdate();
}
private async void txtFilter_TextChanged(object sender, EventArgs e)
{
cts?.Cancel();
cts = new CancellationTokenSource();
RefreshCurrentProfile();
try
{
await Task.Delay(250, cts.Token); // search filter can be slow, wait for user to stop typing for ~250ms before we start refresh
if (cts.Token.IsCancellationRequested) return;
await Task.Run(() =>
{
ApplySearchFilter(cts);
});
}
catch (TaskCanceledException)
{
// Ignore cancellation
}
txtFilter.Focus(); // Setting listbox sometimes steals focus away
}
private void EnableDevmode()