~aleteoryx/muditaos

ref: 53e2fae8fbf21cd87194a0ba39b37dbe36761231 muditaos/module-gui/gui/widgets/text/modes/InputMode.cpp -rw-r--r-- 3.1 KiB
53e2fae8 — Lefucjusz [MOS-178] Add input mode displaying timeout 2 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
// Copyright (c) 2017-2023, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md

#include "InputMode.hpp"
#include <i18n/i18n.hpp>
#include <map>
#include <log/log.hpp>
#include <utility>

/// input mode strings - as these are stored in json (in files...)
const std::map<InputMode::Mode, std::string> input_mode = {
    {InputMode::digit, "numeric"},
    {InputMode::ABC, "upper"},
    {InputMode::abc, "lower"},
    {InputMode::phone, "phone"},
};

static std::string getInputName(InputMode::Mode m)
{
    switch (m) {
    case InputMode::digit:
        return "123";
    case InputMode::Abc:
        return "Abc";
    case InputMode::ABC:
        return "ABC";
    case InputMode::abc:
        return "abc";
    case InputMode::phone:
        return "phone";
    default:
        return "";
    }
}

InputMode::InputMode(std::list<InputMode::Mode> mode_list,
                     std::function<void(const UTF8 &text)> show_type_callback,
                     std::function<void()> restore_after_show_type_callback,
                     std::function<void()> show_special_char_selector,
                     std::function<void(std::function<void()> restore_function)> restore_timer_callback)
    : input_mode_list(std::move(mode_list)), show_type_callback(std::move(show_type_callback)),
      restore_after_show_type_callback(std::move(restore_after_show_type_callback)),
      show_special_char_selector(std::move(show_special_char_selector)),
      restore_timer_callback(std::move(restore_timer_callback))
{
    // failsafe
    if (input_mode_list.empty()) {
        input_mode_list.push_back(Mode::digit);
    }
}

InputMode::Mode InputMode::modeNow() const
{
    return *std::next(input_mode_list.begin(), input_mode_list_pos);
}

/// sets next selected mode using Application pointer
void InputMode::next()
{
    ++input_mode_list_pos;
    if (input_mode_list_pos == input_mode_list.size()) {
        input_mode_list_pos = 0;
    }
    LOG_INFO("%" PRIu32, input_mode_list_pos);
    show_input_type();
}

const std::string &InputMode::get()
{
    auto actualInputMode = input_mode.at(modeNow());
    if (actualInputMode == input_mode.find(InputMode::digit)->second ||
        actualInputMode == input_mode.find(InputMode::phone)->second) {
        return input_mode.at(modeNow());
    }
    return utils::getInputLanguageFilename(actualInputMode);
}

const std::string &InputMode::get(InputMode::Mode mode)
{
    const auto &selectedInputMode = input_mode.at(mode);
    return utils::getInputLanguageFilename(selectedInputMode);
}

void InputMode::show_input_type()
{
    LOG_INFO("Mode: %d", modeNow());
    if (show_type_callback) {
        show_type_callback(getInputName(modeNow()));
    }

    if (restore_timer_callback) {
        restore_timer_callback([this]() { show_restore(); });
    }
}

void InputMode::show_restore()
{
    if (restore_after_show_type_callback) {
        restore_after_show_type_callback();
    }
}

void InputMode::select_special_char()
{
    if (show_special_char_selector) {
        LOG_INFO("Special character selector");
        show_special_char_selector();
    }
}