~aleteoryx/muditaos

ref: b4814861b02051c9b402627577eec80aeb258830 muditaos/module-gui/gui/widgets/Text.hpp -rw-r--r-- 6.1 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
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
// Copyright (c) 2017-2020, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md

#pragma once

#include <memory>
#include <vector>

#include "Alignment.hpp"
#include "InputEvent.hpp"
#include "TextConstants.hpp"
#include "utf8/UTF8.hpp"

#include "BoxLayout.hpp"
#include "InputMode.hpp"
#include "Label.hpp"
#include "Rect.hpp"
#include "Style.hpp"
#include "Lines.hpp"
#include "TextCursor.hpp"
#include "TextDocument.hpp"
#include "TextLine.hpp"
#include "Translator.hpp"
#include "TextLineCursor.hpp"

namespace gui
{
    struct TextBackup
    {
        TextDocument document;
        unsigned int cursorPos;
    };

    class Lines;

    ///  @brief Widget that holds multiple lines of text.
    ///
    ///  Can expand horizontally to it's max size if it needs to fit more text in line
    ///  Can expand vertically if needed to hold lines of text.
    ///  Handles input
    ///        1. Provides text navigation
    ///        2. Provides new letter input with gui::KeyInputMappedTranslation
    ///        3. handles deletion of character
    ///
    ///   This widget stores both: whole text provided to it, and text visible on the screen
    ///   screen visible on the screen is a set of labels
    ///
    class Text : public Rect
    {
        friend TextCursor;
        friend Lines;

      protected:
        // holds list of labels for displaying currently visible text lines.

        TextLineCursor *cursor                 = nullptr;
        std::unique_ptr<TextDocument> document = std::make_unique<TextDocument>(std::list<TextBlock>());
        InputMode *mode                        = nullptr;
        std::unique_ptr<Lines> lines           = nullptr;

        void buildDocument(const UTF8 &text);
        void buildDocument(std::unique_ptr<TextDocument> &&document);
        void buildCursor();
        /// show cursor if cursor should be visible
        void showCursor(bool focus);

      public:
        ExpandMode expandMode    = ExpandMode::EXPAND_NONE;
        EditMode editMode        = EditMode::EDIT;
        KeyCode key_signs_remove = KeyCode::KEY_PND;

        [[nodiscard]] bool isMode(EditMode edit) const
        {
            return editMode == edit;
        }

      protected:
        TextType textType = TextType::MULTI_LINE;
        /// points to default text font to use
        bool underline = false;
        TextFormat format;

        auto moveCursor(const NavigationDirection &direction, std::unique_ptr<TextDocument> &document) -> bool;
        auto handleNavigation(const InputEvent &inputEvent) -> bool;
        auto handleEnter() -> bool;

        void preBuildDrawListHookImplementation(std::list<Command> &commands);
        /// redrawing lines
        /// it redraws visible lines on screen and if needed requests resize in parent
        virtual void drawLines();
        /// redrawing cursor
        void drawCursor();

      public:
        /// Callback when text changed
        /// @param `this` item
        /// @param new text
        using TextChangedCallback = std::function<void(Item &, const UTF8 &)>;

        Text();
        Text(Item *parent,
             const uint32_t &x,
             const uint32_t &y,
             const uint32_t &w,
             const uint32_t &h,
             const UTF8 &text      = "",
             ExpandMode expandMode = ExpandMode::EXPAND_NONE,
             TextType textType     = TextType::MULTI_LINE);
        ~Text() override;

        void setEditMode(EditMode mode);
        void setTextType(TextType type);
        void setUnderline(const bool val);
        virtual void setText(const UTF8 &text);
        void setText(std::unique_ptr<TextDocument> &&document);

        TextBackup backupText() const;
        void restoreFrom(const TextBackup &backup);

        void setTextChangedCallback(TextChangedCallback &&callback);

        void addText(const UTF8 &text);
        void addText(TextBlock text);
        /// @defgroup richtext can be virtualized by parametrized RichTextParser virtual api ( as second param )
        /// @{
        /// set rich text with default RichTextParser - please see RichTextParser documentation on how to use format
        void setRichText(const UTF8 &text);
        /// add rich text with default RichTextParser - please see RichTextParser documentation on how to use format
        void addRichText(const UTF8 &text);
        /// @}
        virtual void clear();
        bool isEmpty();
        virtual UTF8 getText();
        /// saves text from widget to file at specified path
        virtual bool saveText(UTF8 path);
        void setFont(const UTF8 &fontName);
        void setFont(RawFont *font);

        // virtual methods from Item
        bool onInput(const InputEvent &inputEvent) override;
        /// move ownership of mode ptr to Text
        void setInputMode(InputMode *&&mode)
        {
            if (this->mode != nullptr) {
                delete this->mode;
            }
            this->mode = mode;
        };
        bool onFocus(bool state) override;
        bool onDimensionChanged(const BoundingBox &oldDim, const BoundingBox &newDim) override;
        void setRadius(int value) override;
        void setAlignment(const Alignment &value) override;
        void setPadding(const Padding &value) override;
        [[nodiscard]] auto getTextFormat() const noexcept -> const TextFormat &
        {
            return format;
        }

      private:
        gui::KeyInputMappedTranslation translator;

      public:
        TextChangedCallback textChangedCallback;

        bool handleRotateInputMode(const InputEvent &inputEvent);
        bool handleRestoreInputModeUI(const InputEvent &inputEvent);
        bool handleSelectSpecialChar(const InputEvent &inputEvent);
        bool handleActivation(const InputEvent &inputEvent);
        bool handleBackspace(const InputEvent &inputEvent);
        bool handleDigitLongPress(const InputEvent &inputEvent);
        bool handleAddChar(const InputEvent &inputEvent);

        bool addChar(uint32_t utf8);
        bool removeChar();
        InputBound processBound(InputBound bound, const InputEvent &event);
        void onTextChanged();
    };

    char intToAscii(int val);

} /* namespace gui */