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 "window/notifications_manager.h"
#include "platform/platform_specific.h" #include "platform/platform_specific.h"
#include "calls/calls_instance.h" #include "calls/calls_instance.h"
#include "window/section_widget.h"
namespace { namespace {
@ -35,6 +36,11 @@ constexpr auto kAutoLockTimeoutLateMs = TimeMs(3000);
} // namespace } // namespace
AuthSessionData::Variables::Variables()
: floatPlayerColumn(Window::Column::Second)
, floatPlayerCorner(Window::Corner::TopRight) {
}
QByteArray AuthSessionData::serialize() const { QByteArray AuthSessionData::serialize() const {
auto size = sizeof(qint32) * 4; auto size = sizeof(qint32) * 4;
for (auto i = _variables.soundOverrides.cbegin(), e = _variables.soundOverrides.cend(); i != e; ++i) { 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 << i.key() << i.value();
} }
stream << qint32(_variables.tabbedSelectorSectionTooltipShown); stream << qint32(_variables.tabbedSelectorSectionTooltipShown);
stream << qint32(_variables.floatPlayerColumn);
stream << qint32(_variables.floatPlayerCorner);
} }
return result; return result;
} }
@ -79,6 +87,8 @@ void AuthSessionData::constructFromSerialized(const QByteArray &serialized) {
qint32 lastSeenWarningSeen = 0; qint32 lastSeenWarningSeen = 0;
qint32 tabbedSelectorSectionEnabled = 1; qint32 tabbedSelectorSectionEnabled = 1;
qint32 tabbedSelectorSectionTooltipShown = 0; qint32 tabbedSelectorSectionTooltipShown = 0;
qint32 floatPlayerColumn = static_cast<qint32>(Window::Column::Second);
qint32 floatPlayerCorner = static_cast<qint32>(Window::Corner::TopRight);
QMap<QString, QString> soundOverrides; QMap<QString, QString> soundOverrides;
stream >> emojiPanTab; stream >> emojiPanTab;
stream >> lastSeenWarningSeen; stream >> lastSeenWarningSeen;
@ -99,6 +109,9 @@ void AuthSessionData::constructFromSerialized(const QByteArray &serialized) {
if (!stream.atEnd()) { if (!stream.atEnd()) {
stream >> tabbedSelectorSectionTooltipShown; stream >> tabbedSelectorSectionTooltipShown;
} }
if (!stream.atEnd()) {
stream >> floatPlayerColumn >> floatPlayerCorner;
}
if (stream.status() != QDataStream::Ok) { if (stream.status() != QDataStream::Ok) {
LOG(("App Error: Bad data for AuthSessionData::constructFromSerialized()")); LOG(("App Error: Bad data for AuthSessionData::constructFromSerialized()"));
return; return;
@ -114,6 +127,19 @@ void AuthSessionData::constructFromSerialized(const QByteArray &serialized) {
_variables.tabbedSelectorSectionEnabled = (tabbedSelectorSectionEnabled == 1); _variables.tabbedSelectorSectionEnabled = (tabbedSelectorSectionEnabled == 1);
_variables.soundOverrides = std::move(soundOverrides); _variables.soundOverrides = std::move(soundOverrides);
_variables.tabbedSelectorSectionTooltipShown = tabbedSelectorSectionTooltipShown; _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 { QString AuthSessionData::getSoundPath(const QString &key) const {

View File

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

View File

@ -74,6 +74,8 @@ Copyright (c) 2014-2017 John Preston, https://desktop.telegram.org
namespace { namespace {
constexpr auto kSaveFloatPlayerPositionTimeoutMs = TimeMs(1000);
MTPMessagesFilter typeToMediaFilter(MediaOverviewType &type) { MTPMessagesFilter typeToMediaFilter(MediaOverviewType &type) {
switch (type) { switch (type) {
case OverviewPhotos: return MTP_inputMessagesFilterPhotos(); case OverviewPhotos: return MTP_inputMessagesFilterPhotos();
@ -99,7 +101,10 @@ StackItemSection::~StackItemSection() {
} }
template <typename ToggleCallback, typename DraggedCallback> 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); toggle(this, visible);
}, [this, dragged = std::move(dragged)](bool closed) { }, [this, dragged = std::move(dragged)](bool closed) {
dragged(this, closed); dragged(this, closed);
@ -253,8 +258,8 @@ void MainWidget::checkCurrentFloatPlayer() {
}, [this](Float *instance, bool closed) { }, [this](Float *instance, bool closed) {
finishFloatPlayerDrag(instance, closed); finishFloatPlayerDrag(instance, closed);
})); }));
currentFloatPlayer()->corner = _playerFloatCorner; currentFloatPlayer()->column = AuthSession::Current().data().floatPlayerColumn();
currentFloatPlayer()->column = _playerFloatColumn; currentFloatPlayer()->corner = AuthSession::Current().data().floatPlayerCorner();
checkFloatPlayerVisibility(); checkFloatPlayerVisibility();
} }
} }
@ -375,17 +380,19 @@ void MainWidget::updateFloatPlayerColumnCorner(QPoint center) {
Expects(!_playerFloats.empty()); Expects(!_playerFloats.empty());
auto size = _playerFloats.back()->widget->size(); auto size = _playerFloats.back()->widget->size();
auto min = INT_MAX; 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 rect = mapFromGlobal(widget->rectForFloatPlayer(myColumn, playerColumn));
auto left = rect.x() + (size.width() / 2); auto left = rect.x() + (size.width() / 2);
auto right = rect.x() + rect.width() - (size.width() / 2); auto right = rect.x() + rect.width() - (size.width() / 2);
auto top = rect.y() + (size.height() / 2); auto top = rect.y() + (size.height() / 2);
auto bottom = rect.y() + rect.height() - (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) { if (min > distance) {
min = distance; min = distance;
_playerFloatColumn = playerColumn; column = playerColumn;
_playerFloatCorner = corner; corner = checked;
} }
}; };
checkCorner((QPoint(left, top) - center).manhattanLength(), Window::Corner::TopLeft); 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); 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) { void MainWidget::finishFloatPlayerDrag(Float *instance, bool closed) {
instance->dragFrom = instance->widget->pos(); instance->dragFrom = instance->widget->pos();
updateFloatPlayerColumnCorner(instance->widget->geometry().center()); updateFloatPlayerColumnCorner(instance->widget->geometry().center());
instance->column = _playerFloatColumn; instance->column = AuthSession::Current().data().floatPlayerColumn();
instance->corner = _playerFloatCorner; instance->corner = AuthSession::Current().data().floatPlayerCorner();
instance->draggedAnimation.finish(); instance->draggedAnimation.finish();
instance->draggedAnimation.start([this, instance] { updateFloatPlayerPosition(instance); }, 0., 1., st::slideDuration, anim::sineInOut); 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 "history/history_common.h"
#include "core/single_timer.h" #include "core/single_timer.h"
#include "base/weak_unique_ptr.h" #include "base/weak_unique_ptr.h"
#include "window/section_widget.h"
namespace Notify { namespace Notify {
struct PeerUpdate; struct PeerUpdate;
@ -56,7 +55,10 @@ class PlayerWrapWidget;
class TopBarWidget; class TopBarWidget;
class SectionMemento; class SectionMemento;
class SectionWidget; class SectionWidget;
class AbstractSectionWidget;
struct SectionSlideParams; struct SectionSlideParams;
enum class Column;
enum class Corner;
} // namespace Window } // namespace Window
namespace Calls { namespace Calls {
@ -470,8 +472,8 @@ private:
bool hiddenByHistory = false; bool hiddenByHistory = false;
bool visible = false; bool visible = false;
Animation visibleAnimation; Animation visibleAnimation;
Window::Corner corner = Window::Corner::TopRight; Window::Column column;
Window::Column column = Window::Column::Second; Window::Corner corner;
QPoint dragFrom; QPoint dragFrom;
Animation draggedAnimation; Animation draggedAnimation;
object_ptr<Media::Player::Float> widget; object_ptr<Media::Player::Float> widget;
@ -640,8 +642,6 @@ private:
object_ptr<Media::Player::Panel> _playerPanel; object_ptr<Media::Player::Panel> _playerPanel;
bool _playerUsingPanel = false; bool _playerUsingPanel = false;
std::vector<std::unique_ptr<Float>> _playerFloats; 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 QPointer<ConfirmBox> _forwardConfirm; // for single column layout
object_ptr<HistoryHider> _hider = { nullptr }; object_ptr<HistoryHider> _hider = { nullptr };