Move dialogsWidthRatio to AuthSessionData.

This commit is contained in:
John Preston 2017-11-05 17:57:51 +04:00
parent 4771ea7cd4
commit 9a56b2d20f
11 changed files with 64 additions and 82 deletions

View File

@ -73,6 +73,7 @@ QByteArray AuthSessionData::serialize() const {
}
stream << qint32(_variables.thirdSectionInfoEnabled ? 1 : 0);
stream << qint32(_variables.smallDialogsList ? 1 : 0);
stream << qint32(snap(qRound(_variables.dialogsWidthRatio.current() * 1000000), 0, 1000000));
}
return result;
}
@ -94,6 +95,7 @@ void AuthSessionData::constructFromSerialized(const QByteArray &serialized) {
base::flat_set<PeerId> groupStickersSectionHidden;
qint32 thirdSectionInfoEnabled = 0;
qint32 smallDialogsList = 0;
float64 dialogsWidthRatio = _variables.dialogsWidthRatio.current();
stream >> selectorTab;
stream >> lastSeenWarningSeen;
if (!stream.atEnd()) {
@ -131,6 +133,11 @@ void AuthSessionData::constructFromSerialized(const QByteArray &serialized) {
stream >> thirdSectionInfoEnabled;
stream >> smallDialogsList;
}
if (!stream.atEnd()) {
qint32 value = 0;
stream >> value;
dialogsWidthRatio = snap(value / 1000000., 0., 1.);
}
if (stream.status() != QDataStream::Ok) {
LOG(("App Error: Bad data for AuthSessionData::constructFromSerialized()"));
return;
@ -162,7 +169,7 @@ void AuthSessionData::constructFromSerialized(const QByteArray &serialized) {
_variables.groupStickersSectionHidden = std::move(groupStickersSectionHidden);
_variables.thirdSectionInfoEnabled = thirdSectionInfoEnabled;
_variables.smallDialogsList = smallDialogsList;
_variables.dialogsWidthRatio = dialogsWidthRatio;
if (_variables.thirdSectionInfoEnabled) {
_variables.tabbedSelectorSectionEnabled = false;
}

View File

@ -22,6 +22,7 @@ Copyright (c) 2014-2017 John Preston, https://desktop.telegram.org
#include <rpl/event_stream.h>
#include <rpl/filter.h>
#include <rpl/variable.h>
#include "base/timer.h"
namespace Storage {
@ -144,8 +145,8 @@ public:
});
}
void copyFrom(const AuthSessionData &other) {
_variables = other._variables;
void moveFrom(AuthSessionData &&other) {
_variables = std::move(other._variables);
}
QByteArray serialize() const;
void constructFromSerialized(const QByteArray &serialized);
@ -219,6 +220,16 @@ public:
RectPart floatPlayerCorner() const {
return _variables.floatPlayerCorner;
}
void setDialogsWidthRatio(float64 ratio) {
_variables.dialogsWidthRatio = ratio;
}
float64 dialogsWidthRatio() const {
return _variables.dialogsWidthRatio.current();
}
rpl::producer<float64> dialogsWidthRatioChanges() const {
return _variables.dialogsWidthRatio.changes();
}
void setGroupStickersSectionHidden(PeerId peerId) {
_variables.groupStickersSectionHidden.insert(peerId);
}
@ -233,16 +244,19 @@ private:
struct Variables {
Variables();
static constexpr auto kDefaultDialogsWidthRatio = 5. / 14;
bool lastSeenWarningSeen = false;
ChatHelpers::SelectorTab selectorTab;
bool tabbedSelectorSectionEnabled = false;
ChatHelpers::SelectorTab selectorTab; // per-window
bool tabbedSelectorSectionEnabled = false; // per-window
int tabbedSelectorSectionTooltipShown = 0;
QMap<QString, QString> soundOverrides;
Window::Column floatPlayerColumn;
RectPart floatPlayerCorner;
Window::Column floatPlayerColumn; // per-window
RectPart floatPlayerCorner; // per-window
base::flat_set<PeerId> groupStickersSectionHidden;
bool thirdSectionInfoEnabled = true;
bool smallDialogsList = false;
bool thirdSectionInfoEnabled = true; // per-window
bool smallDialogsList = false; // per-window
rpl::variable<float64> dialogsWidthRatio = kDefaultDialogsWidthRatio; // per-window
};
base::Variable<bool> _contactsLoaded = { false };

View File

@ -166,10 +166,6 @@ void joinGroupByHash(const QString &hash) {
if (MainWidget *m = main()) m->joinGroupByHash(hash);
}
void stickersBox(const QString &name) {
if (MainWidget *m = main()) m->stickersBox(MTP_inputStickerSetShortName(MTP_string(name)));
}
void removeDialog(History *history) {
if (MainWidget *m = main()) {
m->removeDialog(history);

View File

@ -136,7 +136,6 @@ void activateBotCommand(const HistoryItem *msg, int row, int col);
void searchByHashtag(const QString &tag, PeerData *inPeer);
void openPeerByName(const QString &username, MsgId msgId = ShowAtUnreadMsgId, const QString &startToken = QString());
void joinGroupByHash(const QString &hash);
void stickersBox(const QString &name);
void removeDialog(History *history);
void showSettings();

View File

@ -262,9 +262,10 @@ MainWidget::MainWidget(
subscribe(_controller->dialogsListDisplayForced(), [this](bool) {
updateDialogsWidthAnimated();
});
subscribe(_controller->dialogsWidthRatio(), [this](float64) {
updateControlsGeometry();
});
Auth().data().dialogsWidthRatioChanges()
| rpl::start_with_next(
[this] { updateControlsGeometry(); },
lifetime());
subscribe(_controller->floatPlayerAreaUpdated(), [this] {
checkFloatPlayerVisibility();
});
@ -3516,7 +3517,7 @@ void MainWidget::resizeEvent(QResizeEvent *e) {
void MainWidget::updateControlsGeometry() {
updateWindowAdaptiveLayout();
if (_controller->dialogsWidthRatio().value() > 0) {
if (Auth().data().dialogsWidthRatio() > 0) {
_a_dialogsWidth.finish();
}
if (!_a_dialogsWidth.animating()) {
@ -3616,12 +3617,12 @@ void MainWidget::updateControlsGeometry() {
}
void MainWidget::updateDialogsWidthAnimated() {
if (_controller->dialogsWidthRatio().value() > 0) {
if (Auth().data().dialogsWidthRatio() > 0) {
return;
}
auto dialogsWidth = _dialogsWidth;
updateWindowAdaptiveLayout();
if (!_controller->dialogsWidthRatio().value()
if (!Auth().data().dialogsWidthRatio()
&& (_dialogsWidth != dialogsWidth
|| _a_dialogsWidth.animating())) {
_dialogs->startWidthAnimation();
@ -3722,16 +3723,15 @@ bool MainWidget::eventFilter(QObject *o, QEvent *e) {
} else if (e->type() == QEvent::MouseButtonRelease) {
_resizingSide = false;
if (!Adaptive::OneColumn()
&& _controller->dialogsWidthRatio().value() > 0) {
_controller->dialogsWidthRatio().set(
float64(_dialogsWidth) / width(),
true);
&& Auth().data().dialogsWidthRatio() > 0) {
Auth().data().setDialogsWidthRatio(
float64(_dialogsWidth) / width());
}
Local::writeUserSettings();
} else if (e->type() == QEvent::MouseMove && _resizingSide) {
auto newWidth = mouseLeft() - _resizingSideShift;
auto newRatio = (newWidth < st::dialogsWidthMin / 2) ? 0. : float64(newWidth) / width();
_controller->dialogsWidthRatio().set(newRatio, true);
Auth().data().setDialogsWidthRatio(newRatio);
}
} else if (e->type() == QEvent::FocusIn) {
if (auto widget = qobject_cast<QWidget*>(o)) {
@ -3771,7 +3771,7 @@ void MainWidget::handleAdaptiveLayoutUpdate() {
void MainWidget::updateWindowAdaptiveLayout() {
auto layout = _controller->computeColumnLayout();
auto dialogsWidthRatio = _controller->dialogsWidthRatio().value();
auto dialogsWidthRatio = Auth().data().dialogsWidthRatio();
// Check if we are in a single-column layout in a wide enough window
// for the normal layout. If so, switch to the normal layout.
@ -3814,7 +3814,7 @@ void MainWidget::updateWindowAdaptiveLayout() {
//}
}
_controller->dialogsWidthRatio().set(dialogsWidthRatio, true);
Auth().data().setDialogsWidthRatio(dialogsWidthRatio);
auto useSmallColumnWidth = !Adaptive::OneColumn()
&& !dialogsWidthRatio

View File

@ -68,7 +68,7 @@ Messenger *Messenger::InstancePointer() {
struct Messenger::Private {
UserId authSessionUserId = 0;
std::unique_ptr<Local::StoredAuthSession> storedAuthSession;
std::unique_ptr<AuthSessionData> storedAuthSession;
MTP::Instance::Config mtpConfig;
MTP::AuthKeysList mtpKeysToDestroy;
base::Timer quitTimer;
@ -337,14 +337,14 @@ void Messenger::setAuthSessionUserId(UserId userId) {
_private->authSessionUserId = userId;
}
void Messenger::setAuthSessionFromStorage(std::unique_ptr<Local::StoredAuthSession> data) {
void Messenger::setAuthSessionFromStorage(std::unique_ptr<AuthSessionData> data) {
Expects(!authSession());
_private->storedAuthSession = std::move(data);
}
AuthSessionData *Messenger::getAuthSessionData() {
if (_private->authSessionUserId) {
return _private->storedAuthSession ? &_private->storedAuthSession->data : nullptr;
return _private->storedAuthSession ? _private->storedAuthSession.get() : nullptr;
} else if (_authSession) {
return &_authSession->data();
}
@ -414,11 +414,8 @@ void Messenger::startMtp() {
}
if (_private->storedAuthSession) {
if (_authSession) {
_authSession->data().copyFrom(_private->storedAuthSession->data);
if (auto window = App::wnd()) {
Assert(window->controller() != nullptr);
window->controller()->dialogsWidthRatio().set(_private->storedAuthSession->dialogsWidthRatio);
}
_authSession->data().moveFrom(
std::move(*_private->storedAuthSession));
}
_private->storedAuthSession.reset();
}

View File

@ -43,10 +43,6 @@ class FileUploader;
class Translator;
class MediaView;
namespace Local {
struct StoredAuthSession;
} // namespace Local
namespace Media {
namespace Audio {
class Instance;
@ -112,7 +108,7 @@ public:
void setMtpMainDcId(MTP::DcId mainDcId);
void setMtpKey(MTP::DcId dcId, const MTP::AuthKey::Data &keyData);
void setAuthSessionUserId(UserId userId);
void setAuthSessionFromStorage(std::unique_ptr<Local::StoredAuthSession> data);
void setAuthSessionFromStorage(std::unique_ptr<AuthSessionData> data);
AuthSessionData *getAuthSessionData();
// Serialization.

View File

@ -571,7 +571,7 @@ enum {
dbiNotificationsCount = 0x45,
dbiNotificationsCorner = 0x46,
dbiThemeKey = 0x47,
dbiDialogsWidthRatio = 0x48,
dbiDialogsWidthRatioOld = 0x48,
dbiUseExternalVideoPlayer = 0x49,
dbiDcOptions = 0x4a,
dbiMtpAuthorization = 0x4b,
@ -646,10 +646,10 @@ enum class WriteMapWhen {
Soon,
};
std::unique_ptr<StoredAuthSession> StoredAuthSessionCache;
StoredAuthSession &GetStoredAuthSessionCache() {
std::unique_ptr<AuthSessionData> StoredAuthSessionCache;
AuthSessionData &GetStoredAuthSessionCache() {
if (!StoredAuthSessionCache) {
StoredAuthSessionCache = std::make_unique<StoredAuthSession>();
StoredAuthSessionCache = std::make_unique<AuthSessionData>();
}
return *StoredAuthSessionCache;
}
@ -1102,12 +1102,12 @@ bool _readSetting(quint32 blockId, QDataStream &stream, int version, ReadSetting
Global::SetNotificationsCorner(static_cast<Notify::ScreenCorner>((v >= 0 && v < 4) ? v : 2));
} break;
case dbiDialogsWidthRatio: {
case dbiDialogsWidthRatioOld: {
qint32 v;
stream >> v;
if (!_checkStreamStatus(stream)) return false;
GetStoredAuthSessionCache().dialogsWidthRatio = v / 1000000.;
GetStoredAuthSessionCache().setDialogsWidthRatio(v / 1000000.);
} break;
case dbiLastSeenWarningSeenOld: {
@ -1115,7 +1115,7 @@ bool _readSetting(quint32 blockId, QDataStream &stream, int version, ReadSetting
stream >> v;
if (!_checkStreamStatus(stream)) return false;
GetStoredAuthSessionCache().data.setLastSeenWarningSeen(v == 1);
GetStoredAuthSessionCache().setLastSeenWarningSeen(v == 1);
} break;
case dbiAuthSessionData: {
@ -1123,7 +1123,7 @@ bool _readSetting(quint32 blockId, QDataStream &stream, int version, ReadSetting
stream >> v;
if (!_checkStreamStatus(stream)) return false;
GetStoredAuthSessionCache().data.constructFromSerialized(v);
GetStoredAuthSessionCache().constructFromSerialized(v);
} break;
case dbiWorkMode: {
@ -1766,18 +1766,8 @@ void _writeUserSettings() {
recentEmojiPreloadData.push_back(qMakePair(item.first->id(), item.second));
}
}
auto userDataInstance = StoredAuthSessionCache ? &StoredAuthSessionCache->data : Messenger::Instance().getAuthSessionData();
auto userDataInstance = StoredAuthSessionCache ? StoredAuthSessionCache.get() : Messenger::Instance().getAuthSessionData();
auto userData = userDataInstance ? userDataInstance->serialize() : QByteArray();
auto dialogsWidthRatio = [] {
if (StoredAuthSessionCache) {
return StoredAuthSessionCache->dialogsWidthRatio;
} else if (auto window = App::wnd()) {
if (auto controller = window->controller()) {
return controller->dialogsWidthRatio().value();
}
}
return Window::Controller::kDefaultDialogsWidthRatio;
};
uint32 size = 21 * (sizeof(quint32) + sizeof(qint32));
size += sizeof(quint32) + Serialize::stringSize(Global::AskDownloadPath() ? QString() : Global::DownloadPath()) + Serialize::bytearraySize(Global::AskDownloadPath() ? QByteArray() : Global::DownloadPathBookmark());
@ -1822,7 +1812,6 @@ void _writeUserSettings() {
data.stream << quint32(dbiDialogsMode) << qint32(Global::DialogsModeEnabled() ? 1 : 0) << static_cast<qint32>(Global::DialogsMode());
data.stream << quint32(dbiModerateMode) << qint32(Global::ModerateModeEnabled() ? 1 : 0);
data.stream << quint32(dbiAutoPlay) << qint32(cAutoPlayGif() ? 1 : 0);
data.stream << quint32(dbiDialogsWidthRatio) << qint32(snap(qRound(dialogsWidthRatio() * 1000000), 0, 1000000));
data.stream << quint32(dbiUseExternalVideoPlayer) << qint32(cUseExternalVideoPlayer());
if (!userData.isEmpty()) {
data.stream << quint32(dbiAuthSessionData) << userData;

View File

@ -32,11 +32,6 @@ struct Cached;
namespace Local {
struct StoredAuthSession {
AuthSessionData data;
float64 dialogsWidthRatio;
};
void start();
void finish();

View File

@ -108,13 +108,13 @@ Controller::ColumnLayout Controller::computeColumnLayout() const {
dialogsWidth = chatWidth = bodyWidth;
} else if (useNormalLayout()) {
layout = Adaptive::WindowLayout::Normal;
dialogsWidth = qRound(bodyWidth * dialogsWidthRatio().value());
dialogsWidth = qRound(bodyWidth * Auth().data().dialogsWidthRatio());
accumulate_max(dialogsWidth, st::columnMinimalWidthLeft);
accumulate_min(dialogsWidth, bodyWidth - st::columnMinimalWidthMain);
chatWidth = bodyWidth - dialogsWidth;
} else {
layout = Adaptive::WindowLayout::ThreeColumn;
dialogsWidth = qRound(bodyWidth * dialogsWidthRatio().value());
dialogsWidth = qRound(bodyWidth * Auth().data().dialogsWidthRatio());
accumulate_max(dialogsWidth, st::columnMinimalWidthLeft);
thirdWidth = st::columnMinimalWidthThird;
accumulate_min(
@ -161,9 +161,8 @@ void Controller::resizeForThirdSection() {
minimalThreeColumnWidth() - layout.bodyWidth,
st::columnMinimalWidthThird);
auto newBodyWidth = layout.bodyWidth + extendBy;
dialogsWidthRatio().set(
(dialogsWidthRatio().value() * layout.bodyWidth) / newBodyWidth,
true);
auto currentRatio = Auth().data().dialogsWidthRatio();
Auth().data().setDialogsWidthRatio((currentRatio * layout.bodyWidth) / newBodyWidth);
window()->tryToExtendWidthBy(extendBy);
Auth().data().setTabbedSelectorSectionEnabled(
@ -181,9 +180,8 @@ void Controller::closeThirdSection() {
auto newBodyWidth = noResize
? layout.bodyWidth
: (layout.bodyWidth - layout.thirdWidth);
dialogsWidthRatio().set(
(dialogsWidthRatio().value() * layout.bodyWidth) / newBodyWidth,
true);
auto currentRatio = Auth().data().dialogsWidthRatio();
Auth().data().setDialogsWidthRatio((currentRatio * layout.bodyWidth) / newBodyWidth);
newWindowSize = QSize(
window()->width() + (newBodyWidth - layout.bodyWidth),
window()->height());

View File

@ -76,8 +76,6 @@ class SectionMemento;
class Controller {
public:
static constexpr auto kDefaultDialogsWidthRatio = 5. / 14;
Controller(not_null<MainWindow*> window) : _window(window) {
}
@ -170,12 +168,6 @@ public:
not_null<PeerData*> peer,
QDate requestedDate);
base::Variable<float64> &dialogsWidthRatio() {
return _dialogsWidthRatio;
}
const base::Variable<float64> &dialogsWidthRatio() const {
return _dialogsWidthRatio;
}
base::Variable<bool> &dialogsListFocused() {
return _dialogsListFocused;
}
@ -199,7 +191,6 @@ private:
base::Observable<void> _gifPauseLevelChanged;
base::Observable<void> _floatPlayerAreaUpdated;
base::Variable<float64> _dialogsWidthRatio = { kDefaultDialogsWidthRatio };
base::Variable<bool> _dialogsListFocused = { false };
base::Variable<bool> _dialogsListDisplayForced = { false };