2014-05-30 08:53:19 +00:00
|
|
|
/*
|
|
|
|
This file is part of Telegram Desktop,
|
2018-01-03 10:23:14 +00:00
|
|
|
the official desktop application for the Telegram messaging service.
|
2014-05-30 08:53:19 +00:00
|
|
|
|
2018-01-03 10:23:14 +00:00
|
|
|
For license and copyright information please follow this link:
|
|
|
|
https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
|
2014-05-30 08:53:19 +00:00
|
|
|
*/
|
2021-10-18 21:36:55 +00:00
|
|
|
#include "ui/boxes/confirm_box.h"
|
2014-05-30 08:53:19 +00:00
|
|
|
|
2017-04-13 08:27:10 +00:00
|
|
|
#include "lang/lang_keys.h"
|
2022-02-28 03:59:36 +00:00
|
|
|
#include "ui/widgets/buttons.h"
|
2019-09-18 11:19:05 +00:00
|
|
|
#include "styles/style_layers.h"
|
2015-09-23 17:43:08 +00:00
|
|
|
|
2021-10-18 22:28:08 +00:00
|
|
|
namespace Ui {
|
2020-03-26 08:10:35 +00:00
|
|
|
|
2022-02-28 03:06:51 +00:00
|
|
|
void ConfirmBox(not_null<Ui::GenericBox*> box, ConfirmBoxArgs &&args) {
|
2022-02-27 05:44:02 +00:00
|
|
|
const auto weak = Ui::MakeWeak(box);
|
|
|
|
const auto lifetime = box->lifetime().make_state<rpl::lifetime>();
|
|
|
|
|
2023-08-18 15:03:50 +00:00
|
|
|
const auto withTitle = !v::is_null(args.title);
|
|
|
|
if (withTitle) {
|
|
|
|
box->setTitle(v::text::take_marked(std::move(args.title)));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!v::is_null(args.text)) {
|
|
|
|
const auto padding = st::boxPadding;
|
|
|
|
const auto use = withTitle
|
|
|
|
? QMargins(padding.left(), 0, padding.right(), padding.bottom())
|
|
|
|
: padding;
|
2022-04-15 09:36:06 +00:00
|
|
|
const auto label = box->addRow(
|
|
|
|
object_ptr<Ui::FlatLabel>(
|
|
|
|
box.get(),
|
|
|
|
v::text::take_marked(std::move(args.text)),
|
|
|
|
args.labelStyle ? *args.labelStyle : st::boxLabel),
|
2023-08-18 15:03:50 +00:00
|
|
|
use);
|
2022-04-15 09:36:06 +00:00
|
|
|
if (args.labelFilter) {
|
|
|
|
label->setClickHandlerFilter(std::move(args.labelFilter));
|
|
|
|
}
|
2023-08-18 15:03:50 +00:00
|
|
|
}
|
2022-02-27 05:44:02 +00:00
|
|
|
|
|
|
|
const auto prepareCallback = [&](ConfirmBoxArgs::Callback &callback) {
|
|
|
|
return [=, confirmed = std::move(callback)]() {
|
|
|
|
if (const auto callbackPtr = std::get_if<1>(&confirmed)) {
|
|
|
|
if (auto callback = (*callbackPtr)) {
|
|
|
|
callback();
|
|
|
|
}
|
|
|
|
} else if (const auto callbackPtr = std::get_if<2>(&confirmed)) {
|
|
|
|
if (auto callback = (*callbackPtr)) {
|
|
|
|
callback(crl::guard(weak, [=] { weak->closeBox(); }));
|
|
|
|
}
|
|
|
|
} else if (weak) {
|
|
|
|
weak->closeBox();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
const auto &defaultButtonStyle = box->getDelegate()->style().button;
|
|
|
|
|
2022-03-08 09:12:34 +00:00
|
|
|
const auto confirmButton = box->addButton(
|
2022-02-27 05:44:02 +00:00
|
|
|
v::text::take_plain(std::move(args.confirmText), tr::lng_box_ok()),
|
|
|
|
[=, c = prepareCallback(args.confirmed)]() {
|
|
|
|
lifetime->destroy();
|
|
|
|
c();
|
|
|
|
},
|
|
|
|
args.confirmStyle ? *args.confirmStyle : defaultButtonStyle);
|
2022-03-08 09:12:34 +00:00
|
|
|
box->events(
|
|
|
|
) | rpl::start_with_next([=](not_null<QEvent*> e) {
|
|
|
|
if ((e->type() != QEvent::KeyPress) || !confirmButton) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const auto k = static_cast<QKeyEvent*>(e.get());
|
|
|
|
if (k->key() == Qt::Key_Enter || k->key() == Qt::Key_Return) {
|
|
|
|
confirmButton->clicked(Qt::KeyboardModifiers(), Qt::LeftButton);
|
|
|
|
}
|
|
|
|
}, box->lifetime());
|
2022-02-27 05:44:02 +00:00
|
|
|
|
|
|
|
if (!args.inform) {
|
|
|
|
const auto cancelButton = box->addButton(
|
|
|
|
v::text::take_plain(std::move(args.cancelText), tr::lng_cancel()),
|
|
|
|
crl::guard(weak, [=, c = prepareCallback(args.cancelled)]() {
|
|
|
|
lifetime->destroy();
|
|
|
|
c();
|
|
|
|
}),
|
|
|
|
args.cancelStyle ? *args.cancelStyle : defaultButtonStyle);
|
|
|
|
|
|
|
|
box->boxClosing(
|
|
|
|
) | rpl::start_with_next(crl::guard(cancelButton, [=] {
|
|
|
|
cancelButton->clicked(Qt::KeyboardModifiers(), Qt::LeftButton);
|
|
|
|
}), *lifetime);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (args.strictCancel) {
|
|
|
|
lifetime->destroy();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
object_ptr<Ui::GenericBox> MakeConfirmBox(ConfirmBoxArgs &&args) {
|
2022-02-28 03:06:51 +00:00
|
|
|
return Box(ConfirmBox, std::move(args));
|
2022-02-27 05:44:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
object_ptr<Ui::GenericBox> MakeInformBox(v::text::data text) {
|
|
|
|
return MakeConfirmBox({
|
|
|
|
.text = std::move(text),
|
|
|
|
.inform = true,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-10-18 22:28:08 +00:00
|
|
|
} // namespace Ui
|