Skip to content

Commit

Permalink
[FancyZones] Open window in a zone fix (#18193)
Browse files Browse the repository at this point in the history
* removed background thread monitoring virtual desktops

* removed virtual desktop callbacks

* centralize work with virtual desktops

* updated virtual desktops sync

* app zone history logs

* logs

* get uwp process path

* spell
  • Loading branch information
SeraphimaZykova committed May 13, 2022
1 parent d15ff6d commit 5f4a26e
Show file tree
Hide file tree
Showing 11 changed files with 255 additions and 248 deletions.
17 changes: 17 additions & 0 deletions src/common/utils/process_path.h
Expand Up @@ -68,6 +68,23 @@ inline std::wstring get_process_path(HWND window) noexcept
return name;
}

inline std::wstring get_process_path_waiting_uwp(HWND window)
{
const static std::wstring appFrameHost = L"ApplicationFrameHost.exe";

int attempt = 0;
auto processPath = get_process_path(window);

while (++attempt < 30 && processPath.length() >= appFrameHost.length() &&
processPath.compare(processPath.length() - appFrameHost.length(), appFrameHost.length(), appFrameHost) == 0)
{
std::this_thread::sleep_for(std::chrono::milliseconds(5));
processPath = get_process_path(window);
}

return processPath;
}

inline std::wstring get_module_filename(HMODULE mod = nullptr)
{
wchar_t buffer[MAX_PATH + 1];
Expand Down
95 changes: 29 additions & 66 deletions src/modules/fancyzones/FancyZonesLib/FancyZones.cpp
Expand Up @@ -59,12 +59,6 @@ struct FancyZones : public winrt::implements<FancyZones, IFancyZones, IFancyZone
m_hinstance(hinstance),
m_windowMoveHandler([this]() {
PostMessageW(m_window, WM_PRIV_LOCATIONCHANGE, NULL, NULL);
}),
m_virtualDesktop([this]() {
PostMessage(m_window, WM_PRIV_VD_INIT, 0, 0);
},
[this]() {
PostMessage(m_window, WM_PRIV_VD_UPDATE, 0, 0);
})
{
this->disableModuleCallback = std::move(disableModuleCallback);
Expand Down Expand Up @@ -92,7 +86,7 @@ struct FancyZones : public winrt::implements<FancyZones, IFancyZones, IFancyZone
monitor = NULL;
}

m_windowMoveHandler.MoveSizeStart(window, monitor, ptScreen, m_workAreaHandler.GetWorkAreasByDesktopId(m_currentDesktopId));
m_windowMoveHandler.MoveSizeStart(window, monitor, ptScreen, m_workAreaHandler.GetWorkAreasByDesktopId(VirtualDesktop::instance().GetCurrentVirtualDesktopId()));
}

void MoveSizeUpdate(HMONITOR monitor, POINT const& ptScreen) noexcept
Expand All @@ -101,12 +95,12 @@ struct FancyZones : public winrt::implements<FancyZones, IFancyZones, IFancyZone
{
monitor = NULL;
}
m_windowMoveHandler.MoveSizeUpdate(monitor, ptScreen, m_workAreaHandler.GetWorkAreasByDesktopId(m_currentDesktopId));
m_windowMoveHandler.MoveSizeUpdate(monitor, ptScreen, m_workAreaHandler.GetWorkAreasByDesktopId(VirtualDesktop::instance().GetCurrentVirtualDesktopId()));
}

void MoveSizeEnd(HWND window, POINT const& ptScreen) noexcept
{
m_windowMoveHandler.MoveSizeEnd(window, ptScreen, m_workAreaHandler.GetWorkAreasByDesktopId(m_currentDesktopId));
m_windowMoveHandler.MoveSizeEnd(window, ptScreen, m_workAreaHandler.GetWorkAreasByDesktopId(VirtualDesktop::instance().GetCurrentVirtualDesktopId()));
}

IFACEMETHODIMP_(void)
Expand Down Expand Up @@ -189,10 +183,7 @@ struct FancyZones : public winrt::implements<FancyZones, IFancyZones, IFancyZone
HWND m_window{};
WindowMoveHandler m_windowMoveHandler;
MonitorWorkAreaHandler m_workAreaHandler;
VirtualDesktop m_virtualDesktop;

GUID m_previousDesktopId{}; // UUID of previously active virtual desktop.
GUID m_currentDesktopId{}; // UUID of the current virtual desktop.
wil::unique_handle m_terminateEditorEvent; // Handle of FancyZonesEditor.exe we launch and wait on

OnThreadExecutor m_dpiUnawareThread;
Expand Down Expand Up @@ -258,8 +249,6 @@ FancyZones::Run() noexcept
}
}

m_virtualDesktop.Init();

m_dpiUnawareThread.submit(OnThreadExecutor::task_t{ [] {
SetThreadDpiAwarenessContext(DPI_AWARENESS_CONTEXT_UNAWARE);
SetThreadDpiHostingBehavior(DPI_HOSTING_BEHAVIOR_MIXED);
Expand All @@ -274,8 +263,7 @@ FancyZones::Run() noexcept
}
});

AppliedLayouts::instance().SetVirtualDesktopCheckCallback(std::bind(&VirtualDesktop::IsVirtualDesktopIdSavedInRegistry, &m_virtualDesktop, std::placeholders::_1));
AppZoneHistory::instance().SetVirtualDesktopCheckCallback(std::bind(&VirtualDesktop::IsVirtualDesktopIdSavedInRegistry, &m_virtualDesktop, std::placeholders::_1));
PostMessage(m_window, WM_PRIV_VD_INIT, 0, 0);
}

// IFancyZones
Expand All @@ -289,8 +277,6 @@ FancyZones::Destroy() noexcept
DestroyWindow(m_window);
m_window = nullptr;
}

m_virtualDesktop.UnInit();
}

// IFancyZonesCallback
Expand Down Expand Up @@ -343,7 +329,7 @@ void FancyZones::MoveWindowIntoZone(HWND window, winrt::com_ptr<IWorkArea> workA

bool FancyZones::MoveToAppLastZone(HWND window, HMONITOR active, HMONITOR primary) noexcept
{
auto workAreaMap = m_workAreaHandler.GetWorkAreasByDesktopId(m_currentDesktopId);
auto workAreaMap = m_workAreaHandler.GetWorkAreasByDesktopId(VirtualDesktop::instance().GetCurrentVirtualDesktopId());
if (workAreaMap.empty())
{
Logger::trace(L"No work area for the current desktop.");
Expand Down Expand Up @@ -390,8 +376,8 @@ void FancyZones::WindowCreated(HWND window) noexcept
return;
}

auto desktopId = m_virtualDesktop.GetDesktopId(window);
if (desktopId.has_value() && *desktopId != m_currentDesktopId)
auto desktopId = VirtualDesktop::instance().GetDesktopId(window);
if (desktopId.has_value() && *desktopId != VirtualDesktop::instance().GetCurrentVirtualDesktopId())
{
// Switch between virtual desktops results with posting same windows messages that also indicate
// creation of new window. We need to check if window being processed is on currently active desktop.
Expand Down Expand Up @@ -537,7 +523,7 @@ void FancyZones::ToggleEditor() noexcept
}

wil::unique_cotaskmem_string virtualDesktopId;
if (!SUCCEEDED(StringFromCLSID(m_currentDesktopId, &virtualDesktopId)))
if (!SUCCEEDED(StringFromCLSID(VirtualDesktop::instance().GetCurrentVirtualDesktopId(), &virtualDesktopId)))
{
return;
}
Expand Down Expand Up @@ -806,28 +792,7 @@ void FancyZones::OnDisplayChange(DisplayChangeType changeType) noexcept
if (changeType == DisplayChangeType::VirtualDesktop ||
changeType == DisplayChangeType::Initialization)
{
m_previousDesktopId = m_currentDesktopId;

auto currentVirtualDesktopId = m_virtualDesktop.GetCurrentVirtualDesktopIdFromRegistry();
if (!currentVirtualDesktopId.has_value())
{
Logger::info("Virtual Desktop id from top level window");
currentVirtualDesktopId = m_virtualDesktop.GetDesktopIdByTopLevelWindows();
}

if (currentVirtualDesktopId.has_value())
{
m_currentDesktopId = *currentVirtualDesktopId;
if (m_previousDesktopId != GUID_NULL && m_currentDesktopId != m_previousDesktopId)
{
Trace::VirtualDesktopChanged();
}

if (m_currentDesktopId == GUID_NULL)
{
Logger::warn("Couldn't retrieve virtual desktop id");
}
}
VirtualDesktop::instance().UpdateVirtualDesktopId();

if (changeType == DisplayChangeType::Initialization)
{
Expand All @@ -848,16 +813,16 @@ void FancyZones::OnDisplayChange(DisplayChangeType changeType) noexcept

void FancyZones::AddWorkArea(HMONITOR monitor, const std::wstring& deviceId) noexcept
{
if (m_workAreaHandler.IsNewWorkArea(m_currentDesktopId, monitor))
if (m_workAreaHandler.IsNewWorkArea(VirtualDesktop::instance().GetCurrentVirtualDesktopId(), monitor))
{
wil::unique_cotaskmem_string virtualDesktopIdStr;
if (!SUCCEEDED(StringFromCLSID(m_currentDesktopId, &virtualDesktopIdStr)))
if (!SUCCEEDED(StringFromCLSID(VirtualDesktop::instance().GetCurrentVirtualDesktopId(), &virtualDesktopIdStr)))
{
Logger::debug(L"Add new work area on virtual desktop {}", virtualDesktopIdStr.get());
}

FancyZonesDataTypes::DeviceIdData uniqueId;
uniqueId.virtualDesktopId = m_currentDesktopId;
uniqueId.virtualDesktopId = VirtualDesktop::instance().GetCurrentVirtualDesktopId();

if (monitor)
{
Expand All @@ -869,7 +834,7 @@ void FancyZones::AddWorkArea(HMONITOR monitor, const std::wstring& deviceId) noe
}

FancyZonesDataTypes::DeviceIdData parentId{};
auto parentArea = m_workAreaHandler.GetWorkArea(m_previousDesktopId, monitor);
auto parentArea = m_workAreaHandler.GetWorkArea(VirtualDesktop::instance().GetPreviousVirtualDesktopId(), monitor);
if (parentArea)
{
parentId = parentArea->UniqueId();
Expand All @@ -878,7 +843,7 @@ void FancyZones::AddWorkArea(HMONITOR monitor, const std::wstring& deviceId) noe
auto workArea = MakeWorkArea(m_hinstance, monitor, uniqueId, parentId);
if (workArea)
{
m_workAreaHandler.AddWorkArea(m_currentDesktopId, monitor, workArea);
m_workAreaHandler.AddWorkArea(VirtualDesktop::instance().GetCurrentVirtualDesktopId(), monitor, workArea);
AppliedLayouts::instance().SaveData();
}
}
Expand Down Expand Up @@ -935,7 +900,7 @@ void FancyZones::UpdateWorkAreas() noexcept

void FancyZones::UpdateWindowsPositions(bool suppressMove) noexcept
{
for (const auto [window, desktopId] : m_virtualDesktop.GetWindowsRelatedToDesktops())
for (const auto [window, desktopId] : VirtualDesktop::instance().GetWindowsRelatedToDesktops())
{
auto zoneIndexSet = FancyZonesWindowProperties::RetrieveZoneIndexProperty(window);
auto workArea = m_workAreaHandler.GetWorkArea(window, desktopId);
Expand All @@ -951,7 +916,7 @@ void FancyZones::CycleTabs(bool reverse) noexcept
auto window = GetForegroundWindow();
HMONITOR current = WorkAreaKeyFromWindow(window);

auto workArea = m_workAreaHandler.GetWorkArea(m_currentDesktopId, current);
auto workArea = m_workAreaHandler.GetWorkArea(VirtualDesktop::instance().GetCurrentVirtualDesktopId(), current);
if (workArea)
{
workArea->CycleTabs(window, reverse);
Expand All @@ -969,7 +934,7 @@ bool FancyZones::OnSnapHotkeyBasedOnZoneNumber(HWND window, DWORD vkCode) noexce
auto currMonitorInfo = std::find(std::begin(monitorInfo), std::end(monitorInfo), current);
do
{
auto workArea = m_workAreaHandler.GetWorkArea(m_currentDesktopId, *currMonitorInfo);
auto workArea = m_workAreaHandler.GetWorkArea(VirtualDesktop::instance().GetCurrentVirtualDesktopId(), *currMonitorInfo);
if (m_windowMoveHandler.MoveWindowIntoZoneByDirectionAndIndex(window, vkCode, false /* cycle through zones */, workArea))
{
Trace::FancyZones::KeyboardSnapWindowToZone(workArea->ZoneSet());
Expand All @@ -996,7 +961,7 @@ bool FancyZones::OnSnapHotkeyBasedOnZoneNumber(HWND window, DWORD vkCode) noexce
}
else
{
auto workArea = m_workAreaHandler.GetWorkArea(m_currentDesktopId, current);
auto workArea = m_workAreaHandler.GetWorkArea(VirtualDesktop::instance().GetCurrentVirtualDesktopId(), current);
// Single monitor environment, or combined multi-monitor environment.
if (FancyZonesSettings::settings().restoreSize)
{
Expand Down Expand Up @@ -1036,7 +1001,7 @@ bool FancyZones::OnSnapHotkeyBasedOnPosition(HWND window, DWORD vkCode) noexcept
{
// Multi monitor environment.
// First, try to stay on the same monitor
bool success = ProcessDirectedSnapHotkey(window, vkCode, false, m_workAreaHandler.GetWorkArea(m_currentDesktopId, current));
bool success = ProcessDirectedSnapHotkey(window, vkCode, false, m_workAreaHandler.GetWorkArea(VirtualDesktop::instance().GetCurrentVirtualDesktopId(), current));
if (success)
{
return true;
Expand All @@ -1055,7 +1020,7 @@ bool FancyZones::OnSnapHotkeyBasedOnPosition(HWND window, DWORD vkCode) noexcept
}
else
{
auto workArea = m_workAreaHandler.GetWorkArea(m_currentDesktopId, monitor);
auto workArea = m_workAreaHandler.GetWorkArea(VirtualDesktop::instance().GetCurrentVirtualDesktopId(), monitor);
if (workArea)
{
auto zoneSet = workArea->ZoneSet();
Expand Down Expand Up @@ -1103,7 +1068,7 @@ bool FancyZones::OnSnapHotkeyBasedOnPosition(HWND window, DWORD vkCode) noexcept
// Sanity check: the current monitor is valid
if (currentMonitorRect.top <= currentMonitorRect.bottom)
{
auto workArea = m_workAreaHandler.GetWorkArea(m_currentDesktopId, current);
auto workArea = m_workAreaHandler.GetWorkArea(VirtualDesktop::instance().GetCurrentVirtualDesktopId(), current);
if (workArea)
{
auto zoneSet = workArea->ZoneSet();
Expand Down Expand Up @@ -1150,7 +1115,7 @@ bool FancyZones::OnSnapHotkeyBasedOnPosition(HWND window, DWORD vkCode) noexcept
else
{
// Single monitor environment, or combined multi-monitor environment.
return ProcessDirectedSnapHotkey(window, vkCode, true, m_workAreaHandler.GetWorkArea(m_currentDesktopId, current));
return ProcessDirectedSnapHotkey(window, vkCode, true, m_workAreaHandler.GetWorkArea(VirtualDesktop::instance().GetCurrentVirtualDesktopId(), current));
}
}

Expand Down Expand Up @@ -1194,16 +1159,16 @@ bool FancyZones::ProcessDirectedSnapHotkey(HWND window, DWORD vkCode, bool cycle

void FancyZones::RegisterVirtualDesktopUpdates() noexcept
{
auto guids = m_virtualDesktop.GetVirtualDesktopIdsFromRegistry();
auto guids = VirtualDesktop::instance().GetVirtualDesktopIdsFromRegistry();
if (guids.has_value())
{
m_workAreaHandler.RegisterUpdates(*guids);
AppZoneHistory::instance().RemoveDeletedVirtualDesktops(*guids);
AppliedLayouts::instance().RemoveDeletedVirtualDesktops(*guids);
}

AppZoneHistory::instance().SyncVirtualDesktops(m_currentDesktopId);
AppliedLayouts::instance().SyncVirtualDesktops(m_currentDesktopId);
AppZoneHistory::instance().SyncVirtualDesktops();
AppliedLayouts::instance().SyncVirtualDesktops();
}

void FancyZones::UpdateHotkey(int hotkeyId, const PowerToysSettings::HotkeyObject& hotkeyObject, bool enable) noexcept
Expand Down Expand Up @@ -1263,8 +1228,6 @@ void FancyZones::SettingsUpdate(SettingId id)
void FancyZones::OnEditorExitEvent() noexcept
{
// Collect information about changes in zone layout after editor exited.
AppZoneHistory::instance().SyncVirtualDesktops(m_currentDesktopId);
AppliedLayouts::instance().SyncVirtualDesktops(m_currentDesktopId);
UpdateZoneSets();
}

Expand All @@ -1286,7 +1249,7 @@ bool FancyZones::ShouldProcessSnapHotkey(DWORD vkCode) noexcept
{
HMONITOR monitor = WorkAreaKeyFromWindow(window);

auto workArea = m_workAreaHandler.GetWorkArea(m_currentDesktopId, monitor);
auto workArea = m_workAreaHandler.GetWorkArea(VirtualDesktop::instance().GetCurrentVirtualDesktopId(), monitor);
if (workArea && workArea->ZoneSet() && workArea->ZoneSet()->LayoutType() != FancyZonesDataTypes::ZoneSetLayoutType::Blank)
{
if (vkCode == VK_UP || vkCode == VK_DOWN)
Expand Down Expand Up @@ -1317,7 +1280,7 @@ void FancyZones::ApplyQuickLayout(int key) noexcept
return;
}

auto workArea = m_workAreaHandler.GetWorkAreaFromCursor(m_currentDesktopId);
auto workArea = m_workAreaHandler.GetWorkAreaFromCursor(VirtualDesktop::instance().GetCurrentVirtualDesktopId());
AppliedLayouts::instance().ApplyLayout(workArea->UniqueId(), layout.value());
AppliedLayouts::instance().SaveData();
UpdateZoneSets();
Expand All @@ -1328,7 +1291,7 @@ void FancyZones::FlashZones() noexcept
{
if (FancyZonesSettings::settings().flashZonesOnQuickSwitch && !m_windowMoveHandler.IsDragEnabled())
{
for (auto [monitor, workArea] : m_workAreaHandler.GetWorkAreasByDesktopId(m_currentDesktopId))
for (auto [monitor, workArea] : m_workAreaHandler.GetWorkAreasByDesktopId(VirtualDesktop::instance().GetCurrentVirtualDesktopId()))
{
workArea->FlashZones();
}
Expand All @@ -1347,7 +1310,7 @@ std::vector<HMONITOR> FancyZones::GetMonitorsSorted() noexcept
std::vector<std::pair<HMONITOR, RECT>> FancyZones::GetRawMonitorData() noexcept
{
std::vector<std::pair<HMONITOR, RECT>> monitorInfo;
const auto& activeWorkAreaMap = m_workAreaHandler.GetWorkAreasByDesktopId(m_currentDesktopId);
const auto& activeWorkAreaMap = m_workAreaHandler.GetWorkAreasByDesktopId(VirtualDesktop::instance().GetCurrentVirtualDesktopId());
for (const auto& [monitor, workArea] : activeWorkAreaMap)
{
if (workArea->ZoneSet() != nullptr)
Expand Down

0 comments on commit 5f4a26e

Please sign in to comment.