~aleteoryx/muditaos

ref: dbc5da5f9d8e11dd45e081334f59c1658a76bb78 muditaos/module-apps/application-settings/windows/LanguageWindow.cpp -rw-r--r-- 4.1 KiB
dbc5da5f — Roman Kubiak [EGD-4158] network access technology added to deviceInfo endpoint (#921) 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
// Copyright (c) 2017-2020, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md

#include <memory>
#include <functional>

#include "service-appmgr/Controller.hpp"

#include "../ApplicationSettings.hpp"

#include "i18/i18.hpp"

#include "Label.hpp"
#include "Margins.hpp"
#include "LanguageWindow.hpp"
#include <Style.hpp>

namespace gui
{

    LanguageWindow::LanguageWindow(app::Application *app) : AppWindow(app, "Languages")
    {
        buildInterface();
    }

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

        destroyInterface();
        buildInterface();
        setFocusItem(options[index]);
    }
    void LanguageWindow::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::select));
        bottomBar->setText(BottomBar::Side::RIGHT, utils::localize.get(style::strings::common::back));

        topBar->setActive(TopBar::Elements::SIGNAL, true);
        topBar->setActive(TopBar::Elements::BATTERY, true);

        setTitle(utils::localize.get("app_settings_title_languages"));

        // add option connectivity option
        options.push_back(addOptionLabel(utils::localize.get("app_settings_language_english"), [=](gui::Item &item) {
            LOG_INFO("selected language: english");
            app::manager::Controller::changeLanguage(application, utils::Lang::En);
            return true;
        }));

        // add option date and time option
        options.push_back(addOptionLabel(utils::localize.get("app_settings_language_polish"), [=](gui::Item &) {
            LOG_INFO("selected language: polish");
            app::manager::Controller::changeLanguage(application, utils::Lang::Pl);
            return true;
        }));

        // add option display option
        options.push_back(addOptionLabel(utils::localize.get("app_settings_language_german"), [=](gui::Item &) {
            LOG_INFO("selected language: german");
            app::manager::Controller::changeLanguage(application, utils::Lang::De);
            return true;
        }));

        options.push_back(addOptionLabel(utils::localize.get("app_settings_language_spanish"), [=](gui::Item &) {
            LOG_INFO("selected language: spanish");
            app::manager::Controller::changeLanguage(application, utils::Lang::Sp);
            return true;
        }));

        // set possition and navigation for labels
        uint32_t posY = 100;
        uint32_t size = options.size();
        for (uint32_t i = 0; i < options.size(); i++) {
            options[i]->setPosition(17, posY);
            posY += 60;
            options[i]->setNavigationItem(NavigationDirection::DOWN, options[(i + 1) % size]);
            options[i]->setNavigationItem(NavigationDirection::UP, options[(size + i - 1) % size]);
        }
    }

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

    void LanguageWindow::invalidate() noexcept
    {
        options.clear();
    }

    gui::Item *LanguageWindow::addOptionLabel(const std::string &text, std::function<bool(Item &)> activatedCallback)
    {
        gui::Label *label = new gui::Label(this, 17, 0, 480 - 34, 60, text);
        label->setPadding(gui::Padding(16, 0, 0, 0));
        label->setFilled(false);
        label->setPenFocusWidth(3);
        label->setPenWidth(0);
        label->setFont(style::window::font::medium);
        label->setAlignment(gui::Alignment(gui::Alignment::Horizontal::Left, gui::Alignment::Vertical::Center));
        label->setRadius(11);
        label->activatedCallback = activatedCallback;

        return label;
    }

    void LanguageWindow::onBeforeShow(ShowMode mode, SwitchData *data)
    {
        setFocusItem(options[0]);
    }

} /* namespace gui */