diff --git a/src/modules/Projects/Projects.sln b/src/modules/Projects/Projects.sln index 0d3ab7d97f..347249c62b 100644 --- a/src/modules/Projects/Projects.sln +++ b/src/modules/Projects/Projects.sln @@ -11,7 +11,10 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "projects-common", "projects projects-common\GuidUtils.h = projects-common\GuidUtils.h projects-common\json.h = projects-common\json.h projects-common\MonitorEnumerator.h = projects-common\MonitorEnumerator.h + projects-common\VirtualDesktop.h = projects-common\VirtualDesktop.h projects-common\WindowEnumerator.h = projects-common\WindowEnumerator.h + projects-common\WindowFilter.h = projects-common\WindowFilter.h + projects-common\WindowUtils.h = projects-common\WindowUtils.h EndProjectSection EndProject Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "ProjectsLauncher", "ProjectsLauncher\ProjectsLauncher.vcxproj", "{2CAC093E-5FCF-4102-9C2C-AC7DD5D9EB96}" diff --git a/src/modules/Projects/ProjectsSnapshotTool/ProjectsSnapshotTool.vcxproj b/src/modules/Projects/ProjectsSnapshotTool/ProjectsSnapshotTool.vcxproj index b845a59291..10f1cb76be 100644 --- a/src/modules/Projects/ProjectsSnapshotTool/ProjectsSnapshotTool.vcxproj +++ b/src/modules/Projects/ProjectsSnapshotTool/ProjectsSnapshotTool.vcxproj @@ -119,9 +119,6 @@ - - - @@ -131,8 +128,6 @@ Create - - diff --git a/src/modules/Projects/ProjectsSnapshotTool/ProjectsSnapshotTool.vcxproj.filters b/src/modules/Projects/ProjectsSnapshotTool/ProjectsSnapshotTool.vcxproj.filters index 2f29e8a81b..872777b29c 100644 --- a/src/modules/Projects/ProjectsSnapshotTool/ProjectsSnapshotTool.vcxproj.filters +++ b/src/modules/Projects/ProjectsSnapshotTool/ProjectsSnapshotTool.vcxproj.filters @@ -18,15 +18,6 @@ Header Files - - Header Files - - - Header Files - - - Header Files - Header Files @@ -44,12 +35,6 @@ Source Files - - Source Files - - - Source Files - Source Files diff --git a/src/modules/Projects/ProjectsSnapshotTool/VirtualDesktop.cpp b/src/modules/Projects/ProjectsSnapshotTool/VirtualDesktop.cpp deleted file mode 100644 index dd0bc49aec..0000000000 --- a/src/modules/Projects/ProjectsSnapshotTool/VirtualDesktop.cpp +++ /dev/null @@ -1,39 +0,0 @@ -#include "pch.h" -#include "VirtualDesktop.h" - -#include - -VirtualDesktop::VirtualDesktop() -{ - auto res = CoCreateInstance(CLSID_VirtualDesktopManager, nullptr, CLSCTX_ALL, IID_PPV_ARGS(&m_vdManager)); - if (FAILED(res)) - { - // Logger::error("Failed to create VirtualDesktopManager instance"); - std::cout << "Failed to create VirtualDesktopManager instance\n"; - } -} - -VirtualDesktop::~VirtualDesktop() -{ - if (m_vdManager) - { - m_vdManager->Release(); - } -} - -VirtualDesktop& VirtualDesktop::instance() -{ - static VirtualDesktop self; - return self; -} - -bool VirtualDesktop::IsWindowOnCurrentDesktop(HWND window) const -{ - BOOL isWindowOnCurrentDesktop = false; - if (m_vdManager) - { - m_vdManager->IsWindowOnCurrentVirtualDesktop(window, &isWindowOnCurrentDesktop); - } - - return isWindowOnCurrentDesktop; -} \ No newline at end of file diff --git a/src/modules/Projects/ProjectsSnapshotTool/VirtualDesktop.h b/src/modules/Projects/ProjectsSnapshotTool/VirtualDesktop.h deleted file mode 100644 index 3e038b7285..0000000000 --- a/src/modules/Projects/ProjectsSnapshotTool/VirtualDesktop.h +++ /dev/null @@ -1,20 +0,0 @@ -#pragma once - -#include -#include - -class VirtualDesktop -{ -public: - static VirtualDesktop& instance(); - - bool IsWindowOnCurrentDesktop(HWND window) const; - -private: - VirtualDesktop(); - ~VirtualDesktop(); - - IVirtualDesktopManager* m_vdManager{ nullptr }; -}; - - diff --git a/src/modules/Projects/ProjectsSnapshotTool/WindowUtils.cpp b/src/modules/Projects/ProjectsSnapshotTool/WindowUtils.cpp deleted file mode 100644 index e45750e276..0000000000 --- a/src/modules/Projects/ProjectsSnapshotTool/WindowUtils.cpp +++ /dev/null @@ -1,114 +0,0 @@ -#include "pch.h" -#include "WindowUtils.h" - -#include - -namespace Common -{ - namespace Display - { - namespace DPIAware - { - constexpr inline int DEFAULT_DPI = 96; - - void InverseConvert(HMONITOR monitor_handle, float& width, float& height) - { - if (monitor_handle == NULL) - { - const POINT ptZero = { 0, 0 }; - monitor_handle = MonitorFromPoint(ptZero, MONITOR_DEFAULTTOPRIMARY); - } - - UINT dpi_x, dpi_y; - if (GetDpiForMonitor(monitor_handle, MDT_EFFECTIVE_DPI, &dpi_x, &dpi_y) == S_OK) - { - width = width * DPIAware::DEFAULT_DPI / dpi_x; - height = height * DPIAware::DEFAULT_DPI / dpi_y; - } - } - } - } -} - -namespace WindowUtils -{ - // 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 ProjectsSnapshotTool[] = L"PROJECTSSNAPSHOTTOOL"; - const wchar_t ProjectsEditor[] = L"PROJECTSEDITOR"; - const wchar_t ProjectsLauncher[] = L"PROJECTSLAUNCHER"; - } - - bool IsRoot(HWND window) noexcept - { - return GetAncestor(window, GA_ROOT) == window; - } - - bool IsMaximized(HWND window) noexcept - { - WINDOWPLACEMENT placement{}; - if (GetWindowPlacement(window, &placement) && - placement.showCmd == SW_SHOWMAXIMIZED) - { - return true; - } - return false; - } - - bool IsExcludedByDefault(HWND window, const std::wstring& processPath, const std::wstring& title) - { - std::wstring processPathUpper = processPath; - CharUpperBuffW(processPathUpper.data(), static_cast(processPathUpper.length())); - - static std::vector defaultExcludedFolders = { NonLocalizable::SystemAppsFolder, NonLocalizable::System, NonLocalizable::System32, NonLocalizable::SystemWOW64 }; - if (Common::Utils::ExcludedApps::find_folder_in_path(processPathUpper, defaultExcludedFolders)) - { - return true; - } - - std::array className; - GetClassNameA(window, className.data(), static_cast(className.size())); - if (Common::Utils::Window::is_system_window(window, className.data())) - { - return true; - } - - if (strcmp(NonLocalizable::SplashClassName, className.data()) == 0) - { - return true; - } - - static std::vector defaultExcludedApps = { NonLocalizable::CoreWindow, NonLocalizable::SearchUI, NonLocalizable::ProjectsEditor, NonLocalizable::ProjectsLauncher, NonLocalizable::ProjectsSnapshotTool }; - return (Common::Utils::ExcludedApps::check_excluded_app(processPathUpper, title, defaultExcludedApps)); - } - - RECT GetWindowRect(HWND window) - { - RECT rect; - if (GetWindowRect(window, &rect)) - { - float width = static_cast(rect.right - rect.left); - float height = static_cast(rect.bottom - rect.top); - float originX = static_cast(rect.left); - float originY = static_cast(rect.top); - - Common::Display::DPIAware::InverseConvert(MonitorFromWindow(window, MONITOR_DEFAULTTONULL), width, height); - Common::Display::DPIAware::InverseConvert(MonitorFromWindow(window, MONITOR_DEFAULTTONULL), originX, originY); - - return RECT(static_cast(std::roundf(originX)), - static_cast(std::roundf(originY)), - static_cast(std::roundf(originX + width)), - static_cast(std::roundf(originY + height))); - } - - return rect; - } -} \ No newline at end of file diff --git a/src/modules/Projects/ProjectsSnapshotTool/main.cpp b/src/modules/Projects/ProjectsSnapshotTool/main.cpp index 6135feca42..029a9dbae0 100644 --- a/src/modules/Projects/ProjectsSnapshotTool/main.cpp +++ b/src/modules/Projects/ProjectsSnapshotTool/main.cpp @@ -1,4 +1,4 @@ -#include "pch.h" +#include "pch.h" #include #include @@ -6,10 +6,10 @@ #include "../projects-common/Data.h" #include "../projects-common/GuidUtils.h" #include "../projects-common/WindowEnumerator.h" +#include "../projects-common/WindowFilter.h" #include "MonitorUtils.h" #include "PackagedAppUtils.h" -#include "WindowFilter.h" int main(int argc, char* argv[]) { diff --git a/src/modules/Projects/projects-common/VirtualDesktop.h b/src/modules/Projects/projects-common/VirtualDesktop.h new file mode 100644 index 0000000000..421445e707 --- /dev/null +++ b/src/modules/Projects/projects-common/VirtualDesktop.h @@ -0,0 +1,49 @@ +#pragma once + +#include +#include +#include + +class VirtualDesktop +{ +public: + static VirtualDesktop& instance() + { + static VirtualDesktop self; + return self; + } + + bool IsWindowOnCurrentDesktop(HWND window) const + { + BOOL isWindowOnCurrentDesktop = false; + if (m_vdManager) + { + m_vdManager->IsWindowOnCurrentVirtualDesktop(window, &isWindowOnCurrentDesktop); + } + + return isWindowOnCurrentDesktop; + } + +private: + VirtualDesktop() + { + auto res = CoCreateInstance(CLSID_VirtualDesktopManager, nullptr, CLSCTX_ALL, IID_PPV_ARGS(&m_vdManager)); + if (FAILED(res)) + { + // Logger::error("Failed to create VirtualDesktopManager instance"); + std::cout << "Failed to create VirtualDesktopManager instance\n"; + } + } + + ~VirtualDesktop() + { + if (m_vdManager) + { + m_vdManager->Release(); + } + } + + IVirtualDesktopManager* m_vdManager{ nullptr }; +}; + + diff --git a/src/modules/Projects/ProjectsSnapshotTool/WindowFilter.h b/src/modules/Projects/projects-common/WindowFilter.h similarity index 100% rename from src/modules/Projects/ProjectsSnapshotTool/WindowFilter.h rename to src/modules/Projects/projects-common/WindowFilter.h diff --git a/src/modules/Projects/ProjectsSnapshotTool/WindowUtils.h b/src/modules/Projects/projects-common/WindowUtils.h similarity index 65% rename from src/modules/Projects/ProjectsSnapshotTool/WindowUtils.h rename to src/modules/Projects/projects-common/WindowUtils.h index 6622c96485..9dec4713b0 100644 --- a/src/modules/Projects/ProjectsSnapshotTool/WindowUtils.h +++ b/src/modules/Projects/projects-common/WindowUtils.h @@ -1,6 +1,7 @@ #pragma once #include +#include #include @@ -10,7 +11,23 @@ namespace Common { namespace DPIAware { - void InverseConvert(HMONITOR monitor_handle, float& width, float& height); + constexpr inline int DEFAULT_DPI = 96; + + inline void InverseConvert(HMONITOR monitor_handle, float& width, float& height) + { + if (monitor_handle == NULL) + { + const POINT ptZero = { 0, 0 }; + monitor_handle = MonitorFromPoint(ptZero, MONITOR_DEFAULTTOPRIMARY); + } + + UINT dpi_x, dpi_y; + if (GetDpiForMonitor(monitor_handle, MDT_EFFECTIVE_DPI, &dpi_x, &dpi_y) == S_OK) + { + width = width * DPIAware::DEFAULT_DPI / dpi_x; + height = height * DPIAware::DEFAULT_DPI / dpi_y; + } + } } } @@ -188,17 +205,90 @@ namespace Common // FancyZones WindowUtils namespace WindowUtils { - bool IsRoot(HWND window) noexcept; - bool IsMaximized(HWND window) noexcept; + // 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 ProjectsSnapshotTool[] = L"PROJECTSSNAPSHOTTOOL"; + const wchar_t ProjectsEditor[] = L"PROJECTSEDITOR"; + const wchar_t ProjectsLauncher[] = L"PROJECTSLAUNCHER"; + } + + 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; + } constexpr bool HasStyle(LONG style, LONG styleToCheck) noexcept { return ((style & styleToCheck) == styleToCheck); } - bool IsExcludedByDefault(HWND window, const std::wstring& processPath, const std::wstring& title); + inline bool IsExcludedByDefault(HWND window, const std::wstring& processPath, const std::wstring& title) + { + std::wstring processPathUpper = processPath; + CharUpperBuffW(processPathUpper.data(), static_cast(processPathUpper.length())); - RECT GetWindowRect(HWND window); + static std::vector defaultExcludedFolders = { NonLocalizable::SystemAppsFolder, NonLocalizable::System, NonLocalizable::System32, NonLocalizable::SystemWOW64 }; + if (Common::Utils::ExcludedApps::find_folder_in_path(processPathUpper, defaultExcludedFolders)) + { + return true; + } + + std::array className; + GetClassNameA(window, className.data(), static_cast(className.size())); + if (Common::Utils::Window::is_system_window(window, className.data())) + { + return true; + } + + if (strcmp(NonLocalizable::SplashClassName, className.data()) == 0) + { + return true; + } + + static std::vector defaultExcludedApps = { NonLocalizable::CoreWindow, NonLocalizable::SearchUI, NonLocalizable::ProjectsEditor, NonLocalizable::ProjectsLauncher, NonLocalizable::ProjectsSnapshotTool }; + return (Common::Utils::ExcludedApps::check_excluded_app(processPathUpper, title, defaultExcludedApps)); + } + + inline RECT GetWindowRect(HWND window) + { + RECT rect; + if (GetWindowRect(window, &rect)) + { + float width = static_cast(rect.right - rect.left); + float height = static_cast(rect.bottom - rect.top); + float originX = static_cast(rect.left); + float originY = static_cast(rect.top); + + Common::Display::DPIAware::InverseConvert(MonitorFromWindow(window, MONITOR_DEFAULTTONULL), width, height); + Common::Display::DPIAware::InverseConvert(MonitorFromWindow(window, MONITOR_DEFAULTTONULL), originX, originY); + + return RECT(static_cast(std::roundf(originX)), + static_cast(std::roundf(originY)), + static_cast(std::roundf(originX + width)), + static_cast(std::roundf(originY + height))); + } + + return rect; + } } // addition for Projects