Decomposed creating of unread badge in list of accounts.

This commit is contained in:
23rd 2022-06-14 01:43:13 +03:00
parent c9292512c0
commit 7a821ca0f4
3 changed files with 61 additions and 30 deletions

View file

@ -516,11 +516,11 @@ void SetupAccountsWrap(
const auto state = raw->lifetime().make_state<State>(raw); const auto state = raw->lifetime().make_state<State>(raw);
if (!active) { if (!active) {
AddUnreadBadge(raw, rpl::single(rpl::empty) | rpl::then( Badge::AddUnread(raw, rpl::single(rpl::empty) | rpl::then(
session->data().unreadBadgeChanges() session->data().unreadBadgeChanges()
) | rpl::map([=] { ) | rpl::map([=] {
auto &owner = session->data(); auto &owner = session->data();
return UnreadBadge{ return Badge::UnreadBadge{
owner.unreadBadge(), owner.unreadBadge(),
owner.unreadBadgeMuted(), owner.unreadBadgeMuted(),
}; };
@ -852,7 +852,9 @@ AccountsEvents SetupAccounts(
}; };
} }
Dialogs::Ui::UnreadBadgeStyle BadgeStyle() { namespace Badge {
Dialogs::Ui::UnreadBadgeStyle Style() {
auto result = Dialogs::Ui::UnreadBadgeStyle(); auto result = Dialogs::Ui::UnreadBadgeStyle();
result.font = st::mainMenuBadgeFont; result.font = st::mainMenuBadgeFont;
result.size = st::mainMenuBadgeSize; result.size = st::mainMenuBadgeSize;
@ -860,8 +862,33 @@ Dialogs::Ui::UnreadBadgeStyle BadgeStyle() {
return result; return result;
} }
void AddUnreadBadge( not_null<Ui::RpWidget*> AddRight(
not_null<Ui::SettingsButton*> button, not_null<Ui::SettingsButton*> button) {
const auto widget = Ui::CreateChild<Ui::RpWidget>(button.get());
rpl::combine(
button->sizeValue(),
widget->sizeValue(),
widget->shownValue()
) | rpl::start_with_next([=](QSize outer, QSize inner, bool shown) {
auto padding = button->st().padding;
if (shown) {
widget->moveToRight(
padding.right(),
(outer.height() - inner.height()) / 2,
outer.width());
padding.setRight(padding.right()
+ inner.width()
+ button->st().style.font->spacew);
}
button->setPaddingOverride(padding);
}, widget->lifetime());
return widget;
}
not_null<Ui::RpWidget*> CreateUnread(
not_null<Ui::RpWidget*> container,
rpl::producer<UnreadBadge> value) { rpl::producer<UnreadBadge> value) {
struct State { struct State {
State(QWidget *parent) : widget(parent) { State(QWidget *parent) : widget(parent) {
@ -869,11 +896,11 @@ void AddUnreadBadge(
} }
Ui::RpWidget widget; Ui::RpWidget widget;
Dialogs::Ui::UnreadBadgeStyle st = BadgeStyle(); Dialogs::Ui::UnreadBadgeStyle st = Style();
int count = 0; int count = 0;
QString string; QString string;
}; };
const auto state = button->lifetime().make_state<State>(button); const auto state = container->lifetime().make_state<State>(container);
std::move( std::move(
value value
@ -902,23 +929,19 @@ void AddUnreadBadge(
state->st); state->st);
}, state->widget.lifetime()); }, state->widget.lifetime());
rpl::combine( return &state->widget;
button->sizeValue(),
state->widget.sizeValue(),
state->widget.shownValue()
) | rpl::start_with_next([=](QSize outer, QSize inner, bool shown) {
auto padding = button->st().padding;
if (shown) {
state->widget.moveToRight(
padding.right(),
(outer.height() - inner.height()) / 2,
outer.width());
padding.setRight(padding.right()
+ inner.width()
+ button->st().style.font->spacew);
}
button->setPaddingOverride(padding);
}, state->widget.lifetime());
} }
void AddUnread(
not_null<Ui::SettingsButton*> button,
rpl::producer<UnreadBadge> value) {
const auto container = AddRight(button);
const auto badge = CreateUnread(container, std::move(value));
badge->sizeValue(
) | rpl::start_with_next([=](const QSize &s) {
container->resize(s);
}, container->lifetime());
}
} // namespace Badge
} // namespace Settings } // namespace Settings

View file

@ -39,14 +39,22 @@ AccountsEvents SetupAccounts(
not_null<Ui::VerticalLayout*> container, not_null<Ui::VerticalLayout*> container,
not_null<Window::SessionController*> controller); not_null<Window::SessionController*> controller);
[[nodiscard]] Dialogs::Ui::UnreadBadgeStyle BadgeStyle(); namespace Badge {
[[nodiscard]] Dialogs::Ui::UnreadBadgeStyle Style();
struct UnreadBadge { struct UnreadBadge {
int count = 0; int count = 0;
bool muted = false; bool muted = false;
}; };
void AddUnreadBadge( [[nodiscard]] not_null<Ui::RpWidget*> AddRight(
not_null<Ui::SettingsButton*> button);
[[nodiscard]] not_null<Ui::RpWidget*> CreateUnread(
not_null<Ui::RpWidget*> container,
rpl::producer<UnreadBadge> value);
void AddUnread(
not_null<Ui::SettingsButton*> button, not_null<Ui::SettingsButton*> button,
rpl::producer<UnreadBadge> value); rpl::producer<UnreadBadge> value);
} // namespace Badge
} // namespace Settings } // namespace Settings

View file

@ -235,7 +235,7 @@ void MainMenu::ToggleAccountsButton::paintUnreadBadge(Painter &p) {
return; return;
} }
auto st = Settings::BadgeStyle(); auto st = Settings::Badge::Style();
const auto right = width() const auto right = width()
- st::mainMenuTogglePosition.x() - st::mainMenuTogglePosition.x()
- st::mainMenuToggleSize * 3; - st::mainMenuToggleSize * 3;
@ -259,7 +259,7 @@ void MainMenu::ToggleAccountsButton::validateUnreadBadge() {
_rightSkip = base; _rightSkip = base;
if (!_unreadBadge.isEmpty()) { if (!_unreadBadge.isEmpty()) {
const auto st = Settings::BadgeStyle(); const auto st = Settings::Badge::Style();
_rightSkip += 2 * st::mainMenuToggleSize _rightSkip += 2 * st::mainMenuToggleSize
+ Dialogs::Ui::CountUnreadBadgeSize(_unreadBadge, st).width(); + Dialogs::Ui::CountUnreadBadgeSize(_unreadBadge, st).width();
} }
@ -513,13 +513,13 @@ void MainMenu::setupArchive() {
}) | rpl::take(1); }) | rpl::take(1);
using namespace Settings; using namespace Settings;
AddUnreadBadge(button, rpl::single(rpl::empty) | rpl::then(std::move( Badge::AddUnread(button, rpl::single(rpl::empty) | rpl::then(std::move(
folderValue folderValue
) | rpl::map([=](not_null<Data::Folder*> folder) { ) | rpl::map([=](not_null<Data::Folder*> folder) {
return folder->owner().chatsList(folder)->unreadStateChanges(); return folder->owner().chatsList(folder)->unreadStateChanges();
}) | rpl::flatten_latest() | rpl::to_empty) | rpl::map([=] { }) | rpl::flatten_latest() | rpl::to_empty) | rpl::map([=] {
const auto loaded = folder(); const auto loaded = folder();
return UnreadBadge{ return Badge::UnreadBadge{
loaded ? loaded->chatListUnreadCount() : 0, loaded ? loaded->chatListUnreadCount() : 0,
true, true,
}; };