diff --git a/Ninjacrab.PersistentWindows.Solution/Common/PersistentWindowProcessor.cs b/Ninjacrab.PersistentWindows.Solution/Common/PersistentWindowProcessor.cs index d186d08..b232f09 100644 --- a/Ninjacrab.PersistentWindows.Solution/Common/PersistentWindowProcessor.cs +++ b/Ninjacrab.PersistentWindows.Solution/Common/PersistentWindowProcessor.cs @@ -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,8 +1521,7 @@ namespace PersistentWindows.Common if (title.Equals(appPos.Title)) { - if (title_match_cnt == 0) - title_match_hid = kid; + title_match_hid = kid; ++title_match_cnt; } @@ -1543,14 +1542,46 @@ 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; diff --git a/Ninjacrab.PersistentWindows.Solution/Common/WinApiBridge/WindowsPosition.cs b/Ninjacrab.PersistentWindows.Solution/Common/WinApiBridge/WindowsPosition.cs index 949d34c..521b285 100644 --- a/Ninjacrab.PersistentWindows.Solution/Common/WinApiBridge/WindowsPosition.cs +++ b/Ninjacrab.PersistentWindows.Solution/Common/WinApiBridge/WindowsPosition.cs @@ -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; } } } diff --git a/Ninjacrab.PersistentWindows.Solution/SystrayShell/Program.cs b/Ninjacrab.PersistentWindows.Solution/SystrayShell/Program.cs index 25b6755..71a5bd5 100644 --- a/Ninjacrab.PersistentWindows.Solution/SystrayShell/Program.cs +++ b/Ninjacrab.PersistentWindows.Solution/SystrayShell/Program.cs @@ -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;