Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve window filtering. #394

Merged
merged 2 commits into from
Sep 18, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions src/common/common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,34 @@ std::optional<POINT> get_mouse_pos() {
return point;
}
}

HWND get_filtered_active_window() {
static auto desktop = GetDesktopWindow();
static auto shell = GetShellWindow();
auto active_window = GetForegroundWindow();
active_window = GetAncestor(active_window, GA_ROOT);
if (active_window == desktop || active_window == shell) {
return nullptr;
}
auto window_styles = GetWindowLong(active_window, GWL_STYLE);
if ((window_styles & WS_CHILD) || (window_styles & WS_DISABLED)) {
return nullptr;
}
window_styles = GetWindowLong(active_window, GWL_EXSTYLE);
if ((window_styles & WS_EX_TOOLWINDOW) ||(window_styles & WS_EX_NOACTIVATE)) {
return nullptr;
}
char class_name[256] = "";
GetClassNameA(active_window, class_name, 256);
if (strcmp(class_name, "SysListView32") == 0 ||
strcmp(class_name, "WorkerW") == 0 ||
strcmp(class_name, "Shell_TrayWnd") == 0 ||
strcmp(class_name, "Shell_SecondaryTrayWnd") == 0 ||
strcmp(class_name, "Progman") == 0) {
return nullptr;
}
return active_window;
}

int width(const RECT& rect) {
return rect.right - rect.left;
Expand Down
3 changes: 3 additions & 0 deletions src/common/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ 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();

// Calculate sizes
int width(const RECT& rect);
int height(const RECT& rect);
Expand Down
4 changes: 2 additions & 2 deletions src/modules/fancyzones/lib/FancyZones.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -562,7 +562,7 @@ void FancyZones::UpdateDragState(require_write_lock) noexcept

void FancyZones::CycleActiveZoneSet(DWORD vkCode) noexcept
{
if (const HWND window = GetForegroundWindow())
if (const HWND window = get_filtered_active_window())
{
if (const HMONITOR monitor = MonitorFromWindow(window, MONITOR_DEFAULTTONULL))
{
Expand All @@ -578,7 +578,7 @@ void FancyZones::CycleActiveZoneSet(DWORD vkCode) noexcept

void FancyZones::OnSnapHotkey(DWORD vkCode) noexcept
{
if (const HWND window = GetForegroundWindow())
if (const HWND window = get_filtered_active_window())
{
if (const HMONITOR monitor = MonitorFromWindow(window, MONITOR_DEFAULTTONULL))
{
Expand Down
1 change: 1 addition & 0 deletions src/modules/fancyzones/lib/pch.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include "ZoneSet.h"
#include "Zone.h"
#include "util.h"
#include "common/common.h"
#include "RegistryHelpers.h"

#pragma comment(lib, "windowsapp")
Expand Down
20 changes: 1 addition & 19 deletions src/modules/shortcut_guide/shortcut_guide.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,6 @@ void OverlayWindow::enable() {
winkey_popup->apply_overlay_opacity(((float)overlayOpacity.value)/100.0f);
target_state = new TargetState(pressTime.value);
winkey_popup->initialize();
desktop = GetDesktopWindow();
shell = GetShellWindow();
}
_enabled = true;
}
Expand Down Expand Up @@ -119,23 +117,7 @@ intptr_t OverlayWindow::signal_event(const wchar_t * name, intptr_t data) {
}

void OverlayWindow::on_held() {
auto active_window = GetForegroundWindow();
active_window = GetAncestor(active_window, GA_ROOT);
if (active_window == desktop || active_window == shell) {
active_window = nullptr;
}
auto window_styles = active_window ? GetWindowLong(active_window, GWL_STYLE) : 0;
if ((window_styles & WS_CHILD) || (window_styles & WS_DISABLED)) {
active_window = nullptr;
}
char class_name[256] = "";
GetClassNameA(active_window, class_name, 256);
if (strcmp(class_name, "SysListView32") == 0 ||
strcmp(class_name, "WorkerW") == 0 ||
strcmp(class_name, "Shell_TrayWnd") == 0 ||
strcmp(class_name, "Shell_SecondaryTrayWnd") == 0) {
active_window = nullptr;
}
auto active_window = get_filtered_active_window();
winkey_popup->show(active_window);
}

Expand Down
2 changes: 0 additions & 2 deletions src/modules/shortcut_guide/shortcut_guide.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,6 @@ class OverlayWindow : public PowertoyModuleIface {
private:
TargetState* target_state;
D2DOverlayWindow *winkey_popup;
HWND desktop, shell;
HWND active_window;
bool _enabled = false;

void init_settings();
Expand Down