From 3eadc62cd590c818f1f0e16a5c41ffdfbe24bf45 Mon Sep 17 00:00:00 2001 From: 23rd <23rd@vivaldi.net> Date: Tue, 30 Apr 2019 21:31:42 +0300 Subject: [PATCH] Added close player button, slightly improved switching of touch bar. --- Telegram/SourceFiles/mainwidget.h | 3 +- .../platform/mac/main_window_mac.mm | 7 +- Telegram/SourceFiles/platform/mac/touchbar.h | 14 ++-- Telegram/SourceFiles/platform/mac/touchbar.mm | 81 ++++++++----------- 4 files changed, 44 insertions(+), 61 deletions(-) diff --git a/Telegram/SourceFiles/mainwidget.h b/Telegram/SourceFiles/mainwidget.h index 5241b7e8d5..7c0ffc337e 100644 --- a/Telegram/SourceFiles/mainwidget.h +++ b/Telegram/SourceFiles/mainwidget.h @@ -285,6 +285,8 @@ public: void notify_userIsBotChanged(UserData *bot); void notify_historyMuteUpdated(History *history); + void closeBothPlayers(); + bool isQuitPrevent(); ~MainWidget(); @@ -347,7 +349,6 @@ private: void setupConnectingWidget(); void createPlayer(); - void closeBothPlayers(); void playerHeightUpdated(); void setCurrentCall(Calls::Call *call); diff --git a/Telegram/SourceFiles/platform/mac/main_window_mac.mm b/Telegram/SourceFiles/platform/mac/main_window_mac.mm index 5d410ee7f4..a4f010e23c 100644 --- a/Telegram/SourceFiles/platform/mac/main_window_mac.mm +++ b/Telegram/SourceFiles/platform/mac/main_window_mac.mm @@ -429,6 +429,9 @@ void MainWindow::initHook() { if (auto window = [view window]) { _private->setNativeWindow(window, view); } + // Create TouchBar. + [NSApplication sharedApplication].automaticCustomizeTouchBarMenuItemEnabled = YES; + _private->_touchBar = [[TouchBar alloc] init:view]; } } @@ -583,10 +586,6 @@ void MainWindow::psFirstShow() { setPositionInited(); createGlobalMenu(); - - // Create TouchBar. - [NSApplication sharedApplication].automaticCustomizeTouchBarMenuItemEnabled = YES; - _private->_touchBar = [[TouchBar alloc] init]; } void MainWindow::createGlobalMenu() { diff --git a/Telegram/SourceFiles/platform/mac/touchbar.h b/Telegram/SourceFiles/platform/mac/touchbar.h index 0cd7cb6dea..4a05cdf003 100644 --- a/Telegram/SourceFiles/platform/mac/touchbar.h +++ b/Telegram/SourceFiles/platform/mac/touchbar.h @@ -13,6 +13,7 @@ namespace { enum class TouchBarType { + None, Main, AudioPlayer, }; @@ -20,18 +21,14 @@ enum class TouchBarType { static NSString * _Nullable BASE_ID = @"telegram.touchbar"; static NSTouchBarCustomizationIdentifier _Nullable customID = @"telegram.touchbar"; -static NSTouchBarCustomizationIdentifier _Nullable customIDMain = @"telegram.touchbar.main"; -static NSTouchBarItemIdentifier _Nullable savedMessages = [NSString stringWithFormat:@"%@.savedMessages", BASE_ID]; +static NSTouchBarCustomizationIdentifier _Nullable customIDMain = @"telegram.touchbarMain"; +static NSTouchBarItemIdentifier _Nullable savedMessages = [NSString stringWithFormat:@"%@.savedMessages", customIDMain]; static NSTouchBarItemIdentifier _Nullable seekBar = [NSString stringWithFormat:@"%@.seekbar", BASE_ID]; static NSTouchBarItemIdentifier _Nullable play = [NSString stringWithFormat:@"%@.play", BASE_ID]; static NSTouchBarItemIdentifier _Nullable nextItem = [NSString stringWithFormat:@"%@.nextItem", BASE_ID]; static NSTouchBarItemIdentifier _Nullable previousItem = [NSString stringWithFormat:@"%@.previousItem", BASE_ID]; -static NSTouchBarItemIdentifier _Nullable nextChapter = [NSString stringWithFormat:@"%@.nextChapter", BASE_ID]; -static NSTouchBarItemIdentifier _Nullable previousChapter = [NSString stringWithFormat:@"%@.previousChapter", BASE_ID]; -static NSTouchBarItemIdentifier _Nullable cycleAudio = [NSString stringWithFormat:@"%@.cycleAudio", BASE_ID]; -static NSTouchBarItemIdentifier _Nullable cycleSubtitle = [NSString stringWithFormat:@"%@.cycleSubtitle", BASE_ID]; +static NSTouchBarItemIdentifier _Nullable closePlayer = [NSString stringWithFormat:@"%@.closePlayer", BASE_ID]; static NSTouchBarItemIdentifier _Nullable currentPosition = [NSString stringWithFormat:@"%@.currentPosition", BASE_ID]; -static NSTouchBarItemIdentifier _Nullable timeLeft = [NSString stringWithFormat:@"%@.timeLeft", BASE_ID]; @interface TouchBar : NSTouchBar @property TouchBarType touchBarType; @@ -39,10 +36,11 @@ static NSTouchBarItemIdentifier _Nullable timeLeft = [NSString stringWithFormat: @property(retain) NSDictionary * _Nullable touchbarItems; @property(retain) NSTouchBar * _Nullable touchBarMain; @property(retain) NSTouchBar * _Nullable touchBarAudioPlayer; +@property(retain) NSView * _Nullable view; @property(nonatomic, assign) double duration; @property(nonatomic, assign) double position; -- (nullable NSTouchBar *) makeTouchBar; +- (id _Nonnull) init:(NSView * _Nonnull)view; - (void)handlePropertyChange:(Media::Player::TrackState)property; @end diff --git a/Telegram/SourceFiles/platform/mac/touchbar.mm b/Telegram/SourceFiles/platform/mac/touchbar.mm index 856ab63e43..d6c3d6af55 100644 --- a/Telegram/SourceFiles/platform/mac/touchbar.mm +++ b/Telegram/SourceFiles/platform/mac/touchbar.mm @@ -26,10 +26,12 @@ #include "styles/style_window.h" namespace { -constexpr auto kPlayPause = 0x000; -constexpr auto kPlaylistPrevious = 0x001; -constexpr auto kPlaylistNext = 0x002; -constexpr auto kSavedMessages = 0x003; +constexpr auto kSavedMessages = 0x001; + +constexpr auto kPlayPause = 0x002; +constexpr auto kPlaylistPrevious = 0x003; +constexpr auto kPlaylistNext = 0x004; +constexpr auto kClosePlayer = 0x005; constexpr auto kMs = 1000; @@ -41,9 +43,10 @@ constexpr auto kSongType = AudioMsgId::Type::Song; @implementation TouchBar -- (instancetype)init { +- (id)init:(NSView *)view { self = [super init]; if (self) { + self.view = view; self.touchbarItems = @{ savedMessages: [NSMutableDictionary dictionaryWithDictionary:@{ @"type": @"button", @@ -74,29 +77,11 @@ constexpr auto kSongType = AudioMsgId::Type::Song; @"cmd": [NSNumber numberWithInt:kPlaylistNext], @"image": [NSImage imageNamed:NSImageNameTouchBarGoForwardTemplate] }], - previousChapter: [NSMutableDictionary dictionaryWithDictionary:@{ + closePlayer: [NSMutableDictionary dictionaryWithDictionary:@{ @"type": @"button", - @"name": @"Previous Chapter", - @"cmd": [NSNumber numberWithInt:kPlayPause], - @"image": [NSImage imageNamed:NSImageNameTouchBarSkipBackTemplate] - }], - nextChapter: [NSMutableDictionary dictionaryWithDictionary:@{ - @"type": @"button", - @"name": @"Next Chapter", - @"cmd": [NSNumber numberWithInt:kPlayPause], - @"image": [NSImage imageNamed:NSImageNameTouchBarSkipAheadTemplate] - }], - cycleAudio: [NSMutableDictionary dictionaryWithDictionary:@{ - @"type": @"button", - @"name": @"Cycle Audio", - @"cmd": [NSNumber numberWithInt:kPlayPause], - @"image": [NSImage imageNamed:NSImageNameTouchBarAudioInputTemplate] - }], - cycleSubtitle: [NSMutableDictionary dictionaryWithDictionary:@{ - @"type": @"button", - @"name": @"Cycle Subtitle", - @"cmd": [NSNumber numberWithInt:kPlayPause], - @"image": [NSImage imageNamed:NSImageNameTouchBarComposeTemplate] + @"name": @"Close Player", + @"cmd": [NSNumber numberWithInt:kClosePlayer], + @"image": [NSImage imageNamed:NSImageNameTouchBarExitFullScreenTemplate] }], currentPosition: [NSMutableDictionary dictionaryWithDictionary:@{ @"type": @"text", @@ -111,25 +96,20 @@ constexpr auto kSongType = AudioMsgId::Type::Song; } - (void) createTouchBar{ + _touchBarMain = [[NSTouchBar alloc] init]; + _touchBarMain.delegate = self; + + _touchBarMain.customizationIdentifier = customIDMain; + _touchBarMain.defaultItemIdentifiers = @[savedMessages]; + _touchBarMain.customizationAllowedItemIdentifiers = @[savedMessages]; + _touchBarAudioPlayer = [[NSTouchBar alloc] init]; _touchBarAudioPlayer.delegate = self; _touchBarAudioPlayer.customizationIdentifier = customID; - _touchBarAudioPlayer.defaultItemIdentifiers = @[savedMessages, play, previousItem, nextItem, seekBar]; - _touchBarAudioPlayer.customizationAllowedItemIdentifiers = @[savedMessages, play, previousItem, - nextItem, currentPosition, seekBar]; - - _touchBarMain = [[NSTouchBar alloc] init]; - _touchBarMain.delegate = self; - - _touchBarMain.customizationIdentifier = customIDMain; - _touchBarMain.defaultItemIdentifiers = @[savedMessages]; - _touchBarMain.customizationAllowedItemIdentifiers = @[savedMessages]; -} - -- (nullable NSTouchBar *) makeTouchBar{ - return [NSApplication sharedApplication].mainWindow.touchBar; -// [NSApplication sharedApplication].mainWindow.touchBar = touchBar; + _touchBarAudioPlayer.defaultItemIdentifiers = @[play, previousItem, nextItem, seekBar, closePlayer]; + _touchBarAudioPlayer.customizationAllowedItemIdentifiers = @[play, previousItem, + nextItem, currentPosition, seekBar, closePlayer]; } - (nullable NSTouchBarItem *)touchBar:(NSTouchBar *)touchBar @@ -170,16 +150,19 @@ constexpr auto kSongType = AudioMsgId::Type::Song; } self.touchBarType = type; if (type == TouchBarType::Main) { - [NSApplication sharedApplication].mainWindow.touchBar = _touchBarMain; + [self.view setTouchBar:_touchBarMain]; } else if (type == TouchBarType::AudioPlayer) { - [NSApplication sharedApplication].mainWindow.touchBar = _touchBarAudioPlayer; + [self.view setTouchBar:_touchBarAudioPlayer]; } } - (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]; } @@ -289,15 +272,17 @@ constexpr auto kSongType = AudioMsgId::Type::Song; const auto command = [self.touchbarItems[identifier][@"cmd"] intValue]; Core::Sandbox::Instance().customEnterFromEventLoop([=] { - if (command == kPlayPause) { + if (command == kSavedMessages) { + App::main()->choosePeer(Auth().userPeerId(), ShowAtUnreadMsgId); + } else if (command == kPlayPause) { Media::Player::instance()->playPause(); } else if (command == kPlaylistPrevious) { Media::Player::instance()->previous(); } else if (command == kPlaylistNext) { Media::Player::instance()->next(); - } else if (command == kSavedMessages) { - App::main()->choosePeer(Auth().userPeerId(), ShowAtUnreadMsgId); - } + } else if (command == kClosePlayer) { + App::main()->closeBothPlayers(); + } }); }