mirror of
https://github.com/AyuGram/AyuGramDesktop.git
synced 2026-08-01 09:55:24 +00:00
408 lines
11 KiB
C++
408 lines
11 KiB
C++
/*
|
|
This file is part of Telegram Desktop,
|
|
the official desktop application for the Telegram messaging service.
|
|
|
|
For license and copyright information please follow this link:
|
|
https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
|
|
*/
|
|
#include "info/profile/info_profile_widget.h"
|
|
|
|
#include "dialogs/ui/dialogs_stories_content.h"
|
|
#include "history/history.h"
|
|
#include "info/profile/info_profile_inner_widget.h"
|
|
#include "info/profile/info_profile_members.h"
|
|
#include "info/settings/info_settings_widget.h"
|
|
#include "ui/widgets/scroll_area.h"
|
|
#include "ui/ui_utility.h"
|
|
#include "data/data_peer.h"
|
|
#include "data/data_channel.h"
|
|
#include "data/data_forum_topic.h"
|
|
#include "data/data_saved_sublist.h"
|
|
#include "data/data_user.h"
|
|
#include "lang/lang_keys.h"
|
|
#include "info/info_controller.h"
|
|
#include "styles/style_info.h"
|
|
|
|
#include <QtWidgets/QApplication>
|
|
#include <QtWidgets/QScrollBar>
|
|
|
|
namespace Info::Settings {
|
|
struct SectionCustomTopBarData;
|
|
} // namespace Info::Settings
|
|
|
|
namespace Info::Profile {
|
|
|
|
using Info::Settings::SectionCustomTopBarData;
|
|
|
|
Memento::Memento(not_null<Controller*> controller)
|
|
: Memento(
|
|
controller->peer(),
|
|
controller->topic(),
|
|
controller->sublist(),
|
|
controller->migratedPeerId(),
|
|
{ v::null }) {
|
|
}
|
|
|
|
Memento::Memento(
|
|
not_null<PeerData*> peer,
|
|
PeerId migratedPeerId,
|
|
Origin origin)
|
|
: Memento(peer, nullptr, nullptr, migratedPeerId, origin) {
|
|
}
|
|
|
|
Memento::Memento(
|
|
not_null<PeerData*> peer,
|
|
Data::ForumTopic *topic,
|
|
Data::SavedSublist *sublist,
|
|
PeerId migratedPeerId,
|
|
Origin origin)
|
|
: ContentMemento(peer, topic, sublist, migratedPeerId)
|
|
, _origin(origin) {
|
|
}
|
|
|
|
Memento::Memento(not_null<Data::ForumTopic*> topic)
|
|
: ContentMemento(topic->peer(), topic, nullptr, 0) {
|
|
}
|
|
|
|
Memento::Memento(not_null<Data::SavedSublist*> sublist)
|
|
: ContentMemento(sublist->owningHistory()->peer, nullptr, sublist, 0) {
|
|
}
|
|
|
|
Section Memento::section() const {
|
|
return Section(Section::Type::Profile);
|
|
}
|
|
|
|
object_ptr<ContentWidget> Memento::createWidget(
|
|
QWidget *parent,
|
|
not_null<Controller*> controller,
|
|
const QRect &geometry) {
|
|
auto result = object_ptr<Widget>(parent, controller, _origin);
|
|
result->setInternalState(geometry, this);
|
|
return result;
|
|
}
|
|
|
|
void Memento::setMembersState(std::unique_ptr<MembersState> state) {
|
|
_membersState = std::move(state);
|
|
}
|
|
|
|
std::unique_ptr<MembersState> Memento::membersState() {
|
|
return std::move(_membersState);
|
|
}
|
|
|
|
Memento::~Memento() = default;
|
|
|
|
Widget::Widget(
|
|
QWidget *parent,
|
|
not_null<Controller*> controller,
|
|
Origin origin)
|
|
: ContentWidget(parent, controller)
|
|
, _inner([&] {
|
|
auto inner = object_ptr<InnerWidget>(this, controller, origin);
|
|
if (inner->hasFlexibleTopBar()) {
|
|
auto filler = setInnerWidget(object_ptr<Ui::RpWidget>(this));
|
|
filler->resize(1, 1);
|
|
|
|
_flexibleScroll.contentHeightValue.events(
|
|
) | rpl::start_with_next([=](int h) {
|
|
filler->resize(filler->width(), h);
|
|
}, filler->lifetime());
|
|
|
|
filler->widthValue(
|
|
) | rpl::start_to_stream(
|
|
_flexibleScroll.fillerWidthValue,
|
|
lifetime());
|
|
|
|
inner->setParent(filler->parentWidget()->parentWidget());
|
|
inner->raise();
|
|
|
|
using InnerPtr = base::unique_qptr<InnerWidget>;
|
|
auto owner = filler->lifetime().make_state<InnerPtr>(
|
|
std::move(inner.release()));
|
|
return owner->get();
|
|
} else {
|
|
return setInnerWidget(std::move(inner));
|
|
}
|
|
}())
|
|
, _pinnedToTop(_inner->createPinnedToTop(this))
|
|
, _pinnedToBottom(_inner->createPinnedToBottom(this)) {
|
|
controller->setSearchEnabledByContent(false);
|
|
|
|
_inner->move(0, 0);
|
|
_inner->scrollToRequests(
|
|
) | rpl::start_with_next([this](Ui::ScrollToRequest request) {
|
|
if (request.ymin < 0) {
|
|
scrollTopRestore(
|
|
qMin(scrollTopSave(), request.ymax));
|
|
} else {
|
|
scrollTo(request);
|
|
}
|
|
}, lifetime());
|
|
|
|
if (_pinnedToTop) {
|
|
_inner->widthValue(
|
|
) | rpl::start_with_next([=](int w) {
|
|
_pinnedToTop->resizeToWidth(w);
|
|
setScrollTopSkip(_pinnedToTop->height());
|
|
}, _pinnedToTop->lifetime());
|
|
|
|
_pinnedToTop->heightValue(
|
|
) | rpl::start_with_next([=](int h) {
|
|
setScrollTopSkip(h);
|
|
}, _pinnedToTop->lifetime());
|
|
}
|
|
|
|
if (_pinnedToBottom) {
|
|
const auto processHeight = [=] {
|
|
setScrollBottomSkip(_pinnedToBottom->height());
|
|
_pinnedToBottom->moveToLeft(
|
|
_pinnedToBottom->x(),
|
|
height() - _pinnedToBottom->height());
|
|
};
|
|
|
|
_inner->sizeValue(
|
|
) | rpl::start_with_next([=](const QSize &s) {
|
|
_pinnedToBottom->resizeToWidth(s.width());
|
|
}, _pinnedToBottom->lifetime());
|
|
|
|
rpl::combine(
|
|
_pinnedToBottom->heightValue(),
|
|
heightValue()
|
|
) | rpl::start_with_next(processHeight, _pinnedToBottom->lifetime());
|
|
}
|
|
|
|
if (_pinnedToTop
|
|
&& _pinnedToTop->minimumHeight()
|
|
&& _inner->hasFlexibleTopBar()) {
|
|
const auto heightDiff = [=] {
|
|
return _pinnedToTop->maximumHeight()
|
|
- _pinnedToTop->minimumHeight();
|
|
};
|
|
|
|
rpl::combine(
|
|
_pinnedToTop->heightValue(),
|
|
_inner->heightValue()
|
|
) | rpl::start_with_next([=](int, int h) {
|
|
_flexibleScroll.contentHeightValue.fire(h + heightDiff());
|
|
}, _pinnedToTop->lifetime());
|
|
|
|
constexpr auto kScrollStepTime = crl::time(260);
|
|
|
|
const auto clearScrollState = [=] {
|
|
_scrollAnimation.stop();
|
|
_scrollTopFrom = 0;
|
|
_scrollTopTo = 0;
|
|
_timeOffset = 0;
|
|
_lastScrollApplied = 0;
|
|
};
|
|
|
|
_scrollAnimation.init([=](crl::time now) {
|
|
const auto progress = float64(now
|
|
- _scrollAnimation.started()
|
|
- _timeOffset) / kScrollStepTime;
|
|
const auto eased = anim::easeOutQuint(1.0, progress);
|
|
const auto scrollCurrent = anim::interpolate(
|
|
_scrollTopFrom,
|
|
_scrollTopTo,
|
|
std::clamp(eased, 0., 1.));
|
|
scroll()->scrollToY(scrollCurrent);
|
|
_lastScrollApplied = scrollCurrent;
|
|
if (progress >= 1) {
|
|
clearScrollState();
|
|
}
|
|
});
|
|
|
|
const auto singleStep = scroll()->verticalScrollBar()->singleStep()
|
|
* QApplication::wheelScrollLines();
|
|
const auto step1 = st::infoProfileTopBarStep1;
|
|
const auto step2 = st::infoProfileTopBarStep2;
|
|
// const auto stepDepreciation = singleStep
|
|
// - st::infoProfileTopBarActionButtonsHeight;
|
|
_scrollTopPrevious = scroll()->scrollTop();
|
|
scrollTopValue(
|
|
) | rpl::start_with_next([=](int top) {
|
|
if (!_pinnedToTop || _applyingFakeScrollState) {
|
|
return;
|
|
}
|
|
const auto diff = top - _scrollTopPrevious;
|
|
_scrollTopPrevious = top;
|
|
if (std::abs(diff) == singleStep) {
|
|
const auto previousValue = top - diff;
|
|
const auto nextStep = (diff > 0)
|
|
? ((previousValue == 0)
|
|
? step1
|
|
: (previousValue == step1)
|
|
? step2
|
|
: -1)
|
|
// : ((top < step1
|
|
// && (top + stepDepreciation != step1
|
|
// || _scrollAnimation.animating()))
|
|
: ((top < step1)
|
|
? 0
|
|
: (top < step2)
|
|
? step1
|
|
: -1);
|
|
{
|
|
_applyingFakeScrollState = true;
|
|
scroll()->scrollToY(previousValue);
|
|
_applyingFakeScrollState = false;
|
|
}
|
|
if (_scrollAnimation.animating()
|
|
&& ((_scrollTopTo > _scrollTopFrom) != (diff > 0))) {
|
|
auto overriddenDirection = true;
|
|
if (_scrollTopTo > _scrollTopFrom) {
|
|
// From going down to going up.
|
|
if (_scrollTopTo == step1) {
|
|
_scrollTopTo = 0;
|
|
} else if (_scrollTopTo == step2) {
|
|
_scrollTopTo = step1;
|
|
} else {
|
|
overriddenDirection = false;
|
|
}
|
|
} else {
|
|
// From going up to going down.
|
|
if (_scrollTopTo == 0) {
|
|
_scrollTopTo = step1;
|
|
} else if (_scrollTopTo == step1) {
|
|
_scrollTopTo = step2;
|
|
} else {
|
|
overriddenDirection = false;
|
|
}
|
|
}
|
|
if (overriddenDirection) {
|
|
_timeOffset = crl::now() - _scrollAnimation.started();
|
|
_scrollTopFrom = _lastScrollApplied
|
|
? _lastScrollApplied
|
|
: previousValue;
|
|
return;
|
|
} else {
|
|
clearScrollState();
|
|
}
|
|
}
|
|
_scrollTopFrom = _lastScrollApplied
|
|
? _lastScrollApplied
|
|
: previousValue;
|
|
if (!_scrollAnimation.animating()) {
|
|
_scrollTopTo = ((nextStep != -1) ? nextStep : top);
|
|
_scrollAnimation.start();
|
|
} else {
|
|
if (_scrollTopTo > _scrollTopFrom) {
|
|
// Down.
|
|
if (_scrollTopTo == step1) {
|
|
_scrollTopTo = step2;
|
|
} else {
|
|
_scrollTopTo += diff;
|
|
}
|
|
} else {
|
|
// Up.
|
|
if (_scrollTopTo == step2) {
|
|
_scrollTopTo = step1;
|
|
} else if (_scrollTopTo == step1) {
|
|
_scrollTopTo = 0;
|
|
} else {
|
|
_scrollTopTo += diff;
|
|
}
|
|
}
|
|
_timeOffset = (crl::now() - _scrollAnimation.started());
|
|
}
|
|
return;
|
|
}
|
|
const auto current = heightDiff() - top;
|
|
_inner->moveToLeft(0, std::min(0, current));
|
|
_pinnedToTop->resize(
|
|
_pinnedToTop->width(),
|
|
std::max(current + _pinnedToTop->minimumHeight(), 0));
|
|
}, _inner->lifetime());
|
|
|
|
_flexibleScroll.fillerWidthValue.events(
|
|
) | rpl::start_with_next([=](int w) {
|
|
_inner->resizeToWidth(w);
|
|
}, _inner->lifetime());
|
|
|
|
setPaintPadding({ 0, _pinnedToTop->minimumHeight(), 0, 0 });
|
|
|
|
setViewport(_pinnedToTop->events(
|
|
) | rpl::filter([](not_null<QEvent*> e) {
|
|
return e->type() == QEvent::Wheel;
|
|
}));
|
|
}
|
|
}
|
|
|
|
void Widget::setInnerFocus() {
|
|
_inner->setFocus();
|
|
}
|
|
|
|
void Widget::enableBackButton() {
|
|
_inner->enableBackButton();
|
|
}
|
|
|
|
rpl::producer<QString> Widget::title() {
|
|
if (const auto topic = controller()->key().topic()) {
|
|
return topic->peer()->isBot()
|
|
? tr::lng_info_thread_title()
|
|
: tr::lng_info_topic_title();
|
|
} else if (controller()->key().sublist()
|
|
&& controller()->key().sublist()->parentChat()) {
|
|
return tr::lng_profile_direct_messages();
|
|
}
|
|
const auto peer = controller()->key().peer();
|
|
if (const auto user = peer->asUser()) {
|
|
return (user->isBot() && !user->isSupport())
|
|
? tr::lng_info_bot_title()
|
|
: tr::lng_info_user_title();
|
|
} else if (const auto channel = peer->asChannel()) {
|
|
return channel->isMonoforum()
|
|
? tr::lng_profile_direct_messages()
|
|
: channel->isMegagroup()
|
|
? tr::lng_info_group_title()
|
|
: tr::lng_info_channel_title();
|
|
} else if (peer->isChat()) {
|
|
return tr::lng_info_group_title();
|
|
}
|
|
Unexpected("Bad peer type in Info::TitleValue()");
|
|
}
|
|
|
|
rpl::producer<Dialogs::Stories::Content> Widget::titleStories() {
|
|
const auto peer = controller()->key().peer();
|
|
if (peer && !peer->isChat()) {
|
|
return Dialogs::Stories::LastForPeer(peer);
|
|
}
|
|
return nullptr;
|
|
}
|
|
|
|
bool Widget::showInternal(not_null<ContentMemento*> memento) {
|
|
if (!controller()->validateMementoPeer(memento)) {
|
|
return false;
|
|
}
|
|
if (auto profileMemento = dynamic_cast<Memento*>(memento.get())) {
|
|
restoreState(profileMemento);
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
void Widget::setInternalState(
|
|
const QRect &geometry,
|
|
not_null<Memento*> memento) {
|
|
setGeometry(geometry);
|
|
Ui::SendPendingMoveResizeEvents(this);
|
|
restoreState(memento);
|
|
}
|
|
|
|
std::shared_ptr<ContentMemento> Widget::doCreateMemento() {
|
|
auto result = std::make_shared<Memento>(controller());
|
|
saveState(result.get());
|
|
return result;
|
|
}
|
|
|
|
void Widget::saveState(not_null<Memento*> memento) {
|
|
memento->setScrollTop(scrollTopSave());
|
|
_inner->saveState(memento);
|
|
}
|
|
|
|
void Widget::restoreState(not_null<Memento*> memento) {
|
|
_inner->restoreState(memento);
|
|
scrollTopRestore(memento->scrollTop());
|
|
}
|
|
|
|
} // namespace Info::Profile
|