Added close player button, slightly improved switching of touch bar.

This commit is contained in:
23rd 2019-04-30 21:31:42 +03:00 committed by John Preston
parent c9f56abce5
commit 3eadc62cd5
4 changed files with 44 additions and 61 deletions

View File

@ -285,6 +285,8 @@ public:
void notify_userIsBotChanged(UserData *bot); void notify_userIsBotChanged(UserData *bot);
void notify_historyMuteUpdated(History *history); void notify_historyMuteUpdated(History *history);
void closeBothPlayers();
bool isQuitPrevent(); bool isQuitPrevent();
~MainWidget(); ~MainWidget();
@ -347,7 +349,6 @@ private:
void setupConnectingWidget(); void setupConnectingWidget();
void createPlayer(); void createPlayer();
void closeBothPlayers();
void playerHeightUpdated(); void playerHeightUpdated();
void setCurrentCall(Calls::Call *call); void setCurrentCall(Calls::Call *call);

View File

@ -429,6 +429,9 @@ void MainWindow::initHook() {
if (auto window = [view window]) { if (auto window = [view window]) {
_private->setNativeWindow(window, view); _private->setNativeWindow(window, view);
} }
// Create TouchBar.
[NSApplication sharedApplication].automaticCustomizeTouchBarMenuItemEnabled = YES;
_private->_touchBar = [[TouchBar alloc] init:view];
} }
} }
@ -583,10 +586,6 @@ void MainWindow::psFirstShow() {
setPositionInited(); setPositionInited();
createGlobalMenu(); createGlobalMenu();
// Create TouchBar.
[NSApplication sharedApplication].automaticCustomizeTouchBarMenuItemEnabled = YES;
_private->_touchBar = [[TouchBar alloc] init];
} }
void MainWindow::createGlobalMenu() { void MainWindow::createGlobalMenu() {

View File

@ -13,6 +13,7 @@
namespace { namespace {
enum class TouchBarType { enum class TouchBarType {
None,
Main, Main,
AudioPlayer, AudioPlayer,
}; };
@ -20,18 +21,14 @@ enum class TouchBarType {
static NSString * _Nullable BASE_ID = @"telegram.touchbar"; static NSString * _Nullable BASE_ID = @"telegram.touchbar";
static NSTouchBarCustomizationIdentifier _Nullable customID = @"telegram.touchbar"; static NSTouchBarCustomizationIdentifier _Nullable customID = @"telegram.touchbar";
static NSTouchBarCustomizationIdentifier _Nullable customIDMain = @"telegram.touchbar.main"; static NSTouchBarCustomizationIdentifier _Nullable customIDMain = @"telegram.touchbarMain";
static NSTouchBarItemIdentifier _Nullable savedMessages = [NSString stringWithFormat:@"%@.savedMessages", BASE_ID]; static NSTouchBarItemIdentifier _Nullable savedMessages = [NSString stringWithFormat:@"%@.savedMessages", customIDMain];
static NSTouchBarItemIdentifier _Nullable seekBar = [NSString stringWithFormat:@"%@.seekbar", BASE_ID]; static NSTouchBarItemIdentifier _Nullable seekBar = [NSString stringWithFormat:@"%@.seekbar", BASE_ID];
static NSTouchBarItemIdentifier _Nullable play = [NSString stringWithFormat:@"%@.play", BASE_ID]; static NSTouchBarItemIdentifier _Nullable play = [NSString stringWithFormat:@"%@.play", BASE_ID];
static NSTouchBarItemIdentifier _Nullable nextItem = [NSString stringWithFormat:@"%@.nextItem", BASE_ID]; static NSTouchBarItemIdentifier _Nullable nextItem = [NSString stringWithFormat:@"%@.nextItem", BASE_ID];
static NSTouchBarItemIdentifier _Nullable previousItem = [NSString stringWithFormat:@"%@.previousItem", BASE_ID]; static NSTouchBarItemIdentifier _Nullable previousItem = [NSString stringWithFormat:@"%@.previousItem", BASE_ID];
static NSTouchBarItemIdentifier _Nullable nextChapter = [NSString stringWithFormat:@"%@.nextChapter", BASE_ID]; static NSTouchBarItemIdentifier _Nullable closePlayer = [NSString stringWithFormat:@"%@.closePlayer", 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 currentPosition = [NSString stringWithFormat:@"%@.currentPosition", BASE_ID]; static NSTouchBarItemIdentifier _Nullable currentPosition = [NSString stringWithFormat:@"%@.currentPosition", BASE_ID];
static NSTouchBarItemIdentifier _Nullable timeLeft = [NSString stringWithFormat:@"%@.timeLeft", BASE_ID];
@interface TouchBar : NSTouchBar @interface TouchBar : NSTouchBar
@property TouchBarType touchBarType; @property TouchBarType touchBarType;
@ -39,10 +36,11 @@ static NSTouchBarItemIdentifier _Nullable timeLeft = [NSString stringWithFormat:
@property(retain) NSDictionary * _Nullable touchbarItems; @property(retain) NSDictionary * _Nullable touchbarItems;
@property(retain) NSTouchBar * _Nullable touchBarMain; @property(retain) NSTouchBar * _Nullable touchBarMain;
@property(retain) NSTouchBar * _Nullable touchBarAudioPlayer; @property(retain) NSTouchBar * _Nullable touchBarAudioPlayer;
@property(retain) NSView * _Nullable view;
@property(nonatomic, assign) double duration; @property(nonatomic, assign) double duration;
@property(nonatomic, assign) double position; @property(nonatomic, assign) double position;
- (nullable NSTouchBar *) makeTouchBar; - (id _Nonnull) init:(NSView * _Nonnull)view;
- (void)handlePropertyChange:(Media::Player::TrackState)property; - (void)handlePropertyChange:(Media::Player::TrackState)property;
@end @end

View File

@ -26,10 +26,12 @@
#include "styles/style_window.h" #include "styles/style_window.h"
namespace { namespace {
constexpr auto kPlayPause = 0x000; constexpr auto kSavedMessages = 0x001;
constexpr auto kPlaylistPrevious = 0x001;
constexpr auto kPlaylistNext = 0x002; constexpr auto kPlayPause = 0x002;
constexpr auto kSavedMessages = 0x003; constexpr auto kPlaylistPrevious = 0x003;
constexpr auto kPlaylistNext = 0x004;
constexpr auto kClosePlayer = 0x005;
constexpr auto kMs = 1000; constexpr auto kMs = 1000;
@ -41,9 +43,10 @@ constexpr auto kSongType = AudioMsgId::Type::Song;
@implementation TouchBar @implementation TouchBar
- (instancetype)init { - (id)init:(NSView *)view {
self = [super init]; self = [super init];
if (self) { if (self) {
self.view = view;
self.touchbarItems = @{ self.touchbarItems = @{
savedMessages: [NSMutableDictionary dictionaryWithDictionary:@{ savedMessages: [NSMutableDictionary dictionaryWithDictionary:@{
@"type": @"button", @"type": @"button",
@ -74,29 +77,11 @@ constexpr auto kSongType = AudioMsgId::Type::Song;
@"cmd": [NSNumber numberWithInt:kPlaylistNext], @"cmd": [NSNumber numberWithInt:kPlaylistNext],
@"image": [NSImage imageNamed:NSImageNameTouchBarGoForwardTemplate] @"image": [NSImage imageNamed:NSImageNameTouchBarGoForwardTemplate]
}], }],
previousChapter: [NSMutableDictionary dictionaryWithDictionary:@{ closePlayer: [NSMutableDictionary dictionaryWithDictionary:@{
@"type": @"button", @"type": @"button",
@"name": @"Previous Chapter", @"name": @"Close Player",
@"cmd": [NSNumber numberWithInt:kPlayPause], @"cmd": [NSNumber numberWithInt:kClosePlayer],
@"image": [NSImage imageNamed:NSImageNameTouchBarSkipBackTemplate] @"image": [NSImage imageNamed:NSImageNameTouchBarExitFullScreenTemplate]
}],
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]
}], }],
currentPosition: [NSMutableDictionary dictionaryWithDictionary:@{ currentPosition: [NSMutableDictionary dictionaryWithDictionary:@{
@"type": @"text", @"type": @"text",
@ -111,25 +96,20 @@ constexpr auto kSongType = AudioMsgId::Type::Song;
} }
- (void) createTouchBar{ - (void) createTouchBar{
_touchBarMain = [[NSTouchBar alloc] init];
_touchBarMain.delegate = self;
_touchBarMain.customizationIdentifier = customIDMain;
_touchBarMain.defaultItemIdentifiers = @[savedMessages];
_touchBarMain.customizationAllowedItemIdentifiers = @[savedMessages];
_touchBarAudioPlayer = [[NSTouchBar alloc] init]; _touchBarAudioPlayer = [[NSTouchBar alloc] init];
_touchBarAudioPlayer.delegate = self; _touchBarAudioPlayer.delegate = self;
_touchBarAudioPlayer.customizationIdentifier = customID; _touchBarAudioPlayer.customizationIdentifier = customID;
_touchBarAudioPlayer.defaultItemIdentifiers = @[savedMessages, play, previousItem, nextItem, seekBar]; _touchBarAudioPlayer.defaultItemIdentifiers = @[play, previousItem, nextItem, seekBar, closePlayer];
_touchBarAudioPlayer.customizationAllowedItemIdentifiers = @[savedMessages, play, previousItem, _touchBarAudioPlayer.customizationAllowedItemIdentifiers = @[play, previousItem,
nextItem, currentPosition, seekBar]; nextItem, currentPosition, seekBar, closePlayer];
_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;
} }
- (nullable NSTouchBarItem *)touchBar:(NSTouchBar *)touchBar - (nullable NSTouchBarItem *)touchBar:(NSTouchBar *)touchBar
@ -170,16 +150,19 @@ constexpr auto kSongType = AudioMsgId::Type::Song;
} }
self.touchBarType = type; self.touchBarType = type;
if (type == TouchBarType::Main) { if (type == TouchBarType::Main) {
[NSApplication sharedApplication].mainWindow.touchBar = _touchBarMain; [self.view setTouchBar:_touchBarMain];
} else if (type == TouchBarType::AudioPlayer) { } else if (type == TouchBarType::AudioPlayer) {
[NSApplication sharedApplication].mainWindow.touchBar = _touchBarAudioPlayer; [self.view setTouchBar:_touchBarAudioPlayer];
} }
} }
- (void)handlePropertyChange:(Media::Player::TrackState)property { - (void)handlePropertyChange:(Media::Player::TrackState)property {
// #TODO: fix hiding of touch bar when last track is ended.
if (property.state == Media::Player::State::Stopped) { if (property.state == Media::Player::State::Stopped) {
[self setTouchBar:TouchBarType::Main]; [self setTouchBar:TouchBarType::Main];
return; return;
} else if (property.state == Media::Player::State::StoppedAtEnd) {
[self setTouchBar:TouchBarType::AudioPlayer];
} else { } else {
[self setTouchBar:TouchBarType::AudioPlayer]; [self setTouchBar:TouchBarType::AudioPlayer];
} }
@ -289,15 +272,17 @@ constexpr auto kSongType = AudioMsgId::Type::Song;
const auto command = [self.touchbarItems[identifier][@"cmd"] intValue]; const auto command = [self.touchbarItems[identifier][@"cmd"] intValue];
Core::Sandbox::Instance().customEnterFromEventLoop([=] { Core::Sandbox::Instance().customEnterFromEventLoop([=] {
if (command == kPlayPause) { if (command == kSavedMessages) {
App::main()->choosePeer(Auth().userPeerId(), ShowAtUnreadMsgId);
} else if (command == kPlayPause) {
Media::Player::instance()->playPause(); Media::Player::instance()->playPause();
} else if (command == kPlaylistPrevious) { } else if (command == kPlaylistPrevious) {
Media::Player::instance()->previous(); Media::Player::instance()->previous();
} else if (command == kPlaylistNext) { } else if (command == kPlaylistNext) {
Media::Player::instance()->next(); Media::Player::instance()->next();
} else if (command == kSavedMessages) { } else if (command == kClosePlayer) {
App::main()->choosePeer(Auth().userPeerId(), ShowAtUnreadMsgId); App::main()->closeBothPlayers();
} }
}); });
} }