lib_ui/ui/toast/toast.cpp
2020-04-29 18:52:18 +04:00

86 lines
1.8 KiB
C++

// This file is part of Desktop App Toolkit,
// a set of libraries for developing nice desktop applications.
//
// For license and copyright information please follow this link:
// https://github.com/desktop-app/legal/blob/master/LEGAL
//
#include "ui/toast/toast.h"
#include "ui/toast/toast_manager.h"
#include "ui/toast/toast_widget.h"
#include "styles/style_widgets.h"
namespace Ui {
namespace Toast {
namespace {
QPointer<QWidget> DefaultParent;
} // namespace
Instance::Instance(
const Config &config,
not_null<QWidget*> widgetParent,
const Private &)
: _st(config.st)
, _hideAtMs(crl::now() + config.durationMs) {
_widget = std::make_unique<internal::Widget>(widgetParent, config);
_a_opacity.start(
[=] { opacityAnimationCallback(); },
0.,
1.,
_st->durationFadeIn);
}
void SetDefaultParent(not_null<QWidget*> parent) {
DefaultParent = parent;
}
void Show(not_null<QWidget*> parent, const Config &config) {
const auto manager = internal::Manager::instance(parent);
manager->addToast(std::make_unique<Instance>(
config,
parent,
Instance::Private()));
}
void Show(const Config &config) {
if (const auto parent = DefaultParent.data()) {
Show(parent, config);
}
}
void Show(not_null<QWidget*> parent, const QString &text) {
Show(parent, Config{ .text = { text }, .st = &st::defaultToast });
}
void Show(const QString &text) {
Show(Config{ .text = { text }, .st = &st::defaultToast });
}
void Instance::opacityAnimationCallback() {
_widget->setShownLevel(_a_opacity.value(_hiding ? 0. : 1.));
_widget->update();
if (!_a_opacity.animating()) {
if (_hiding) {
hide();
}
}
}
void Instance::hideAnimated() {
_hiding = true;
_a_opacity.start(
[=] { opacityAnimationCallback(); },
1.,
0.,
_st->durationFadeOut);
}
void Instance::hide() {
_widget->hide();
_widget->deleteLater();
}
} // namespace Toast
} // namespace Ui