~aleteoryx/muditaos

ref: ca9579a252e7ea6186b2dedb4dd6f66309eb0d93 muditaos/module-apps/apps-common/windows/AppWindow.cpp -rw-r--r-- 8.8 KiB
ca9579a2 — Dawid Wojtas [BH-1590] Light press ends the Power Nap 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
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
// Copyright (c) 2017-2022, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md

#include "AppWindow.hpp"
#include "ApplicationCommon.hpp"
#include "InputEvent.hpp"
#include "StatusBar.hpp"
#include "status-bar/Time.hpp"
#include <Style.hpp>
#include <i18n/i18n.hpp>
#include <service-appmgr/Controller.hpp>
#include <service-audio/AudioServiceAPI.hpp>

using namespace style::header;

namespace gui
{

    AppWindow::AppWindow(app::ApplicationCommon *app, std::string name) : Window(name), application{app}
    {
        setSize(style::window_width, style::window_height);
    }

    void AppWindow::destroyInterface()
    {
        erase(statusBar);
        erase(header);
        erase(navBar);
        statusBar = nullptr;
        header    = nullptr;
        navBar    = nullptr;
    }

    void AppWindow::rebuild()
    {}

    void AppWindow::buildInterface()
    {
        auto config          = configureStatusBar(application->getStatusBarConfiguration());
        namespace status_bar = style::status_bar;
        statusBar            = new gui::status_bar::StatusBar(this,
                                                   status_bar::default_horizontal_pos,
                                                   status_bar::default_vertical_pos,
                                                   status_bar::width,
                                                   status_bar::height);
        statusBar->configure(std::move(config));

        header = new gui::header::Header(this,
                                         style::header::default_horizontal_pos,
                                         style::header::default_vertical_pos,
                                         style::header::width,
                                         style::header::height);

        navBar = new gui::nav_bar::NavBar(this,
                                          style::nav_bar::default_horizontal_pos,
                                          style::nav_bar::default_vertical_pos,
                                          style::nav_bar::width,
                                          style::nav_bar::height);
        navBar->setActive(nav_bar::Side::Left, false);
        navBar->setActive(nav_bar::Side::Center, false);
        navBar->setActive(nav_bar::Side::Right, false);
    }

    status_bar::Configuration AppWindow::configureStatusBar(status_bar::Configuration appConfiguration)
    {
        return appConfiguration;
    }

    void AppWindow::applyToStatusBar(StatusBarConfigurationChangeFunction configChange)
    {
        if (configChange) {
            auto newConfiguration = configChange(statusBar->getConfiguration());
            statusBar->configure(std::move(newConfiguration));
        }
    }

    bool AppWindow::updateBluetooth(sys::bluetooth::BluetoothMode mode)
    {
        if (statusBar == nullptr) {
            return false;
        }

        return statusBar->updateBluetooth(mode);
    }

    bool AppWindow::updateAlarmClock(bool status)
    {
        if (statusBar == nullptr) {
            return false;
        }
        return statusBar->updateAlarmClock(status);
    }

    bool AppWindow::updateSim()
    {
        if (statusBar == nullptr) {
            return false;
        }
        return statusBar->updateSim();
    }

    bool AppWindow::updateBatteryStatus()
    {
        if (statusBar == nullptr) {
            return false;
        }
        return statusBar->updateBattery();
    }
    // updates battery level in the window
    bool AppWindow::updateSignalStrength()
    {
        if (statusBar == nullptr) {
            return false;
        }
        return statusBar->updateSignalStrength();
    }

    bool AppWindow::updateNetworkAccessTechnology()
    {
        if (statusBar == nullptr) {
            return false;
        }
        return statusBar->updateNetworkAccessTechnology();
    }

    void AppWindow::updatePhoneMode(sys::phone_modes::PhoneMode mode)
    {
        auto fn = [&](gui::status_bar::Configuration cfg) -> gui::status_bar::Configuration {
            gui::status_bar::Configuration ret(cfg);
            ret.setPhoneMode(mode);
            return ret;
        };

        applyToStatusBar(std::move(fn));
    }

    bool AppWindow::preventsAutoLocking() const noexcept
    {
        return preventsAutoLock;
    }

    RefreshModes AppWindow::updateTime()
    {
        if (statusBar == nullptr) {
            return RefreshModes::GUI_REFRESH_NONE;
        }
        return statusBar->updateTime() ? RefreshModes::GUI_REFRESH_FAST : RefreshModes::GUI_REFRESH_NONE;
    }

    void AppWindow::setTitle(const UTF8 &text)
    {
        header->setTitle(text);
    }

    UTF8 AppWindow::getTitle()
    {
        return header->getTitle();
    }

    bool AppWindow::onDatabaseMessage(sys::Message *msg)
    {
        return false;
    }

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

        if (inputEvent.isLongRelease(gui::KeyCode::KEY_RF)) {
            LOG_INFO("exit to main menu");
            app::manager::Controller::sendAction(application, app::manager::actions::Home);
        }

        if (inputEvent.isLongRelease(gui::KeyCode::KEY_PND)) {
            LOG_INFO("Locking phone");

            application->getPhoneLockSubject().lock();
        }

        if ((inputEvent.isShortRelease())) {
            switch (inputEvent.getKeyCode()) {
            case KeyCode::HEADSET_VOLUP:
                [[fallthrough]];
            case KeyCode::KEY_VOLUP: {
                return application->increaseCurrentVolume();
            }
            case KeyCode::HEADSET_VOLDN:
                [[fallthrough]];
            case KeyCode::KEY_VOLDN: {
                return application->decreaseCurrentVolume();
            }
            case KeyCode::KEY_RF: {
                application->returnToPreviousWindow();
                return true;
            }
            default:
                break;
            }
        }

        if (inputEvent.is(KeyCode::KEY_TORCH)) {
            if (inputEvent.isLongRelease()) {
                application->toggleTorchOnOff();
                return true;
            }
            else if (inputEvent.isShortRelease()) {
                application->toggleTorchColor();
                return true;
            }
        }

        return false;
    }

    void AppWindow::navBarTemporaryMode(const UTF8 &text, bool emptyOthers)
    {
        navBarTemporaryMode(text, nav_bar::Side::Left, emptyOthers);
    }

    void AppWindow::navBarTemporaryMode(const UTF8 &text, nav_bar::Side side, bool emptyOthers)
    {
        if (navBar == nullptr) {
            return;
        }

        navBar->store();

        if (emptyOthers) {
            navBar->setText(nav_bar::Side::Left, "");
            navBar->setText(nav_bar::Side::Center, "");
            navBar->setText(nav_bar::Side::Right, "");
        }

        switch (side) {
        case nav_bar::Side::Left:
            navBar->setText(nav_bar::Side::Left, text);
            break;
        case nav_bar::Side::Center:
            navBar->setText(nav_bar::Side::Center, text);
            break;
        case nav_bar::Side::Right:
            navBar->setText(nav_bar::Side::Right, text);
            break;
        }
        application->refreshWindow(gui::RefreshModes::GUI_REFRESH_FAST);
    }

    void AppWindow::navBarRestoreFromTemporaryMode()
    {
        if (navBar == nullptr) {
            return;
        }

        navBar->restore();
        application->refreshWindow(gui::RefreshModes::GUI_REFRESH_FAST);
    }

    void AppWindow::setNavBarText(const UTF8 &text, nav_bar::Side side)
    {
        navBar->setText(side, text);
    }

    void AppWindow::clearNavBarText(nav_bar::Side side)
    {
        navBar->setText(side, "");
    }

    bool AppWindow::selectSpecialCharacter()
    {
        application->clearLongPressTimeout();
        return app::manager::Controller::sendAction(
            application,
            app::manager::actions::ShowSpecialInput,
            std::make_unique<gui::SwitchSpecialChar>(gui::SwitchSpecialChar::Type::Request, application->GetName()),
            app::manager::OnSwitchBehaviour::RunInBackground);
    }

    BoundingBox AppWindow::bodySize()
    {
        return {0,
                header->offset_h(),
                this->getWidth(),
                this->getHeight() - this->header->offset_h() - navBar->getHeight()};
    }

    void AppWindow::setNavBarActive(nav_bar::Side side, bool value)
    {
        navBar->setActive(side, value);
    }

    void AppWindow::accept(GuiVisitor &visitor)
    {
        visitor.visit(*this);
    }

    std::string AppWindow::getUniqueName()
    {
        constexpr auto separator = "/";
        return application->GetName() + separator + getName();
    }

} /* namespace gui */