~aleteoryx/muditaos

ref: d5f84437cab54acb9fc0b8f96deb6f5d46fed3de muditaos/module-apps/apps-common/WindowsStack.cpp -rw-r--r-- 3.8 KiB
d5f84437 — Lefucjusz [MOS-92] Fix continuing music playback after BT disconnection 3 years ago
                                                                                
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
// Copyright (c) 2017-2022, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md

#include "WindowsStack.hpp"

#include <utility>
#include "WindowsFactory.hpp"
#include "windows/AppWindow.hpp"

namespace app
{
    std::map<std::string, std::unique_ptr<gui::AppWindow>>::const_iterator WindowsStack::begin() const
    {
        return std::begin(windows);
    }

    std::map<std::string, std::unique_ptr<gui::AppWindow>>::const_iterator WindowsStack::end() const
    {
        return std::end(windows);
    }

    void WindowsStack::push(const std::string &name,
                            std::unique_ptr<gui::AppWindow> window,
                            const gui::popup::Disposition &disposition)
    {
        /// Note: this is the place which will destroy old window if there was one
        windows[name] = std::move(window);
        stack.push_back(WindowData(name, disposition));
    }

    gui::AppWindow *WindowsStack::get(const std::string &name) const
    {
        auto ret = windows.find(name);
        return ret == std::end(windows) ? nullptr : ret->second.get();
    }

    std::optional<WindowData> WindowsStack::getWindowData(uint32_t depth) const
    {
        if (depth >= stack.size()) {
            return std::nullopt;
        }
        return {*std::prev(stack.end(), depth + 1)};
    }

    std::optional<std::string> WindowsStack::get(uint32_t depth) const
    {
        if (auto windowData = getWindowData(depth)) {
            return {windowData->name};
        }
        return std::nullopt;
    }

    [[nodiscard]] bool WindowsStack::isEmpty() const noexcept
    {
        return stack.empty();
    }

    /// return false on pop empty
    bool WindowsStack::pop() noexcept
    {
        if (!stack.empty()) {
            stack.pop_back();
            return true;
        }
        return false;
    }

    bool WindowsStack::pop(const std::string &window)
    {
        auto ret = findInStack(window);
        if (ret != stack.end()) {
            stack.erase(std::next(ret), stack.end());
            return true;
        }
        return false;
    }

    bool WindowsStack::popLastWindow()
    {
        if (stack.size() == 1) {
            stack.clear();
            return true;
        }
        return false;
    }

    bool WindowsStack::drop(const std::string &window)
    {
        auto popWindow = findInStack(window);
        if (popWindow != stack.end()) {
            stack.erase(popWindow);
            return true;
        }
        return false;
    }

    bool WindowsStack::rebuildWindows(app::WindowsFactory &windowsFactory, ApplicationCommon *app)
    {
        if (windows.empty()) {
            return false;
        }
        for (auto &[name, window] : windows) {
            windows[name] = windowsFactory.build(app, name);
        }
        return true;
    }

    void WindowsStack::clear()
    {
        stack.clear();
        windows.clear();
    }

    decltype(WindowsStack::stack)::iterator WindowsStack::findInStack(const std::string &window)
    {
        return std::find_if(stack.begin(), stack.end(), [&](auto &el) { return el.name == window; });
    }

    bool WindowsStack::isWindowOnStack(const std::string &window)
    {
        return findInStack(window) != stack.end();
    }

    void WindowsStack::dropPendingPopups()
    {
        auto it = stack.rbegin();
        while (it != stack.rend()) {
            LOG_DEBUG("Current window on stack: %s, type: %s",
                      it->name.c_str(),
                      magic_enum::enum_name(it->disposition.windowtype).data());
            if (it->disposition.windowtype == gui::popup::Disposition::WindowType::Popup) {
                LOG_DEBUG("Erasing: %s", it->name.c_str());
                stack.erase(std::next(it).base());
            }
            std::advance(it, 1);
        }
    }

} // namespace app