summaryrefslogtreecommitdiff
path: root/src/desktop/state/FloatState.hpp
blob: ff1c4e0768a3d020931c24809de489365228d3a8 (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
56
57
58
59
#pragma once

#include <unordered_map>
#include <string>
#include <cstddef>
#include <optional>

#include "../../helpers/math/Math.hpp"
#include "../DesktopTypes.hpp"
#include "../view/Window.hpp"

namespace Desktop {
    struct SFloatCacheKey {
        size_t hash;

        SFloatCacheKey(PHLWINDOW window, bool initial) {
            // Base hash from class/title
            size_t baseHash = initial ? (std::hash<std::string>{}(window->m_initialClass) ^ (std::hash<std::string>{}(window->m_initialTitle) << 1)) :
                                        (std::hash<std::string>{}(window->m_class) ^ (std::hash<std::string>{}(window->m_title) << 1));

            // Use empty string as default tag value
            std::string tagValue = "";
            if (auto xdgTag = window->xdgTag())
                tagValue = xdgTag.value();

            // Combine hashes
            hash = baseHash ^ (std::hash<std::string>{}(tagValue) << 2);
        }

        bool operator==(const SFloatCacheKey& other) const {
            return hash == other.hash;
        }
    };
}

namespace std {
    template <>
    struct hash<Desktop::SFloatCacheKey> {
        size_t operator()(const Desktop::SFloatCacheKey& id) const {
            return id.hash;
        }
    };
}

namespace Desktop {
    class CFloatStateCache {
      public:
        CFloatStateCache()  = default;
        ~CFloatStateCache() = default;

        void                    remember(PHLWINDOW window, const Vector2D& size);
        std::optional<Vector2D> get(PHLWINDOW window);

      private:
        std::unordered_map<SFloatCacheKey, Vector2D> m_storedSizes;
    };

    UP<CFloatStateCache>& floatState();
}