Save floating player position in localstorage.

This commit is contained in:
John Preston 2017-05-24 13:04:29 +03:00
parent ee6d80673a
commit 75dcce0b3c
4 changed files with 73 additions and 14 deletions

View File

@ -28,6 +28,7 @@ Copyright (c) 2014-2017 John Preston, https://desktop.telegram.org
#include "window/notifications_manager.h"
#include "platform/platform_specific.h"
#include "calls/calls_instance.h"
#include "window/section_widget.h"
namespace {
@ -35,6 +36,11 @@ constexpr auto kAutoLockTimeoutLateMs = TimeMs(3000);
} // namespace
AuthSessionData::Variables::Variables()
: floatPlayerColumn(Window::Column::Second)
, floatPlayerCorner(Window::Corner::TopRight) {
}
QByteArray AuthSessionData::serialize() const {
auto size = sizeof(qint32) * 4;
for (auto i = _variables.soundOverrides.cbegin(), e = _variables.soundOverrides.cend(); i != e; ++i) {
@ -59,6 +65,8 @@ QByteArray AuthSessionData::serialize() const {
stream << i.key() << i.value();
}
stream << qint32(_variables.tabbedSelectorSectionTooltipShown);
stream << qint32(_variables.floatPlayerColumn);
stream << qint32(_variables.floatPlayerCorner);
}
return result;
}
@ -79,6 +87,8 @@ void AuthSessionData::constructFromSerialized(const QByteArray &serialized) {
qint32 lastSeenWarningSeen = 0;
qint32 tabbedSelectorSectionEnabled = 1;
qint32 tabbedSelectorSectionTooltipShown = 0;
qint32 floatPlayerColumn = static_cast<qint32>(Window::Column::Second);
qint32 floatPlayerCorner = static_cast<qint32>(Window::Corner::TopRight);
QMap<QString, QString> soundOverrides;
stream >> emojiPanTab;
stream >> lastSeenWarningSeen;
@ -99,6 +109,9 @@ void AuthSessionData::constructFromSerialized(const QByteArray &serialized) {
if (!stream.atEnd()) {
stream >> tabbedSelectorSectionTooltipShown;
}
if (!stream.atEnd()) {
stream >> floatPlayerColumn >> floatPlayerCorner;
}
if (stream.status() != QDataStream::Ok) {
LOG(("App Error: Bad data for AuthSessionData::constructFromSerialized()"));
return;
@ -114,6 +127,19 @@ void AuthSessionData::constructFromSerialized(const QByteArray &serialized) {
_variables.tabbedSelectorSectionEnabled = (tabbedSelectorSectionEnabled == 1);
_variables.soundOverrides = std::move(soundOverrides);
_variables.tabbedSelectorSectionTooltipShown = tabbedSelectorSectionTooltipShown;
auto uncheckedColumn = static_cast<Window::Column>(floatPlayerColumn);
switch (uncheckedColumn) {
case Window::Column::First:
case Window::Column::Second:
case Window::Column::Third: _variables.floatPlayerColumn = uncheckedColumn; break;
}
auto uncheckedCorner = static_cast<Window::Corner>(floatPlayerCorner);
switch (uncheckedCorner) {
case Window::Corner::TopLeft:
case Window::Corner::TopRight:
case Window::Corner::BottomLeft:
case Window::Corner::BottomRight: _variables.floatPlayerCorner = uncheckedCorner; break;
}
}
QString AuthSessionData::getSoundPath(const QString &key) const {

View File

@ -30,6 +30,8 @@ namespace Window {
namespace Notifications {
class System;
} // namespace Notifications
enum class Column;
enum class Corner;
} // namespace Window
namespace Calls {
@ -102,14 +104,30 @@ public:
int tabbedSelectorSectionTooltipShown() const {
return _variables.tabbedSelectorSectionTooltipShown;
}
void setFloatPlayerColumn(Window::Column column) {
_variables.floatPlayerColumn = column;
}
Window::Column floatPlayerColumn() const {
return _variables.floatPlayerColumn;
}
void setFloatPlayerCorner(Window::Corner corner) {
_variables.floatPlayerCorner = corner;
}
Window::Corner floatPlayerCorner() const {
return _variables.floatPlayerCorner;
}
private:
struct Variables {
Variables();
bool lastSeenWarningSeen = false;
EmojiPanelTab emojiPanelTab = EmojiPanelTab::Emoji;
bool tabbedSelectorSectionEnabled = true;
int tabbedSelectorSectionTooltipShown = 0;
QMap<QString, QString> soundOverrides;
Window::Column floatPlayerColumn;
Window::Corner floatPlayerCorner;
};
base::Variable<bool> _contactsLoaded = { false };

View File

@ -74,6 +74,8 @@ Copyright (c) 2014-2017 John Preston, https://desktop.telegram.org
namespace {
constexpr auto kSaveFloatPlayerPositionTimeoutMs = TimeMs(1000);
MTPMessagesFilter typeToMediaFilter(MediaOverviewType &type) {
switch (type) {
case OverviewPhotos: return MTP_inputMessagesFilterPhotos();
@ -99,7 +101,10 @@ StackItemSection::~StackItemSection() {
}
template <typename ToggleCallback, typename DraggedCallback>
MainWidget::Float::Float(QWidget *parent, HistoryItem *item, ToggleCallback toggle, DraggedCallback dragged) : widget(parent, item, [this, toggle = std::move(toggle)](bool visible) {
MainWidget::Float::Float(QWidget *parent, HistoryItem *item, ToggleCallback toggle, DraggedCallback dragged)
: column(Window::Column::Second)
, corner(Window::Corner::TopRight)
, widget(parent, item, [this, toggle = std::move(toggle)](bool visible) {
toggle(this, visible);
}, [this, dragged = std::move(dragged)](bool closed) {
dragged(this, closed);
@ -253,8 +258,8 @@ void MainWidget::checkCurrentFloatPlayer() {
}, [this](Float *instance, bool closed) {
finishFloatPlayerDrag(instance, closed);
}));
currentFloatPlayer()->corner = _playerFloatCorner;
currentFloatPlayer()->column = _playerFloatColumn;
currentFloatPlayer()->column = AuthSession::Current().data().floatPlayerColumn();
currentFloatPlayer()->corner = AuthSession::Current().data().floatPlayerCorner();
checkFloatPlayerVisibility();
}
}
@ -375,17 +380,19 @@ void MainWidget::updateFloatPlayerColumnCorner(QPoint center) {
Expects(!_playerFloats.empty());
auto size = _playerFloats.back()->widget->size();
auto min = INT_MAX;
auto checkSection = [this, center, size, &min](Window::AbstractSectionWidget *widget, Window::Column myColumn, Window::Column playerColumn) {
auto column = AuthSession::Current().data().floatPlayerColumn();
auto corner = AuthSession::Current().data().floatPlayerCorner();
auto checkSection = [this, center, size, &min, &column, &corner](Window::AbstractSectionWidget *widget, Window::Column myColumn, Window::Column playerColumn) {
auto rect = mapFromGlobal(widget->rectForFloatPlayer(myColumn, playerColumn));
auto left = rect.x() + (size.width() / 2);
auto right = rect.x() + rect.width() - (size.width() / 2);
auto top = rect.y() + (size.height() / 2);
auto bottom = rect.y() + rect.height() - (size.height() / 2);
auto checkCorner = [this, playerColumn, &min](int distance, Window::Corner corner) {
auto checkCorner = [this, playerColumn, &min, &column, &corner](int distance, Window::Corner checked) {
if (min > distance) {
min = distance;
_playerFloatColumn = playerColumn;
_playerFloatCorner = corner;
column = playerColumn;
corner = checked;
}
};
checkCorner((QPoint(left, top) - center).manhattanLength(), Window::Corner::TopLeft);
@ -418,14 +425,22 @@ void MainWidget::updateFloatPlayerColumnCorner(QPoint center) {
checkSection(_history, Window::Column::Second, Window::Column::Third);
}
}
if (AuthSession::Current().data().floatPlayerColumn() != column) {
AuthSession::Current().data().setFloatPlayerColumn(column);
AuthSession::Current().saveDataDelayed(kSaveFloatPlayerPositionTimeoutMs);
}
if (AuthSession::Current().data().floatPlayerCorner() != corner) {
AuthSession::Current().data().setFloatPlayerCorner(corner);
AuthSession::Current().saveDataDelayed(kSaveFloatPlayerPositionTimeoutMs);
}
}
void MainWidget::finishFloatPlayerDrag(Float *instance, bool closed) {
instance->dragFrom = instance->widget->pos();
updateFloatPlayerColumnCorner(instance->widget->geometry().center());
instance->column = _playerFloatColumn;
instance->corner = _playerFloatCorner;
instance->column = AuthSession::Current().data().floatPlayerColumn();
instance->corner = AuthSession::Current().data().floatPlayerCorner();
instance->draggedAnimation.finish();
instance->draggedAnimation.start([this, instance] { updateFloatPlayerPosition(instance); }, 0., 1., st::slideDuration, anim::sineInOut);

View File

@ -24,7 +24,6 @@ Copyright (c) 2014-2017 John Preston, https://desktop.telegram.org
#include "history/history_common.h"
#include "core/single_timer.h"
#include "base/weak_unique_ptr.h"
#include "window/section_widget.h"
namespace Notify {
struct PeerUpdate;
@ -56,7 +55,10 @@ class PlayerWrapWidget;
class TopBarWidget;
class SectionMemento;
class SectionWidget;
class AbstractSectionWidget;
struct SectionSlideParams;
enum class Column;
enum class Corner;
} // namespace Window
namespace Calls {
@ -470,8 +472,8 @@ private:
bool hiddenByHistory = false;
bool visible = false;
Animation visibleAnimation;
Window::Corner corner = Window::Corner::TopRight;
Window::Column column = Window::Column::Second;
Window::Column column;
Window::Corner corner;
QPoint dragFrom;
Animation draggedAnimation;
object_ptr<Media::Player::Float> widget;
@ -640,8 +642,6 @@ private:
object_ptr<Media::Player::Panel> _playerPanel;
bool _playerUsingPanel = false;
std::vector<std::unique_ptr<Float>> _playerFloats;
Window::Corner _playerFloatCorner = Window::Corner::TopRight;
Window::Column _playerFloatColumn = Window::Column::Second;
QPointer<ConfirmBox> _forwardConfirm; // for single column layout
object_ptr<HistoryHider> _hider = { nullptr };