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);
if (!active) {
AddUnreadBadge(raw, rpl::single(rpl::empty) | rpl::then(
Badge::AddUnread(raw, rpl::single(rpl::empty) | rpl::then(
session->data().unreadBadgeChanges()
) | rpl::map([=] {
auto &owner = session->data();
return UnreadBadge{
return Badge::UnreadBadge{
owner.unreadBadge(),
owner.unreadBadgeMuted(),
};
@ -852,7 +852,9 @@ AccountsEvents SetupAccounts(
};
}
Dialogs::Ui::UnreadBadgeStyle BadgeStyle() {
namespace Badge {
Dialogs::Ui::UnreadBadgeStyle Style() {
auto result = Dialogs::Ui::UnreadBadgeStyle();
result.font = st::mainMenuBadgeFont;
result.size = st::mainMenuBadgeSize;
@ -860,8 +862,33 @@ Dialogs::Ui::UnreadBadgeStyle BadgeStyle() {
return result;
}
void AddUnreadBadge(
not_null<Ui::SettingsButton*> button,
not_null<Ui::RpWidget*> AddRight(
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) {
struct State {
State(QWidget *parent) : widget(parent) {
@ -869,11 +896,11 @@ void AddUnreadBadge(
}
Ui::RpWidget widget;
Dialogs::Ui::UnreadBadgeStyle st = BadgeStyle();
Dialogs::Ui::UnreadBadgeStyle st = Style();
int count = 0;
QString string;
};
const auto state = button->lifetime().make_state<State>(button);
const auto state = container->lifetime().make_state<State>(container);
std::move(
value
@ -902,23 +929,19 @@ void AddUnreadBadge(
state->st);
}, state->widget.lifetime());
rpl::combine(
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());
return &state->widget;
}
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

View File

@ -39,14 +39,22 @@ AccountsEvents SetupAccounts(
not_null<Ui::VerticalLayout*> container,
not_null<Window::SessionController*> controller);
[[nodiscard]] Dialogs::Ui::UnreadBadgeStyle BadgeStyle();
namespace Badge {
[[nodiscard]] Dialogs::Ui::UnreadBadgeStyle Style();
struct UnreadBadge {
int count = 0;
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,
rpl::producer<UnreadBadge> value);
} // namespace Badge
} // namespace Settings

View File

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