Files
PowerToys/src/modules/Projects/projects-common/WindowUtils.h

139 lines
4.2 KiB
C
Raw Normal View History

2024-05-21 16:55:15 +02:00
#pragma once
#include <Windows.h>
2024-06-03 16:49:08 +02:00
#include <ShellScalingApi.h>
2024-05-21 16:55:15 +02:00
#include <algorithm>
2024-06-12 19:31:43 +02:00
#include <common/Display/dpi_aware.h>
#include <common/utils/excluded_apps.h>
#include <common/utils/window.h>
2024-05-21 16:55:15 +02:00
// FancyZones WindowUtils
namespace WindowUtils
{
2024-06-03 16:49:08 +02:00
// Non-Localizable strings
namespace NonLocalizable
{
const wchar_t SystemAppsFolder[] = L"SYSTEMAPPS";
const wchar_t System[] = L"WINDOWS/SYSTEM";
const wchar_t System32[] = L"SYSTEM32";
const wchar_t SystemWOW64[] = L"SYSTEMWOW64";
const char SplashClassName[] = "MsoSplash";
const wchar_t CoreWindow[] = L"WINDOWS.UI.CORE.COREWINDOW";
const wchar_t SearchUI[] = L"SEARCHUI.EXE";
const wchar_t HelpWindow[] = L"C:\\WINDOWS\\HH.EXE";
const wchar_t ProjectsSnapshotTool[] = L"POWERTOYS.PROJECTSSNAPSHOTTOOL";
const wchar_t ProjectsEditor[] = L"POWERTOYS.PROJECTSEDITOR";
const wchar_t ProjectsLauncher[] = L"POWERTOYS.PROJECTSLAUNCHER";
2024-06-03 16:49:08 +02:00
}
inline bool IsRoot(HWND window) noexcept
{
return GetAncestor(window, GA_ROOT) == window;
}
inline bool IsMaximized(HWND window) noexcept
{
WINDOWPLACEMENT placement{};
if (GetWindowPlacement(window, &placement) &&
placement.showCmd == SW_SHOWMAXIMIZED)
{
return true;
}
return false;
}
2024-05-21 16:55:15 +02:00
constexpr bool HasStyle(LONG style, LONG styleToCheck) noexcept
{
return ((style & styleToCheck) == styleToCheck);
}
2024-06-03 16:49:08 +02:00
inline bool IsExcludedByDefault(HWND window, const std::wstring& processPath, const std::wstring& title)
{
std::wstring processPathUpper = processPath;
CharUpperBuffW(processPathUpper.data(), static_cast<DWORD>(processPathUpper.length()));
2024-05-21 16:55:15 +02:00
static std::vector<std::wstring> defaultExcludedFolders = {
NonLocalizable::SystemAppsFolder,
NonLocalizable::System,
NonLocalizable::System32,
NonLocalizable::SystemWOW64
};
2024-06-12 19:31:43 +02:00
if (find_folder_in_path(processPathUpper, defaultExcludedFolders))
2024-06-03 16:49:08 +02:00
{
return true;
}
std::array<char, 256> className;
GetClassNameA(window, className.data(), static_cast<int>(className.size()));
2024-06-12 19:31:43 +02:00
if (is_system_window(window, className.data()))
2024-06-03 16:49:08 +02:00
{
return true;
}
if (strcmp(NonLocalizable::SplashClassName, className.data()) == 0)
{
return true;
}
static std::vector<std::wstring> defaultExcludedApps = {
NonLocalizable::CoreWindow,
NonLocalizable::SearchUI,
NonLocalizable::HelpWindow,
NonLocalizable::ProjectsEditor,
NonLocalizable::ProjectsLauncher,
NonLocalizable::ProjectsSnapshotTool,
};
2024-06-12 19:31:43 +02:00
return (check_excluded_app(window, processPathUpper, defaultExcludedApps));
2024-06-03 16:49:08 +02:00
}
inline RECT GetWindowRect(HWND window)
{
RECT rect;
if (GetWindowRect(window, &rect))
{
float width = static_cast<float>(rect.right - rect.left);
float height = static_cast<float>(rect.bottom - rect.top);
float originX = static_cast<float>(rect.left);
float originY = static_cast<float>(rect.top);
2024-06-12 19:31:43 +02:00
DPIAware::InverseConvert(MonitorFromWindow(window, MONITOR_DEFAULTTONULL), width, height);
DPIAware::InverseConvert(MonitorFromWindow(window, MONITOR_DEFAULTTONULL), originX, originY);
2024-06-03 16:49:08 +02:00
return RECT(static_cast<LONG>(std::roundf(originX)),
static_cast<LONG>(std::roundf(originY)),
static_cast<LONG>(std::roundf(originX + width)),
static_cast<LONG>(std::roundf(originY + height)));
}
return rect;
}
2024-05-21 16:55:15 +02:00
}
// addition for Projects
namespace WindowUtils
{
inline bool IsMinimized(HWND window)
{
return IsIconic(window);
}
#define MAX_TITLE_LENGTH 255
inline std::wstring GetWindowTitle(HWND window)
{
WCHAR title[MAX_TITLE_LENGTH];
int len = GetWindowTextW(window, title, MAX_TITLE_LENGTH);
if (len <= 0)
{
return {};
}
return std::wstring(title);
}
/*bool IsFullscreen(HWND window)
{
TODO
}*/
}