nheko/src/ui/SnackBar.h

93 lines
2.0 KiB
C
Raw Normal View History

2021-03-05 00:35:15 +01:00
// SPDX-FileCopyrightText: 2021 Nheko Contributors
//
// SPDX-License-Identifier: GPL-3.0-or-later
2017-10-08 15:49:56 +02:00
#pragma once
#include <QCoreApplication>
#include <QPaintEvent>
2021-03-05 14:04:11 +01:00
#include <QPropertyAnimation>
2017-10-08 15:49:56 +02:00
#include <QTimer>
#include <deque>
2017-10-08 15:49:56 +02:00
#include "OverlayWidget.h"
2017-10-15 21:08:51 +02:00
enum class SnackBarPosition
{
2017-10-08 15:49:56 +02:00
Bottom,
Top,
};
class SnackBar : public OverlayWidget
{
Q_OBJECT
2018-07-25 22:07:56 +02:00
Q_PROPERTY(QColor bgColor READ backgroundColor WRITE setBackgroundColor)
Q_PROPERTY(QColor textColor READ textColor WRITE setTextColor)
2021-03-05 14:04:11 +01:00
Q_PROPERTY(double offset READ offset WRITE setOffset NOTIFY offsetChanged)
2018-07-25 22:07:56 +02:00
2017-10-08 15:49:56 +02:00
public:
explicit SnackBar(QWidget *parent);
2018-07-25 22:07:56 +02:00
QColor backgroundColor() const { return bgColor_; }
void setBackgroundColor(const QColor &color)
{
bgColor_ = color;
update();
}
QColor textColor() const { return textColor_; }
void setTextColor(const QColor &color)
{
textColor_ = color;
update();
}
void setPosition(SnackBarPosition pos)
{
position_ = pos;
update();
}
2017-10-08 15:49:56 +02:00
2021-03-05 14:04:11 +01:00
double offset() { return offset_; }
void setOffset(double offset)
{
if (offset != offset_) {
offset_ = offset;
emit offsetChanged();
}
}
2017-10-08 15:49:56 +02:00
public slots:
void showMessage(const QString &msg);
2021-03-05 14:04:11 +01:00
signals:
void offsetChanged();
2017-10-08 15:49:56 +02:00
protected:
void paintEvent(QPaintEvent *event) override;
void mousePressEvent(QMouseEvent *event) override;
private slots:
void hideMessage();
private:
void stopTimers();
void start();
QColor bgColor_;
QColor textColor_;
qreal bgOpacity_;
qreal offset_;
std::deque<QString> messages_;
2017-10-08 15:49:56 +02:00
QTimer hideTimer_;
2017-10-08 15:49:56 +02:00
2018-07-25 22:07:56 +02:00
double boxHeight_;
2017-10-08 15:49:56 +02:00
2021-03-05 14:04:11 +01:00
QPropertyAnimation offset_anim;
2017-10-08 15:49:56 +02:00
SnackBarPosition position_;
};