mirror of
https://github.com/AyuGram/AyuGramDesktop.git
synced 2025-06-05 06:33:57 +02:00
Added ConfirmBox implementation with GenericBox.
This commit is contained in:
parent
6478b7b129
commit
1394b1d56f
3 changed files with 103 additions and 1 deletions
|
@ -36,8 +36,79 @@ TextParseOptions kMarkedTextBoxOptions = {
|
||||||
Qt::LayoutDirectionAuto, // dir
|
Qt::LayoutDirectionAuto, // dir
|
||||||
};
|
};
|
||||||
|
|
||||||
|
void GenericConfirmBox(not_null<Ui::GenericBox*> box, ConfirmBoxArgs &&args) {
|
||||||
|
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();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
} // namespace
|
} // namespace
|
||||||
|
|
||||||
|
object_ptr<Ui::GenericBox> MakeConfirmBox(ConfirmBoxArgs &&args) {
|
||||||
|
return Box(GenericConfirmBox, std::move(args));
|
||||||
|
}
|
||||||
|
|
||||||
|
object_ptr<Ui::GenericBox> MakeInformBox(v::text::data text) {
|
||||||
|
return MakeConfirmBox({
|
||||||
|
.text = std::move(text),
|
||||||
|
.inform = true,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
ConfirmBox::ConfirmBox(
|
ConfirmBox::ConfirmBox(
|
||||||
QWidget*,
|
QWidget*,
|
||||||
const QString &text,
|
const QString &text,
|
||||||
|
|
|
@ -8,9 +8,40 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include "boxes/abstract_box.h"
|
#include "boxes/abstract_box.h"
|
||||||
|
#include "ui/layers/generic_box.h"
|
||||||
|
#include "ui/text/text_variant.h"
|
||||||
|
|
||||||
namespace Ui {
|
namespace Ui {
|
||||||
|
|
||||||
|
struct ConfirmBoxArgs {
|
||||||
|
using Callback = std::variant<
|
||||||
|
v::null_t,
|
||||||
|
Fn<void()>,
|
||||||
|
Fn<void(Fn<void()>)>>;
|
||||||
|
|
||||||
|
v::text::data text;
|
||||||
|
Callback confirmed = v::null;
|
||||||
|
Callback cancelled = v::null;
|
||||||
|
|
||||||
|
v::text::data confirmText;
|
||||||
|
v::text::data cancelText;
|
||||||
|
|
||||||
|
const style::RoundButton *confirmStyle = nullptr;
|
||||||
|
const style::RoundButton *cancelStyle = nullptr;
|
||||||
|
|
||||||
|
const style::FlatLabel *labelStyle = nullptr;
|
||||||
|
Fn<bool(const ClickHandlerPtr&, Qt::MouseButton)> labelFilter;
|
||||||
|
|
||||||
|
bool inform = false;
|
||||||
|
// If strict cancel is set the cancel.callback() is only called
|
||||||
|
// if the cancel button was pressed.
|
||||||
|
bool strictCancel = false;
|
||||||
|
};
|
||||||
|
|
||||||
|
[[nodiscard]] object_ptr<Ui::GenericBox> MakeConfirmBox(
|
||||||
|
ConfirmBoxArgs &&args);
|
||||||
|
[[nodiscard]] object_ptr<Ui::GenericBox> MakeInformBox(v::text::data text);
|
||||||
|
|
||||||
class InformBox;
|
class InformBox;
|
||||||
class ConfirmBox : public Ui::BoxContent, public ClickHandlerHost {
|
class ConfirmBox : public Ui::BoxContent, public ClickHandlerHost {
|
||||||
public:
|
public:
|
||||||
|
|
|
@ -1 +1 @@
|
||||||
Subproject commit 5214f96bbafa1d879fca39c3374cb182687084c8
|
Subproject commit 6392af756fe51c72b6ffd8be4f429f449e1e1d1e
|
Loading…
Add table
Reference in a new issue