~aleteoryx/muditaos

ref: 9f758505d0a650417b91b62a2f6224b8c5bdfba2 muditaos/module-apps/apps-common/notifications/NotificationListItem.cpp -rw-r--r-- 5.2 KiB
9f758505 — Lukasz Skrzypczak [EGD-7204] Revert of msp register and clear IRQ flags 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
// Copyright (c) 2017-2021, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md

#include "NotificationListItem.hpp"

#include "TextFixedSize.hpp"
#include "RichTextParser.hpp"
#include "FontManager.hpp"
#include "Image.hpp"

#include <widgets/Style.hpp>
#include <map>

using namespace gui;

namespace
{
    auto buildImageInactive(const UTF8 &img) -> gui::Image *
    {
        auto thumbnail        = new gui::Image(img);
        thumbnail->activeItem = false;
        return thumbnail;
    }

    auto buildNotificationIcon(const UTF8 &icon) -> gui::Image *
    {
        auto thumbnail = buildImageInactive(icon);
        thumbnail->setMinimumWidth(style::notifications::iconWidth);
        thumbnail->setAlignment(Alignment(gui::Alignment::Horizontal::Right, gui::Alignment::Vertical::Center));
        thumbnail->setMargins(
            gui::Margins(style::window::default_left_margin, 0, style::window::default_right_margin, 0));
        return thumbnail;
    }

    auto buildNotificationNameLabel(uint32_t width) -> gui::TextFixedSize *
    {
        auto text =
            new gui::TextFixedSize(nullptr, 0, 0, style::notifications::textMinWidth, style::notifications::itemHeight);

        text->setMaximumSize(style::notifications::textMaxWidth, Axis::X);
        text->setAlignment(Alignment(gui::Alignment::Horizontal::Left, gui::Alignment::Vertical::Center));
        text->setPenWidth(style::window::default_border_no_focus_w);
        text->setUnderline(false);
        text->setFont(style::window::font::medium);
        text->activeItem = false;
        text->setTextLimitType(TextLimitType::MaxLines, 1);
        return text;
    }

    void setNotificationHboxLayoutProperties(gui::HBox *hbox)
    {
        hbox->setAlignment(Alignment(gui::Alignment::Vertical::Center));
        hbox->setMargins(gui::Margins(0, 0, 0, 10));
        hbox->setPenWidth(style::window::default_border_no_focus_w);
        hbox->setPenFocusWidth(style::window::default_border_focus_w);
        hbox->setEdges(RectangleEdge::None);
        hbox->setMaximumSize(style::window::default_body_width, style::notifications::itemHeight);
    }

    constexpr auto maxNotificationValue = "99+";
    constexpr auto singleNotification   = "1";

    auto buildNotificationCountText(const UTF8 &indicator) -> gui::Text *
    {
        auto number = new gui::Text();
        if (indicator.length() > 2) {
            number->setText(maxNotificationValue);
        }
        else if (indicator == singleNotification) {
            number->clear();
        }
        else {
            number->setText(indicator);
        }

        number->setMinimumWidth(number->getText().length() * style::notifications::digitSize);
        number->setFont(style::window::font::mediumbold);
        number->setPenWidth(style::window::default_border_no_focus_w);
        number->setMargins(gui::Margins(0, 0, style::window::default_right_margin, 0));
        number->setAlignment(Alignment(gui::Alignment::Horizontal::Right, gui::Alignment::Vertical::Center));
        number->activeItem = false;
        return number;
    }

    std::map<notifications::NotificationType, UTF8> typeToIcon{
        {notifications::NotificationType::NotSeenSms, "messages_notification_icon"},
        {notifications::NotificationType::NotSeenCall, "calls_notification_icon"},
        {notifications::NotificationType::Tethering, "tethering_notification_icon"}};
} // namespace

NotificationListItem::NotificationListItem(NotificationType type) : type{type}
{
    this->setMinimumHeight(style::notifications::itemHeight);
    this->setMaximumWidth(style::window::default_body_width);

    box = new gui::HBox(this, 0, 0, style::window::default_body_width, style::notifications::itemHeight);
    box->addWidget(buildNotificationIcon(typeToIcon[type]));
    text = buildNotificationNameLabel(box->area().w);
    box->addWidget(text);

    setNotificationHboxLayoutProperties(box);
}

void NotificationListItem::setName(const UTF8 &name, bool isRich, gui::text::RichTextParser::TokenMap &&tokens)
{
    if (isRich) {
        TextFormat format(FontManager::getInstance().getFont(style::window::font::medium));
        text::RichTextParser rtParser;
        auto parsedText = rtParser.parse(name, &format, std::move(tokens));
        text->setText(std::move(parsedText));
    }
    else {
        text->setText(name);
    }
}

void NotificationListItem::setDismissible(bool isDismissible) noexcept
{
    dismissible = isDismissible;
}

bool NotificationListItem::isDismissible() const noexcept
{
    return dismissible;
}

notifications::NotificationType NotificationListItem::getType() const noexcept
{
    return type;
}

NotificationWithEventCounter::NotificationWithEventCounter(notifications::NotificationType type, const UTF8 &indicator)
    : NotificationListItem(type)
{
    box->addWidget(buildImageInactive("dot_12px_hard_alpha_W_G"));
    box->addWidget(buildNotificationCountText(indicator));
    text->setMaximumSize(text->getSize(Axis::X), Axis::X);
}

NotificationWithOnOffButton::NotificationWithOnOffButton(notifications::NotificationType type, gui::ButtonState state)
    : NotificationListItem(type)
{
    auto button = new ButtonOnOff(nullptr, state);
    button->setMargins(Margins(0, 0, 20, 0));
    box->addWidget(button);
}