From 9a3519cd0aa58baea2cdb650b84e4fbaf89a445c Mon Sep 17 00:00:00 2001 From: seraphima Date: Wed, 3 Jul 2024 14:50:39 +0200 Subject: [PATCH] refactoring --- .../ProjectsSnapshotTool.vcxproj | 2 + .../ProjectsSnapshotTool.vcxproj.filters | 6 ++ .../ProjectsSnapshotTool/SnapshotUtils.cpp | 73 +++++++++++++++++++ .../ProjectsSnapshotTool/SnapshotUtils.h | 8 ++ .../Projects/ProjectsSnapshotTool/main.cpp | 64 +--------------- .../Projects/projects-common/AppUtils.h | 10 +-- 6 files changed, 98 insertions(+), 65 deletions(-) create mode 100644 src/modules/Projects/ProjectsSnapshotTool/SnapshotUtils.cpp create mode 100644 src/modules/Projects/ProjectsSnapshotTool/SnapshotUtils.h diff --git a/src/modules/Projects/ProjectsSnapshotTool/ProjectsSnapshotTool.vcxproj b/src/modules/Projects/ProjectsSnapshotTool/ProjectsSnapshotTool.vcxproj index e18c060513..76f754072e 100644 --- a/src/modules/Projects/ProjectsSnapshotTool/ProjectsSnapshotTool.vcxproj +++ b/src/modules/Projects/ProjectsSnapshotTool/ProjectsSnapshotTool.vcxproj @@ -127,12 +127,14 @@ Create + + diff --git a/src/modules/Projects/ProjectsSnapshotTool/ProjectsSnapshotTool.vcxproj.filters b/src/modules/Projects/ProjectsSnapshotTool/ProjectsSnapshotTool.vcxproj.filters index 3d02cddf48..b7af4120cc 100644 --- a/src/modules/Projects/ProjectsSnapshotTool/ProjectsSnapshotTool.vcxproj.filters +++ b/src/modules/Projects/ProjectsSnapshotTool/ProjectsSnapshotTool.vcxproj.filters @@ -27,6 +27,9 @@ Header Files + + Header Files + @@ -35,6 +38,9 @@ Source Files + + Source Files + diff --git a/src/modules/Projects/ProjectsSnapshotTool/SnapshotUtils.cpp b/src/modules/Projects/ProjectsSnapshotTool/SnapshotUtils.cpp new file mode 100644 index 0000000000..3775920823 --- /dev/null +++ b/src/modules/Projects/ProjectsSnapshotTool/SnapshotUtils.cpp @@ -0,0 +1,73 @@ +#include "pch.h" +#include "SnapshotUtils.h" + +#include + +#include +#include +#include + +#include +#include + +namespace SnapshotUtils +{ + std::vector GetApps(const std::function getMonitorNumberFromWindowHandle) + { + std::vector apps{}; + + auto installedApps = Utils::Apps::GetAppsList(); + auto windows = WindowEnumerator::Enumerate(WindowFilter::Filter); + + for (const auto window : windows) + { + // filter by window rect size + RECT rect = WindowUtils::GetWindowRect(window); + if (rect.right - rect.left <= 0 || rect.bottom - rect.top <= 0) + { + continue; + } + + // filter by window title + std::wstring title = WindowUtils::GetWindowTitle(window); + if (title.empty()) + { + continue; + } + + // filter by app path + std::wstring processPath = get_process_path_waiting_uwp(window); + if (processPath.empty() || WindowUtils::IsExcludedByDefault(window, processPath, title)) + { + continue; + } + + auto data = Utils::Apps::GetApp(processPath, installedApps); + if (!data.has_value() || data->name.empty()) + { + continue; + } + + Project::Application app{ + .name = data.value().name, + .title = title, + .path = processPath, + .packageFullName = data.value().packageFullName, + .commandLineArgs = L"", + .isMinimized = WindowUtils::IsMinimized(window), + .isMaximized = WindowUtils::IsMaximized(window), + .position = Project::Application::Position{ + .x = rect.left, + .y = rect.top, + .width = rect.right - rect.left, + .height = rect.bottom - rect.top, + }, + .monitor = getMonitorNumberFromWindowHandle(window), + }; + + apps.push_back(app); + } + + return apps; + } +} \ No newline at end of file diff --git a/src/modules/Projects/ProjectsSnapshotTool/SnapshotUtils.h b/src/modules/Projects/ProjectsSnapshotTool/SnapshotUtils.h new file mode 100644 index 0000000000..4274ecdf66 --- /dev/null +++ b/src/modules/Projects/ProjectsSnapshotTool/SnapshotUtils.h @@ -0,0 +1,8 @@ +#pragma once + +#include + +namespace SnapshotUtils +{ + std::vector GetApps(const std::function getMonitorNumberFromWindowHandle); +}; diff --git a/src/modules/Projects/ProjectsSnapshotTool/main.cpp b/src/modules/Projects/ProjectsSnapshotTool/main.cpp index 017108f655..411c4f1534 100644 --- a/src/modules/Projects/ProjectsSnapshotTool/main.cpp +++ b/src/modules/Projects/ProjectsSnapshotTool/main.cpp @@ -1,17 +1,14 @@ #include "pch.h" #include -#include -#include #include #include #include -#include -#include #include #include +#include #include #include @@ -56,44 +53,8 @@ int APIENTRY WinMain(HINSTANCE hInst, HINSTANCE hInstPrev, LPSTR cmdLine, int cm Project project{ .id = CreateGuidString(), .name = ProjectNameUtils::CreateProjectName(projects), .creationTime = creationTime }; Logger::trace(L"Creating project {}:{}", project.name, project.id); - // save monitor configuration project.monitors = MonitorUtils::IdentifyMonitors(); - - // get list of windows - auto windows = WindowEnumerator::Enumerate(WindowFilter::Filter); - - // get installed apps list - auto apps = Utils::Apps::GetAppsList(); - - for (const auto& window : windows) - { - // filter by window rect size - RECT rect = WindowUtils::GetWindowRect(window); - if (rect.right - rect.left <= 0 || rect.bottom - rect.top <= 0) - { - continue; - } - - // filter by window title - std::wstring title = WindowUtils::GetWindowTitle(window); - if (title.empty()) - { - continue; - } - - // filter by app path - std::wstring processPath = get_process_path_waiting_uwp(window); - if (processPath.empty() || WindowUtils::IsExcludedByDefault(window, processPath, title)) - { - continue; - } - - auto data = Utils::Apps::GetApp(processPath, apps); - if (!data.has_value() || data->name.empty()) - { - continue; - } - + project.apps = SnapshotUtils::GetApps([&](HWND window) -> unsigned int { auto windowMonitor = MonitorFromWindow(window, MONITOR_DEFAULTTOPRIMARY); unsigned int monitorNumber = 0; for (const auto& monitor : project.monitors) @@ -105,25 +66,8 @@ int APIENTRY WinMain(HINSTANCE hInst, HINSTANCE hInstPrev, LPSTR cmdLine, int cm } } - Project::Application app { - .name = data.value().name, - .title = title, - .path = processPath, - .packageFullName = data.value().packageFullName, - .commandLineArgs = L"", - .isMinimized = WindowUtils::IsMinimized(window), - .isMaximized = WindowUtils::IsMaximized(window), - .position = Project::Application::Position { - .x = rect.left, - .y = rect.top, - .width = rect.right - rect.left, - .height = rect.bottom - rect.top, - }, - .monitor = monitorNumber, - }; - - project.apps.push_back(app); - } + return monitorNumber; + }); projects.push_back(project); ProjectsJsonUtils::Write(fileName, projects); diff --git a/src/modules/Projects/projects-common/AppUtils.h b/src/modules/Projects/projects-common/AppUtils.h index 8135bb375b..fac0a12b28 100644 --- a/src/modules/Projects/projects-common/AppUtils.h +++ b/src/modules/Projects/projects-common/AppUtils.h @@ -16,12 +16,12 @@ namespace Utils { namespace NonLocalizable { - const wchar_t* PackageFullNameProp = L"System.AppUserModel.PackageFullName"; - const wchar_t* PackageInstallPathProp = L"System.AppUserModel.PackageInstallPath"; - const wchar_t* InstallPathProp = L"System.Link.TargetParsingPath"; + constexpr const wchar_t* PackageFullNameProp = L"System.AppUserModel.PackageFullName"; + constexpr const wchar_t* PackageInstallPathProp = L"System.AppUserModel.PackageInstallPath"; + constexpr const wchar_t* InstallPathProp = L"System.Link.TargetParsingPath"; - const wchar_t* FileExplorerName = L"File Explorer"; - const wchar_t* FileExplorerPath = L"C:\\WINDOWS\\EXPLORER.EXE"; + constexpr const wchar_t* FileExplorerName = L"File Explorer"; + constexpr const wchar_t* FileExplorerPath = L"C:\\WINDOWS\\EXPLORER.EXE"; } struct AppData