mirror of
https://github.com/microsoft/PowerToys.git
synced 2025-01-18 06:29:44 +08:00
Fancyzones: unify window filtering (#1184)
Makes FancyZone use the same code for filtering windows when windows are being dragged and when moved by WinKey + arrows.
This commit is contained in:
parent
4ef8f3da2b
commit
25e882eb78
@ -203,41 +203,6 @@ public:
|
|||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
bool IsInterestingWindow(HWND window)
|
|
||||||
{
|
|
||||||
auto style = GetWindowLongPtr(window, GWL_STYLE);
|
|
||||||
auto exStyle = GetWindowLongPtr(window, GWL_EXSTYLE);
|
|
||||||
// Ignore:
|
|
||||||
if (GetAncestor(window, GA_ROOT) != window || // windows that are not top-level
|
|
||||||
GetWindow(window, GW_OWNER) != nullptr || // windows that have an owner - like Save As dialogs
|
|
||||||
(style & WS_CHILD) != 0 || // windows that are child elements of other windows - like buttons
|
|
||||||
(style & WS_DISABLED) != 0 || // windows that are disabled
|
|
||||||
(exStyle & WS_EX_TOOLWINDOW) != 0 || // toolbar windows
|
|
||||||
!IsWindowVisible(window)) // invisible windows
|
|
||||||
{
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
// Filter some windows like the Start menu or Cortana
|
|
||||||
auto windowAndPath = get_filtered_base_window_and_path(window);
|
|
||||||
if (windowAndPath.hwnd == nullptr)
|
|
||||||
{
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
// Filter out user specified apps
|
|
||||||
CharUpperBuffW(windowAndPath.process_path.data(), (DWORD)windowAndPath.process_path.length());
|
|
||||||
if (m_settings)
|
|
||||||
{
|
|
||||||
for (const auto& excluded : m_settings->GetSettings().excludedAppsArray)
|
|
||||||
{
|
|
||||||
if (windowAndPath.process_path.find(excluded) != std::wstring::npos)
|
|
||||||
{
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
void Disable(bool const traceEvent)
|
void Disable(bool const traceEvent)
|
||||||
{
|
{
|
||||||
if (m_app) {
|
if (m_app) {
|
||||||
@ -256,7 +221,6 @@ private:
|
|||||||
void MoveSizeEnd(HWND window, POINT const& ptScreen) noexcept;
|
void MoveSizeEnd(HWND window, POINT const& ptScreen) noexcept;
|
||||||
void MoveSizeUpdate(POINT const& ptScreen) noexcept;
|
void MoveSizeUpdate(POINT const& ptScreen) noexcept;
|
||||||
|
|
||||||
HANDLE m_movedWindow = nullptr;
|
|
||||||
winrt::com_ptr<IFancyZones> m_app;
|
winrt::com_ptr<IFancyZones> m_app;
|
||||||
winrt::com_ptr<IFancyZonesSettings> m_settings;
|
winrt::com_ptr<IFancyZonesSettings> m_settings;
|
||||||
std::wstring app_name;
|
std::wstring app_name;
|
||||||
@ -317,10 +281,7 @@ void FancyZonesModule::HandleWinHookEvent(WinHookEvent* data) noexcept
|
|||||||
{
|
{
|
||||||
if (data->idObject == OBJID_WINDOW)
|
if (data->idObject == OBJID_WINDOW)
|
||||||
{
|
{
|
||||||
if (IsInterestingWindow(data->hwnd))
|
m_app.as<IFancyZonesCallback>()->WindowCreated(data->hwnd);
|
||||||
{
|
|
||||||
m_app.as<IFancyZonesCallback>()->WindowCreated(data->hwnd);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
@ -332,23 +293,15 @@ void FancyZonesModule::HandleWinHookEvent(WinHookEvent* data) noexcept
|
|||||||
|
|
||||||
void FancyZonesModule::MoveSizeStart(HWND window, POINT const& ptScreen) noexcept
|
void FancyZonesModule::MoveSizeStart(HWND window, POINT const& ptScreen) noexcept
|
||||||
{
|
{
|
||||||
if (IsInterestingWindow(window))
|
if (auto monitor = MonitorFromPoint(ptScreen, MONITOR_DEFAULTTONULL))
|
||||||
{
|
{
|
||||||
if (auto monitor = MonitorFromPoint(ptScreen, MONITOR_DEFAULTTONULL))
|
m_app.as<IFancyZonesCallback>()->MoveSizeStart(window, monitor, ptScreen);
|
||||||
{
|
|
||||||
m_movedWindow = window;
|
|
||||||
m_app.as<IFancyZonesCallback>()->MoveSizeStart(window, monitor, ptScreen);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void FancyZonesModule::MoveSizeEnd(HWND window, POINT const& ptScreen) noexcept
|
void FancyZonesModule::MoveSizeEnd(HWND window, POINT const& ptScreen) noexcept
|
||||||
{
|
{
|
||||||
if (IsInterestingWindow(window) || (window != nullptr && window == m_movedWindow))
|
m_app.as<IFancyZonesCallback>()->MoveSizeEnd(window, ptScreen);
|
||||||
{
|
|
||||||
m_movedWindow = nullptr;
|
|
||||||
m_app.as<IFancyZonesCallback>()->MoveSizeEnd(window, ptScreen);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void FancyZonesModule::MoveSizeUpdate(POINT const& ptScreen) noexcept
|
void FancyZonesModule::MoveSizeUpdate(POINT const& ptScreen) noexcept
|
||||||
|
@ -97,6 +97,7 @@ private:
|
|||||||
require_write_lock(const std::unique_lock<T>& lock) { lock; }
|
require_write_lock(const std::unique_lock<T>& lock) { lock; }
|
||||||
};
|
};
|
||||||
|
|
||||||
|
bool IsInterestingWindow(HWND window) noexcept;
|
||||||
void UpdateZoneWindows() noexcept;
|
void UpdateZoneWindows() noexcept;
|
||||||
void MoveWindowsOnDisplayChange() noexcept;
|
void MoveWindowsOnDisplayChange() noexcept;
|
||||||
void UpdateDragState(require_write_lock) noexcept;
|
void UpdateDragState(require_write_lock) noexcept;
|
||||||
@ -199,8 +200,11 @@ IFACEMETHODIMP_(void) FancyZones::Destroy() noexcept
|
|||||||
// IFancyZonesCallback
|
// IFancyZonesCallback
|
||||||
IFACEMETHODIMP_(void) FancyZones::MoveSizeStart(HWND window, HMONITOR monitor, POINT const& ptScreen) noexcept
|
IFACEMETHODIMP_(void) FancyZones::MoveSizeStart(HWND window, HMONITOR monitor, POINT const& ptScreen) noexcept
|
||||||
{
|
{
|
||||||
std::unique_lock writeLock(m_lock);
|
if (IsInterestingWindow(window))
|
||||||
MoveSizeStartInternal(window, monitor, ptScreen, writeLock);
|
{
|
||||||
|
std::unique_lock writeLock(m_lock);
|
||||||
|
MoveSizeStartInternal(window, monitor, ptScreen, writeLock);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// IFancyZonesCallback
|
// IFancyZonesCallback
|
||||||
@ -213,8 +217,11 @@ IFACEMETHODIMP_(void) FancyZones::MoveSizeUpdate(HMONITOR monitor, POINT const&
|
|||||||
// IFancyZonesCallback
|
// IFancyZonesCallback
|
||||||
IFACEMETHODIMP_(void) FancyZones::MoveSizeEnd(HWND window, POINT const& ptScreen) noexcept
|
IFACEMETHODIMP_(void) FancyZones::MoveSizeEnd(HWND window, POINT const& ptScreen) noexcept
|
||||||
{
|
{
|
||||||
std::unique_lock writeLock(m_lock);
|
if (window == m_windowMoveSize || IsInterestingWindow(window))
|
||||||
MoveSizeEndInternal(window, ptScreen, writeLock);
|
{
|
||||||
|
std::unique_lock writeLock(m_lock);
|
||||||
|
MoveSizeEndInternal(window, ptScreen, writeLock);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// IFancyZonesCallback
|
// IFancyZonesCallback
|
||||||
@ -234,7 +241,7 @@ IFACEMETHODIMP_(void) FancyZones::VirtualDesktopInitialize() noexcept
|
|||||||
// IFancyZonesCallback
|
// IFancyZonesCallback
|
||||||
IFACEMETHODIMP_(void) FancyZones::WindowCreated(HWND window) noexcept
|
IFACEMETHODIMP_(void) FancyZones::WindowCreated(HWND window) noexcept
|
||||||
{
|
{
|
||||||
if (m_settings->GetSettings().appLastZone_moveWindows)
|
if (m_settings->GetSettings().appLastZone_moveWindows && IsInterestingWindow(window))
|
||||||
{
|
{
|
||||||
auto processPath = get_process_path(window);
|
auto processPath = get_process_path(window);
|
||||||
if (!processPath.empty())
|
if (!processPath.empty())
|
||||||
@ -586,6 +593,41 @@ LRESULT CALLBACK FancyZones::s_WndProc(HWND window, UINT message, WPARAM wparam,
|
|||||||
DefWindowProc(window, message, wparam, lparam);
|
DefWindowProc(window, message, wparam, lparam);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool FancyZones::IsInterestingWindow(HWND window) noexcept
|
||||||
|
{
|
||||||
|
auto style = GetWindowLongPtr(window, GWL_STYLE);
|
||||||
|
auto exStyle = GetWindowLongPtr(window, GWL_EXSTYLE);
|
||||||
|
// Ignore:
|
||||||
|
if (GetAncestor(window, GA_ROOT) != window || // windows that are not top-level
|
||||||
|
GetWindow(window, GW_OWNER) != nullptr || // windows that have an owner - like Save As dialogs
|
||||||
|
(style & WS_CHILD) != 0 || // windows that are child elements of other windows - like buttons
|
||||||
|
(style & WS_DISABLED) != 0 || // windows that are disabled
|
||||||
|
(exStyle & WS_EX_TOOLWINDOW) != 0 || // toolbar windows
|
||||||
|
!IsWindowVisible(window)) // invisible windows
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
// Filter some windows like the Start menu or Cortana
|
||||||
|
auto windowAndPath = get_filtered_base_window_and_path(window);
|
||||||
|
if (windowAndPath.hwnd == nullptr)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
// Filter out user specified apps
|
||||||
|
CharUpperBuffW(windowAndPath.process_path.data(), (DWORD)windowAndPath.process_path.length());
|
||||||
|
if (m_settings)
|
||||||
|
{
|
||||||
|
for (const auto& excluded : m_settings->GetSettings().excludedAppsArray)
|
||||||
|
{
|
||||||
|
if (windowAndPath.process_path.find(excluded) != std::wstring::npos)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
void FancyZones::UpdateZoneWindows() noexcept
|
void FancyZones::UpdateZoneWindows() noexcept
|
||||||
{
|
{
|
||||||
auto callback = [](HMONITOR monitor, HDC, RECT *, LPARAM data) -> BOOL
|
auto callback = [](HMONITOR monitor, HDC, RECT *, LPARAM data) -> BOOL
|
||||||
@ -678,12 +720,9 @@ void FancyZones::UpdateDragState(require_write_lock) noexcept
|
|||||||
|
|
||||||
void FancyZones::CycleActiveZoneSet(DWORD vkCode) noexcept
|
void FancyZones::CycleActiveZoneSet(DWORD vkCode) noexcept
|
||||||
{
|
{
|
||||||
if (const HWND window = get_filtered_active_window())
|
auto window = GetForegroundWindow();
|
||||||
|
if (IsInterestingWindow(window))
|
||||||
{
|
{
|
||||||
if (GetWindow(window, GW_OWNER) != nullptr)
|
|
||||||
{
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
if (const HMONITOR monitor = MonitorFromWindow(window, MONITOR_DEFAULTTONULL))
|
if (const HMONITOR monitor = MonitorFromWindow(window, MONITOR_DEFAULTTONULL))
|
||||||
{
|
{
|
||||||
std::shared_lock readLock(m_lock);
|
std::shared_lock readLock(m_lock);
|
||||||
@ -698,12 +737,9 @@ void FancyZones::CycleActiveZoneSet(DWORD vkCode) noexcept
|
|||||||
|
|
||||||
void FancyZones::OnSnapHotkey(DWORD vkCode) noexcept
|
void FancyZones::OnSnapHotkey(DWORD vkCode) noexcept
|
||||||
{
|
{
|
||||||
if (const HWND window = get_filtered_active_window())
|
auto window = GetForegroundWindow();
|
||||||
|
if (IsInterestingWindow(window))
|
||||||
{
|
{
|
||||||
if (GetWindow(window, GW_OWNER) != nullptr)
|
|
||||||
{
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
if (const HMONITOR monitor = MonitorFromWindow(window, MONITOR_DEFAULTTONULL))
|
if (const HMONITOR monitor = MonitorFromWindow(window, MONITOR_DEFAULTTONULL))
|
||||||
{
|
{
|
||||||
std::shared_lock readLock(m_lock);
|
std::shared_lock readLock(m_lock);
|
||||||
|
Loading…
Reference in New Issue
Block a user