Skip to content

Commit

Permalink
Common: remove hwnd_data_cache (microsoft#1223)
Browse files Browse the repository at this point in the history
The cache was introduced to improve performance by not querying the
OS for the window process path every time we need to check if the window
is interesting to FancyZones. Since then other changes were made to the
the way we check the windows. Right now, the IsInterestingWindow function is
called when:

  1) WinKey + arrows are used
  2) window is started to be dragged
  3) window is created

1) and 2) are initiated by the user, happen only once per interaction so
their performance impact can be dismissed. The 3) happens all the time
but for the most part the check for WS_CHILD or
GetAncestor(window, GA_ROOT) == window will filter those out. In the
end, only top-level windows will be queried for their path.

Removing the cache improves code readability and will make code
maintenance easier.
  • Loading branch information
bzoz authored and udit3333 committed Feb 20, 2020
1 parent 1fcd8dc commit 489e30b
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 197 deletions.
59 changes: 56 additions & 3 deletions src/common/common.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#include "pch.h"
#include "common.h"
#include "hwnd_data_cache.h"
#include <dwmapi.h>
#pragma comment(lib, "dwmapi.lib")
#include <strsafe.h>
Expand Down Expand Up @@ -34,12 +33,66 @@ std::optional<POINT> get_mouse_pos() {
}
}

// Test if a window is part of the shell or the task bar.
// We compare the HWND against HWND of the desktop and shell windows,
// we also filter out some window class names know to belong to
// the taskbar.
static bool is_system_window(HWND hwnd, const char* class_name) {
static auto system_classes = { "SysListView32", "WorkerW", "Shell_TrayWnd", "Shell_SecondaryTrayWnd", "Progman" };
static auto system_hwnds = { GetDesktopWindow(), GetShellWindow() };
for (auto system_hwnd : system_hwnds) {
if (hwnd == system_hwnd) {
return true;
}
}
for (const auto& system_class : system_classes) {
if (strcmp(system_class, class_name) == 0) {
return true;
}
}
return false;
}

WindowAndProcPath get_filtered_base_window_and_path(HWND window) {
return hwnd_cache.get_window_and_path(window);
WindowAndProcPath result;
auto root = GetAncestor(window, GA_ROOT);
if (!IsWindowVisible(root)) {
return result;
}
auto style = GetWindowLong(root, GWL_STYLE);
auto exStyle = GetWindowLong(root, GWL_EXSTYLE);
// WS_POPUP need to have a border or minimize/maximize buttons,
// otherwise the window is "not interesting"
if ((style & WS_POPUP) == WS_POPUP &&
(style & WS_THICKFRAME) == 0 &&
(style & WS_MINIMIZEBOX) == 0 &&
(style & WS_MAXIMIZEBOX) == 0) {
return result;
}
if ((style & WS_CHILD) == WS_CHILD ||
(style & WS_DISABLED) == WS_DISABLED ||
(exStyle & WS_EX_TOOLWINDOW) == WS_EX_TOOLWINDOW ||
(exStyle & WS_EX_NOACTIVATE) == WS_EX_NOACTIVATE) {
return result;
}
std::array<char, 256> class_name;
GetClassNameA(root, class_name.data(), static_cast<int>(class_name.size()));
if (is_system_window(root, class_name.data())) {
return result;
}
auto process_path = get_process_path(root);
// Check for Cortana:
if (strcmp(class_name.data(), "Windows.UI.Core.CoreWindow") == 0 &&
process_path.ends_with(L"SearchUI.exe")) {
return result;
}
result.hwnd = root;
result.process_path = std::move(process_path);
return result;
}

HWND get_filtered_active_window() {
return hwnd_cache.get_window(GetForegroundWindow());
return get_filtered_base_window_and_path(GetForegroundWindow()).hwnd;
}

int width(const RECT& rect) {
Expand Down
6 changes: 4 additions & 2 deletions src/common/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,16 @@ std::optional<RECT> get_button_pos(HWND hwnd);
std::optional<RECT> get_window_pos(HWND hwnd);
// Gets mouse postion.
std::optional<POINT> get_mouse_pos();
// Gets active window, filtering out all "non standard" windows like the taskbar, etc.
HWND get_filtered_active_window();

// Gets window ancestor (usualy the window we want to do stuff with), filtering out all "non standard" windows like the taskbar, etc. and provide the app process path
struct WindowAndProcPath {
HWND hwnd = nullptr;
std::wstring process_path;
};
WindowAndProcPath get_filtered_base_window_and_path(HWND window);
// Gets active window, filtering out all "non standard" windows like the taskbar, etc.
HWND get_filtered_active_window();


// Calculate sizes
int width(const RECT& rect);
Expand Down
2 changes: 0 additions & 2 deletions src/common/common.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,6 @@
<ClInclude Include="notifications.h" />
<ClInclude Include="window_helpers.h" />
<ClInclude Include="icon_helpers.h" />
<ClInclude Include="hwnd_data_cache.h" />
<ClInclude Include="json.h" />
<ClInclude Include="monitors.h" />
<ClInclude Include="on_thread_executor.h" />
Expand All @@ -125,7 +124,6 @@
<ClCompile Include="d2d_text.cpp" />
<ClCompile Include="d2d_window.cpp" />
<ClCompile Include="dpi_aware.cpp" />
<ClCompile Include="hwnd_data_cache.cpp" />
<ClCompile Include="json.cpp" />
<ClCompile Include="monitors.cpp" />
<ClCompile Include="notifications.cpp" />
Expand Down
6 changes: 0 additions & 6 deletions src/common/common.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,6 @@
<ClInclude Include="on_thread_executor.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="hwnd_data_cache.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="json.h">
<Filter>Header Files</Filter>
</ClInclude>
Expand Down Expand Up @@ -132,9 +129,6 @@
<ClCompile Include="on_thread_executor.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="hwnd_data_cache.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="json.cpp">
<Filter>Source Files</Filter>
</ClCompile>
Expand Down
129 changes: 0 additions & 129 deletions src/common/hwnd_data_cache.cpp

This file was deleted.

55 changes: 0 additions & 55 deletions src/common/hwnd_data_cache.h

This file was deleted.

0 comments on commit 489e30b

Please sign in to comment.