2
0
mirror of https://github.com/kotatogram/kotatogram-desktop synced 2025-08-31 06:35:14 +00:00

Implement SingleChoiceBox using Ui::GenericBox.

This commit is contained in:
John Preston
2021-02-01 17:51:01 +04:00
parent eb11185de7
commit cb2d77d386
4 changed files with 82 additions and 97 deletions

View File

@@ -16,56 +16,42 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
#include "styles/style_boxes.h"
#include "styles/style_layers.h"
SingleChoiceBox::SingleChoiceBox(
QWidget*,
rpl::producer<QString> title,
const std::vector<QString> &optionTexts,
int initialSelection,
Fn<void(int)> callback,
const style::Checkbox *st,
const style::Radio *radioSt)
: _title(std::move(title))
, _optionTexts(optionTexts)
, _initialSelection(initialSelection)
, _callback(callback)
, _st(st ? *st : st::defaultBoxCheckbox)
, _radioSt(radioSt ? *radioSt : st::defaultRadio) {
}
void SingleChoiceBox(
not_null<Ui::GenericBox*> box,
SingleChoiceBoxArgs &&args) {
box->setTitle(std::move(args.title));
void SingleChoiceBox::prepare() {
setTitle(std::move(_title));
box->addButton(tr::lng_box_ok(), [=] { box->closeBox(); });
addButton(tr::lng_box_ok(), [=] { closeBox(); });
const auto group = std::make_shared<Ui::RadiobuttonGroup>(
args.initialSelection);
const auto group = std::make_shared<Ui::RadiobuttonGroup>(_initialSelection);
const auto content = Ui::CreateChild<Ui::VerticalLayout>(this);
content->add(object_ptr<Ui::FixedHeightWidget>(
content,
const auto layout = box->verticalLayout();
layout->add(object_ptr<Ui::FixedHeightWidget>(
layout,
st::boxOptionListPadding.top() + st::autolockButton.margin.top()));
auto &&ints = ranges::view::ints(0, ranges::unreachable);
for (const auto &[i, text] : ranges::view::zip(ints, _optionTexts)) {
content->add(
for (const auto &[i, text] : ranges::view::zip(ints, args.options)) {
layout->add(
object_ptr<Ui::Radiobutton>(
content,
layout,
group,
i,
text,
_st,
_radioSt),
args.st ? *args.st : st::defaultBoxCheckbox,
args.radioSt ? *args.radioSt : st::defaultRadio),
QMargins(
st::boxPadding.left() + st::boxOptionListPadding.left(),
0,
st::boxPadding.right(),
st::boxOptionListSkip));
}
const auto callback = args.callback.value();
group->setChangedCallback([=](int value) {
const auto weak = Ui::MakeWeak(this);
_callback(value);
const auto weak = Ui::MakeWeak(box);
callback(value);
if (weak) {
closeBox();
box->closeBox();
}
});
setDimensionsToContent(st::boxWidth, content);
}

View File

@@ -7,38 +7,26 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
*/
#pragma once
#include "boxes/abstract_box.h"
#include <vector>
namespace Ui {
class Radiobutton;
} // namespace Ui
#include "ui/layers/generic_box.h"
#include "base/required.h"
namespace style {
struct Checkbox;
struct Radio;
} // namespace style
class SingleChoiceBox : public Ui::BoxContent {
public:
SingleChoiceBox(
QWidget*,
rpl::producer<QString> title,
const std::vector<QString> &optionTexts,
int initialSelection,
Fn<void(int)> callback,
const style::Checkbox *st = nullptr,
const style::Radio *radioSt = nullptr);
protected:
void prepare() override;
private:
rpl::producer<QString> _title;
std::vector<QString> _optionTexts;
int _initialSelection = 0;
Fn<void(int)> _callback;
const style::Checkbox &_st;
const style::Radio &_radioSt;
struct SingleChoiceBoxArgs {
template <typename T>
using required = base::required<T>;
required<rpl::producer<QString>> title;
const std::vector<QString> &options;
int initialSelection = 0;
required<Fn<void(int)>> callback;
const style::Checkbox *st = nullptr;
const style::Radio *radioSt = nullptr;
};
void SingleChoiceBox(
not_null<Ui::GenericBox*> box,
SingleChoiceBoxArgs &&args);