summary refs log tree commit diff
path: root/src/ui/SnackBar.cpp
blob: 2453369d26f1b71e4af91cb5de08acab20172928 (plain) (blame)
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
// SPDX-FileCopyrightText: 2021 Nheko Contributors
//
// SPDX-License-Identifier: GPL-3.0-or-later

#include <QPainter>

#include "SnackBar.h"

constexpr int STARTING_OFFSET         = 1;
constexpr int ANIMATION_DURATION      = 6'000;
constexpr int BOX_PADDING             = 10;
constexpr double MIN_WIDTH            = 400.0;
constexpr double MIN_WIDTH_PERCENTAGE = 0.3;

SnackBar::SnackBar(QWidget *parent)
  : OverlayWidget(parent)
  , offset_anim(this, "offset", this)
{
        QFont font;
        font.setPointSizeF(font.pointSizeF() * 1.2);
        font.setWeight(50);
        setFont(font);

        boxHeight_ = QFontMetrics(font).height() * 2;
        offset_    = STARTING_OFFSET;
        position_  = SnackBarPosition::Top;

        hideTimer_.setSingleShot(true);

        offset_anim.setStartValue(1.0);
        offset_anim.setEndValue(0.0);
        offset_anim.setDuration(100);
        offset_anim.setEasingCurve(QEasingCurve::OutCubic);

        connect(this, &SnackBar::offsetChanged, this, [this]() mutable {
                        repaint();
        });
        connect(
          &offset_anim, &QPropertyAnimation::finished, this, [this]() { hideTimer_.start(10000); });

        connect(&hideTimer_, SIGNAL(timeout()), this, SLOT(hideMessage()));

        hide();
}

void
SnackBar::start()
{
        if (messages_.empty())
                return;

        show();
        raise();

        offset_anim.start();
}

void
SnackBar::hideMessage()
{
        stopTimers();
        hide();

        if (!messages_.empty())
                // Moving on to the next message.
                messages_.pop_front();

        // Resetting the starting position of the widget.
        offset_ = STARTING_OFFSET;

        if (!messages_.empty())
                start();
}

void
SnackBar::stopTimers()
{
        hideTimer_.stop();
}

void
SnackBar::showMessage(const QString &msg)
{
        messages_.push_back(msg);

        // There is already an active message.
        if (isVisible())
                return;

        start();
}

void
SnackBar::mousePressEvent(QMouseEvent *)
{
        hideMessage();
}

void
SnackBar::paintEvent(QPaintEvent *event)
{
        Q_UNUSED(event)

        if (messages_.empty())
                return;

        auto message_ = messages_.front();

        QPainter p(this);
        p.setRenderHint(QPainter::Antialiasing);

        QBrush brush;
        brush.setStyle(Qt::SolidPattern);
        brush.setColor(bgColor_);
        p.setBrush(brush);

        QRect r(0, 0, std::max(MIN_WIDTH, width() * MIN_WIDTH_PERCENTAGE), boxHeight_);

        p.setPen(Qt::white);
        QRect br = p.boundingRect(r, Qt::AlignHCenter | Qt::AlignTop | Qt::TextWordWrap, message_);

        p.setPen(Qt::NoPen);
        r = br.united(r).adjusted(-BOX_PADDING, -BOX_PADDING, BOX_PADDING, BOX_PADDING);

        const qreal s = 1 - offset_;

        if (position_ == SnackBarPosition::Bottom)
                p.translate((width() - (r.width() - 2 * BOX_PADDING)) / 2,
                            height() - BOX_PADDING - s * (r.height()));
        else
                p.translate((width() - (r.width() - 2 * BOX_PADDING)) / 2,
                            s * (r.height()) - 2 * BOX_PADDING);

        br.moveCenter(r.center());
        p.drawRoundedRect(r.adjusted(0, 0, 0, 4), 4, 4);
        p.setPen(textColor_);
        p.drawText(br, Qt::AlignHCenter | Qt::AlignTop | Qt::TextWordWrap, message_);
}