2017-05-22 15:25:49 +00:00
|
|
|
/*
|
|
|
|
This file is part of Telegram Desktop,
|
2018-01-03 10:23:14 +00:00
|
|
|
the official desktop application for the Telegram messaging service.
|
2017-05-22 15:25:49 +00:00
|
|
|
|
2018-01-03 10:23:14 +00:00
|
|
|
For license and copyright information please follow this link:
|
|
|
|
https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
|
2017-05-22 15:25:49 +00:00
|
|
|
*/
|
|
|
|
#include "media/player/media_player_float.h"
|
|
|
|
|
2017-11-19 14:41:52 +00:00
|
|
|
#include <rpl/merge.h>
|
2017-09-26 11:49:16 +00:00
|
|
|
#include "data/data_document.h"
|
2018-01-04 10:22:53 +00:00
|
|
|
#include "data/data_session.h"
|
2018-01-14 16:02:25 +00:00
|
|
|
#include "data/data_media_types.h"
|
2019-08-02 13:21:09 +00:00
|
|
|
#include "history/view/media/history_view_media.h"
|
2018-01-13 12:45:11 +00:00
|
|
|
#include "history/history_item.h"
|
2020-06-25 12:16:09 +00:00
|
|
|
#include "history/history.h"
|
2018-01-17 18:20:55 +00:00
|
|
|
#include "history/view/history_view_element.h"
|
2019-02-13 12:36:59 +00:00
|
|
|
#include "media/audio/media_audio.h"
|
2019-12-11 12:09:21 +00:00
|
|
|
#include "media/streaming/media_streaming_instance.h"
|
2019-02-27 11:36:19 +00:00
|
|
|
#include "media/view/media_view_playback_progress.h"
|
2018-11-05 11:16:09 +00:00
|
|
|
#include "media/player/media_player_instance.h"
|
2019-06-06 10:21:40 +00:00
|
|
|
#include "window/window_session_controller.h"
|
2018-11-05 11:16:09 +00:00
|
|
|
#include "window/section_widget.h"
|
2020-06-25 12:16:09 +00:00
|
|
|
#include "core/application.h"
|
|
|
|
#include "core/core_settings.h"
|
2019-07-24 11:45:24 +00:00
|
|
|
#include "main/main_session.h"
|
2020-06-25 15:01:10 +00:00
|
|
|
#include "main/main_account.h"
|
2019-09-13 06:06:02 +00:00
|
|
|
#include "facades.h"
|
|
|
|
#include "app.h"
|
2017-10-05 15:35:52 +00:00
|
|
|
#include "styles/style_media_player.h"
|
2017-05-22 15:25:49 +00:00
|
|
|
#include "styles/style_history.h"
|
|
|
|
|
2019-09-04 07:19:15 +00:00
|
|
|
#include <QtWidgets/QApplication>
|
|
|
|
|
2017-05-22 15:25:49 +00:00
|
|
|
namespace Media {
|
|
|
|
namespace Player {
|
|
|
|
|
2017-10-05 15:35:52 +00:00
|
|
|
Float::Float(
|
|
|
|
QWidget *parent,
|
2018-01-21 14:49:42 +00:00
|
|
|
not_null<HistoryItem*> item,
|
2018-06-04 15:35:11 +00:00
|
|
|
Fn<void(bool visible)> toggleCallback,
|
|
|
|
Fn<void(bool closed)> draggedCallback)
|
2017-10-05 15:35:52 +00:00
|
|
|
: RpWidget(parent)
|
2017-05-22 15:25:49 +00:00
|
|
|
, _item(item)
|
2017-05-23 18:00:57 +00:00
|
|
|
, _toggleCallback(std::move(toggleCallback))
|
|
|
|
, _draggedCallback(std::move(draggedCallback)) {
|
2018-01-14 16:02:25 +00:00
|
|
|
auto media = _item->media();
|
2017-08-17 09:06:26 +00:00
|
|
|
Assert(media != nullptr);
|
2017-05-22 15:25:49 +00:00
|
|
|
|
2018-01-14 16:02:25 +00:00
|
|
|
auto document = media->document();
|
2017-08-17 09:06:26 +00:00
|
|
|
Assert(document != nullptr);
|
2017-12-10 10:26:58 +00:00
|
|
|
Assert(document->isVideoMessage());
|
2017-05-22 15:25:49 +00:00
|
|
|
|
|
|
|
auto margin = st::mediaPlayerFloatMargin;
|
|
|
|
auto size = 2 * margin + st::mediaPlayerFloatSize;
|
|
|
|
resize(size, size);
|
|
|
|
|
|
|
|
prepareShadow();
|
|
|
|
|
2020-06-25 12:16:09 +00:00
|
|
|
document->session().data().itemRepaintRequest(
|
2017-12-22 07:05:20 +00:00
|
|
|
) | rpl::start_with_next([this](auto item) {
|
|
|
|
if (_item == item) {
|
|
|
|
repaintItem();
|
|
|
|
}
|
|
|
|
}, lifetime());
|
2019-08-06 16:40:08 +00:00
|
|
|
|
2020-06-25 12:16:09 +00:00
|
|
|
document->session().data().itemRemoved(
|
2017-12-22 07:05:20 +00:00
|
|
|
) | rpl::start_with_next([this](auto item) {
|
|
|
|
if (_item == item) {
|
|
|
|
detach();
|
|
|
|
}
|
|
|
|
}, lifetime());
|
2017-05-23 14:40:25 +00:00
|
|
|
|
2020-06-25 15:01:10 +00:00
|
|
|
document->session().account().sessionChanges(
|
|
|
|
) | rpl::start_with_next([=] {
|
|
|
|
detach();
|
|
|
|
}, lifetime());
|
|
|
|
|
2017-05-23 14:40:25 +00:00
|
|
|
setCursor(style::cur_pointer);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Float::mousePressEvent(QMouseEvent *e) {
|
|
|
|
_down = true;
|
2017-05-23 18:00:57 +00:00
|
|
|
_downPoint = e->pos();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Float::mouseMoveEvent(QMouseEvent *e) {
|
|
|
|
if (_down && (e->pos() - _downPoint).manhattanLength() > QApplication::startDragDistance()) {
|
|
|
|
_down = false;
|
|
|
|
_drag = true;
|
|
|
|
_dragLocalPoint = e->pos();
|
|
|
|
} else if (_drag) {
|
|
|
|
auto delta = (e->pos() - _dragLocalPoint);
|
|
|
|
move(pos() + delta);
|
|
|
|
setOpacity(outRatio());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
float64 Float::outRatio() const {
|
|
|
|
auto parent = parentWidget()->rect();
|
|
|
|
auto min = 1.;
|
|
|
|
if (x() < parent.x()) {
|
|
|
|
accumulate_min(min, 1. - (parent.x() - x()) / float64(width()));
|
|
|
|
}
|
|
|
|
if (y() < parent.y()) {
|
|
|
|
accumulate_min(min, 1. - (parent.y() - y()) / float64(height()));
|
|
|
|
}
|
|
|
|
if (x() + width() > parent.x() + parent.width()) {
|
|
|
|
accumulate_min(min, 1. - (x() + width() - parent.x() - parent.width()) / float64(width()));
|
|
|
|
}
|
|
|
|
if (y() + height() > parent.y() + parent.height()) {
|
|
|
|
accumulate_min(min, 1. - (y() + height() - parent.y() - parent.height()) / float64(height()));
|
|
|
|
}
|
|
|
|
return snap(min, 0., 1.);
|
2017-05-23 14:40:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Float::mouseReleaseEvent(QMouseEvent *e) {
|
2018-01-21 14:49:42 +00:00
|
|
|
if (base::take(_down) && _item) {
|
2019-03-26 08:54:51 +00:00
|
|
|
pauseResume();
|
2017-05-23 14:40:25 +00:00
|
|
|
}
|
2017-05-23 18:00:57 +00:00
|
|
|
if (_drag) {
|
|
|
|
finishDrag(outRatio() < 0.5);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Float::finishDrag(bool closed) {
|
|
|
|
_drag = false;
|
|
|
|
if (_draggedCallback) {
|
|
|
|
_draggedCallback(closed);
|
|
|
|
}
|
2017-05-23 14:40:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Float::mouseDoubleClickEvent(QMouseEvent *e) {
|
|
|
|
if (_item) {
|
|
|
|
// Handle second click.
|
2019-03-26 08:54:51 +00:00
|
|
|
pauseResume();
|
2017-05-23 14:40:25 +00:00
|
|
|
Ui::showPeerHistoryAtItem(_item);
|
|
|
|
}
|
2017-05-22 15:25:49 +00:00
|
|
|
}
|
|
|
|
|
2019-03-26 08:54:51 +00:00
|
|
|
void Float::pauseResume() {
|
2019-12-11 10:15:48 +00:00
|
|
|
if (const auto streamed = getStreamed()) {
|
|
|
|
if (streamed->paused()) {
|
|
|
|
streamed->resume();
|
2019-03-26 08:54:51 +00:00
|
|
|
} else {
|
2019-12-11 10:15:48 +00:00
|
|
|
streamed->pause();
|
2019-03-26 08:54:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-22 15:25:49 +00:00
|
|
|
void Float::detach() {
|
|
|
|
if (_item) {
|
|
|
|
_item = nullptr;
|
|
|
|
if (_toggleCallback) {
|
|
|
|
_toggleCallback(false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Float::prepareShadow() {
|
|
|
|
auto shadow = QImage(size() * cIntRetinaFactor(), QImage::Format_ARGB32_Premultiplied);
|
|
|
|
shadow.fill(Qt::transparent);
|
|
|
|
shadow.setDevicePixelRatio(cRetinaFactor());
|
|
|
|
{
|
|
|
|
Painter p(&shadow);
|
|
|
|
PainterHighQualityEnabler hq(p);
|
|
|
|
p.setPen(Qt::NoPen);
|
|
|
|
p.setBrush(st::shadowFg);
|
|
|
|
auto extend = 2 * st::lineWidth;
|
|
|
|
p.drawEllipse(getInnerRect().marginsAdded(QMargins(extend, extend, extend, extend)));
|
|
|
|
}
|
2017-05-24 14:36:58 +00:00
|
|
|
_shadow = App::pixmapFromImageInPlace(Images::prepareBlur(std::move(shadow)));
|
2017-05-22 15:25:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
QRect Float::getInnerRect() const {
|
|
|
|
auto margin = st::mediaPlayerFloatMargin;
|
|
|
|
return rect().marginsRemoved(QMargins(margin, margin, margin, margin));
|
|
|
|
}
|
|
|
|
|
|
|
|
void Float::paintEvent(QPaintEvent *e) {
|
|
|
|
Painter p(this);
|
|
|
|
|
|
|
|
p.setOpacity(_opacity);
|
|
|
|
p.drawPixmap(0, 0, _shadow);
|
|
|
|
|
|
|
|
if (!fillFrame() && _toggleCallback) {
|
|
|
|
_toggleCallback(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
auto inner = getInnerRect();
|
|
|
|
p.drawImage(inner.topLeft(), _frame);
|
|
|
|
|
2018-01-21 14:49:42 +00:00
|
|
|
const auto playback = getPlayback();
|
2019-03-07 11:35:28 +00:00
|
|
|
const auto progress = playback ? playback->value() : 1.;
|
2017-05-22 15:25:49 +00:00
|
|
|
if (progress > 0.) {
|
|
|
|
auto pen = st::historyVideoMessageProgressFg->p;
|
|
|
|
auto was = p.pen();
|
|
|
|
pen.setWidth(st::radialLine);
|
|
|
|
pen.setCapStyle(Qt::RoundCap);
|
|
|
|
p.setPen(pen);
|
|
|
|
p.setOpacity(_opacity * st::historyVideoMessageProgressOpacity);
|
|
|
|
|
|
|
|
auto from = QuarterArcLength;
|
|
|
|
auto len = -qRound(FullArcLength * progress);
|
|
|
|
auto stepInside = st::radialLine / 2;
|
|
|
|
{
|
|
|
|
PainterHighQualityEnabler hq(p);
|
|
|
|
p.drawArc(inner.marginsRemoved(QMargins(stepInside, stepInside, stepInside, stepInside)), from, len);
|
|
|
|
}
|
|
|
|
|
|
|
|
//p.setPen(was);
|
|
|
|
//p.setOpacity(_opacity);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-11 12:09:21 +00:00
|
|
|
Streaming::Instance *Float::getStreamed() const {
|
2019-12-11 10:15:48 +00:00
|
|
|
return instance()->roundVideoStreamed(_item);
|
|
|
|
}
|
|
|
|
|
2019-02-27 11:36:19 +00:00
|
|
|
View::PlaybackProgress *Float::getPlayback() const {
|
2019-03-26 08:54:51 +00:00
|
|
|
return instance()->roundVideoPlayback(_item);
|
2017-05-22 15:25:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool Float::hasFrame() const {
|
2019-12-11 12:09:21 +00:00
|
|
|
return (getStreamed() != nullptr);
|
2017-05-22 15:25:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool Float::fillFrame() {
|
|
|
|
auto creating = _frame.isNull();
|
|
|
|
if (creating) {
|
2018-01-21 14:49:42 +00:00
|
|
|
_frame = QImage(
|
|
|
|
getInnerRect().size() * cIntRetinaFactor(),
|
|
|
|
QImage::Format_ARGB32_Premultiplied);
|
2017-05-22 15:25:49 +00:00
|
|
|
_frame.setDevicePixelRatio(cRetinaFactor());
|
|
|
|
}
|
2018-01-21 14:49:42 +00:00
|
|
|
auto frameInner = [&] {
|
|
|
|
return QRect(QPoint(), _frame.size() / cIntRetinaFactor());
|
2017-05-22 15:25:49 +00:00
|
|
|
};
|
2019-12-11 12:09:21 +00:00
|
|
|
if (const auto streamed = getStreamed()) {
|
2019-03-26 08:54:51 +00:00
|
|
|
auto request = Streaming::FrameRequest::NonStrict();
|
|
|
|
request.outer = request.resize = _frame.size();
|
|
|
|
request.radius = ImageRoundRadius::Ellipse;
|
2019-12-11 12:09:21 +00:00
|
|
|
auto frame = streamed->frame(request);
|
2017-05-22 15:25:49 +00:00
|
|
|
if (!frame.isNull()) {
|
|
|
|
_frame.fill(Qt::transparent);
|
|
|
|
|
|
|
|
Painter p(&_frame);
|
|
|
|
PainterHighQualityEnabler hq(p);
|
2019-03-26 08:54:51 +00:00
|
|
|
p.drawImage(frameInner(), frame);
|
2017-05-22 15:25:49 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (creating) {
|
|
|
|
_frame.fill(Qt::transparent);
|
|
|
|
|
|
|
|
Painter p(&_frame);
|
|
|
|
PainterHighQualityEnabler hq(p);
|
|
|
|
p.setPen(Qt::NoPen);
|
|
|
|
p.setBrush(st::imageBg);
|
|
|
|
p.drawEllipse(frameInner());
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Float::repaintItem() {
|
|
|
|
update();
|
|
|
|
if (hasFrame() && _toggleCallback) {
|
|
|
|
_toggleCallback(true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-05 11:16:09 +00:00
|
|
|
|
|
|
|
template <typename ToggleCallback, typename DraggedCallback>
|
|
|
|
FloatController::Item::Item(
|
|
|
|
not_null<QWidget*> parent,
|
|
|
|
not_null<HistoryItem*> item,
|
|
|
|
ToggleCallback toggle,
|
|
|
|
DraggedCallback dragged)
|
|
|
|
: animationSide(RectPart::Right)
|
|
|
|
, column(Window::Column::Second)
|
|
|
|
, corner(RectPart::TopRight)
|
|
|
|
, widget(
|
|
|
|
parent,
|
|
|
|
item,
|
|
|
|
[=, toggle = std::move(toggle)](bool visible) {
|
|
|
|
toggle(this, visible);
|
|
|
|
},
|
|
|
|
[=, dragged = std::move(dragged)](bool closed) {
|
|
|
|
dragged(this, closed);
|
|
|
|
}) {
|
|
|
|
}
|
|
|
|
|
|
|
|
FloatController::FloatController(not_null<FloatDelegate*> delegate)
|
|
|
|
: _delegate(delegate)
|
2020-06-25 14:17:37 +00:00
|
|
|
, _parent(_delegate->floatPlayerWidget()) {
|
2018-11-05 11:16:09 +00:00
|
|
|
subscribe(Media::Player::instance()->trackChangedNotifier(), [=](
|
|
|
|
AudioMsgId::Type type) {
|
|
|
|
if (type == AudioMsgId::Type::Voice) {
|
|
|
|
checkCurrent();
|
|
|
|
}
|
|
|
|
});
|
2018-11-05 13:18:54 +00:00
|
|
|
|
|
|
|
startDelegateHandling();
|
|
|
|
}
|
|
|
|
|
|
|
|
void FloatController::replaceDelegate(not_null<FloatDelegate*> delegate) {
|
|
|
|
_delegateLifetime.destroy();
|
|
|
|
|
|
|
|
_delegate = delegate;
|
|
|
|
_parent = _delegate->floatPlayerWidget();
|
|
|
|
|
|
|
|
startDelegateHandling();
|
|
|
|
|
|
|
|
for (const auto &item : _items) {
|
|
|
|
item->widget->setParent(_parent);
|
|
|
|
}
|
|
|
|
checkVisibility();
|
|
|
|
}
|
|
|
|
|
|
|
|
void FloatController::startDelegateHandling() {
|
|
|
|
_delegate->floatPlayerCheckVisibilityRequests(
|
|
|
|
) | rpl::start_with_next([=] {
|
|
|
|
checkVisibility();
|
|
|
|
}, _delegateLifetime);
|
|
|
|
|
|
|
|
_delegate->floatPlayerHideAllRequests(
|
|
|
|
) | rpl::start_with_next([=] {
|
|
|
|
hideAll();
|
|
|
|
}, _delegateLifetime);
|
|
|
|
|
|
|
|
_delegate->floatPlayerShowVisibleRequests(
|
|
|
|
) | rpl::start_with_next([=] {
|
|
|
|
showVisible();
|
|
|
|
}, _delegateLifetime);
|
|
|
|
|
|
|
|
_delegate->floatPlayerRaiseAllRequests(
|
|
|
|
) | rpl::start_with_next([=] {
|
|
|
|
raiseAll();
|
|
|
|
}, _delegateLifetime);
|
|
|
|
|
|
|
|
_delegate->floatPlayerUpdatePositionsRequests(
|
|
|
|
) | rpl::start_with_next([=] {
|
|
|
|
updatePositions();
|
|
|
|
}, _delegateLifetime);
|
|
|
|
|
|
|
|
_delegate->floatPlayerFilterWheelEventRequests(
|
|
|
|
) | rpl::start_with_next([=](
|
|
|
|
const FloatDelegate::FloatPlayerFilterWheelEventRequest &request) {
|
|
|
|
*request.result = filterWheelEvent(request.object, request.event);
|
|
|
|
}, _delegateLifetime);
|
2020-06-25 14:17:37 +00:00
|
|
|
|
|
|
|
_delegate->floatPlayerAreaUpdates(
|
|
|
|
) | rpl::start_with_next([=] {
|
|
|
|
checkVisibility();
|
|
|
|
}, _delegateLifetime);
|
2018-11-05 11:16:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void FloatController::checkCurrent() {
|
|
|
|
const auto state = Media::Player::instance()->current(AudioMsgId::Type::Voice);
|
2020-06-25 12:16:09 +00:00
|
|
|
const auto audio = state.audio();
|
2018-11-05 11:16:09 +00:00
|
|
|
const auto fullId = state.contextId();
|
|
|
|
const auto last = current();
|
|
|
|
if (last
|
2020-06-25 12:16:09 +00:00
|
|
|
&& audio
|
2018-11-05 11:16:09 +00:00
|
|
|
&& !last->widget->detached()
|
2020-06-25 12:16:09 +00:00
|
|
|
&& (&last->widget->item()->history()->session() == &audio->session())
|
|
|
|
&& (last->widget->item()->fullId() == fullId)) {
|
2018-11-05 11:16:09 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (last) {
|
|
|
|
last->widget->detach();
|
|
|
|
}
|
2020-06-25 12:16:09 +00:00
|
|
|
if (!audio) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (const auto item = audio->session().data().message(fullId)) {
|
2018-11-05 11:16:09 +00:00
|
|
|
if (const auto media = item->media()) {
|
|
|
|
if (const auto document = media->document()) {
|
|
|
|
if (document->isVideoMessage()) {
|
|
|
|
create(item);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void FloatController::create(not_null<HistoryItem*> item) {
|
|
|
|
_items.push_back(std::make_unique<Item>(
|
|
|
|
_parent,
|
|
|
|
item,
|
|
|
|
[=](not_null<Item*> instance, bool visible) {
|
|
|
|
instance->hiddenByWidget = !visible;
|
|
|
|
toggle(instance);
|
|
|
|
},
|
|
|
|
[=](not_null<Item*> instance, bool closed) {
|
|
|
|
finishDrag(instance, closed);
|
|
|
|
}));
|
2020-06-25 12:16:09 +00:00
|
|
|
current()->column = Core::App().settings().floatPlayerColumn();
|
|
|
|
current()->corner = Core::App().settings().floatPlayerCorner();
|
2018-11-05 11:16:09 +00:00
|
|
|
checkVisibility();
|
|
|
|
}
|
|
|
|
|
|
|
|
void FloatController::toggle(not_null<Item*> instance) {
|
|
|
|
auto visible = !instance->hiddenByHistory && !instance->hiddenByWidget && instance->widget->isReady();
|
|
|
|
if (instance->visible != visible) {
|
|
|
|
instance->widget->resetMouseState();
|
|
|
|
instance->visible = visible;
|
|
|
|
if (!instance->visibleAnimation.animating() && !instance->hiddenByDrag) {
|
|
|
|
auto finalRect = QRect(getPosition(instance), instance->widget->size());
|
|
|
|
instance->animationSide = getSide(finalRect.center());
|
|
|
|
}
|
|
|
|
instance->visibleAnimation.start([=] {
|
|
|
|
updatePosition(instance);
|
|
|
|
}, visible ? 0. : 1., visible ? 1. : 0., st::slideDuration, visible ? anim::easeOutCirc : anim::linear);
|
|
|
|
updatePosition(instance);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void FloatController::checkVisibility() {
|
2018-11-05 13:18:54 +00:00
|
|
|
const auto instance = current();
|
2018-11-05 11:16:09 +00:00
|
|
|
if (!instance) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-11-05 13:18:54 +00:00
|
|
|
const auto item = instance->widget->item();
|
|
|
|
instance->hiddenByHistory = item
|
|
|
|
? _delegate->floatPlayerIsVisible(item)
|
|
|
|
: false;
|
2018-11-05 11:16:09 +00:00
|
|
|
toggle(instance);
|
|
|
|
updatePosition(instance);
|
|
|
|
}
|
|
|
|
|
|
|
|
void FloatController::hideAll() {
|
|
|
|
for (const auto &instance : _items) {
|
|
|
|
instance->widget->hide();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void FloatController::showVisible() {
|
|
|
|
for (const auto &instance : _items) {
|
|
|
|
if (instance->visible) {
|
|
|
|
instance->widget->show();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void FloatController::raiseAll() {
|
|
|
|
for (const auto &instance : _items) {
|
|
|
|
instance->widget->raise();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void FloatController::updatePositions() {
|
|
|
|
for (const auto &instance : _items) {
|
|
|
|
updatePosition(instance.get());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
std::optional<bool> FloatController::filterWheelEvent(
|
2018-11-05 13:18:54 +00:00
|
|
|
not_null<QObject*> object,
|
|
|
|
not_null<QEvent*> event) {
|
2018-11-05 11:16:09 +00:00
|
|
|
for (const auto &instance : _items) {
|
|
|
|
if (instance->widget == object) {
|
|
|
|
const auto section = _delegate->floatPlayerGetSection(
|
|
|
|
instance->column);
|
2020-06-25 14:17:37 +00:00
|
|
|
return section->floatPlayerHandleWheelEvent(event);
|
2018-11-05 11:16:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return std::nullopt;
|
|
|
|
}
|
|
|
|
|
|
|
|
void FloatController::updatePosition(not_null<Item*> instance) {
|
2019-04-02 09:13:30 +00:00
|
|
|
auto visible = instance->visibleAnimation.value(instance->visible ? 1. : 0.);
|
2018-11-05 11:16:09 +00:00
|
|
|
if (visible == 0. && !instance->visible) {
|
|
|
|
instance->widget->hide();
|
|
|
|
if (instance->widget->detached()) {
|
|
|
|
InvokeQueued(instance->widget, [=] {
|
|
|
|
remove(instance);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!instance->widget->dragged()) {
|
|
|
|
if (instance->widget->isHidden()) {
|
|
|
|
instance->widget->show();
|
|
|
|
}
|
|
|
|
|
2019-04-02 09:13:30 +00:00
|
|
|
auto dragged = instance->draggedAnimation.value(1.);
|
2018-11-05 11:16:09 +00:00
|
|
|
auto position = QPoint();
|
|
|
|
if (instance->hiddenByDrag) {
|
|
|
|
instance->widget->setOpacity(instance->widget->countOpacityByParent());
|
|
|
|
position = getHiddenPosition(instance->dragFrom, instance->widget->size(), instance->animationSide);
|
|
|
|
} else {
|
|
|
|
instance->widget->setOpacity(visible * visible);
|
|
|
|
position = getPosition(instance);
|
|
|
|
if (visible < 1.) {
|
|
|
|
auto hiddenPosition = getHiddenPosition(position, instance->widget->size(), instance->animationSide);
|
|
|
|
position.setX(anim::interpolate(hiddenPosition.x(), position.x(), visible));
|
|
|
|
position.setY(anim::interpolate(hiddenPosition.y(), position.y(), visible));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (dragged < 1.) {
|
|
|
|
position.setX(anim::interpolate(instance->dragFrom.x(), position.x(), dragged));
|
|
|
|
position.setY(anim::interpolate(instance->dragFrom.y(), position.y(), dragged));
|
|
|
|
}
|
|
|
|
instance->widget->move(position);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
QPoint FloatController::getHiddenPosition(
|
|
|
|
QPoint position,
|
|
|
|
QSize size,
|
|
|
|
RectPart side) const {
|
|
|
|
switch (side) {
|
|
|
|
case RectPart::Left: return { -size.width(), position.y() };
|
|
|
|
case RectPart::Top: return { position.x(), -size.height() };
|
|
|
|
case RectPart::Right: return { _parent->width(), position.y() };
|
|
|
|
case RectPart::Bottom: return { position.x(), _parent->height() };
|
|
|
|
}
|
|
|
|
Unexpected("Bad side in MainWidget::getFloatPlayerHiddenPosition().");
|
|
|
|
}
|
|
|
|
|
|
|
|
QPoint FloatController::getPosition(not_null<Item*> instance) const {
|
|
|
|
const auto section = _delegate->floatPlayerGetSection(instance->column);
|
2020-06-25 14:17:37 +00:00
|
|
|
const auto rect = section->floatPlayerAvailableRect();
|
2018-11-05 11:16:09 +00:00
|
|
|
auto position = rect.topLeft();
|
|
|
|
if (IsBottomCorner(instance->corner)) {
|
|
|
|
position.setY(position.y() + rect.height() - instance->widget->height());
|
|
|
|
}
|
|
|
|
if (IsRightCorner(instance->corner)) {
|
|
|
|
position.setX(position.x() + rect.width() - instance->widget->width());
|
|
|
|
}
|
|
|
|
return _parent->mapFromGlobal(position);
|
|
|
|
}
|
|
|
|
|
|
|
|
RectPart FloatController::getSide(QPoint center) const {
|
|
|
|
const auto left = std::abs(center.x());
|
|
|
|
const auto right = std::abs(_parent->width() - center.x());
|
|
|
|
const auto top = std::abs(center.y());
|
|
|
|
const auto bottom = std::abs(_parent->height() - center.y());
|
|
|
|
if (left < right && left < top && left < bottom) {
|
|
|
|
return RectPart::Left;
|
|
|
|
} else if (right < top && right < bottom) {
|
|
|
|
return RectPart::Right;
|
|
|
|
} else if (top < bottom) {
|
|
|
|
return RectPart::Top;
|
|
|
|
}
|
|
|
|
return RectPart::Bottom;
|
|
|
|
}
|
|
|
|
|
|
|
|
void FloatController::remove(not_null<Item*> instance) {
|
|
|
|
auto widget = std::move(instance->widget);
|
|
|
|
auto i = ranges::find_if(_items, [&](auto &item) {
|
|
|
|
return (item.get() == instance);
|
|
|
|
});
|
|
|
|
Assert(i != _items.end());
|
|
|
|
_items.erase(i);
|
|
|
|
|
|
|
|
// ~QWidget() can call HistoryInner::enterEvent() which can
|
|
|
|
// lead to repaintHistoryItem() and we'll have an instance
|
|
|
|
// in _items with destroyed widget. So we destroy the
|
|
|
|
// instance first and only after that destroy the widget.
|
|
|
|
widget.destroy();
|
|
|
|
}
|
|
|
|
|
|
|
|
void FloatController::updateColumnCorner(QPoint center) {
|
|
|
|
Expects(!_items.empty());
|
|
|
|
|
|
|
|
auto size = _items.back()->widget->size();
|
|
|
|
auto min = INT_MAX;
|
2020-06-25 12:16:09 +00:00
|
|
|
auto column = Core::App().settings().floatPlayerColumn();
|
|
|
|
auto corner = Core::App().settings().floatPlayerCorner();
|
2018-11-05 11:16:09 +00:00
|
|
|
auto checkSection = [&](
|
2020-06-25 14:17:37 +00:00
|
|
|
not_null<FloatSectionDelegate*> widget,
|
2018-11-05 11:16:09 +00:00
|
|
|
Window::Column widgetColumn) {
|
2020-06-25 14:17:37 +00:00
|
|
|
auto rect = _parent->mapFromGlobal(
|
|
|
|
widget->floatPlayerAvailableRect());
|
2018-11-05 11:16:09 +00:00
|
|
|
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 = [&](QPoint point, RectPart checked) {
|
|
|
|
auto distance = (point - center).manhattanLength();
|
|
|
|
if (min > distance) {
|
|
|
|
min = distance;
|
|
|
|
column = widgetColumn;
|
|
|
|
corner = checked;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
checkCorner({ left, top }, RectPart::TopLeft);
|
|
|
|
checkCorner({ right, top }, RectPart::TopRight);
|
|
|
|
checkCorner({ left, bottom }, RectPart::BottomLeft);
|
|
|
|
checkCorner({ right, bottom }, RectPart::BottomRight);
|
|
|
|
};
|
|
|
|
|
|
|
|
_delegate->floatPlayerEnumerateSections(checkSection);
|
|
|
|
|
2020-06-25 12:16:09 +00:00
|
|
|
auto &settings = Core::App().settings();
|
|
|
|
if (settings.floatPlayerColumn() != column) {
|
|
|
|
settings.setFloatPlayerColumn(column);
|
|
|
|
Core::App().saveSettingsDelayed();
|
2018-11-05 11:16:09 +00:00
|
|
|
}
|
2020-06-25 12:16:09 +00:00
|
|
|
if (settings.floatPlayerCorner() != corner) {
|
|
|
|
settings.setFloatPlayerCorner(corner);
|
|
|
|
Core::App().saveSettingsDelayed();
|
2018-11-05 11:16:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void FloatController::finishDrag(not_null<Item*> instance, bool closed) {
|
|
|
|
instance->dragFrom = instance->widget->pos();
|
|
|
|
const auto center = instance->widget->geometry().center();
|
|
|
|
if (closed) {
|
|
|
|
instance->hiddenByDrag = true;
|
|
|
|
instance->animationSide = getSide(center);
|
|
|
|
}
|
|
|
|
updateColumnCorner(center);
|
2020-06-25 12:16:09 +00:00
|
|
|
instance->column = Core::App().settings().floatPlayerColumn();
|
|
|
|
instance->corner = Core::App().settings().floatPlayerCorner();
|
2018-11-05 11:16:09 +00:00
|
|
|
|
2019-04-02 09:13:30 +00:00
|
|
|
instance->draggedAnimation.stop();
|
2018-11-05 11:16:09 +00:00
|
|
|
instance->draggedAnimation.start(
|
|
|
|
[=] { updatePosition(instance); },
|
|
|
|
0.,
|
|
|
|
1.,
|
|
|
|
st::slideDuration,
|
|
|
|
anim::sineInOut);
|
|
|
|
updatePosition(instance);
|
|
|
|
|
|
|
|
if (closed) {
|
|
|
|
if (const auto item = instance->widget->item()) {
|
2018-11-05 13:18:54 +00:00
|
|
|
_closeEvents.fire(item->fullId());
|
2018-11-05 11:16:09 +00:00
|
|
|
}
|
|
|
|
instance->widget->detach();
|
2020-06-26 12:01:37 +00:00
|
|
|
Media::Player::instance()->stop(AudioMsgId::Type::Voice);
|
2018-11-05 11:16:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-22 15:25:49 +00:00
|
|
|
} // namespace Player
|
|
|
|
} // namespace Media
|