simplify window recovery procedure

This commit is contained in:
Kang Yu 2020-01-29 10:58:30 -08:00
parent c82aaa3c59
commit a84b5f07bc

View file

@ -101,8 +101,7 @@ namespace Ninjacrab.PersistentWindows.Common
foreach (var window in appWindows) foreach (var window in appWindows)
{ {
ApplicationDisplayMetrics curDisplayMetrics = null; ApplicationDisplayMetrics curDisplayMetrics = null;
bool updateScreenCoord; if (NeedUpdateWindow(displayKey, window, out curDisplayMetrics))
if (NeedUpdateWindow(displayKey, window, out curDisplayMetrics, out updateScreenCoord))
{ {
updateApps.Add(curDisplayMetrics); updateApps.Add(curDisplayMetrics);
updateLogs.Add(string.Format("Captured {0,-8} at [{1,4}x{2,4}] size [{3,4}x{4,4}] V:{5} {6} ", updateLogs.Add(string.Format("Captured {0,-8} at [{1,4}x{2,4}] size [{3,4}x{4,4}] V:{5} {6} ",
@ -205,7 +204,7 @@ namespace Ninjacrab.PersistentWindows.Common
); );
} }
private bool NeedUpdateWindow(string displayKey, SystemWindow window, out ApplicationDisplayMetrics curDisplayMetric, out bool updateScreenCoord) private bool NeedUpdateWindow(string displayKey, SystemWindow window, out ApplicationDisplayMetrics curDisplayMetric)
{ {
WindowPlacement windowPlacement = new WindowPlacement(); WindowPlacement windowPlacement = new WindowPlacement();
User32.GetWindowPlacement(window.HWnd, ref windowPlacement); User32.GetWindowPlacement(window.HWnd, ref windowPlacement);
@ -233,7 +232,6 @@ namespace Ninjacrab.PersistentWindows.Common
}; };
bool needUpdate = false; bool needUpdate = false;
updateScreenCoord = false;
if (!monitorApplications[displayKey].ContainsKey(curDisplayMetric.Key)) if (!monitorApplications[displayKey].ContainsKey(curDisplayMetric.Key))
{ {
needUpdate = true; needUpdate = true;
@ -251,13 +249,12 @@ namespace Ninjacrab.PersistentWindows.Common
{ {
if (!prevDisplayMetrics.ScreenPosition.Equals(curDisplayMetric.ScreenPosition)) if (!prevDisplayMetrics.ScreenPosition.Equals(curDisplayMetric.ScreenPosition))
{ {
updateScreenCoord = true;
needUpdate = true; needUpdate = true;
} }
else if (!prevDisplayMetrics.EqualPlacement(curDisplayMetric)) else if (!prevDisplayMetrics.EqualPlacement(curDisplayMetric))
{ {
// WindowPlacement.NormalPosition change must conform to ScreenPosition change // WindowPlacement.NormalPosition change must conform to ScreenPosition change
Log.Error("The WindowPlacement.NormalPosition change does not conform to ScreenPosition change {0} {1} {2}", Log.Trace("The WindowPlacement.NormalPosition change does not conform to ScreenPosition change {0} {1} {2}",
window.Process.ProcessName, processId, window.HWnd.ToString("X8")); window.Process.ProcessName, processId, window.HWnd.ToString("X8"));
} }
} }
@ -326,8 +323,7 @@ namespace Ninjacrab.PersistentWindows.Common
IntPtr hwnd = prevDisplayMetrics.HWnd; IntPtr hwnd = prevDisplayMetrics.HWnd;
ApplicationDisplayMetrics curDisplayMetrics = null; ApplicationDisplayMetrics curDisplayMetrics = null;
bool updateScreenCoord; if (!NeedUpdateWindow(displayKey, window, out curDisplayMetrics))
if (!NeedUpdateWindow(displayKey, window, out curDisplayMetrics, out updateScreenCoord))
{ {
// window position has no change // window position has no change
continue; continue;
@ -346,6 +342,7 @@ namespace Ninjacrab.PersistentWindows.Common
*/ */
bool success; bool success;
// recover NormalPosition (the workspace position prior to snap)
success = User32.SetWindowPlacement(hwnd, ref windowPlacement); success = User32.SetWindowPlacement(hwnd, ref windowPlacement);
Log.Info("SetWindowPlacement({0} [{1}x{2}]-[{3}x{4}]) - {5}", Log.Info("SetWindowPlacement({0} [{1}x{2}]-[{3}x{4}]) - {5}",
window.Process.ProcessName, window.Process.ProcessName,
@ -355,10 +352,7 @@ namespace Ninjacrab.PersistentWindows.Common
windowPlacement.NormalPosition.Height, windowPlacement.NormalPosition.Height,
success); success);
if (updateScreenCoord) // recover current screen position
{
if (NeedUpdateWindow(displayKey, window, out curDisplayMetrics, out updateScreenCoord))
{
RECT rect = prevDisplayMetrics.ScreenPosition; RECT rect = prevDisplayMetrics.ScreenPosition;
success |= User32.MoveWindow(hwnd, rect.Left, rect.Top, rect.Width, rect.Height, true); success |= User32.MoveWindow(hwnd, rect.Left, rect.Top, rect.Width, rect.Height, true);
Log.Info("MoveWindow({0} [{1}x{2}]-[{3}x{4}]) - {5}", Log.Info("MoveWindow({0} [{1}x{2}]-[{3}x{4}]) - {5}",
@ -368,8 +362,6 @@ namespace Ninjacrab.PersistentWindows.Common
rect.Width, rect.Width,
rect.Height, rect.Height,
success); success);
}
}
if (!success) if (!success)
{ {