~aleteoryx/muditaos

ref: 2695cc830472190ce1401869bb67f5506a3e17e6 muditaos/module-apps/application-notes/windows/NotesOptions.cpp -rw-r--r-- 4.0 KiB
2695cc83 — piotrleniec-mudita [DW-31] Add commit message format checking on CI (#1201) 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
// Copyright (c) 2017-2020, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md

#include "NotesOptions.hpp"

#include <Options.hpp>
#include <common_data/Clipboard.hpp>

#include <module-apps/windows/DialogMetadata.hpp>
#include <module-apps/messages/DialogMetadataMessage.hpp>

#include <module-utils/i18n/i18n.hpp>

#include <module-gui/gui/widgets/Text.hpp>

namespace app::notes
{
    namespace
    {
        void addOption(const std::string &translationId,
                       std::function<bool(gui::Item &)> onClickCallback,
                       std::list<gui::Option> &options)
        {
            options.emplace_back(utils::localize.get(translationId), onClickCallback);
        }

        void removeNote(const NotesRecord &record, Application *application, AbstractNotesRepository &notesRepository)
        {
            gui::DialogMetadata meta;
            meta.action = [record, application, &notesRepository] {
                notesRepository.remove(
                    record, [application](bool) { application->switchWindow(gui::name::window::main_window); });
                return true;
            };
            meta.text = utils::localize.get("app_notes_note_delete_confirmation");
            meta.icon = "phonebook_contact_delete_trashcan";
            application->switchWindow(gui::name::window::note_confirm_dialog,
                                      std::make_unique<gui::DialogMetadataMessage>(meta));
        }
    } // namespace

    std::list<gui::Option> noteListOptions(Application *application,
                                           const NotesRecord &record,
                                           AbstractNotesRepository &notesRepository)
    {
        std::list<gui::Option> options;
        addOption(
            {"app_notes_delete_note"},
            [application, record, &notesRepository](gui::Item &item) {
                removeNote(record, application, notesRepository);
                return true;
            },
            options);
        return options;
    }

    std::list<gui::Option> notePreviewOptions(Application *application,
                                              const NotesRecord &record,
                                              AbstractNotesRepository &notesRepository,
                                              gui::Text *textWidget)
    {
        std::list<gui::Option> options;
        addOption(
            {"app_notes_copy_text"},
            [application, textWidget](gui::Item &item) {
                if (textWidget != nullptr) {
                    Clipboard::getInstance().copy(textWidget->getText());
                }
                application->returnToPreviousWindow();
                return true;
            },
            options);
        addOption(
            {"app_notes_delete_note"},
            [application, record, &notesRepository](gui::Item &item) {
                removeNote(record, application, notesRepository);
                return true;
            },
            options);
        return options;
    }

    std::list<gui::Option> noteEditOptions(Application *application, const NotesRecord &record, gui::Text *textWidget)
    {
        std::list<gui::Option> options;
        addOption(
            {"app_notes_copy_text"},
            [application, textWidget](gui::Item &item) {
                if (textWidget != nullptr) {
                    Clipboard::getInstance().copy(textWidget->getText());
                }
                application->returnToPreviousWindow();
                return true;
            },
            options);
        addOption(
            {"app_notes_copy_paste"},
            [application, textWidget](gui::Item &item) {
                if (textWidget != nullptr) {
                    textWidget->addText(Clipboard::getInstance().paste());
                }
                application->returnToPreviousWindow();
                return true;
            },
            options);
        return options;
    }
} // namespace app::notes