~aleteoryx/muditaos

ref: 7fbaf735ed99f5b0d26adb686cfa6ed2bb4267d5 muditaos/module-gui/gui/widgets/TextLineCursor.cpp -rw-r--r-- 10.0 KiB
7fbaf735 — Przemyslaw Brudny [EGD-7813] Option Window titles localizations fix 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
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
// Copyright (c) 2017-2021, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md

#include "TextLineCursor.hpp"
#include "Text.hpp"
#include <log/log.hpp>

#define debug_text_cursor(...)
// #define debug_text_cursor(...) LOG_DEBUG(__VA_ARGS__)

namespace gui
{
    TextLineCursor::TextLineCursor(gui::Text *parent, unsigned int pos, unsigned int block)
        : TextCursor(parent, pos, block)
    {}

    auto TextLineCursor::checkNextLineDocumentEnd(unsigned int selectedLineNumber) -> bool
    {
        auto nextLine     = text->lines->getLine(selectedLineNumber + 1);
        auto selectedLine = text->lines->getLine(selectedLineNumber);

        return nextLine->length() == 0 && selectedLine->getEnd() != TextBlock::End::Newline;
    }

    void TextLineCursor::handleDownNavigation(unsigned int selectedLineNumber, unsigned int selectedLineCursorPosition)
    {
        auto selectedLine        = text->lines->getLine(selectedLineNumber);
        auto nextLine            = text->lines->getLine(selectedLineNumber + 1);
        auto nextLineEndAddition = nextLine->getEnd() == TextBlock::End::Newline ? 1 : 0;

        auto noNewAtBeginningAddition =
            selectedLine->getEnd() == TextBlock::End::None && selectedLineCursorPosition == 0 ? 1 : 0;

        auto nextLineMoveCount = nextLine->length() > selectedLineCursorPosition
                                     ? selectedLineCursorPosition
                                     : nextLine->length() - nextLineEndAddition;

        // Next line will be displayed as part of Right move action on right lines border
        auto moveCount =
            (selectedLine->length() - selectedLineCursorPosition) + nextLineMoveCount + noNewAtBeginningAddition;

        moveCursor(NavigationDirection::RIGHT, moveCount);
    }

    void TextLineCursor::handleUpNavigation(unsigned int selectedLineNumber, unsigned int selectedLineCursorPosition)
    {
        auto previousLine            = text->lines->getLine(selectedLineNumber - 1);
        auto previousLineEndAddition = previousLine->getEnd() == TextBlock::End::Newline ? 1 : 0;

        auto previousLineMoveCount = previousLine->length() > selectedLineCursorPosition
                                         ? previousLine->length() - previousLineEndAddition - selectedLineCursorPosition
                                         : 0;

        auto newLineAtBeginningSubtraction =
            (previousLine->getEnd() == TextBlock::End::Newline) && (selectedLineCursorPosition == 0) &&
                    (selectedLineNumber >= 2) &&
                    (text->lines->getLine(selectedLineNumber - 2)->getEnd() == TextBlock::End::None)
                ? 1
                : 0;

        auto moveCount = selectedLineCursorPosition + previousLineEndAddition + previousLineMoveCount -
                         newLineAtBeginningSubtraction;

        moveCursor(NavigationDirection::LEFT, moveCount);
    }

    auto TextLineCursor::displayPreviousLine() -> bool
    {
        if (!text->lines->previousLinesStart.empty()) {

            auto [lineStartBlockNumber, lineStartBlockPosition] = text->lines->previousLinesStart.back();
            text->lines->previousLinesStart.pop_back();
            text->lines->drawStartConditions = {lineStartBlockNumber, lineStartBlockPosition};

            text->drawLines();

            auto moveCount = text->lines->first().length();

            // update cursor position on screen so it points in same place after scroll
            onScreenPosition += moveCount;

            return true;
        }

        return false;
    }

    auto TextLineCursor::displayNextLine() -> bool
    {
        if (text->lines->stopCondition != LinesDrawStop::OutOfText) {

            text->lines->addToPreviousLinesStartList(text->lines->first().getLineStartBlockNumber(),
                                                     text->lines->first().getLineStartBlockPosition());

            auto moveCount = text->lines->first().length();

            text->lines->drawStartConditions = {text->lines->getLine(1)->getLineStartBlockNumber(),
                                                text->lines->getLine(1)->getLineStartBlockPosition()};

            text->drawLines();

            // update cursor position on screen so it points in same place after scroll
            onScreenPosition -= moveCount;

            return true;
        }

        return false;
    }

    auto TextLineCursor::moveCursor(gui::NavigationDirection direction) -> gui::TextCursor::Move
    {
        debug_text_cursor("Before move cursor: screen pos: %d block: %d pos: %d %s",
                          onScreenPosition,
                          getBlockNumber(),
                          BlockCursor::getPosition(),
                          atBegin() ? "at begin" : "middle");

        if (!checkDocument()) {
            return Move::Error;
        }

        auto [selectedLine, selectedLineCursorPosition, selectedLineNumber] = getSelectedLine();

        // last line is sometimes not visible (sometimes empty) and added only for scrolling purposes.
        unsigned int lastVisibleLineNumber = text->lines->countVisible() - 1;

        debug_text_cursor(
            "Selected line cursor pos: %d, selected line nr: %d", selectedLineCursorPosition, selectedLineNumber);

        // No lines draw yet to navigate top bottom -> check left/right text navigation
        if (selectedLine == nullptr) {
            return TextCursor::moveCursor(direction);
        }

        /// up - corner case
        if ((checkNpos() || (direction == NavigationDirection::UP && selectedLineNumber == 0))) {

            if (!text->lines->previousLinesStart.empty()) {

                displayPreviousLine();

                return moveCursor(NavigationDirection::UP);
            }
            else {
                return TextCursor::Move::Start;
            }
        }

        /// down - corner case
        if ((checkNpos() || (direction == NavigationDirection::DOWN && selectedLineNumber == lastVisibleLineNumber))) {

            if (text->lines->stopCondition != LinesDrawStop::OutOfText) {

                if (checkNextLineDocumentEnd(selectedLineNumber)) {
                    return TextCursor::Move::End;
                }

                handleDownNavigation(selectedLineNumber, selectedLineCursorPosition);

                return TextCursor::Move::Down;
            }
            else {
                return TextCursor::Move::End;
            }
        }

        /// left - corner case
        if ((checkNpos() || (direction == NavigationDirection::LEFT && selectedLineNumber == 0))) {

            if (!text->lines->previousLinesStart.empty() &&
                (text->lines->first().getLineStartBlockNumber() == getBlockNumber() &&
                 text->lines->first().getLineStartBlockPosition() == BlockCursor::getPosition())) {

                displayPreviousLine();

                return TextCursor::moveCursor(NavigationDirection::LEFT);
            }
            else {
                return TextCursor::moveCursor(direction);
            }
        }

        /// right - corner case
        if ((checkNpos() || (direction == NavigationDirection::RIGHT && selectedLineNumber >= lastVisibleLineNumber))) {

            auto onLastLine = selectedLineNumber == lastVisibleLineNumber;
            auto onLastLineLastCharacter =
                onLastLine &&
                (selectedLineCursorPosition ==
                 (text->lines->getLine(lastVisibleLineNumber)->length() -
                  (text->lines->getLine(lastVisibleLineNumber)->getEnd() == TextBlock::End::Newline ? 1 : 0)));

            if (text->lines->stopCondition != LinesDrawStop::OutOfText &&
                (onLastLineLastCharacter || (selectedLineNumber > lastVisibleLineNumber))) {

                if (onLastLine && checkNextLineDocumentEnd(selectedLineNumber)) {
                    return TextCursor::Move::End;
                }

                auto ret = TextCursor::moveCursor(NavigationDirection::RIGHT);

                displayNextLine();

                return ret;
            }
            else {
                return TextCursor::moveCursor(direction);
            }
        }

        if (direction == NavigationDirection::UP) {

            handleUpNavigation(selectedLineNumber, selectedLineCursorPosition);

            debug_text_cursor("After move cursor: screen pos: %d block: %d pos: %d %s",
                              onScreenPosition,
                              getBlockNumber(),
                              BlockCursor::getPosition(),
                              atBegin() ? "at begin" : "middle");

            return Move::Up;
        }

        if (direction == NavigationDirection::DOWN) {

            if (checkNextLineDocumentEnd(selectedLineNumber)) {
                return TextCursor::Move::End;
            }

            handleDownNavigation(selectedLineNumber, selectedLineCursorPosition);

            debug_text_cursor("After move cursor: screen pos: %d block: %d pos: %d %s",
                              onScreenPosition,
                              getBlockNumber(),
                              BlockCursor::getPosition(),
                              atBegin() ? "at begin" : "middle");

            return Move::Down;
        }

        return TextCursor::moveCursor(direction);
    }

    TextCursor::Move TextLineCursor::moveCursor(NavigationDirection direction, unsigned int n)
    {
        auto ret = TextCursor::Move::Start;

        for (unsigned int i = 0; i < n; i++) {
            ret = TextLineCursor::moveCursor(direction);

            if (ret == Move::Start || ret == Move::End || ret == Move::Error) {
                break;
            }
        }
        return ret;
    }

    bool TextLineCursor::removeChar()
    {
        auto linesSize = text->lines->size();

        auto ret = TextCursor::removeChar();

        // After sign removal check if lines count decreased - if so add previous line,
        if (ret && linesSize > text->lines->size()) {
            displayPreviousLine();
        }

        return ret;
    }

} // namespace gui