Use shared_ptr<Factory> as settings section id.

This commit is contained in:
John Preston 2023-09-29 09:38:17 +04:00
parent 6ba922d7b0
commit 5ef48cac9c
8 changed files with 37 additions and 48 deletions

View File

@ -45,7 +45,7 @@ Widget::Widget(
, _self(controller->key().settingsSelf())
, _type(controller->section().settingsType())
, _inner([&] {
auto inner = _type()->create(this, controller->parentController());
auto inner = _type->create(this, controller->parentController());
if (inner->hasFlexibleTopBar()) {
auto filler = setInnerWidget(object_ptr<Ui::RpWidget>(this));
filler->resize(1, 1);

View File

@ -162,7 +162,7 @@ public:
}
[[nodiscard]] static Type Id() {
return &SectionMetaImplementation<SectionType>::Meta;
return SectionFactory<SectionType>::Instance();
}
[[nodiscard]] Type id() const final override {
return Id();

View File

@ -52,14 +52,16 @@ using Button = Ui::SettingsButton;
class AbstractSection;
struct SectionMeta {
struct AbstractSectionFactory {
[[nodiscard]] virtual object_ptr<AbstractSection> create(
not_null<QWidget*> parent,
not_null<Window::SessionController*> controller) const = 0;
virtual ~AbstractSectionFactory() = default;
};
template <typename SectionType>
struct SectionMetaImplementation : SectionMeta {
struct SectionFactory : AbstractSectionFactory {
object_ptr<AbstractSection> create(
not_null<QWidget*> parent,
not_null<Window::SessionController*> controller
@ -67,9 +69,9 @@ struct SectionMetaImplementation : SectionMeta {
return object_ptr<SectionType>(parent, controller);
}
[[nodiscard]] static not_null<SectionMeta*> Meta() {
static SectionMetaImplementation result;
return &result;
[[nodiscard]] static const std::shared_ptr<SectionFactory> &Instance() {
static const auto result = std::make_shared<SectionFactory>();
return result;
}
};
@ -120,7 +122,7 @@ public:
using AbstractSection::AbstractSection;
[[nodiscard]] static Type Id() {
return &SectionMetaImplementation<SectionType>::Meta;
return SectionFactory<SectionType>::Instance();
}
[[nodiscard]] Type id() const final override {
return Id();

View File

@ -330,7 +330,7 @@ public:
}
[[nodiscard]] static Type Id() {
return &SectionMetaImplementation<SectionType>::Meta;
return SectionFactory<SectionType>::Instance();
}
[[nodiscard]] Type id() const final override {
return Id();

View File

@ -171,7 +171,7 @@ void AddTypeButton(
st::settingsNotificationType,
{ icon });
button->setClickedCallback([=] {
showOther(NotificationsTypeId(type));
showOther(NotificationsType::Id(type));
});
const auto session = &controller->session();
@ -290,7 +290,7 @@ void AddTypeButton(
tr::lng_notification_exceptions_view(),
[=] {
box->closeBox();
showOther(NotificationsTypeId(type));
showOther(NotificationsType::Id(type));
});
}));
}

View File

@ -36,6 +36,20 @@ namespace {
using Notify = Data::DefaultNotify;
struct Factory : AbstractSectionFactory {
explicit Factory(Notify type) : type(type) {
}
object_ptr<AbstractSection> create(
not_null<QWidget*> parent,
not_null<Window::SessionController*> controller
) const final override {
return object_ptr<NotificationsType>(parent, controller, type);
}
const Notify type = {};
};
class AddExceptionBoxController final
: public ChatsListBoxController
, public base::has_weak_ptr {
@ -351,11 +365,6 @@ void ExceptionsController::sort() {
delegate()->peerListSortRows(predicate);
}
template <Notify kType>
[[nodiscard]] Type Id() {
return &NotificationsTypeMetaImplementation<kType>::Meta;
}
[[nodiscard]] rpl::producer<QString> Title(Notify type) {
switch (type) {
case Notify::User: return tr::lng_notification_title_private_chats();
@ -562,15 +571,6 @@ void SetupExceptions(
} // namespace
Type NotificationsTypeId(Notify type) {
switch (type) {
case Notify::User: return Id<Notify::User>();
case Notify::Group: return Id<Notify::Group>();
case Notify::Broadcast: return Id<Notify::Broadcast>();
}
Unexpected("Type in NotificationTypeId.");
}
NotificationsType::NotificationsType(
QWidget *parent,
not_null<Window::SessionController*> controller,
@ -589,8 +589,8 @@ rpl::producer<QString> NotificationsType::title() {
Unexpected("Type in NotificationsType.");
}
Type NotificationsType::id() const {
return NotificationsTypeId(_type);
Type NotificationsType::Id(Notify type) {
return std::make_shared<Factory>(type);
}
void NotificationsType::setupContent(

View File

@ -25,32 +25,19 @@ public:
[[nodiscard]] rpl::producer<QString> title() override;
[[nodiscard]] Type id() const final override;
[[nodiscard]] static Type Id(Data::DefaultNotify type);
[[nodiscard]] Type id() const final override {
return Id(_type);
}
private:
void setupContent(not_null<Window::SessionController*> controller);
Data::DefaultNotify _type;
const Data::DefaultNotify _type;
};
template <Data::DefaultNotify kType>
struct NotificationsTypeMetaImplementation : SectionMeta {
object_ptr<AbstractSection> create(
not_null<QWidget*> parent,
not_null<Window::SessionController*> controller
) const final override {
return object_ptr<NotificationsType>(parent, controller, kType);
}
[[nodiscard]] static not_null<SectionMeta*> Meta() {
static NotificationsTypeMetaImplementation result;
return &result;
}
};
[[nodiscard]] Type NotificationsTypeId(Data::DefaultNotify type);
[[nodiscard]] bool NotificationsEnabledForType(
not_null<Main::Session*> session,
Data::DefaultNotify type);

View File

@ -9,7 +9,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
namespace Settings {
struct SectionMeta;
using Type = not_null<SectionMeta*>(*)();
struct AbstractSectionFactory;
using Type = std::shared_ptr<AbstractSectionFactory>;
} // namespace Settings