~aleteoryx/muditaos

ref: 2a53becd2524fc0dba7260632d3ec409eb3a61fd muditaos/module-apps/application-special-input/widgets/SpecialInputTableWidget.cpp -rw-r--r-- 4.5 KiB
2a53becd — Maciej Janicki [BH-1136] Fix bootloop after low power 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
// Copyright (c) 2017-2021, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md

#include "SpecialInputTableWidget.hpp"
#include "ApplicationSpecialInput.hpp"
#include "SpecialCharactersTableStyle.hpp"
#include <service-appmgr/Controller.hpp>
#include <messages/AppMessage.hpp>
#include <Style.hpp>
#include <cassert>
#include <locale>
#include <codecvt>

namespace gui
{

    using namespace specialInputStyle;

    SpecialInputTableWidget::SpecialInputTableWidget(app::ApplicationCommon *app, std::list<Carrier> &&carrier)
        : application(app)
    {
        setMinimumSize(specialCharacterTableWidget::window_grid_w, specialCharacterTableWidget::window_grid_h);
        setEdges(RectangleEdge::None);

        box = new GridLayout(0,
                             0,
                             specialCharacterTableWidget::window_grid_w,
                             specialCharacterTableWidget::window_grid_h,
                             {specialCharacterTableWidget::char_grid_w, specialCharacterTableWidget::char_grid_h});

        for (auto &carrier : carrier) {
            box->addWidget(carrier.item);
            decorateActionActivated(carrier.item, carrier.val);
        }
        addWidget(box);
        inputCallback = [&](gui::Item &item, const gui::InputEvent &event) {
            if (!event.isShortRelease()) {
                return false;
            }
            return box->onInput(event);
        };

        auto defaultBorderCallback = box->borderCallback;

        box->borderCallback = [defaultBorderCallback](const InputEvent &inputEvent) -> bool {
            if (!inputEvent.isShortRelease()) {
                return false;
            }
            if (inputEvent.is(KeyCode::KEY_UP) || inputEvent.is(KeyCode::KEY_DOWN)) {
                return false;
            }
            else {
                return defaultBorderCallback(inputEvent);
            }
        };

        focusChangedCallback = [&](Item &item) {
            if (getFocusItem() == nullptr) {
                setFocusItem(box);
            }
            return true;
        };

        dimensionChangedCallback = [&](gui::Item &, const BoundingBox &newDim) -> bool {
            box->setArea(
                {0, 0, specialCharacterTableWidget::window_grid_w, specialCharacterTableWidget::window_grid_h});
            return true;
        };
    }

    void SpecialInputTableWidget::decorateActionActivated(Item *it, const std::string &str)
    {
        assert(application);
        auto app = dynamic_cast<app::ApplicationSpecialInput *>(application);
        assert(app);

        it->activatedCallback = [=](Item &it) {
            setFocusItem(nullptr);
            LOG_INFO("handled special char for %s", application->getCurrentWindow()->getName().c_str());
            auto switchData =
                std::make_unique<gui::SwitchSpecialChar>(gui::SwitchSpecialChar::Type::Response, app->requester, str);
            return app::manager::Controller::switchBack(
                application, std::make_unique<app::manager::SwitchBackRequest>(app->requester, std::move(switchData)));
        };
    }

    auto generateNewLineSign() -> Carrier
    {
        auto rect = new Rect(
            nullptr, 0, 0, specialCharacterTableWidget::char_label_w, specialCharacterTableWidget::char_label_h);
        rect->setEdges(gui::RectangleEdge::Top | gui::RectangleEdge::Bottom);
        rect->setPenWidth(0);
        new gui::Image(rect,
                       specialCharacterTableWidget::char_newline_x,
                       specialCharacterTableWidget::char_newline_y,
                       specialCharacterTableWidget::char_label_w,
                       specialCharacterTableWidget::char_label_h,
                       "enter_icon_alpha_W_M");
        return {rect, std::string(&text::newline, 1)};
    }

    auto generateCharSign(uint32_t val) -> Carrier
    {
        std::wstring_convert<std::codecvt_utf8<char32_t>, char32_t> convert;
        std::string schar_string = convert.to_bytes(std::u32string(1, val));
        auto el                  = new gui::Label(
            nullptr, 0, 0, specialCharacterTableWidget::char_label_w, specialCharacterTableWidget::char_label_h);
        style::window::decorate(el);
        el->setAlignment(gui::Alignment(gui::Alignment::Horizontal::Center, gui::Alignment::Vertical::Center));
        el->setFont(style::window::font::medium);
        el->setText(schar_string);
        return {el, schar_string};
    }
} /* namespace gui */