single_choice_box.cpp 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. /*
  2. This file is part of Telegram Desktop,
  3. the official desktop application for the Telegram messaging service.
  4. For license and copyright information please follow this link:
  5. https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
  6. */
  7. #include "ui/boxes/single_choice_box.h"
  8. #include "lang/lang_keys.h"
  9. #include "ui/widgets/checkbox.h"
  10. #include "ui/wrap/vertical_layout.h"
  11. #include "ui/wrap/padding_wrap.h"
  12. #include "styles/style_boxes.h"
  13. #include "styles/style_layers.h"
  14. void SingleChoiceBox(
  15. not_null<Ui::GenericBox*> box,
  16. SingleChoiceBoxArgs &&args) {
  17. box->setTitle(std::move(args.title));
  18. box->addButton(tr::lng_box_ok(), [=] { box->closeBox(); });
  19. const auto group = std::make_shared<Ui::RadiobuttonGroup>(
  20. args.initialSelection);
  21. const auto layout = box->verticalLayout();
  22. layout->add(object_ptr<Ui::FixedHeightWidget>(
  23. layout,
  24. st::boxOptionListPadding.top() + st::autolockButton.margin.top()));
  25. auto &&ints = ranges::views::ints(0, ranges::unreachable);
  26. for (const auto &[i, text] : ranges::views::zip(ints, args.options)) {
  27. layout->add(
  28. object_ptr<Ui::Radiobutton>(
  29. layout,
  30. group,
  31. i,
  32. text,
  33. args.st ? *args.st : st::defaultBoxCheckbox,
  34. args.radioSt ? *args.radioSt : st::defaultRadio),
  35. QMargins(
  36. st::boxPadding.left() + st::boxOptionListPadding.left(),
  37. 0,
  38. st::boxPadding.right(),
  39. st::boxOptionListSkip));
  40. }
  41. const auto callback = args.callback.value();
  42. group->setChangedCallback([=](int value) {
  43. const auto weak = Ui::MakeWeak(box);
  44. callback(value);
  45. if (weak) {
  46. box->closeBox();
  47. }
  48. });
  49. }