Skip to content

Commit 6b0ffbb

Browse files
bzozudit3333
authored andcommitted
Common: remove hwnd_data_cache (microsoft#1223)
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.
1 parent 5528134 commit 6b0ffbb

File tree

6 files changed

+60
-197
lines changed

6 files changed

+60
-197
lines changed

src/common/common.cpp

+56-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
#include "pch.h"
22
#include "common.h"
3-
#include "hwnd_data_cache.h"
43
#include <dwmapi.h>
54
#pragma comment(lib, "dwmapi.lib")
65
#include <strsafe.h>
@@ -34,12 +33,66 @@ std::optional<POINT> get_mouse_pos() {
3433
}
3534
}
3635

36+
// Test if a window is part of the shell or the task bar.
37+
// We compare the HWND against HWND of the desktop and shell windows,
38+
// we also filter out some window class names know to belong to
39+
// the taskbar.
40+
static bool is_system_window(HWND hwnd, const char* class_name) {
41+
static auto system_classes = { "SysListView32", "WorkerW", "Shell_TrayWnd", "Shell_SecondaryTrayWnd", "Progman" };
42+
static auto system_hwnds = { GetDesktopWindow(), GetShellWindow() };
43+
for (auto system_hwnd : system_hwnds) {
44+
if (hwnd == system_hwnd) {
45+
return true;
46+
}
47+
}
48+
for (const auto& system_class : system_classes) {
49+
if (strcmp(system_class, class_name) == 0) {
50+
return true;
51+
}
52+
}
53+
return false;
54+
}
55+
3756
WindowAndProcPath get_filtered_base_window_and_path(HWND window) {
38-
return hwnd_cache.get_window_and_path(window);
57+
WindowAndProcPath result;
58+
auto root = GetAncestor(window, GA_ROOT);
59+
if (!IsWindowVisible(root)) {
60+
return result;
61+
}
62+
auto style = GetWindowLong(root, GWL_STYLE);
63+
auto exStyle = GetWindowLong(root, GWL_EXSTYLE);
64+
// WS_POPUP need to have a border or minimize/maximize buttons,
65+
// otherwise the window is "not interesting"
66+
if ((style & WS_POPUP) == WS_POPUP &&
67+
(style & WS_THICKFRAME) == 0 &&
68+
(style & WS_MINIMIZEBOX) == 0 &&
69+
(style & WS_MAXIMIZEBOX) == 0) {
70+
return result;
71+
}
72+
if ((style & WS_CHILD) == WS_CHILD ||
73+
(style & WS_DISABLED) == WS_DISABLED ||
74+
(exStyle & WS_EX_TOOLWINDOW) == WS_EX_TOOLWINDOW ||
75+
(exStyle & WS_EX_NOACTIVATE) == WS_EX_NOACTIVATE) {
76+
return result;
77+
}
78+
std::array<char, 256> class_name;
79+
GetClassNameA(root, class_name.data(), static_cast<int>(class_name.size()));
80+
if (is_system_window(root, class_name.data())) {
81+
return result;
82+
}
83+
auto process_path = get_process_path(root);
84+
// Check for Cortana:
85+
if (strcmp(class_name.data(), "Windows.UI.Core.CoreWindow") == 0 &&
86+
process_path.ends_with(L"SearchUI.exe")) {
87+
return result;
88+
}
89+
result.hwnd = root;
90+
result.process_path = std::move(process_path);
91+
return result;
3992
}
4093

4194
HWND get_filtered_active_window() {
42-
return hwnd_cache.get_window(GetForegroundWindow());
95+
return get_filtered_base_window_and_path(GetForegroundWindow()).hwnd;
4396
}
4497

4598
int width(const RECT& rect) {

src/common/common.h

+4-2
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,16 @@ std::optional<RECT> get_button_pos(HWND hwnd);
1212
std::optional<RECT> get_window_pos(HWND hwnd);
1313
// Gets mouse postion.
1414
std::optional<POINT> get_mouse_pos();
15-
// Gets active window, filtering out all "non standard" windows like the taskbar, etc.
16-
HWND get_filtered_active_window();
15+
1716
// 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
1817
struct WindowAndProcPath {
1918
HWND hwnd = nullptr;
2019
std::wstring process_path;
2120
};
2221
WindowAndProcPath get_filtered_base_window_and_path(HWND window);
22+
// Gets active window, filtering out all "non standard" windows like the taskbar, etc.
23+
HWND get_filtered_active_window();
24+
2325

2426
// Calculate sizes
2527
int width(const RECT& rect);

src/common/common.vcxproj

-2
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,6 @@
102102
<ClInclude Include="notifications.h" />
103103
<ClInclude Include="window_helpers.h" />
104104
<ClInclude Include="icon_helpers.h" />
105-
<ClInclude Include="hwnd_data_cache.h" />
106105
<ClInclude Include="json.h" />
107106
<ClInclude Include="monitors.h" />
108107
<ClInclude Include="on_thread_executor.h" />
@@ -125,7 +124,6 @@
125124
<ClCompile Include="d2d_text.cpp" />
126125
<ClCompile Include="d2d_window.cpp" />
127126
<ClCompile Include="dpi_aware.cpp" />
128-
<ClCompile Include="hwnd_data_cache.cpp" />
129127
<ClCompile Include="json.cpp" />
130128
<ClCompile Include="monitors.cpp" />
131129
<ClCompile Include="notifications.cpp" />

src/common/common.vcxproj.filters

-6
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,6 @@
7272
<ClInclude Include="on_thread_executor.h">
7373
<Filter>Header Files</Filter>
7474
</ClInclude>
75-
<ClInclude Include="hwnd_data_cache.h">
76-
<Filter>Header Files</Filter>
77-
</ClInclude>
7875
<ClInclude Include="json.h">
7976
<Filter>Header Files</Filter>
8077
</ClInclude>
@@ -132,9 +129,6 @@
132129
<ClCompile Include="on_thread_executor.cpp">
133130
<Filter>Source Files</Filter>
134131
</ClCompile>
135-
<ClCompile Include="hwnd_data_cache.cpp">
136-
<Filter>Source Files</Filter>
137-
</ClCompile>
138132
<ClCompile Include="json.cpp">
139133
<Filter>Source Files</Filter>
140134
</ClCompile>

src/common/hwnd_data_cache.cpp

-129
This file was deleted.

src/common/hwnd_data_cache.h

-55
This file was deleted.

0 commit comments

Comments
 (0)