Repository: hyprwm/Hyprland
File: src/managers/KeybindManager.cpp (C++)

Code before:
    return {};
}

SDispatchResult CKeybindManager::focusCurrentOrLast(std::string args) {
    const auto PWINDOWPREV = g_pCompositor->m_lastWindow.lock() ? (g_pCompositor->m_windowFocusHistory.size() < 2 ? nullptr : g_pCompositor->m_windowFocusHistory[1].lock()) :
                                                                  (g_pCompositor->m_windowFocusHistory.empty() ? nullptr : g_pCompositor->m_windowFocusHistory[0].lock());

    if (!PWINDOWPREV)
        return {.success = false, .error = "Window not found"};

    switchToWindow(PWINDOWPREV);

    return {};
}

SDispatchResult CKeybindManager::swapActive(std::string args) {
    char       arg         = args[0];
    const auto PLASTWINDOW = g_pCompositor->m_lastWindow.lock();

    if (!PLASTWINDOW)
        return {.success = false, .error = "Window to swap with not found"};

    if (PLASTWINDOW->isFullscreen())
        return {.success = false, .error = "Can't swap fullscreen window"};

    if (args.starts_with("address:")) {
        const auto PWINDOWTOCHANGETO = g_pCompositor->getWindowByRegex(args);

        if (!PWINDOWTOCHANGETO) {
            Debug::log(ERR, "Cannot move {}, is invalid window address", args);
            return {.success = false, .error = std::format("Cannot move {}, is invalid window address", args)};
        }

        if (PLASTWINDOW->m_workspace != PWINDOWTOCHANGETO->m_workspace) {
            Debug::log(ERR, "Cannot move {}, is in different workspace", args);
            return {.success = false, .error = std::format("Cannot move {}, is in different workspace", args)};
        }

        Debug::log(LOG, "Swapping active window with {}", arg);
        g_pLayoutManager->getCurrentLayout()->switchWindows(PLASTWINDOW, PWINDOWTOCHANGETO);
        return {};
    }

    if (!isDirection(args)) {
        Debug::log(ERR, "Cannot move window in direction {}, unsupported direction. Supported: l,r,u/t,d/b", arg);
        return {.success = false, .error = std::format("Cannot move window in direction {}, unsupported direction. Supported: l,r,u/t,d/b", arg)};
    }

    Debug::log(LOG, "Swapping active window in direction {}", arg);

    const auto PWINDOWTOCHANGETO = g_pCompositor->getWindowInDirection(PLASTWINDOW, arg);

Review comment (bug):
I'd say do it the other way round. If it's not a direction, try to find the window with `g_pCompositor->getWindowByRegex`. That will do address, class, etc.

Just check if you didnt get nullptr or the active window

Code after:

    return {};
}

SDispatchResult CKeybindManager::focusCurrentOrLast(std::string args) {
    const auto PWINDOWPREV = g_pCompositor->m_lastWindow.lock() ? (g_pCompositor->m_windowFocusHistory.size() < 2 ? nullptr : g_pCompositor->m_windowFocusHistory[1].lock()) :
                                                                  (g_pCompositor->m_windowFocusHistory.empty() ? nullptr : g_pCompositor->m_windowFocusHistory[0].lock());

    if (!PWINDOWPREV)
        return {.success = false, .error = "Window not found"};

    switchToWindow(PWINDOWPREV);

    return {};
}

SDispatchResult CKeybindManager::swapActive(std::string args) {
    char       arg               = args[0];
    const auto PLASTWINDOW       = g_pCompositor->m_lastWindow.lock();
    PHLWINDOW  PWINDOWTOCHANGETO = nullptr;

    if (!PLASTWINDOW)
        return {.success = false, .error = "Window to swap with not found"};

    if (PLASTWINDOW->isFullscreen())
        return {.success = false, .error = "Can't swap fullscreen window"};

    if (isDirection(args))
        PWINDOWTOCHANGETO = g_pCompositor->getWindowInDirection(PLASTWINDOW, arg);
    else
        PWINDOWTOCHANGETO = g_pCompositor->getWindowByRegex(args);

    if (!PWINDOWTOCHANGETO || PWINDOWTOCHANGETO == PLASTWINDOW) {
        Debug::log(ERR, "Can't swap with {}, invalid window", args);
        return {.success = false, .error = std::format("Can't swap with {}, invalid window", args)};
    }

    Debug::log(LOG, "Swapping active window with {}", args);

    updateRelativeCursorCoords();
    g_pLayoutManager->getCurrentLayout()->switchWindows(PLASTWINDOW, PWINDOWTOCHANGETO);
    PLASTWINDOW->warpCursor();
    return {};
}

SDispatchResult CKeybindManager::moveActiveTo(std::string args) {
    char arg    = args[0];
    bool silent = args.ends_with(" silent");
    if (silent)
        args = args.substr(0, args.length() - 7);