From 9059b2e617d2d5531520d844b1a3f29a05527648 Mon Sep 17 00:00:00 2001 From: 23rd <23rd@vivaldi.net> Date: Thu, 2 May 2019 13:00:05 +0300 Subject: [PATCH] Fixes for Audio Player in touchbar. - Removed unused Platform::SetTouchBar. - Added rpl::event_stream in Media Player Instance for handle closing player. - Fixed hiding of touchbar when last track is ended. - Fixed display of time when last track is ended. - Slightly refactored. --- Telegram/SourceFiles/core/shortcuts.cpp | 1 + .../media/player/media_player_instance.h | 9 ++++ .../media/player/media_player_widget.cpp | 2 - .../platform/linux/specific_linux.cpp | 4 -- .../platform/mac/specific_mac_p.mm | 4 -- Telegram/SourceFiles/platform/mac/touchbar.mm | 42 ++++++++----------- .../SourceFiles/platform/platform_specific.h | 2 - .../SourceFiles/platform/win/specific_win.cpp | 4 -- 8 files changed, 27 insertions(+), 41 deletions(-) diff --git a/Telegram/SourceFiles/core/shortcuts.cpp b/Telegram/SourceFiles/core/shortcuts.cpp index dad91d9734..1859b3ffd4 100644 --- a/Telegram/SourceFiles/core/shortcuts.cpp +++ b/Telegram/SourceFiles/core/shortcuts.cpp @@ -490,6 +490,7 @@ bool HandleEvent(not_null event) { void ToggleMediaShortcuts(bool toggled) { Data.toggleMedia(toggled); Platform::SetWatchingMediaKeys(toggled); + Media::Player::instance()->playerWidgetToggledNotify(toggled); } void ToggleSupportShortcuts(bool toggled) { diff --git a/Telegram/SourceFiles/media/player/media_player_instance.h b/Telegram/SourceFiles/media/player/media_player_instance.h index b200492562..ccec2dad0d 100644 --- a/Telegram/SourceFiles/media/player/media_player_instance.h +++ b/Telegram/SourceFiles/media/player/media_player_instance.h @@ -146,6 +146,14 @@ public: rpl::producer<> playlistChanges(AudioMsgId::Type type) const; + void playerWidgetToggledNotify(bool toggled) { + _playerWidgetToggled.fire_copy({toggled}); + } + rpl::producer playerWidgetToggled() const { + return _playerWidgetToggled.events(); + } + + void documentLoadProgress(DocumentData *document); void handleLogout(); @@ -251,6 +259,7 @@ private: base::Observable _trackChangedNotifier; base::Observable _repeatChangedNotifier; + rpl::event_stream _playerWidgetToggled; rpl::lifetime _lifetime; }; diff --git a/Telegram/SourceFiles/media/player/media_player_widget.cpp b/Telegram/SourceFiles/media/player/media_player_widget.cpp index a9cb654030..411a868791 100644 --- a/Telegram/SourceFiles/media/player/media_player_widget.cpp +++ b/Telegram/SourceFiles/media/player/media_player_widget.cpp @@ -167,7 +167,6 @@ Widget::Widget(QWidget *parent) : RpWidget(parent) }); setType(AudioMsgId::Type::Song); _playPause->finishTransform(); - Platform::SetTouchBar(Platform::TouchBarType::AudioPlayer); } void Widget::updateVolumeToggleIcon() { @@ -192,7 +191,6 @@ void Widget::setCloseCallback(Fn callback) { } void Widget::stopAndClose() { - Platform::SetTouchBar(Platform::TouchBarType::None); _voiceIsActive = false; if (_type == AudioMsgId::Type::Voice) { const auto songData = instance()->current(AudioMsgId::Type::Song); diff --git a/Telegram/SourceFiles/platform/linux/specific_linux.cpp b/Telegram/SourceFiles/platform/linux/specific_linux.cpp index ccfafddcc2..31f32cc664 100644 --- a/Telegram/SourceFiles/platform/linux/specific_linux.cpp +++ b/Telegram/SourceFiles/platform/linux/specific_linux.cpp @@ -406,10 +406,6 @@ void RequestPermission(PermissionType type, Fn resultCal void OpenSystemSettingsForPermission(PermissionType type) { } -void SetTouchBar(TouchBarType type) { - // TouchBar is Mac only feature. -} - bool OpenSystemSettings(SystemSettingsType type) { if (type == SystemSettingsType::Audio) { auto options = std::vector(); diff --git a/Telegram/SourceFiles/platform/mac/specific_mac_p.mm b/Telegram/SourceFiles/platform/mac/specific_mac_p.mm index 75f5805ea1..43f28cf086 100644 --- a/Telegram/SourceFiles/platform/mac/specific_mac_p.mm +++ b/Telegram/SourceFiles/platform/mac/specific_mac_p.mm @@ -225,10 +225,6 @@ void SetApplicationIcon(const QIcon &icon) { [image release]; } -void SetTouchBar(TouchBarType type) { - -} - void InitOnTopPanel(QWidget *panel) { Expects(!panel->windowHandle()); diff --git a/Telegram/SourceFiles/platform/mac/touchbar.mm b/Telegram/SourceFiles/platform/mac/touchbar.mm index 5b0b77cda4..745aacd4b0 100644 --- a/Telegram/SourceFiles/platform/mac/touchbar.mm +++ b/Telegram/SourceFiles/platform/mac/touchbar.mm @@ -283,6 +283,15 @@ auto lifetime = rpl::lifetime(); [self createTouchBar]; [self setTouchBar:TouchBarType::Main]; + Media::Player::instance()->playerWidgetToggled( + ) | rpl::start_with_next([=](bool toggled) { + if (!toggled) { + [self setTouchBar:TouchBarType::Main]; + } else { + [self setTouchBar:TouchBarType::AudioPlayer]; + } + }, lifetime); + return self; } @@ -361,18 +370,12 @@ auto lifetime = rpl::lifetime(); } - (void)handlePropertyChange:(Media::Player::TrackState)property { - // #TODO: fix hiding of touch bar when last track is ended. - if (property.state == Media::Player::State::Stopped) { - [self setTouchBar:TouchBarType::Main]; - return; - } else if (property.state == Media::Player::State::StoppedAtEnd) { - [self setTouchBar:TouchBarType::AudioPlayer]; - } else { - [self setTouchBar:TouchBarType::AudioPlayer]; - } - self.position = property.position < 0 ? 0 : property.position; self.duration = property.length; + if (Media::Player::IsStoppedOrStopping(property.state)) { + self.position = 0; + self.duration = 0; + } [self updateTouchBarTimeItems]; NSButton *playButton = self.touchbarItems[play][@"view"]; if (property.state == Media::Player::State::Playing) { @@ -395,7 +398,7 @@ auto lifetime = rpl::lifetime(); NSString *stime = hours > 0 ? [NSString stringWithFormat:@"%d:", hours] : @""; stime = (stime.length > 0 || minutes > 9) ? [NSString stringWithFormat:@"%@%02d:", stime, minutes] : - [NSString stringWithFormat:@"%d:", minutes]; + [NSString stringWithFormat:@"%02d:", minutes]; stime = [NSString stringWithFormat:@"%@%02d", stime, seconds]; return stime; @@ -422,24 +425,12 @@ auto lifetime = rpl::lifetime(); toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 - constant:(int)ceil(size.width * 1.5)]; + constant:(int)ceil(size.width) * 1.2]; [field addConstraint:con]; [self.touchbarItems[identifier] setObject:con forKey:@"constrain"]; } } -- (void)updateTouchBarTimeItemConstrains { - [self removeConstraintForIdentifier:currentPosition]; - - if (self.duration <= 0) { - [self applyConstraintFromString:[self formatTime:self.position] - forIdentifier:currentPosition]; - } else { - NSString *durFormat = [self formatTime:self.duration]; - [self applyConstraintFromString:durFormat forIdentifier:currentPosition]; - } -} - - (void)updateTouchBarTimeItems { NSSlider *seekSlider = self.touchbarItems[seekBar][@"view"]; NSTextField *curPosItem = self.touchbarItems[currentPosition][@"view"]; @@ -460,7 +451,8 @@ auto lifetime = rpl::lifetime(); timeToString(self.position), timeToString(self.duration)]; - [self updateTouchBarTimeItemConstrains]; + [self removeConstraintForIdentifier:currentPosition]; + [self applyConstraintFromString:curPosItem.stringValue forIdentifier:currentPosition]; } - (NSString *)getIdentifierFromView:(id)view { diff --git a/Telegram/SourceFiles/platform/platform_specific.h b/Telegram/SourceFiles/platform/platform_specific.h index 3c8a782db6..f376f01daa 100644 --- a/Telegram/SourceFiles/platform/platform_specific.h +++ b/Telegram/SourceFiles/platform/platform_specific.h @@ -45,8 +45,6 @@ void RequestPermission(PermissionType type, Fn resultCal void OpenSystemSettingsForPermission(PermissionType type); bool OpenSystemSettings(SystemSettingsType type); -void SetTouchBar(TouchBarType type); - [[nodiscard]] QString SystemLanguage(); [[nodiscard]] QString SystemCountry(); diff --git a/Telegram/SourceFiles/platform/win/specific_win.cpp b/Telegram/SourceFiles/platform/win/specific_win.cpp index ff1045e58e..ea8071405a 100644 --- a/Telegram/SourceFiles/platform/win/specific_win.cpp +++ b/Telegram/SourceFiles/platform/win/specific_win.cpp @@ -646,10 +646,6 @@ void OpenSystemSettingsForPermission(PermissionType type) { } } -void SetTouchBar(TouchBarType type) { - // TouchBar is Mac only feature. -} - bool OpenSystemSettings(SystemSettingsType type) { if (type == SystemSettingsType::Audio) { crl::on_main([] {