~aleteoryx/muditaos

ref: c737af962fe5ae148eb4c53609e467bad2df785c muditaos/module-apps/application-settings/windows/LanguageWindow.cpp -rw-r--r-- 3.5 KiB
c737af96 — RobertPiet [EGD-4914] input and display languages connection to settings problem… (#1188) 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
// Copyright (c) 2017-2020, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md

#include <functional>

#include "service-appmgr/Controller.hpp"

#include "../ApplicationSettings.hpp"

#include "module-utils/i18n/i18n.hpp"

#include "Label.hpp"
#include "LanguageWindow.hpp"
#include <Style.hpp>
#include <module-apps/application-settings-new/widgets/SettingsStyle.hpp>

namespace gui
{
    namespace window::name
    {
        constexpr inline auto languages = "Languages";
    }

    LanguageWindow::LanguageWindow(app::Application *app) : AppWindow(app, gui::window::name::languages)
    {
        buildInterface();
        setFocusItem(options[0]);
    }

    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"));
        const auto &langList = loader.getAvailableDisplayLanguages();
        for (const auto &lang : langList) {
            options.push_back(addOptionLabel(lang, [=](gui::Item &item) {
                app::manager::Controller::changeDisplayLanguage(application, lang);
                return true;
            }));
        }

        // set position and navigation for labels
        uint32_t size = options.size();
        for (uint32_t i = 0; i < size; i++) {
            options[i]->setPosition(style::settings::window::languageChange::options_posX,
                                    style::settings::window::languageChange::options_posY +
                                        (i * style::settings::window::languageChange::options_distance_between));
            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 */