summaryrefslogtreecommitdiff
path: root/src/desktop/history/WindowHistoryTracker.cpp
blob: edaa2b5e1a55a0dd73da53eb9b681c003162bf9c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include "WindowHistoryTracker.hpp"

#include "../../managers/HookSystemManager.hpp"
#include "../view/Window.hpp"

using namespace Desktop;
using namespace Desktop::History;

SP<CWindowHistoryTracker> History::windowTracker() {
    static SP<CWindowHistoryTracker> tracker = makeShared<CWindowHistoryTracker>();
    return tracker;
}

CWindowHistoryTracker::CWindowHistoryTracker() {
    static auto P = g_pHookSystem->hookDynamic("openWindowEarly", [this](void* self, SCallbackInfo& info, std::any data) {
        auto window = std::any_cast<PHLWINDOW>(data);

        // add a last track
        m_history.insert(m_history.begin(), window);
    });

    static auto P1 = g_pHookSystem->hookDynamic("activeWindow", [this](void* self, SCallbackInfo& info, std::any data) {
        auto window = std::any_cast<Desktop::View::SWindowActiveEvent>(data).window;

        track(window);
    });
}

void CWindowHistoryTracker::track(PHLWINDOW w) {
    std::erase(m_history, w);
    m_history.emplace_back(w);
}

const std::vector<PHLWINDOWREF>& CWindowHistoryTracker::fullHistory() {
    gc();
    return m_history;
}

std::vector<PHLWINDOWREF> CWindowHistoryTracker::historyForWorkspace(PHLWORKSPACE ws) {
    gc();
    std::vector<PHLWINDOWREF> windows;

    for (const auto& w : m_history) {
        if (w->m_workspace != ws)
            continue;

        windows.emplace_back(w);
    }

    return windows;
}

void CWindowHistoryTracker::gc() {
    std::erase_if(m_history, [](const auto& e) { return !e; });
}