~aleteoryx/muditaos

muditaos/module-apps/application-messages/widgets/BaseThreadItem.cpp -rw-r--r-- 5.2 KiB
a405cad6Aleteoryx trim readme 6 days 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
// Copyright (c) 2017-2024, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/blob/master/LICENSE.md

#include "BaseThreadItem.hpp"
#include "MessagesStyle.hpp"

namespace gui
{
    namespace
    {
        constexpr auto NumberImportancePrefix = '#';
    } // namespace

    BaseThreadItem::BaseThreadItem()
    {
        using namespace style;
        setMargins(Margins(0, style::margins::small, 0, style::margins::small));
        setMinimumSize(window::default_body_width, style::messages::threadItem::sms_thread_item_h);
        setMaximumSize(window::default_body_width, style::messages::threadItem::sms_thread_item_h);

        setRadius(0);
        setEdges(RectangleEdge::Bottom | RectangleEdge::Top);

        setPenFocusWidth(window::default_border_focus_w);
        setPenWidth(window::default_border_no_focus_w);

        contact = createEmptyLabel(this);
        contact->setFont(style::window::font::bigbold);
        contact->setTextEllipsisType(TextEllipsis::Right);
        contact->setAlignment(gui::Alignment{gui::Alignment::Horizontal::Left, gui::Alignment::Vertical::Center});

        numberImportance = createEmptyLabel(this);
        numberImportance->setFont(style::window::font::small);
        numberImportance->setAlignment(
            gui::Alignment{gui::Alignment::Horizontal::Left, gui::Alignment::Vertical::Center});

        timestamp = createEmptyLabel(this);
        timestamp->setFont(style::window::font::small);
        timestamp->setTextEllipsisType(TextEllipsis::Right);
        timestamp->setAlignment(gui::Alignment{gui::Alignment::Horizontal::Right, gui::Alignment::Vertical::Center});

        snippetPrefix = createEmptyLabel(this);
        snippetPrefix->setFont(style::window::font::mediumlight);
        snippetPrefix->setAlignment(gui::Alignment{gui::Alignment::Horizontal::Left, gui::Alignment::Vertical::Center});

        snippet = createEmptyLabel(this);
        snippet->setFont(style::window::font::medium);
        snippet->setTextEllipsisType(TextEllipsis::Right);
        snippet->setAlignment(gui::Alignment{gui::Alignment::Horizontal::Left, gui::Alignment::Vertical::Center});
    }

    gui::Label *BaseThreadItem::createEmptyLabel(Item *parent)
    {
        using namespace style;

        auto label = new gui::Label(parent, 0, 0, 0, 0);
        label->setPenFocusWidth(window::default_border_no_focus_w);
        label->setPenWidth(window::default_border_no_focus_w);
        return label;
    }

    void BaseThreadItem::onDimensionChangedTop(const BoundingBox & /*oldDim*/, const BoundingBox &newDim)
    {
        namespace msgStyle = style::messages::threadItem;

        contact->setPosition(msgStyle::leftMargin, msgStyle::topMargin);
        contact->setSize(newDim.w - msgStyle::cotactWidthOffset, newDim.h / 2 - msgStyle::topMargin);

        const auto isNumberImportanceSet = !numberImportance->getText().empty();
        if (isNumberImportanceSet) {
            contact->setSize(contact->getWidth() - msgStyle::numberImportanceWidth, Axis::X);
            numberImportance->setPosition(
                msgStyle::leftMargin + contact->getWidth() + msgStyle::numberImportanceLeftMargin, msgStyle::topMargin);
            numberImportance->setSize(msgStyle::numberImportanceWidth, newDim.h / 2 - msgStyle::topMargin);
        }

        timestamp->setPosition(newDim.w - msgStyle::timestampWidth, msgStyle::topMargin);
        timestamp->setSize(msgStyle::timestampWidth, newDim.h / 2 - msgStyle::topMargin);
    }

    void BaseThreadItem::onDimensionChangedBottom(const BoundingBox & /*oldDim*/, const BoundingBox &newDim)
    {
        resizeSnippet(newDim);
    }

    void BaseThreadItem::resizeSnippet(const BoundingBox &dimensions, unsigned int leftOffset)
    {
        namespace msgStyle = style::messages::threadItem;

        const auto leftMargin = msgStyle::leftMargin + leftOffset;
        if (const auto isPrefixSet = !snippetPrefix->getText().empty(); isPrefixSet) {
            snippetPrefix->setPosition(leftMargin, dimensions.h / 2);
            snippetPrefix->setSize(snippetPrefix->getTextNeedSpace(), dimensions.h / 2 - msgStyle::bottomMargin);

            const auto prefixSpace = snippetPrefix->getWidth() + msgStyle::snippetLeftMargin;
            snippet->setPosition(leftMargin + prefixSpace, dimensions.h / 2);
            snippet->setSize(dimensions.w - msgStyle::previewWidthOffset - prefixSpace - leftOffset,
                             dimensions.h / 2 - msgStyle::bottomMargin);
        }
        else {
            snippetPrefix->setPosition(0, 0);
            snippetPrefix->setSize(0, 0);
            snippet->setPosition(leftMargin, dimensions.h / 2);
            snippet->setSize(dimensions.w - msgStyle::previewWidthOffset - leftOffset,
                             dimensions.h / 2 - msgStyle::bottomMargin);
        }
    }

    bool BaseThreadItem::onDimensionChanged(const BoundingBox &oldDim, const BoundingBox &newDim)
    {
        onDimensionChangedTop(oldDim, newDim);
        onDimensionChangedBottom(oldDim, newDim);
        return true;
    }

    void BaseThreadItem::displayNumberImportance(long int id)
    {
        numberImportance->setText(NumberImportancePrefix + std::to_string(id));
    }
} // namespace gui