~aleteoryx/muditaos

ref: b4814861b02051c9b402627577eec80aeb258830 muditaos/module-gui/gui/widgets/Lines.cpp -rw-r--r-- 3.9 KiB
b4814861 — Roman Kubiak [EGD-4318] enable service desktop (#973) 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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
// Copyright (c) 2017-2020, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md

#include "Lines.hpp"
#include "TextLineCursor.hpp"
#include "Text.hpp"

namespace gui
{
    // LEFT/RIGHT/UP/DOWN
    auto Lines::checkNavigationBounds(TextLineCursor &cursor, InputEvent event) -> gui::InputBound
    {
        auto dir = inputToNavigation(event);
        if (dir == NavigationDirection::NONE) {
            return InputBound::UNDEFINED;
        }

        auto screen_bound = scroll_position + max_lines_count - 1;
        auto lines_bound  = lines.size() - 1;

        if (dir == NavigationDirection::UP || dir == NavigationDirection::LEFT) {
            screen_bound = 0;
            lines_bound  = 0;
        }

        if (dir == NavigationDirection::LEFT && cursor.getPosOnScreen() > 0) {
            return InputBound::CAN_MOVE;
        }

        unsigned int pos = cursor.BlockCursor::getPosition();
        auto textLine    = getTextLine(cursor.getScreenLine());

        if (textLine == nullptr) {
            return InputBound::NO_DATA;
        }

        size_t lineLength = textLine->length();

        if (dir == NavigationDirection::RIGHT && pos < lineLength) {
            return InputBound::CAN_MOVE;
        }

        if (cursor.getScreenLine() >= lines_bound) {
            return InputBound::NO_DATA;
        }

        if (cursor.getScreenLine() >= screen_bound) {
            return InputBound::HIT_BOUND;
        }

        return InputBound::CAN_MOVE;
    }

    auto Lines::checkAdditionBounds(TextLineCursor &cursor, InputEvent event) -> gui::InputBound
    {
        auto keymap = parent->mode != nullptr ? parent->mode->get() : "";
        auto code   = gui::Profiles::get(keymap).get(event.key.key_code, 0);

        auto format        = cursor->getFormat();
        uint32_t line      = cursor.getScreenLine();
        TextLine *textLine = getTextLine(line);

        if (textLine == nullptr || !cursor) {
            return InputBound::CAN_ADD;
        }

        auto bound = textLine->checkBounds(cursor, code, format);
        if (bound == InputBound::CANT_PROCESS && line == scroll_position) {
            // TODO -> to be corrected in next PR
            return InputBound::CAN_ADD;
        }

        return InputBound::CAN_ADD;
    }

    auto Lines::checkRemovalBounds(TextLineCursor &cursor, InputEvent event) -> gui::InputBound
    {
        if (lines.empty()) {
            return InputBound::CANT_PROCESS;
        }

        // TODO -> to be corrected in next PR
        return InputBound::CAN_REMOVE;

        uint32_t line = cursor.getScreenLine();
        uint32_t pos  = cursor.getPosOnScreen();

        if (pos == 0) {
            if (line == scroll_position + max_lines_count) {
                return InputBound::HIT_BOUND;
            }
            if (line == 0) {
                return InputBound::CANT_PROCESS;
            }
        }

        return InputBound::CAN_REMOVE;
    }

    void Lines::updateScrollPosition(NavigationDirection dir, unsigned int lines_to_scroll)
    {
        if (dir == NavigationDirection::UP) {
            scroll_position -= lines_to_scroll;
        }

        if (dir == NavigationDirection::DOWN) {
            scroll_position += lines_to_scroll;
        }
    }

    void Lines::linesVAlign(Length parentSize)
    {
        for (auto &line : lines) {
            line.alignV(parent->getAlignment(Axis::Y), parentSize, linesHeight());
        }
    }

    void Lines::linesHAlign(Length parentSize)
    {
        for (auto &line : lines) {
            line.alignH(parent->getAlignment(Axis::X), parentSize);
        }
    }

    void Lines::draw(TextCursor &cursor)
    {
        parent->drawLines();
    }

    TextLine *Lines::getTextLine(uint32_t line)
    {
        if (lines.empty() || line >= lines.size()) {
            return nullptr;
        }

        auto it = std::next(lines.begin(), line);
        return &*it;
    } // namespace gui

} // namespace gui