20QSize outerSize(
const QxToast *toast,
int plate)
22 const int width = plate + 2 * QxToast::ShadowMargin;
23 return QSize(width, toast->heightForWidth(width));
28QxToastManagerPrivate::QxToastManagerPrivate() =
default;
30void QxToastManagerPrivate::adopt(
QxToast *toast)
38 QObject::connect(toast, &QxToast::dismissRequested, q, [
this, toast]() {
41 QObject::connect(toast, &QxToast::dismissed, toast, &QObject::deleteLater);
42 QObject::connect(toast, &QxToast::sizeHintChanged, q, [
this]() {
46 m_toasts.append(toast);
49void QxToastManagerPrivate::remove(
QxToast *toast)
53 const int index = m_toasts.indexOf(toast);
60 m_toasts.removeAt(index);
62 Q_EMIT q->toastDismissed(toast);
69void QxToastManagerPrivate::relayout()
75 const int shadow = QxToast::ShadowMargin;
79 const int gap = qMax(0, m_spacing - 2 * shadow);
80 const QRect area = m_host->rect();
81 const bool atTop = m_position == QxToastManager::TopLeft || m_position == QxToastManager::TopRight;
82 const bool atLeft = m_position == QxToastManager::TopLeft || m_position == QxToastManager::BottomLeft;
84 const int plate = plateWidth();
89 for (
int i = 0; i < m_toasts.count(); ++i) {
90 stackHeight += outerSize(m_toasts.at(i), plate).height() - 2 * shadow;
92 stackHeight += qMax(0, m_toasts.count() - 1) * m_spacing;
94 int top = atTop ? area.top() + m_margin : area.bottom() - m_margin - stackHeight + 1;
96 for (
int i = 0; i < m_toasts.count(); ++i) {
97 QxToast *toast = m_toasts.at(i);
98 const QSize outer = outerSize(toast, plate);
99 const int left = atLeft ? area.left() + m_margin : area.right() - m_margin - plate + 1;
100 toast->setGeometry(left - shadow, top - shadow, outer.width(), outer.height());
101 top += outer.height() + gap;
105int QxToastManagerPrivate::plateWidth()
const
110 const int available = qMax(1, m_host->width() - 2 * m_margin);
111 return qMin(m_maxWidth, available);
114QxToastManager::QxToastManager(QWidget *host, QObject *parent)
123 host->installEventFilter(
this);
141 return d->m_maxVisible;
147 d->m_maxVisible = qMax(1,
count);
149 while (d->m_toasts.count() > d->m_maxVisible) {
150 d->remove(d->m_toasts.first());
158 return d->m_position;
180 d->m_margin = qMax(0, pixels);
193 d->m_spacing = qMax(0, pixels);
200 return d->m_maxWidth;
216 return d->m_fadeDuration;
222 d->m_fadeDuration = qMax(0, ms);
223 for (
QxToast *toast : d->m_toasts) {
224 toast->setFadeDuration(d->m_fadeDuration);
231 return d->m_toasts.count();
243 if (index < 0 || index >= d->m_toasts.count()) {
246 return d->m_toasts.at(index);
252 return d->m_toasts.contains(toast);
259 qWarning(
"QxToastManager: there is no host window to show a toast in");
266 while (!d->m_toasts.isEmpty() && d->m_toasts.count() >= d->m_maxVisible) {
267 d->remove(d->m_toasts.first());
285 const QList<QxToast *>
toasts = d->m_toasts;
294 if (watched == d->m_host && event->type() == QEvent::Resize) {
297 return QObject::eventFilter(watched, event);
The state behind QxToastManager: the host, the stack and the placement rules.
The stack of toasts of one window.
Position position() const
void setSpacing(int pixels)
void setMargin(int pixels)
int maxWidth() const
Widest a plate may get; a narrow host shrinks it further.
~QxToastManager() override
void setMaxVisible(int count)
QList< QxToast * > toasts() const
The toasts that are on screen, oldest first - the order they stack in.
void setPosition(Position position)
void setFadeDuration(int ms)
Position
The corner of the host the stack is anchored to.
int margin() const
Distance kept between the plate and the two edges it is anchored to.
int fadeDuration() const
Milliseconds the fade in and the fade out take; zero turns them off.
void dismissAll()
Dismisses every toast of the stack, oldest first.
void setMaxWidth(int pixels)
bool contains(QxToast *toast) const
void toastShown(QxToast *toast)
A toast has joined the stack and is on its way in.
int spacing() const
Distance between two plates of the stack.
QxToast * show(const QString &text, QxToast::Level level=QxToast::Information, int timeoutMs=QxToast::DefaultTimeout)
Creates a toast, puts it at the end of the stack and starts it.
QxToast * toastAt(int index) const
The toast at index of the stack, null when it is out of range.
int maxVisible() const
How many toasts share the screen before the oldest one is evicted.
bool eventFilter(QObject *watched, QEvent *event) override
One short message, shown over the window it belongs to and gone a few seconds later.
Level
What the message is about; it selects the icon and the accent colour.
void dismiss()
Takes the toast off screen: the countdown stops, the fade out runs and dismissed() follows once the w...
void setFadeDuration(int ms)
Zero turns the animation off, which makes the life cycle immediate.
static constexpr int MaxPlateWidth
void start()
Puts the toast on screen, fades it in and starts the countdown.
static constexpr int MinPlateWidth
The narrowest and the widest plate a toast settles for, whatever width it is given.
void setTimeout(int timeoutMs)
#define QX_APP_END_NAMESPACE
#define Q_NULLPTR
Copyleft (C) 2025 maminjie canpool@163.com SPDX-License-Identifier: MulanPSL-2.0
#define QX_APP_BEGIN_NAMESPACE
#define QX_FINI_PRIVATE()