~aleteoryx/muditaos

ref: b95894a8abc10cddf0b58dda4b2569b01eec0d7e muditaos/module-apps/application-notes/windows/NoteEditWindow.cpp -rw-r--r-- 7.5 KiB
b95894a8 — Lefucjusz [MOS-1064] Fix no input language selected for French/Spanish 1 year, 9 months 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
// Copyright (c) 2017-2023, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md

#include "NoteEditWindow.hpp"
#include "DialogMetadataMessage.hpp"

#include <sstream>

#include <Style.hpp>

#include <application-notes/ApplicationNotes.hpp>
#include <module-apps/application-notes/windows/NotesOptions.hpp>
#include <module-apps/application-notes/data/NoteSwitchData.hpp>
#include <module-apps/application-notes/style/NoteEditStyle.hpp>
#include <apps-common/messages/OptionsWindow.hpp>
#include <apps-common/options/OptionWindowName.hpp>

#include <i18n/i18n.hpp>

#include <ctime>

#include <Clipboard.hpp>

namespace app::notes
{
    namespace
    {
        constexpr auto MaxCharactersCount = 4000U;
    } // namespace

    NoteEditWindow::NoteEditWindow(app::ApplicationCommon *app,
                                   std::unique_ptr<NoteEditWindowContract::Presenter> &&windowPresenter)
        : NoteEditWindow(app, std::move(windowPresenter), gui::name::window::note_edit)
    {}

    NoteEditWindow::NoteEditWindow(app::ApplicationCommon *app,
                                   std::unique_ptr<NoteEditWindowContract::Presenter> &&windowPresenter,
                                   const std::string &windowName)
        : gui::AppWindow{app, windowName}, presenter{std::move(windowPresenter)}
    {
        presenter->attach(this);
        buildInterface();
    }

    NoteEditWindow::~NoteEditWindow() noexcept
    {
        destroyInterface();
    }

    void NoteEditWindow::rebuild()
    {
        destroyInterface();
        buildInterface();
    }

    void NoteEditWindow::buildInterface()
    {
        AppWindow::buildInterface();

        namespace editStyle = app::notes::style::edit;
        charactersCounter   = new gui::Label(
            this, editStyle::LeftMargin, editStyle::TopMargin, editStyle::Width, editStyle::counter::Height);
        charactersCounter->setEdges(gui::RectangleEdge::None);
        charactersCounter->setFont(::style::window::font::verysmall);
        charactersCounter->setAlignment(gui::Alignment{gui::Alignment::Horizontal::Right});

        edit = new gui::Text(this,
                             editStyle::LeftMargin,
                             editStyle::TopMargin + editStyle::counter::Height + editStyle::text::TopMargin,
                             editStyle::Width,
                             editStyle::text::Height);
        edit->setAlignment(gui::Alignment{gui::Alignment::Vertical::Top});
        edit->setPenFocusWidth(::style::window::default_border_focus_w);
        edit->setPenWidth(::style::window::default_border_rect_no_focus);
        edit->setEdges(gui::RectangleEdge::None);
        edit->setFont(::style::window::font::medium);
        edit->setInputMode(new InputMode(
            {InputMode::Abc, InputMode::ABC, InputMode::abc, InputMode::digit},
            [=](const UTF8 &text) { navBarTemporaryMode(text); },
            [=]() { navBarRestoreFromTemporaryMode(); },
            [=]() { selectSpecialCharacter(); },
            [=](std::function<void()> restoreFunction) {
                application->getCurrentWindow()->startInputModeRestoreTimer(std::move(restoreFunction));
            }));
        edit->setTextChangedCallback([this](Item &, const UTF8 &text) {
            const auto textLength        = text.length();
            optionsLabelEnabled          = (textLength != 0) || Clipboard::getInstance().hasData();
            navBar->setActive(gui::nav_bar::Side::Left, optionsLabelEnabled);

            setCharactersCount(textLength);
            onCharactersCountChanged(textLength);
        });
        edit->setTextLimitType(gui::TextLimitType::MaxSignsCount, MaxCharactersCount);

        navBar->setActive(gui::nav_bar::Side::Left, true);
        navBar->setText(gui::nav_bar::Side::Left, utils::translate(::style::strings::common::options));

        navBar->setActive(gui::nav_bar::Side::Center, true);
        navBar->setText(gui::nav_bar::Side::Center, utils::translate(::style::strings::common::save));

        navBar->setActive(gui::nav_bar::Side::Right, true);
        navBar->setText(gui::nav_bar::Side::Right, utils::translate(::style::strings::common::back));

        setFocusItem(edit);
    }

    void NoteEditWindow::setCharactersCount(std::uint32_t count)
    {
        std::ostringstream counterText;
        counterText << count << '/' << MaxCharactersCount;
        charactersCounter->setText(counterText.str());
    }

    void NoteEditWindow::onCharactersCountChanged([[maybe_unused]] std::uint32_t count)
    {
        presenter->onNoteChanged();
    }

    void NoteEditWindow::destroyInterface()
    {
        erase();
        charactersCounter = nullptr;
        edit              = nullptr;
    }

    void NoteEditWindow::onBeforeShow([[maybe_unused]] gui::ShowMode mode, gui::SwitchData *data)
    {
        const auto editData = dynamic_cast<NoteSwitchData *>(data);
        if (editData == nullptr) {
            return;
        }

        notesRecord = editData->getRecord();
        setNoteText(notesRecord->snippet);

        setTitle(isNoteEmpty() ? utils::translate("app_notes_new_note") : utils::translate("app_notes_edit_note"));
    }

    void NoteEditWindow::setNoteText(const UTF8 &text)
    {
        edit->setText(text);
    }

    bool NoteEditWindow::onInput(const gui::InputEvent &inputEvent)
    {
        if (inputEvent.isShortRelease()) {
            if (inputEvent.is(gui::KeyCode::KEY_ENTER) && !edit->getText().empty()) {
                saveNote();
                auto switchData                        = std::make_unique<NoteSwitchData>(notesRecord);
                switchData->ignoreCurrentWindowOnStack = true;
                application->switchWindow(gui::name::window::note_preview, std::move(switchData));
            }
            if (inputEvent.is(gui::KeyCode::KEY_LF) && optionsLabelEnabled) {
                application->switchWindow(
                    window::name::option_window,
                    std::make_unique<gui::OptionsWindowOptions>(noteEditOptions(application, edit)));
            }
        }
        if (isAnyUnsavedUserDataInWindow() &&
            (inputEvent.isShortRelease(gui::KeyCode::KEY_RF) || inputEvent.isLongRelease(gui::KeyCode::KEY_RF))) {

            // Show a popup warning about possible data loss
            auto metaData = std::make_unique<gui::DialogMetadataMessage>(
                gui::DialogMetadata{utils::translate("unsaved_changes"),
                                    "delete_128px_W_G",
                                    utils::translate("exit_without_saving"),
                                    "",
                                    [=]() -> bool {
                                        application->returnToPreviousWindow(); // To exit this popup
                                        application->returnToPreviousWindow(); // To switch back to previous window
                                        return true;
                                    }});

            application->switchWindow(gui::name::window::notes_dialog_yes_no, std::move(metaData));
            return true;
        }
        return AppWindow::onInput(inputEvent);
    }

    void NoteEditWindow::saveNote()
    {
        notesRecord->date    = std::time(nullptr);
        notesRecord->snippet = edit->getText();
        presenter->save(notesRecord);
    }

    bool NoteEditWindow::isNoteEmpty() const noexcept
    {
        return (edit != nullptr) ? edit->isEmpty() : true;
    }

    bool NoteEditWindow::isAnyUnsavedUserDataInWindow() const
    {
        return notesRecord->snippet != edit->getText();
    }
} // namespace app::notes