2014-05-30 12:53:19 +04:00
|
|
|
/*
|
|
|
|
This file is part of Telegram Desktop,
|
2018-01-03 13:23:14 +03:00
|
|
|
the official desktop application for the Telegram messaging service.
|
2014-05-30 12:53:19 +04:00
|
|
|
|
2018-01-03 13:23:14 +03:00
|
|
|
For license and copyright information please follow this link:
|
|
|
|
https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
|
2014-05-30 12:53:19 +04:00
|
|
|
*/
|
2021-10-19 00:36:55 +03:00
|
|
|
#include "ui/boxes/confirm_box.h"
|
2014-05-30 12:53:19 +04:00
|
|
|
|
2017-04-13 11:27:10 +03:00
|
|
|
#include "lang/lang_keys.h"
|
2016-11-11 16:46:04 +03:00
|
|
|
#include "ui/widgets/checkbox.h"
|
2018-12-05 12:07:17 +04:00
|
|
|
#include "ui/wrap/vertical_layout.h"
|
2019-09-18 14:19:05 +03:00
|
|
|
#include "styles/style_layers.h"
|
|
|
|
#include "styles/style_boxes.h"
|
2015-09-23 20:43:08 +03:00
|
|
|
|
2021-10-19 01:28:08 +03:00
|
|
|
namespace Ui {
|
2020-03-26 12:10:35 +04:00
|
|
|
|
2022-02-28 06:06:51 +03:00
|
|
|
void ConfirmBox(not_null<Ui::GenericBox*> box, ConfirmBoxArgs &&args) {
|
2022-02-27 08:44:02 +03:00
|
|
|
const auto weak = Ui::MakeWeak(box);
|
|
|
|
const auto lifetime = box->lifetime().make_state<rpl::lifetime>();
|
|
|
|
|
|
|
|
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),
|
|
|
|
st::boxPadding);
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
box->addButton(
|
|
|
|
v::text::take_plain(std::move(args.confirmText), tr::lng_box_ok()),
|
|
|
|
[=, c = prepareCallback(args.confirmed)]() {
|
|
|
|
lifetime->destroy();
|
|
|
|
c();
|
|
|
|
},
|
|
|
|
args.confirmStyle ? *args.confirmStyle : defaultButtonStyle);
|
|
|
|
|
|
|
|
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.labelFilter) {
|
|
|
|
label->setClickHandlerFilter(std::move(args.labelFilter));
|
|
|
|
}
|
|
|
|
if (args.strictCancel) {
|
|
|
|
lifetime->destroy();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
object_ptr<Ui::GenericBox> MakeConfirmBox(ConfirmBoxArgs &&args) {
|
2022-02-28 06:06:51 +03:00
|
|
|
return Box(ConfirmBox, std::move(args));
|
2022-02-27 08:44:02 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
object_ptr<Ui::GenericBox> MakeInformBox(v::text::data text) {
|
|
|
|
return MakeConfirmBox({
|
|
|
|
.text = std::move(text),
|
|
|
|
.inform = true,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-12-05 12:07:17 +04:00
|
|
|
ConfirmDontWarnBox::ConfirmDontWarnBox(
|
|
|
|
QWidget*,
|
2019-06-12 22:11:41 +02:00
|
|
|
rpl::producer<TextWithEntities> text,
|
2018-12-05 12:07:17 +04:00
|
|
|
const QString &checkbox,
|
2019-06-18 18:53:27 +02:00
|
|
|
rpl::producer<QString> confirm,
|
2018-12-05 12:07:17 +04:00
|
|
|
FnMut<void(bool)> callback)
|
2019-06-18 18:53:27 +02:00
|
|
|
: _confirm(std::move(confirm))
|
2019-06-12 22:11:41 +02:00
|
|
|
, _content(setupContent(std::move(text), checkbox, std::move(callback))) {
|
2018-12-05 12:07:17 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void ConfirmDontWarnBox::prepare() {
|
|
|
|
setDimensionsToContent(st::boxWidth, _content);
|
2019-06-18 18:53:27 +02:00
|
|
|
addButton(std::move(_confirm), [=] { _callback(); });
|
|
|
|
addButton(tr::lng_cancel(), [=] { closeBox(); });
|
2018-12-05 12:07:17 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
not_null<Ui::RpWidget*> ConfirmDontWarnBox::setupContent(
|
2019-06-12 22:11:41 +02:00
|
|
|
rpl::producer<TextWithEntities> text,
|
2018-12-05 12:07:17 +04:00
|
|
|
const QString &checkbox,
|
|
|
|
FnMut<void(bool)> callback) {
|
|
|
|
const auto result = Ui::CreateChild<Ui::VerticalLayout>(this);
|
|
|
|
result->add(
|
|
|
|
object_ptr<Ui::FlatLabel>(
|
|
|
|
result,
|
2019-06-12 22:11:41 +02:00
|
|
|
std::move(text),
|
2018-12-05 12:07:17 +04:00
|
|
|
st::boxLabel),
|
|
|
|
st::boxPadding);
|
|
|
|
const auto control = result->add(
|
|
|
|
object_ptr<Ui::Checkbox>(
|
|
|
|
result,
|
|
|
|
checkbox,
|
|
|
|
false,
|
|
|
|
st::defaultBoxCheckbox),
|
|
|
|
style::margins(
|
|
|
|
st::boxPadding.left(),
|
|
|
|
st::boxPadding.bottom(),
|
|
|
|
st::boxPadding.right(),
|
|
|
|
st::boxPadding.bottom()));
|
|
|
|
_callback = [=, callback = std::move(callback)]() mutable {
|
|
|
|
const auto checked = control->checked();
|
|
|
|
auto local = std::move(callback);
|
|
|
|
closeBox();
|
|
|
|
local(checked);
|
|
|
|
};
|
|
|
|
return result;
|
|
|
|
}
|
2021-10-19 01:28:08 +03:00
|
|
|
|
|
|
|
} // namespace Ui
|