Added initial implementation of switching between touch bars.

This commit is contained in:
23rd 2019-04-29 19:55:11 +03:00 committed by John Preston
parent 58604406f8
commit c9f56abce5
3 changed files with 248 additions and 205 deletions

View File

@ -189,9 +189,6 @@ MainWindow::Private::Private(MainWindow *window)
, _observer([[MainWindowObserver alloc] init:this]) {
_generalPasteboard = [NSPasteboard generalPasteboard];
_touchBar = [[TouchBar alloc] init];
[_touchBar setTouchBarType:Platform::TouchBarType::AudioPlayer];
@autoreleasepool {
[[[NSWorkspace sharedWorkspace] notificationCenter] addObserver:_observer selector:@selector(activeSpaceDidChange:) name:NSWorkspaceActiveSpaceDidChangeNotification object:nil];
@ -217,10 +214,6 @@ void MainWindow::Private::setWindowBadge(const QString &str) {
void MainWindow::Private::setWindowTitle(const QString &str) {
_public->setWindowTitle(str);
if ([[NSApplication sharedApplication] respondsToSelector:@selector(isAutomaticCustomizeTouchBarMenuItemEnabled)]) {
[NSApplication sharedApplication].automaticCustomizeTouchBarMenuItemEnabled = YES;
}
[NSApplication sharedApplication].mainWindow.touchBar = [_touchBar makeTouchBar];
updateNativeTitle();
}
@ -590,6 +583,10 @@ void MainWindow::psFirstShow() {
setPositionInited();
createGlobalMenu();
// Create TouchBar.
[NSApplication sharedApplication].automaticCustomizeTouchBarMenuItemEnabled = YES;
_private->_touchBar = [[TouchBar alloc] init];
}
void MainWindow::createGlobalMenu() {

View File

@ -11,8 +11,17 @@
#include "media/player/media_player_instance.h"
#import <Cocoa/Cocoa.h>
namespace {
enum class TouchBarType {
Main,
AudioPlayer,
};
} // namespace
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 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];
@ -25,9 +34,11 @@ static NSTouchBarItemIdentifier _Nullable currentPosition = [NSString stringWith
static NSTouchBarItemIdentifier _Nullable timeLeft = [NSString stringWithFormat:@"%@.timeLeft", BASE_ID];
@interface TouchBar : NSTouchBar
@property Platform::TouchBarType touchBarType;
@property TouchBarType touchBarType;
@property(retain) NSDictionary * _Nullable touchbarItems;
@property(retain) NSTouchBar * _Nullable touchBarMain;
@property(retain) NSTouchBar * _Nullable touchBarAudioPlayer;
@property(nonatomic, assign) double duration;
@property(nonatomic, assign) double position;

View File

@ -29,12 +29,12 @@ namespace {
constexpr auto kPlayPause = 0x000;
constexpr auto kPlaylistPrevious = 0x001;
constexpr auto kPlaylistNext = 0x002;
constexpr auto kSavedMessages = 0x002;
constexpr auto kSavedMessages = 0x003;
constexpr auto kMs = 1000;
constexpr auto kSongType = AudioMsgId::Type::Song;
}
} // namespace
@interface TouchBar()<NSTouchBarDelegate>
@end
@ -44,13 +44,16 @@ constexpr auto kSongType = AudioMsgId::Type::Song;
- (instancetype)init {
self = [super init];
if (self) {
self.touchBarType = Platform::TouchBarType::None;
self.touchbarItems = @{
savedMessages: [NSMutableDictionary dictionaryWithDictionary:@{
@"type": @"button",
@"name": @"Saved Messages",
@"cmd": [NSNumber numberWithInt:kSavedMessages],
@"image": [NSImage imageNamed:NSImageNameTouchBarBookmarksTemplate],
}],
seekBar: [NSMutableDictionary dictionaryWithDictionary:@{
@"type": @"slider",
@"name": @"Seek Bar",
@"cmd": [NSNumber numberWithInt:kPlayPause]
@"name": @"Seek Bar"
}],
play: [NSMutableDictionary dictionaryWithDictionary:@{
@"type": @"button",
@ -101,21 +104,32 @@ constexpr auto kSongType = AudioMsgId::Type::Song;
}]
};
}
[self createTouchBar];
[self setTouchBar:TouchBarType::Main];
return self;
}
- (void) createTouchBar{
_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{
NSTouchBar *touchBar = [[NSTouchBar alloc] init];
touchBar.delegate = self;
touchBar.customizationIdentifier = @"TOUCH_BAR";
touchBar.customizationIdentifier = customID;
touchBar.defaultItemIdentifiers = @[play, previousItem, nextItem, seekBar];
touchBar.customizationAllowedItemIdentifiers = @[play, seekBar, previousItem,
nextItem, previousChapter, nextChapter, cycleAudio, cycleSubtitle,
currentPosition];
return touchBar;
return [NSApplication sharedApplication].mainWindow.touchBar;
// [NSApplication sharedApplication].mainWindow.touchBar = touchBar;
}
- (nullable NSTouchBarItem *)touchBar:(NSTouchBar *)touchBar
@ -150,7 +164,26 @@ constexpr auto kSongType = AudioMsgId::Type::Song;
return nil;
}
- (void)setTouchBar:(TouchBarType)type {
if (self.touchBarType == type) {
return;
}
self.touchBarType = type;
if (type == TouchBarType::Main) {
[NSApplication sharedApplication].mainWindow.touchBar = _touchBarMain;
} else if (type == TouchBarType::AudioPlayer) {
[NSApplication sharedApplication].mainWindow.touchBar = _touchBarAudioPlayer;
}
}
- (void)handlePropertyChange:(Media::Player::TrackState)property {
if (property.state == Media::Player::State::Stopped) {
[self setTouchBar:TouchBarType::Main];
return;
} else {
[self setTouchBar:TouchBarType::AudioPlayer];
}
self.position = property.position < 0 ? 0 : property.position;
self.duration = property.length;
[self updateTouchBarTimeItems];
@ -254,7 +287,7 @@ constexpr auto kSongType = AudioMsgId::Type::Song;
- (void)buttonAction:(NSButton *)sender {
NSString *identifier = [self getIdentifierFromView:sender];
const auto command = [self.touchbarItems[identifier][@"cmd"] intValue];
LOG(("BUTTON %1").arg(command));
Core::Sandbox::Instance().customEnterFromEventLoop([=] {
if (command == kPlayPause) {
Media::Player::instance()->playPause();
@ -262,6 +295,8 @@ constexpr auto kSongType = AudioMsgId::Type::Song;
Media::Player::instance()->previous();
} else if (command == kPlaylistNext) {
Media::Player::instance()->next();
} else if (command == kSavedMessages) {
App::main()->choosePeer(Auth().userPeerId(), ShowAtUnreadMsgId);
}
});
}