~aleteoryx/muditaos

ref: 7e86527c6079b536e2fc2f2fbc5816faf48383c5 muditaos/module-apps/apps-common/WindowsStack.cpp -rw-r--r-- 3.0 KiB
7e86527c — Bartosz Cichocki [EGD-7773] Fix Bell alarm popup handling 4 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
// Copyright (c) 2017-2021, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md

#include "WindowsStack.hpp"
#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)
    {
        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; });
    }

} // namespace app