mirror of
https://github.com/kangyu-california/PersistentWindows.git
synced 2025-05-12 13:35:39 +02:00
simplify window recovery procedure
This commit is contained in:
parent
c82aaa3c59
commit
a84b5f07bc
1 changed files with 15 additions and 23 deletions
|
@ -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,21 +352,16 @@ namespace Ninjacrab.PersistentWindows.Common
|
||||||
windowPlacement.NormalPosition.Height,
|
windowPlacement.NormalPosition.Height,
|
||||||
success);
|
success);
|
||||||
|
|
||||||
if (updateScreenCoord)
|
// recover current screen position
|
||||||
{
|
RECT rect = prevDisplayMetrics.ScreenPosition;
|
||||||
if (NeedUpdateWindow(displayKey, window, out curDisplayMetrics, out updateScreenCoord))
|
success |= User32.MoveWindow(hwnd, rect.Left, rect.Top, rect.Width, rect.Height, true);
|
||||||
{
|
Log.Info("MoveWindow({0} [{1}x{2}]-[{3}x{4}]) - {5}",
|
||||||
RECT rect = prevDisplayMetrics.ScreenPosition;
|
window.Process.ProcessName,
|
||||||
success |= User32.MoveWindow(hwnd, rect.Left, rect.Top, rect.Width, rect.Height, true);
|
rect.Left,
|
||||||
Log.Info("MoveWindow({0} [{1}x{2}]-[{3}x{4}]) - {5}",
|
rect.Top,
|
||||||
window.Process.ProcessName,
|
rect.Width,
|
||||||
rect.Left,
|
rect.Height,
|
||||||
rect.Top,
|
success);
|
||||||
rect.Width,
|
|
||||||
rect.Height,
|
|
||||||
success);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!success)
|
if (!success)
|
||||||
{
|
{
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue