Skip to content

Commit

Permalink
Hide floating player if video message is visible.
Browse files Browse the repository at this point in the history
  • Loading branch information
john-preston committed May 24, 2017
1 parent b7550f6 commit c58a482
Show file tree
Hide file tree
Showing 9 changed files with 77 additions and 32 deletions.
29 changes: 18 additions & 11 deletions Telegram/SourceFiles/historywidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -662,16 +662,10 @@ void HistoryWidget::scrollToCurrentVoiceMessage(FullMsgId fromId, FullMsgId toId
// And the scrollTop will be reset back to scrollTopItem + scrollTopOffset.
notify_handlePendingHistoryUpdate();

auto fromTop = _list->itemTop(from);
auto toTop = _list->itemTop(to);
if (fromTop < 0 || toTop < 0) {
return;
}

auto scrollTop = _scroll->scrollTop();
auto scrollBottom = scrollTop + _scroll->height();
auto fromBottom = fromTop + from->height();
if (fromTop < scrollBottom && fromBottom > scrollTop) {
if (toTop >= 0 && !isItemCompletelyHidden(from)) {
auto scrollTop = _scroll->scrollTop();
auto scrollBottom = scrollTop + _scroll->height();
auto toBottom = toTop + to->height();
if ((toTop < scrollTop && toBottom < scrollBottom) || (toTop > scrollTop && toBottom > scrollBottom)) {
auto scrollTo = snap(itemTopForHighlight(to), 0, _scroll->scrollTopMax());
Expand Down Expand Up @@ -2636,17 +2630,30 @@ void HistoryWidget::onScroll() {
}
}

bool HistoryWidget::isItemCompletelyHidden(HistoryItem *item) const {
auto top = _list->itemTop(item);
if (top < 0) {
return true;
}

auto bottom = top + item->height();
auto scrollTop = _scroll->scrollTop();
auto scrollBottom = scrollTop + _scroll->height();
return (top >= scrollBottom || bottom <= scrollTop);
}

void HistoryWidget::visibleAreaUpdated() {
if (_list && !_scroll->isHidden()) {
int scrollTop = _scroll->scrollTop();
int scrollBottom = scrollTop + _scroll->height();
auto scrollTop = _scroll->scrollTop();
auto scrollBottom = scrollTop + _scroll->height();
_list->visibleAreaUpdated(scrollTop, scrollBottom);
if (_history->loadedAtBottom() && (_history->unreadCount() > 0 || (_migrated && _migrated->unreadCount() > 0))) {
auto showFrom = (_migrated && _migrated->showFrom) ? _migrated->showFrom : (_history ? _history->showFrom : nullptr);
if (showFrom && !showFrom->detached() && scrollBottom > _list->itemTop(showFrom) && App::wnd()->doWeReadServerHistory()) {
historyWasRead(ReadServerHistoryChecks::OnlyIfUnread);
}
}
controller()->floatPlayerAreaUpdated().notify(true);
}
}

Expand Down
1 change: 1 addition & 0 deletions Telegram/SourceFiles/historywidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@ class HistoryWidget final : public Window::AbstractSectionWidget, public RPCSend
void dragLeaveEvent(QDragLeaveEvent *e) override;
void dropEvent(QDropEvent *e) override;

bool isItemCompletelyHidden(HistoryItem *item) const;
void updateTopBarSelection();

bool paintTopBar(Painter &p, int decreaseWidth, TimeMs ms);
Expand Down
43 changes: 39 additions & 4 deletions Telegram/SourceFiles/mainwidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,9 @@ MainWidget::MainWidget(QWidget *parent, gsl::not_null<Window::Controller*> contr
subscribe(_controller->dialogsWidthRatio(), [this](float64) {
updateControlsGeometry();
});
subscribe(_controller->floatPlayerAreaUpdated(), [this] {
checkFloatPlayerVisibility();
});

QCoreApplication::instance()->installEventFilter(this);

Expand Down Expand Up @@ -234,7 +237,7 @@ void MainWidget::checkCurrentFloatPlayer() {
auto state = Media::Player::instance()->current(AudioMsgId::Type::Voice);
auto fullId = state.contextId();
auto last = currentFloatPlayer();
if (!last || last->widget->itemId() != fullId) {
if (!last || last->widget->detached() || last->widget->item()->fullId() != fullId) {
if (last) {
last->widget->detach();
}
Expand All @@ -243,18 +246,21 @@ void MainWidget::checkCurrentFloatPlayer() {
if (auto document = media->getDocument()) {
if (document->isRoundVideo()) {
_playerFloats.push_back(std::make_unique<Float>(this, item, [this](Float *instance, bool visible) {
toggleFloatPlayer(instance, visible);
instance->hiddenByWidget = !visible;
toggleFloatPlayer(instance);
}));
toggleFloatPlayer(currentFloatPlayer(), true);
checkFloatPlayerVisibility();
}
}
}
}
}
}

void MainWidget::toggleFloatPlayer(Float *instance, bool visible) {
void MainWidget::toggleFloatPlayer(Float *instance) {
auto visible = !instance->hiddenByHistory && !instance->hiddenByWidget && !instance->widget->detached();
if (instance->visible != visible) {
instance->widget->resetMouseState();
instance->visible = visible;
instance->visibleAnimation.start([this, instance] {
updateFloatPlayerPosition(instance);
Expand All @@ -263,6 +269,20 @@ void MainWidget::toggleFloatPlayer(Float *instance, bool visible) {
}
}

void MainWidget::checkFloatPlayerVisibility() {
auto instance = currentFloatPlayer();
if (!instance) {
return;
}

if (_history->isHidden() || _history->isItemCompletelyHidden(instance->widget->item())) {
instance->hiddenByHistory = false;
} else {
instance->hiddenByHistory = true;
}
toggleFloatPlayer(instance);
}

void MainWidget::updateFloatPlayerPosition(Float *instance) {
auto visible = instance->visibleAnimation.current(instance->visible ? 1. : 0.);
if (visible == 0. && !instance->visible) {
Expand Down Expand Up @@ -755,6 +775,7 @@ void MainWidget::noHider(HistoryHider *destroyed) {
} else {
_history->showAnimated(Window::SlideDirection::FromRight, animationParams);
}
checkFloatPlayerVisibility();
}
} else {
if (_forwardConfirm) {
Expand Down Expand Up @@ -796,6 +817,7 @@ void MainWidget::hiderLayer(object_ptr<HistoryHider> h) {
updateControlsGeometry();
_dialogs->activate();
}
checkFloatPlayerVisibility();
}

void MainWidget::forwardLayer(int forwardSelected) {
Expand Down Expand Up @@ -2498,6 +2520,8 @@ void MainWidget::ui_showPeerHistory(quint64 peerId, qint32 showAtMsgId, Ui::Show
}
_dialogs->update();
}

checkFloatPlayerVisibility();
}

PeerData *MainWidget::ui_getPeerForMouseAction() {
Expand Down Expand Up @@ -2625,6 +2649,7 @@ void MainWidget::showMediaOverview(PeerData *peer, MediaOverviewType type, bool
_history->hide();
if (Adaptive::OneColumn()) _dialogs->hide();

checkFloatPlayerVisibility();
orderWidgets();
}

Expand Down Expand Up @@ -2784,6 +2809,7 @@ void MainWidget::showNewWideSection(const Window::SectionMemento *memento, bool
_wideSection->showFast();
}

checkFloatPlayerVisibility();
orderWidgets();
}

Expand Down Expand Up @@ -3128,6 +3154,9 @@ void MainWidget::hideAll() {
_player->hide();
_playerHeight = 0;
}
for (auto &instance : _playerFloats) {
instance->widget->hide();
}
}

void MainWidget::showAll() {
Expand Down Expand Up @@ -3199,6 +3228,12 @@ void MainWidget::showAll() {
_playerHeight = _player->contentHeight();
}
updateControlsGeometry();
if (auto instance = currentFloatPlayer()) {
checkFloatPlayerVisibility();
if (instance->visible) {
instance->widget->show();
}
}

App::wnd()->checkHistoryActivation();
}
Expand Down
5 changes: 4 additions & 1 deletion Telegram/SourceFiles/mainwidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -466,6 +466,8 @@ public slots:
template <typename ToggleCallback>
Float(QWidget *parent, HistoryItem *item, ToggleCallback callback);

bool hiddenByWidget = false;
bool hiddenByHistory = false;
bool visible = false;
Animation visibleAnimation;
Window::Corner corner = Window::Corner::TopRight;
Expand Down Expand Up @@ -574,7 +576,8 @@ public slots:

void clearCachedBackground();
void checkCurrentFloatPlayer();
void toggleFloatPlayer(Float *instance, bool visible);
void toggleFloatPlayer(Float *instance);
void checkFloatPlayerVisibility();
void updateFloatPlayerPosition(Float *instance);
void removeFloatPlayer(Float *instance);
Float *currentFloatPlayer() const {
Expand Down
19 changes: 7 additions & 12 deletions Telegram/SourceFiles/media/media_audio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -887,7 +887,6 @@ void Mixer::seek(AudioMsgId::Type type, int64 position) {
resetFadeStartPosition(type, position - current->bufferedPosition);
} else {
setStoppedState(current);
if (streamCreated) alSourceStop(current->stream.source);
}
switch (current->state.state) {
case State::Pausing:
Expand Down Expand Up @@ -1009,6 +1008,10 @@ TrackState Mixer::currentState(AudioMsgId::Type type) {
void Mixer::setStoppedState(Track *current, State state) {
current->state.state = state;
current->state.position = 0;
if (current->isStreamCreated()) {
alSourceStop(current->stream.source);
alSourcef(current->stream.source, AL_GAIN, 1);
}
}

void Mixer::clearStoppedAtStart(const AudioMsgId &audio) {
Expand Down Expand Up @@ -1209,18 +1212,14 @@ int32 Fader::updateOnePlayback(Mixer::Track *track, bool &hasPlaying, bool &hasF
if (fading || playing) {
fading = false;
playing = false;
if (track->isStreamCreated()) {
alSourceStop(track->stream.source);
alSourcef(track->stream.source, AL_GAIN, 1);
if (errorHappened()) return EmitError;
}
if (track->state.state == State::Pausing) {
track->state.state = State::PausedAtEnd;
setStoppedState(track, State::PausedAtEnd);
} else if (track->state.state == State::Stopping) {
setStoppedState(track, State::Stopped);
} else {
setStoppedState(track, State::StoppedAtEnd);
}
if (errorHappened()) return EmitError;
emitSignals |= EmitStopped;
}
} else if (fading && state == AL_PLAYING) {
Expand All @@ -1232,9 +1231,6 @@ int32 Fader::updateOnePlayback(Mixer::Track *track, bool &hasPlaying, bool &hasF

switch (track->state.state) {
case State::Stopping: {
alSourceStop(track->stream.source);
if (errorHappened()) return EmitError;

setStoppedState(track);
state = AL_STOPPED;
} break;
Expand Down Expand Up @@ -1284,8 +1280,7 @@ int32 Fader::updateOnePlayback(Mixer::Track *track, bool &hasPlaying, bool &hasF
}

void Fader::setStoppedState(Mixer::Track *track, State state) {
track->state.state = state;
track->state.position = 0;
mixer()->setStoppedState(track, state);
}

void Fader::onSuppressSong() {
Expand Down
1 change: 1 addition & 0 deletions Telegram/SourceFiles/media/media_audio.h
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,7 @@ private slots:

};

// Thread: Any. Must be locked: AudioMutex.
void setStoppedState(Track *current, State state = State::Stopped);

Track *trackForType(AudioMsgId::Type type, int index = -1); // -1 uses currentIndex(type)
Expand Down
3 changes: 1 addition & 2 deletions Telegram/SourceFiles/media/media_audio_loaders.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,7 @@ AudioMsgId Loaders::clear(AudioMsgId::Type type) {
}

void Loaders::setStoppedState(Mixer::Track *track, State state) {
track->state.state = state;
track->state.position = 0;
mixer()->setStoppedState(track, state);
}

void Loaders::emitError(AudioMsgId::Type type) {
Expand Down
4 changes: 2 additions & 2 deletions Telegram/SourceFiles/media/player/media_player_float.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ class Float : public TWidget, private base::Subscriber {
public:
Float(QWidget *parent, HistoryItem *item, base::lambda<void(bool visible)> toggleCallback);

FullMsgId itemId() const {
return _item ? _item->fullId() : FullMsgId();
HistoryItem *item() const {
return _item;
}
void setOpacity(float64 opacity) {
_opacity = opacity;
Expand Down
4 changes: 4 additions & 0 deletions Telegram/SourceFiles/window/window_controller.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@ class Controller {
return _gifPauseLevelChanged;
}
bool isGifPausedAtLeastFor(GifPauseReason reason) const;
base::Observable<void> &floatPlayerAreaUpdated() {
return _floatPlayerAreaUpdated;
}

struct ColumnLayout {
int bodyWidth;
Expand Down Expand Up @@ -104,6 +107,7 @@ class Controller {

GifPauseReasons _gifPauseReasons = { 0 };
base::Observable<void> _gifPauseLevelChanged;
base::Observable<void> _floatPlayerAreaUpdated;

base::Variable<float64> _dialogsWidthRatio = { kDefaultDialogsWidthRatio };
base::Variable<bool> _dialogsListFocused = { false };
Expand Down

0 comments on commit c58a482

Please sign in to comment.