refix #369, #388, #392: improve algorithm matching new window to killed one. Add command option -pos_match_threshold, using 40 as default value

This commit is contained in:
Kang Yu 2025-04-28 13:50:30 -07:00
parent 8c5d6475ad
commit cce0f7f52d
3 changed files with 48 additions and 6 deletions

View file

@ -43,7 +43,7 @@ namespace PersistentWindows.Common
private const int PauseRestoreTaskbar = 3500; //cursor idle time before dragging taskbar
private const int MinClassNamePrefix = 8; //allow partial class name matching during inheritance
private const int MaxDiffPos = 1000; //allow matching to window of different position
public int MaxDiffPos = 40; //allow matching window of slightly different position
private bool initialized = false;
@ -1521,7 +1521,6 @@ namespace PersistentWindows.Common
if (title.Equals(appPos.Title))
{
if (title_match_cnt == 0)
title_match_hid = kid;
++title_match_cnt;
}
@ -1543,15 +1542,47 @@ namespace PersistentWindows.Common
if (pos_match_cnt == 1)
return pos_match_hid;
if (similar_pos_cnt == 1 || diff_size < MaxDiffPos)
if (diff_size <= MaxDiffPos)
{
Log.Event($"found similar match with pos diff of {diff_size}");
Log.Event($"matching window with position diff of {diff_size}");
return similar_pos_hid;
}
/*
if (title_match_cnt == 1)
{
Log.Event($"matching window with same title");
return title_match_hid;
}
*/
if (!monitorApplications.ContainsKey(curDisplayKey))
return IntPtr.Zero;
int proc_name_match_cnt = 0;
int class_name_match_cnt = 0;
foreach(var h in monitorApplications[curDisplayKey].Keys)
{
foreach (var dm in monitorApplications[curDisplayKey][h])
{
if (dm.ProcessName == procName)
proc_name_match_cnt++;
if (dm.ClassName == className)
class_name_match_cnt++;
break;
}
}
//force match if hwnd is the first live window of the app
if (proc_name_match_cnt == 0)
return similar_pos_hid;
if (proc_name_match_cnt == 1 && class_name_match_cnt == 0)
return similar_pos_hid;
//force match if hwnd-like window has multiple instantiations but has only one top-level matching candidate
if (similar_pos_cnt == 1 && class_name_match_cnt > 0)
return similar_pos_hid;
}
return IntPtr.Zero;
}

View file

@ -64,7 +64,8 @@ namespace PersistentWindows.Common.WinApiBridge
public int Diff(RECT r)
{
return Math.Abs(Left - r.Left) + Math.Abs(Right - r.Right) + Math.Abs(Top - r.Top) + Math.Abs(Bottom - r.Bottom);
int diff = Math.Abs(Left - r.Left) + Math.Abs(Right - r.Right) + Math.Abs(Top - r.Top) + Math.Abs(Bottom - r.Bottom);
return diff / 4;
}
}
}

View file

@ -74,6 +74,7 @@ if not errorlevel 1 goto wait_to_finish";
bool offscreen_fix = true;
bool fix_unminimized_window = true;
bool enhanced_offscreen_fix = false;
bool set_pos_match_threshold = false;
bool auto_restore_missing_windows = false;
bool auto_restore_from_db_at_startup = false;
bool auto_restore_last_capture_at_startup = false;
@ -143,6 +144,12 @@ if not errorlevel 1 goto wait_to_finish";
restore_snapshot = SnapshotCharToId(arg[0]);
continue;
}
else if (set_pos_match_threshold)
{
set_pos_match_threshold = false;
pwp.MaxDiffPos = int.Parse(arg);
continue;
}
switch(arg)
{
@ -266,6 +273,9 @@ if not errorlevel 1 goto wait_to_finish";
case "-auto_restore_new_window_to_last_capture=1":
pwp.autoRestoreNewWindowToLastCapture = true;
break;
case "-pos_match_threshold":
set_pos_match_threshold = true;
break;
case "-auto_restore_missing_windows":
case "-auto_restore_missing_windows=1":
auto_restore_missing_windows = true;