Repository: microsoft/PowerToys
File: src/modules/Workspaces/WorkspacesSnapshotTool/SnapshotUtils.cpp (C++)

Code before:
                }

                if (pwaAppId.has_value())
                {
                    auto pwaName = pwaHelper.SearchPwaName(pwaAppId.value(), windowAumid);
                    Logger::info(L"Found {} PWA app with name {}, appId: {}", (isEdge ? L"Edge" : (isChrome ? L"Chrome" : L"unknown")), pwaName, pwaAppId.value());

                    appData.pwaAppId = pwaAppId.value();
                    appData.name = pwaName + L" (" + appData.name + L")";
                }
            }

            bool isMinimized = WindowUtils::IsMinimized(window);
            unsigned int monitorNumber = getMonitorNumberFromWindowHandle(window);

            if (isMinimized)
            {
                // set the screen area as position, the values we get for the minimized windows are out of the screens' area
                WorkspacesData::WorkspacesProject::Monitor::MonitorRect monitorRect = getMonitorRect(monitorNumber);
                rect.left = monitorRect.left;
                rect.top = monitorRect.top;
                rect.right = monitorRect.left + monitorRect.width;
                rect.bottom = monitorRect.top + monitorRect.height;
            }

            std::wstring guid = Utils::Apps::GetGuidFromHwnd(window);

            WorkspacesData::WorkspacesProject::Application app{
                .id = guid,
                .name = appData.name,
                .title = title,
                .path = appData.installPath,
                .packageFullName = appData.packageFullName,
                .appUserModelId = appData.appUserModelId,
                .pwaAppId = appData.pwaAppId,
                .commandLineArgs = L"",
                .isElevated = IsProcessElevated(pid),
                .canLaunchElevated = appData.canLaunchElevated,
                .isMinimized = isMinimized,
                .isMaximized = WindowUtils::IsMaximized(window),
                .position = WorkspacesData::WorkspacesProject::Application::Position{
                    .x = rect.left,
                    .y = rect.top,
                    .width = rect.right - rect.left,
                    .height = rect.bottom - rect.top,
                },
                .monitor = monitorNumber,
            };

            apps.push_back(app);
        }

Review comment (bug):
There is a possible scenario that could lead to the same app IDs in different workspaces, which could lead to bugs.  
For example, if I click Launch and Edit, leave those launched windows opened, and create a new workspace with those windows -> then we'll have the same IDs for apps.  

My suggestion is to pass an argument to the snapshot tool with a flag if it's a creation of a new workspace or "Launch and Edit" (the same as we do for Launcher).
When we're creating a new workspace, we should create a new GUID for each app, and when editing, get those from windows.

Code after:
                    pwaAppId = pwaHelper.GetChromeAppId(windowAumid);
                }

                if (pwaAppId.has_value())
                {
                    auto pwaName = pwaHelper.SearchPwaName(pwaAppId.value(), windowAumid);
                    Logger::info(L"Found {} PWA app with name {}, appId: {}", (isEdge ? L"Edge" : (isChrome ? L"Chrome" : L"unknown")), pwaName, pwaAppId.value());

                    appData.pwaAppId = pwaAppId.value();
                    appData.name = pwaName + L" (" + appData.name + L")";
                }
            }

            bool isMinimized = WindowUtils::IsMinimized(window);
            unsigned int monitorNumber = getMonitorNumberFromWindowHandle(window);

            if (isMinimized)
            {
                // set the screen area as position, the values we get for the minimized windows are out of the screens' area
                WorkspacesData::WorkspacesProject::Monitor::MonitorRect monitorRect = getMonitorRect(monitorNumber);
                rect.left = monitorRect.left;
                rect.top = monitorRect.top;
                rect.right = monitorRect.left + monitorRect.width;
                rect.bottom = monitorRect.top + monitorRect.height;
            }

            std::wstring guid = isGuidNeeded ? WorkspacesWindowProperties::GetGuidFromHwnd(window) : L"";

            WorkspacesData::WorkspacesProject::Application app{
                .id = guid,
                .name = appData.name,
                .title = title,
                .path = appData.installPath,
                .packageFullName = appData.packageFullName,
                .appUserModelId = appData.appUserModelId,
                .pwaAppId = appData.pwaAppId,
                .commandLineArgs = L"",
                .isElevated = IsProcessElevated(pid),
                .canLaunchElevated = appData.canLaunchElevated,
                .isMinimized = isMinimized,
                .isMaximized = WindowUtils::IsMaximized(window),
                .position = WorkspacesData::WorkspacesProject::Application::Position{
                    .x = rect.left,
                    .y = rect.top,
                    .width = rect.right - rect.left,
                    .height = rect.bottom - rect.top,
                },
                .monitor = monitorNumber,
            };

            apps.push_back(app);