~aleteoryx/muditaos

ref: 7d7003d62639426be8eb00f4fc288b68139f5566 muditaos/module-apps/application-desktop/windows/PowerOffWindow.cpp -rw-r--r-- 8.3 KiB
7d7003d6 — Marcin Smoczyński Merge branch 'master' into stable 5 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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
// Copyright (c) 2017-2020, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md

#include "InputEvent.hpp"
#include "gui/widgets/BottomBar.hpp"
#include "gui/widgets/TopBar.hpp"
#include "GuiTimer.hpp"
#include "log/log.hpp"

// module-utils
#include <i18n/i18n.hpp>

#include "PowerOffWindow.hpp"
#include "../ApplicationDesktop.hpp"

// services
#include <service-appmgr/model/ApplicationManager.hpp>

#include "service-cellular/ServiceCellular.hpp"
#include <Style.hpp>

namespace gui
{

    PowerOffWindow::PowerOffWindow(app::Application *app) : AppWindow(app, app::window::name::desktop_poweroff)
    {
        buildInterface();
    }

    void PowerOffWindow::rebuild()
    {
        // find which widget has focus
        uint32_t index = 0;
        for (uint32_t i = 0; i < selectionLabels.size(); i++)
            if (selectionLabels[i] == getFocusItem()) {
                index = i;
                break;
            }

        destroyInterface();
        buildInterface();
        setFocusItem(selectionLabels[index]);
    }
    void PowerOffWindow::buildInterface()
    {
        AppWindow::buildInterface();
        bottomBar->setActive(BottomBar::Side::CENTER, true);
        bottomBar->setActive(BottomBar::Side::RIGHT, true);
        bottomBar->setText(BottomBar::Side::CENTER, utils::localize.get(style::strings::common::confirm));
        bottomBar->setText(BottomBar::Side::RIGHT, utils::localize.get(style::strings::common::back));

        powerImage     = new gui::Image(this, 177, 132, 0, 0, "pin_lock_info");
        powerDownImage = new gui::Image(this, 0, 0, 0, 0, "logo");
        powerDownImage->setVisible(false);

        // title label
        titleLabel = new gui::Label(this, 0, 60, 480, 40);
        titleLabel->setFilled(false);
        titleLabel->setBorderColor(gui::ColorFullBlack);
        titleLabel->setFont(style::header::font::title);
        titleLabel->setText(utils::localize.get("app_desktop_poweroff_title"));
        titleLabel->setEdges(RectangleEdge::None);
        titleLabel->setAlignment(gui::Alignment(gui::Alignment::Horizontal::Center, gui::Alignment::Vertical::Bottom));

        // label with question for powering down
        infoLabel = new gui::Label(this, 0, 294, 480, 30);
        infoLabel->setFilled(false);
        infoLabel->setBorderColor(gui::ColorNoColor);
        infoLabel->setFont(style::window::font::medium);
        infoLabel->setAlignment(gui::Alignment(gui::Alignment::Horizontal::Center, gui::Alignment::Vertical::Bottom));
        infoLabel->setText(utils::localize.get("app_desktop_poweroff_question"));

        uint32_t pinLabelX = 46;
        uint32_t pinLabelY = 350;
        for (uint32_t i = 0; i < 4; i++) {
            gui::Label *label = new gui::Label(this, pinLabelX, pinLabelY, 193, 75);
            label->setFilled(false);
            label->setBorderColor(gui::ColorFullBlack);
            label->setPenWidth(0);
            label->setPenFocusWidth(2);
            label->setRadius(5);
            label->setFont(style::window::font::medium);
            label->setEdges(RectangleEdge::All);
            label->setAlignment(gui::Alignment(gui::Alignment::Horizontal::Center, gui::Alignment::Vertical::Center));
            selectionLabels.push_back(label);
            pinLabelX += 193;
        }

        selectionLabels[0]->setText(utils::localize.get(style::strings::common::no));
        selectionLabels[1]->setText(utils::localize.get(style::strings::common::yes));

        pinLabelX = 46;
        pinLabelY += 75;
        eventMgrLabel = new gui::Label(this, pinLabelX, pinLabelY, 193 * 2, 75);
        eventMgrLabel->setFilled(false);
        eventMgrLabel->setBorderColor(gui::ColorFullBlack);
        eventMgrLabel->setPenWidth(0);
        eventMgrLabel->setPenFocusWidth(2);
        eventMgrLabel->setRadius(5);
        eventMgrLabel->setFont(style::window::font::bigbold);
        eventMgrLabel->setText("TURN PWR MGR OFF");
        eventMgrLabel->setEdges(RectangleEdge::All);
        eventMgrLabel->setAlignment(
            gui::Alignment(gui::Alignment::Horizontal::Center, gui::Alignment::Vertical::Center));

        // define navigation between labels
        selectionLabels[0]->setNavigationItem(NavigationDirection::LEFT, selectionLabels[1]);
        selectionLabels[0]->setNavigationItem(NavigationDirection::RIGHT, selectionLabels[1]);
        selectionLabels[0]->setNavigationItem(NavigationDirection::DOWN, eventMgrLabel);

        selectionLabels[1]->setNavigationItem(NavigationDirection::LEFT, selectionLabels[0]);
        selectionLabels[1]->setNavigationItem(NavigationDirection::RIGHT, selectionLabels[0]);
        selectionLabels[1]->setNavigationItem(NavigationDirection::DOWN, eventMgrLabel);

        eventMgrLabel->setNavigationItem(NavigationDirection::UP, selectionLabels[0]);

        // callbacks for getting focus
        selectionLabels[0]->focusChangedCallback = [=](gui::Item &item) {
            if (item.focus)
                this->state = State::Return;
            return true;
        };

        selectionLabels[1]->focusChangedCallback = [=](gui::Item &item) {
            if (item.focus)
                this->state = State::PowerDown;
            return true;
        };

        selectionLabels[1]->activatedCallback = [=](gui::Item &item) {
            LOG_INFO("Closing system");
            application->setShutdownFlag();

            bottomBar->setVisible(false);
            topBar->setVisible(false);
            selectionLabels[0]->setVisible(false);
            selectionLabels[1]->setVisible(false);
            eventMgrLabel->setVisible(false);
            powerImage->setVisible(false);
            powerDownImage->setVisible(true);
            titleLabel->setVisible(false);
            infoLabel->setVisible(false);

            application->refreshWindow(RefreshModes::GUI_REFRESH_DEEP);
            scheduleSystemShutdown();
            return true;
        };

        eventMgrLabel->activatedCallback = [=](gui::Item &item) {
            static bool state = false;
            if (state == false) {
                LOG_INFO("SUSPEND SYSTEM");
                state = true;
            }
            else {
                LOG_INFO("RESUME SYSTEM");
                state = false;
            }
            return true;
        };
    }

    // Temporary solution for shutting down the system.
    // The former solution removed from service-gui during its refactor.
    // To be reimplemented in SystemManager in upcoming commits.
    void PowerOffWindow::scheduleSystemShutdown()
    {
        constexpr auto SystemShutdownDelayInMs = 500;
        auto powerOffTimer                     = std::make_unique<app::GuiTimer>("PowerOffTimer", application);
        powerOffTimer->setInterval(SystemShutdownDelayInMs);
        timerCallback = [this](Item &, Timer &timer) {
            detachTimer(timer);
            sys::SystemManager::CloseSystem(application);
            return true;
        };
        powerOffTimer->start();
        application->connect(std::move(powerOffTimer), this);
    }

    void PowerOffWindow::destroyInterface()
    {
        erase();
        invalidate();
    }

    void PowerOffWindow::invalidate() noexcept
    {
        titleLabel     = nullptr;
        infoLabel      = nullptr;
        eventMgrLabel  = nullptr;
        powerImage     = nullptr;
        powerDownImage = nullptr;
        selectionLabels.clear();
    }

    void PowerOffWindow::onBeforeShow(ShowMode mode, SwitchData *data)
    {
        // on entering screen always set default result as returning to home screen and set focus to "No" label
        state = State::Return;
        setFocusItem(selectionLabels[0]);
    }

    bool PowerOffWindow::onInput(const InputEvent &inputEvent)
    {
        // check if any of the lower inheritance onInput methods catch the event
        if (AppWindow::onInput(inputEvent)) {
            return true;
        }

        // process only short press, consume rest
        if (inputEvent.state != gui::InputEvent::State::keyReleasedShort)
            return true;

        // if enter was pressed check state and power down or return to main desktop's window
        if (inputEvent.keyCode == KeyCode::KEY_ENTER) {
            if (state != State::PowerDown) {
                application->switchWindow("MainWindow");
            }
        }

        return false;
    }

} /* namespace gui */