2019-04-29 16:41:51 +03:00
|
|
|
/*
|
|
|
|
This file is part of Telegram Desktop,
|
|
|
|
the official desktop application for the Telegram messaging service.
|
|
|
|
|
|
|
|
For license and copyright information please follow this link:
|
|
|
|
https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
|
|
|
|
*/
|
|
|
|
|
|
|
|
#import "touchbar.h"
|
|
|
|
#import <QuartzCore/QuartzCore.h>
|
|
|
|
|
2019-05-01 16:18:31 +03:00
|
|
|
#include "auth_session.h"
|
2019-05-04 13:22:21 +03:00
|
|
|
#include "core/application.h"
|
|
|
|
#include "core/sandbox.h"
|
|
|
|
#include "data/data_folder.h"
|
2019-05-01 16:18:31 +03:00
|
|
|
#include "data/data_session.h"
|
|
|
|
#include "history/history.h"
|
2019-05-04 13:22:21 +03:00
|
|
|
#include "mainwidget.h"
|
|
|
|
#include "mainwindow.h"
|
2019-05-01 21:09:40 +03:00
|
|
|
#include "observer_peer.h"
|
2019-05-04 11:54:38 +03:00
|
|
|
#include "styles/style_media_player.h"
|
2019-05-04 13:22:21 +03:00
|
|
|
#include "ui/empty_userpic.h"
|
2019-04-29 16:41:51 +03:00
|
|
|
|
|
|
|
namespace {
|
2019-05-01 20:50:59 +03:00
|
|
|
//https://developer.apple.com/design/human-interface-guidelines/macos/touch-bar/touch-bar-icons-and-images/
|
|
|
|
constexpr auto kIdealIconSize = 36;
|
|
|
|
constexpr auto kMaximumIconSize = 44;
|
|
|
|
|
2019-04-30 21:31:42 +03:00
|
|
|
constexpr auto kPlayPause = 0x002;
|
|
|
|
constexpr auto kPlaylistPrevious = 0x003;
|
|
|
|
constexpr auto kPlaylistNext = 0x004;
|
|
|
|
constexpr auto kClosePlayer = 0x005;
|
2019-04-29 19:55:11 +03:00
|
|
|
|
2019-04-29 16:41:51 +03:00
|
|
|
constexpr auto kMs = 1000;
|
2019-04-29 19:55:11 +03:00
|
|
|
|
2019-04-29 16:41:51 +03:00
|
|
|
constexpr auto kSongType = AudioMsgId::Type::Song;
|
2019-05-01 20:50:59 +03:00
|
|
|
|
|
|
|
constexpr auto kSavedMessagesId = 0;
|
2019-05-01 22:10:09 +03:00
|
|
|
constexpr auto kArchiveId = -1;
|
2019-04-29 19:55:11 +03:00
|
|
|
} // namespace
|
2019-04-29 16:41:51 +03:00
|
|
|
|
2019-05-01 16:18:31 +03:00
|
|
|
NSImage *qt_mac_create_nsimage(const QPixmap &pm);
|
|
|
|
|
|
|
|
@interface PinnedDialogButton : NSCustomTouchBarItem {
|
|
|
|
}
|
|
|
|
|
|
|
|
@property(nonatomic, assign) int number;
|
|
|
|
@property(nonatomic, assign) bool waiting;
|
|
|
|
@property(nonatomic, assign) PeerData * peer;
|
2019-05-07 20:08:00 +03:00
|
|
|
@property(nonatomic, assign) bool isDeletedFromView;
|
2019-05-01 16:18:31 +03:00
|
|
|
|
|
|
|
- (id) init:(int)num;
|
|
|
|
- (NSImage *) getPinImage;
|
|
|
|
- (void)buttonActionPin:(NSButton *)sender;
|
2019-05-02 16:26:47 +03:00
|
|
|
- (void)updatePinnedDialog;
|
2019-05-01 16:18:31 +03:00
|
|
|
|
|
|
|
@end // @interface PinnedDialogButton
|
|
|
|
|
|
|
|
@implementation PinnedDialogButton : NSCustomTouchBarItem
|
|
|
|
|
2019-05-02 10:59:15 +03:00
|
|
|
auto lifetime = rpl::lifetime();
|
|
|
|
|
2019-05-01 16:18:31 +03:00
|
|
|
- (id) init:(int)num {
|
2019-05-01 20:50:59 +03:00
|
|
|
if (num == kSavedMessagesId) {
|
2019-05-08 12:29:38 +03:00
|
|
|
self = [super initWithIdentifier:savedMessages];
|
|
|
|
self.waiting = false;
|
|
|
|
self.customizationLabel = [NSString stringWithFormat:@"Pinned Dialog %d", num];
|
2019-05-01 22:10:09 +03:00
|
|
|
} else if (num == kArchiveId) {
|
2019-05-08 12:29:38 +03:00
|
|
|
self = [super initWithIdentifier:archiveFolder];
|
|
|
|
self.waiting = false;
|
|
|
|
self.customizationLabel = @"Archive Folder";
|
|
|
|
} else {
|
|
|
|
NSString *identifier = [NSString stringWithFormat:@"%@.pinnedDialog%d", customIDMain, num];
|
|
|
|
self = [super initWithIdentifier:identifier];
|
|
|
|
self.waiting = true;
|
|
|
|
self.customizationLabel = @"Saved Messages";
|
2019-05-01 20:50:59 +03:00
|
|
|
}
|
2019-05-01 16:18:31 +03:00
|
|
|
if (!self) {
|
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
self.number = num;
|
|
|
|
|
|
|
|
NSButton *button = [NSButton buttonWithImage:[self getPinImage] target:self action:@selector(buttonActionPin:)];
|
|
|
|
[button setBordered:NO];
|
|
|
|
[button sizeToFit];
|
|
|
|
self.view = button;
|
2019-05-08 12:29:38 +03:00
|
|
|
|
|
|
|
if (num <= kSavedMessagesId) {
|
|
|
|
return self;
|
|
|
|
}
|
2019-05-01 16:18:31 +03:00
|
|
|
|
2019-05-02 16:26:47 +03:00
|
|
|
if (self.peer) {
|
|
|
|
Notify::PeerUpdateViewer(
|
|
|
|
self.peer,
|
|
|
|
Notify::PeerUpdate::Flag::PhotoChanged
|
|
|
|
) | rpl::start_with_next([=] {
|
|
|
|
self.waiting = true;
|
2019-05-07 20:08:00 +03:00
|
|
|
[self updatePinnedDialog];
|
2019-05-02 16:26:47 +03:00
|
|
|
}, lifetime);
|
|
|
|
}
|
|
|
|
|
2019-05-01 16:18:31 +03:00
|
|
|
base::ObservableViewer(
|
|
|
|
Auth().downloaderTaskFinished()
|
2019-05-01 21:09:40 +03:00
|
|
|
) | rpl::start_with_next([=] {
|
2019-05-01 16:18:31 +03:00
|
|
|
if (self.waiting) {
|
2019-05-07 20:08:00 +03:00
|
|
|
[self updatePinnedDialog];
|
2019-05-01 16:18:31 +03:00
|
|
|
}
|
2019-05-02 10:59:15 +03:00
|
|
|
}, lifetime);
|
2019-05-01 16:18:31 +03:00
|
|
|
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2019-05-02 16:26:47 +03:00
|
|
|
- (void) updatePinnedDialog {
|
|
|
|
NSButton *button = self.view;
|
|
|
|
button.image = [self getPinImage];
|
|
|
|
}
|
|
|
|
|
2019-05-02 13:25:05 +03:00
|
|
|
- (void) buttonActionPin:(NSButton *)sender {
|
2019-05-01 16:18:31 +03:00
|
|
|
Core::Sandbox::Instance().customEnterFromEventLoop([=] {
|
2019-05-01 22:10:09 +03:00
|
|
|
App::main()->choosePeer(self.number == kSavedMessagesId || self.number == kArchiveId
|
2019-05-01 20:50:59 +03:00
|
|
|
? Auth().userPeerId()
|
|
|
|
: self.peer->id, ShowAtUnreadMsgId);
|
2019-05-01 16:18:31 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-05-04 14:06:25 +03:00
|
|
|
- (bool) isSelfPeer {
|
|
|
|
return !self.peer ? false : self.peer->id == Auth().userPeerId();
|
|
|
|
}
|
2019-05-01 16:18:31 +03:00
|
|
|
|
|
|
|
- (NSImage *) getPinImage {
|
2019-05-04 14:06:25 +03:00
|
|
|
// Don't draw self userpic if we pin Saved Messages.
|
|
|
|
if (self.number <= kSavedMessagesId || [self isSelfPeer]) {
|
2019-05-01 20:50:59 +03:00
|
|
|
const int s = kIdealIconSize * cRetinaFactor();
|
|
|
|
auto *pix = new QPixmap(s, s);
|
|
|
|
Painter paint(pix);
|
|
|
|
paint.fillRect(QRectF(0, 0, s, s), QColor(0, 0, 0, 255));
|
|
|
|
|
2019-05-01 22:10:09 +03:00
|
|
|
if (self.number == kArchiveId) {
|
|
|
|
paint.fillRect(QRectF(0, 0, s, s), QColor(0, 0, 0, 255));
|
|
|
|
if (const auto folder = Auth().data().folderLoaded(Data::Folder::kId)) {
|
|
|
|
folder->paintUserpic(paint, 0, 0, s);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
Ui::EmptyUserpic::PaintSavedMessages(paint, 0, 0, s, s);
|
|
|
|
}
|
2019-05-01 20:50:59 +03:00
|
|
|
pix->setDevicePixelRatio(cRetinaFactor());
|
2019-05-04 11:54:38 +03:00
|
|
|
return [qt_mac_create_nsimage(*pix) autorelease];
|
2019-05-01 20:50:59 +03:00
|
|
|
}
|
2019-05-01 16:18:31 +03:00
|
|
|
if (!self.peer) {
|
2019-05-02 16:26:47 +03:00
|
|
|
// Random picture.
|
|
|
|
return [NSImage imageNamed:NSImageNameTouchBarAddTemplate];
|
2019-05-01 16:18:31 +03:00
|
|
|
}
|
|
|
|
self.waiting = !self.peer->userpicLoaded();
|
2019-05-01 20:50:59 +03:00
|
|
|
auto pixmap = self.peer->genUserpic(kIdealIconSize);
|
2019-05-01 16:18:31 +03:00
|
|
|
pixmap.setDevicePixelRatio(cRetinaFactor());
|
2019-05-04 11:54:38 +03:00
|
|
|
return [qt_mac_create_nsimage(pixmap) autorelease];
|
2019-05-01 16:18:31 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
|
|
|
|
@interface TouchBar()<NSTouchBarDelegate>
|
|
|
|
@end // @interface TouchBar
|
|
|
|
|
2019-04-29 16:41:51 +03:00
|
|
|
@implementation TouchBar
|
|
|
|
|
2019-05-02 13:25:05 +03:00
|
|
|
- (id) init:(NSView *)view {
|
2019-04-29 19:55:11 +03:00
|
|
|
self = [super init];
|
|
|
|
if (self) {
|
2019-05-04 13:24:56 +03:00
|
|
|
const auto iconSize = kIdealIconSize / 3;
|
2019-04-30 21:31:42 +03:00
|
|
|
self.view = view;
|
2019-04-29 19:55:11 +03:00
|
|
|
self.touchbarItems = @{
|
2019-05-02 13:25:05 +03:00
|
|
|
pinnedPanel: [NSMutableDictionary dictionaryWithDictionary:@{
|
2019-05-01 16:18:31 +03:00
|
|
|
@"type": @"pinned",
|
|
|
|
}],
|
2019-04-29 19:55:11 +03:00
|
|
|
seekBar: [NSMutableDictionary dictionaryWithDictionary:@{
|
|
|
|
@"type": @"slider",
|
|
|
|
@"name": @"Seek Bar"
|
|
|
|
}],
|
|
|
|
play: [NSMutableDictionary dictionaryWithDictionary:@{
|
|
|
|
@"type": @"button",
|
|
|
|
@"name": @"Play Button",
|
|
|
|
@"cmd": [NSNumber numberWithInt:kPlayPause],
|
2019-05-04 13:24:56 +03:00
|
|
|
@"image": createImageFromStyleIcon(st::touchBarIconPlayerPause, iconSize),
|
|
|
|
@"imageAlt": createImageFromStyleIcon(st::touchBarIconPlayerPlay, iconSize),
|
2019-04-29 19:55:11 +03:00
|
|
|
}],
|
|
|
|
previousItem: [NSMutableDictionary dictionaryWithDictionary:@{
|
|
|
|
@"type": @"button",
|
|
|
|
@"name": @"Previous Playlist Item",
|
|
|
|
@"cmd": [NSNumber numberWithInt:kPlaylistPrevious],
|
2019-05-04 13:24:56 +03:00
|
|
|
@"image": createImageFromStyleIcon(st::touchBarIconPlayerPrevious, iconSize),
|
2019-04-29 19:55:11 +03:00
|
|
|
}],
|
|
|
|
nextItem: [NSMutableDictionary dictionaryWithDictionary:@{
|
|
|
|
@"type": @"button",
|
|
|
|
@"name": @"Next Playlist Item",
|
|
|
|
@"cmd": [NSNumber numberWithInt:kPlaylistNext],
|
2019-05-04 13:24:56 +03:00
|
|
|
@"image": createImageFromStyleIcon(st::touchBarIconPlayerNext, iconSize),
|
2019-04-29 19:55:11 +03:00
|
|
|
}],
|
2019-04-30 21:31:42 +03:00
|
|
|
closePlayer: [NSMutableDictionary dictionaryWithDictionary:@{
|
2019-04-29 19:55:11 +03:00
|
|
|
@"type": @"button",
|
2019-04-30 21:31:42 +03:00
|
|
|
@"name": @"Close Player",
|
|
|
|
@"cmd": [NSNumber numberWithInt:kClosePlayer],
|
2019-05-04 13:24:56 +03:00
|
|
|
@"image": createImageFromStyleIcon(st::touchBarIconPlayerClose, iconSize),
|
2019-04-29 19:55:11 +03:00
|
|
|
}],
|
|
|
|
currentPosition: [NSMutableDictionary dictionaryWithDictionary:@{
|
|
|
|
@"type": @"text",
|
|
|
|
@"name": @"Current Position"
|
|
|
|
}]
|
|
|
|
};
|
|
|
|
}
|
2019-05-01 16:18:31 +03:00
|
|
|
[self createTouchBar];
|
|
|
|
[self setTouchBar:TouchBarType::Main];
|
2019-04-29 19:55:11 +03:00
|
|
|
|
2019-05-02 13:00:05 +03:00
|
|
|
Media::Player::instance()->playerWidgetToggled(
|
|
|
|
) | rpl::start_with_next([=](bool toggled) {
|
|
|
|
if (!toggled) {
|
|
|
|
[self setTouchBar:TouchBarType::Main];
|
|
|
|
} else {
|
|
|
|
[self setTouchBar:TouchBarType::AudioPlayer];
|
|
|
|
}
|
|
|
|
}, lifetime);
|
|
|
|
|
2019-05-02 14:02:54 +03:00
|
|
|
Core::App().passcodeLockChanges(
|
|
|
|
) | rpl::start_with_next([=](bool locked) {
|
|
|
|
if (locked) {
|
|
|
|
self.touchBarTypeBeforeLock = self.touchBarType;
|
|
|
|
[self setTouchBar:TouchBarType::None];
|
|
|
|
} else {
|
|
|
|
[self setTouchBar:self.touchBarTypeBeforeLock];
|
|
|
|
}
|
|
|
|
}, lifetime);
|
|
|
|
|
2019-05-07 20:08:00 +03:00
|
|
|
Auth().data().pinnedDialogsOrderUpdated(
|
|
|
|
) | rpl::start_with_next([self] {
|
|
|
|
[self updatePinnedButtons];
|
|
|
|
}, lifetime);
|
|
|
|
|
|
|
|
[self updatePinnedButtons];
|
|
|
|
|
2019-04-29 19:55:11 +03:00
|
|
|
return self;
|
2019-04-29 16:41:51 +03:00
|
|
|
}
|
|
|
|
|
2019-05-07 20:08:00 +03:00
|
|
|
- (void) updatePinnedButtons {
|
|
|
|
const auto &order = Auth().data().pinnedChatsOrder(nullptr);
|
|
|
|
auto isSelfPeerPinned = false;
|
|
|
|
PinnedDialogButton *selfChatButton;
|
|
|
|
NSCustomTouchBarItem *item = [self.touchBarMain itemForIdentifier:pinnedPanel];
|
|
|
|
NSStackView *stack = item.view;
|
|
|
|
|
|
|
|
for (PinnedDialogButton *button in self.mainPinnedButtons) {
|
|
|
|
const auto num = button.number;
|
|
|
|
if (num <= kSavedMessagesId) {
|
|
|
|
if (num == kSavedMessagesId) {
|
|
|
|
selfChatButton = button;
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
const auto numIsTooLarge = num > order.size();
|
|
|
|
[button.view setHidden:numIsTooLarge];
|
|
|
|
if (numIsTooLarge) {
|
|
|
|
button.peer = nil;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
const auto pinned = order.at(num - 1);
|
|
|
|
if (const auto history = pinned.history()) {
|
|
|
|
button.peer = history->peer;
|
|
|
|
[button updatePinnedDialog];
|
|
|
|
if (history->peer->id == Auth().userPeerId()) {
|
|
|
|
isSelfPeerPinned = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If self chat is pinned, delete from view saved messages button.
|
2019-05-08 12:29:38 +03:00
|
|
|
if (isSelfPeerPinned && !selfChatButton.isDeletedFromView) {
|
|
|
|
selfChatButton.isDeletedFromView = true;
|
|
|
|
[stack removeView:selfChatButton.view];
|
|
|
|
}
|
|
|
|
if (!isSelfPeerPinned && selfChatButton.isDeletedFromView) {
|
|
|
|
selfChatButton.isDeletedFromView = false;
|
|
|
|
[stack insertView:selfChatButton.view atIndex:0 inGravity:NSStackViewGravityLeading];
|
2019-05-07 20:08:00 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-04 13:24:56 +03:00
|
|
|
NSImage *createImageFromStyleIcon(const style::icon &icon, int size = kIdealIconSize) {
|
|
|
|
const auto instance = icon.instance(QColor(255, 255, 255, 255), 100);
|
|
|
|
auto pixmap = QPixmap::fromImage(instance);
|
|
|
|
pixmap.setDevicePixelRatio(cRetinaFactor());
|
|
|
|
NSImage *image = [qt_mac_create_nsimage(pixmap) autorelease];
|
|
|
|
[image setSize:NSMakeSize(size, size)];
|
2019-05-04 11:54:38 +03:00
|
|
|
return image;
|
|
|
|
}
|
|
|
|
|
2019-05-02 13:25:05 +03:00
|
|
|
- (void) createTouchBar {
|
2019-04-30 21:31:42 +03:00
|
|
|
_touchBarMain = [[NSTouchBar alloc] init];
|
|
|
|
_touchBarMain.delegate = self;
|
2019-05-02 13:25:05 +03:00
|
|
|
_touchBarMain.defaultItemIdentifiers = @[pinnedPanel];
|
2019-04-30 21:31:42 +03:00
|
|
|
|
2019-05-01 16:18:31 +03:00
|
|
|
_touchBarAudioPlayer = [[NSTouchBar alloc] init];
|
|
|
|
_touchBarAudioPlayer.delegate = self;
|
|
|
|
_touchBarAudioPlayer.customizationIdentifier = customID;
|
|
|
|
_touchBarAudioPlayer.defaultItemIdentifiers = @[play, previousItem, nextItem, seekBar, closePlayer];
|
|
|
|
_touchBarAudioPlayer.customizationAllowedItemIdentifiers = @[play, previousItem,
|
|
|
|
nextItem, currentPosition, seekBar, closePlayer];
|
2019-04-29 16:41:51 +03:00
|
|
|
}
|
|
|
|
|
2019-05-02 13:25:05 +03:00
|
|
|
- (nullable NSTouchBarItem *) touchBar:(NSTouchBar *)touchBar
|
|
|
|
makeItemForIdentifier:(NSTouchBarItemIdentifier)identifier {
|
2019-05-01 16:18:31 +03:00
|
|
|
|
2019-04-29 19:55:11 +03:00
|
|
|
if ([self.touchbarItems[identifier][@"type"] isEqualToString:@"slider"]) {
|
|
|
|
NSSliderTouchBarItem *item = [[NSSliderTouchBarItem alloc] initWithIdentifier:identifier];
|
|
|
|
item.slider.minValue = 0.0f;
|
|
|
|
item.slider.maxValue = 1.0f;
|
|
|
|
item.target = self;
|
|
|
|
item.action = @selector(seekbarChanged:);
|
|
|
|
item.customizationLabel = self.touchbarItems[identifier][@"name"];
|
|
|
|
[self.touchbarItems[identifier] setObject:item.slider forKey:@"view"];
|
|
|
|
return item;
|
|
|
|
} else if ([self.touchbarItems[identifier][@"type"] isEqualToString:@"button"]) {
|
|
|
|
NSCustomTouchBarItem *item = [[NSCustomTouchBarItem alloc] initWithIdentifier:identifier];
|
|
|
|
NSImage *image = self.touchbarItems[identifier][@"image"];
|
|
|
|
NSButton *button = [NSButton buttonWithImage:image target:self action:@selector(buttonAction:)];
|
|
|
|
item.view = button;
|
|
|
|
item.customizationLabel = self.touchbarItems[identifier][@"name"];
|
|
|
|
[self.touchbarItems[identifier] setObject:button forKey:@"view"];
|
|
|
|
return item;
|
|
|
|
} else if ([self.touchbarItems[identifier][@"type"] isEqualToString:@"text"]) {
|
|
|
|
NSCustomTouchBarItem *item = [[NSCustomTouchBarItem alloc] initWithIdentifier:identifier];
|
2019-05-02 13:25:05 +03:00
|
|
|
NSTextField *text = [NSTextField labelWithString:@"00:00 / 00:00"];
|
2019-04-29 19:55:11 +03:00
|
|
|
text.alignment = NSTextAlignmentCenter;
|
|
|
|
item.view = text;
|
|
|
|
item.customizationLabel = self.touchbarItems[identifier][@"name"];
|
|
|
|
[self.touchbarItems[identifier] setObject:text forKey:@"view"];
|
|
|
|
return item;
|
2019-05-01 16:18:31 +03:00
|
|
|
} else if ([self.touchbarItems[identifier][@"type"] isEqualToString:@"pinned"]) {
|
2019-05-01 20:50:59 +03:00
|
|
|
NSCustomTouchBarItem *item = [[NSCustomTouchBarItem alloc] initWithIdentifier:identifier];
|
2019-05-07 20:08:00 +03:00
|
|
|
self.mainPinnedButtons = [[NSMutableArray alloc] init];
|
|
|
|
NSStackView *stackView = [[NSStackView alloc] init];
|
2019-05-01 16:18:31 +03:00
|
|
|
|
2019-05-04 13:22:21 +03:00
|
|
|
for (auto i = kSavedMessagesId; i <= Global::PinnedDialogsCountMax(); i++) {
|
2019-05-07 20:08:00 +03:00
|
|
|
PinnedDialogButton *button = [[PinnedDialogButton alloc] init:i];
|
|
|
|
[self.mainPinnedButtons addObject:button];
|
|
|
|
[stackView addView:button.view inGravity:NSStackViewGravityCenter];
|
2019-05-01 20:50:59 +03:00
|
|
|
}
|
2019-05-07 20:08:00 +03:00
|
|
|
|
2019-05-01 20:50:59 +03:00
|
|
|
[stackView setSpacing:-15];
|
|
|
|
item.view = stackView;
|
2019-05-01 16:18:31 +03:00
|
|
|
[self.touchbarItems[identifier] setObject:item.view forKey:@"view"];
|
|
|
|
return item;
|
2019-04-29 19:55:11 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil;
|
|
|
|
}
|
2019-04-29 16:41:51 +03:00
|
|
|
|
2019-05-02 13:25:05 +03:00
|
|
|
- (void) setTouchBar:(TouchBarType)type {
|
2019-04-29 19:55:11 +03:00
|
|
|
if (self.touchBarType == type) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (type == TouchBarType::Main) {
|
2019-04-30 21:31:42 +03:00
|
|
|
[self.view setTouchBar:_touchBarMain];
|
2019-04-29 19:55:11 +03:00
|
|
|
} else if (type == TouchBarType::AudioPlayer) {
|
2019-05-04 12:43:22 +03:00
|
|
|
if (!isCurrentSongExists()
|
|
|
|
|| Media::Player::instance()->getActiveType() != kSongType) {
|
|
|
|
return;
|
|
|
|
}
|
2019-04-30 21:31:42 +03:00
|
|
|
[self.view setTouchBar:_touchBarAudioPlayer];
|
2019-05-04 12:43:22 +03:00
|
|
|
} else if (type == TouchBarType::AudioPlayerForce) {
|
|
|
|
[self.view setTouchBar:_touchBarAudioPlayer];
|
|
|
|
self.touchBarType = TouchBarType::AudioPlayer;
|
|
|
|
return;
|
2019-05-02 14:02:54 +03:00
|
|
|
} else if (type == TouchBarType::None) {
|
|
|
|
[self.view setTouchBar:nil];
|
2019-04-29 19:55:11 +03:00
|
|
|
}
|
2019-05-04 12:43:22 +03:00
|
|
|
self.touchBarType = type;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline bool isCurrentSongExists() {
|
|
|
|
return Media::Player::instance()->current(kSongType).audio() != nullptr;
|
2019-04-29 16:41:51 +03:00
|
|
|
}
|
|
|
|
|
2019-05-02 13:25:05 +03:00
|
|
|
- (void) handlePropertyChange:(Media::Player::TrackState)property {
|
2019-05-04 12:43:22 +03:00
|
|
|
if (property.id.type() == kSongType) {
|
|
|
|
[self setTouchBar:TouchBarType::AudioPlayerForce];
|
|
|
|
} else {
|
|
|
|
return;
|
|
|
|
}
|
2019-04-29 19:55:11 +03:00
|
|
|
self.position = property.position < 0 ? 0 : property.position;
|
|
|
|
self.duration = property.length;
|
2019-05-02 13:00:05 +03:00
|
|
|
if (Media::Player::IsStoppedOrStopping(property.state)) {
|
|
|
|
self.position = 0;
|
|
|
|
self.duration = 0;
|
|
|
|
}
|
2019-04-29 19:55:11 +03:00
|
|
|
[self updateTouchBarTimeItems];
|
|
|
|
NSButton *playButton = self.touchbarItems[play][@"view"];
|
|
|
|
if (property.state == Media::Player::State::Playing) {
|
|
|
|
playButton.image = self.touchbarItems[play][@"image"];
|
|
|
|
} else {
|
|
|
|
playButton.image = self.touchbarItems[play][@"imageAlt"];
|
|
|
|
}
|
|
|
|
|
|
|
|
[self.touchbarItems[nextItem][@"view"]
|
|
|
|
setEnabled:Media::Player::instance()->nextAvailable(kSongType)];
|
|
|
|
[self.touchbarItems[previousItem][@"view"]
|
|
|
|
setEnabled:Media::Player::instance()->previousAvailable(kSongType)];
|
2019-04-29 16:41:51 +03:00
|
|
|
}
|
|
|
|
|
2019-05-02 13:25:05 +03:00
|
|
|
- (NSString *) formatTime:(int)time {
|
2019-04-29 19:55:11 +03:00
|
|
|
const int seconds = time % 60;
|
|
|
|
const int minutes = (time / 60) % 60;
|
|
|
|
const int hours = time / (60 * 60);
|
2019-04-29 16:41:51 +03:00
|
|
|
|
2019-04-29 19:55:11 +03:00
|
|
|
NSString *stime = hours > 0 ? [NSString stringWithFormat:@"%d:", hours] : @"";
|
|
|
|
stime = (stime.length > 0 || minutes > 9) ?
|
|
|
|
[NSString stringWithFormat:@"%@%02d:", stime, minutes] :
|
2019-05-02 13:00:05 +03:00
|
|
|
[NSString stringWithFormat:@"%02d:", minutes];
|
2019-04-29 19:55:11 +03:00
|
|
|
stime = [NSString stringWithFormat:@"%@%02d", stime, seconds];
|
2019-04-29 16:41:51 +03:00
|
|
|
|
2019-04-29 19:55:11 +03:00
|
|
|
return stime;
|
2019-04-29 16:41:51 +03:00
|
|
|
}
|
|
|
|
|
2019-05-02 13:25:05 +03:00
|
|
|
- (void) removeConstraintForIdentifier:(NSTouchBarItemIdentifier)identifier {
|
2019-04-29 19:55:11 +03:00
|
|
|
NSTextField *field = self.touchbarItems[identifier][@"view"];
|
|
|
|
[field removeConstraint:self.touchbarItems[identifier][@"constrain"]];
|
2019-04-29 16:41:51 +03:00
|
|
|
}
|
|
|
|
|
2019-05-02 13:25:05 +03:00
|
|
|
- (void) applyConstraintFromString:(NSString *)string
|
|
|
|
forIdentifier:(NSTouchBarItemIdentifier)identifier {
|
2019-04-29 19:55:11 +03:00
|
|
|
NSTextField *field = self.touchbarItems[identifier][@"view"];
|
|
|
|
if (field) {
|
|
|
|
NSString *fString = [[string componentsSeparatedByCharactersInSet:
|
|
|
|
[NSCharacterSet decimalDigitCharacterSet]] componentsJoinedByString:@"0"];
|
|
|
|
NSTextField *textField = [NSTextField labelWithString:fString];
|
|
|
|
NSSize size = [textField frame].size;
|
2019-04-29 16:41:51 +03:00
|
|
|
|
2019-04-29 19:55:11 +03:00
|
|
|
NSLayoutConstraint *con =
|
|
|
|
[NSLayoutConstraint constraintWithItem:field
|
|
|
|
attribute:NSLayoutAttributeWidth
|
|
|
|
relatedBy:NSLayoutRelationEqual
|
|
|
|
toItem:nil
|
|
|
|
attribute:NSLayoutAttributeNotAnAttribute
|
|
|
|
multiplier:1.0
|
2019-05-02 13:00:05 +03:00
|
|
|
constant:(int)ceil(size.width) * 1.2];
|
2019-04-29 19:55:11 +03:00
|
|
|
[field addConstraint:con];
|
|
|
|
[self.touchbarItems[identifier] setObject:con forKey:@"constrain"];
|
|
|
|
}
|
2019-04-29 16:41:51 +03:00
|
|
|
}
|
|
|
|
|
2019-05-02 13:25:05 +03:00
|
|
|
- (void) updateTouchBarTimeItems {
|
2019-04-29 19:55:11 +03:00
|
|
|
NSSlider *seekSlider = self.touchbarItems[seekBar][@"view"];
|
|
|
|
NSTextField *curPosItem = self.touchbarItems[currentPosition][@"view"];
|
2019-04-29 16:41:51 +03:00
|
|
|
|
2019-04-29 19:55:11 +03:00
|
|
|
if (self.duration <= 0) {
|
|
|
|
seekSlider.enabled = NO;
|
|
|
|
seekSlider.doubleValue = 0;
|
|
|
|
} else {
|
|
|
|
seekSlider.enabled = YES;
|
|
|
|
if (!seekSlider.highlighted) {
|
|
|
|
seekSlider.doubleValue = (self.position / self.duration) * seekSlider.maxValue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
const auto timeToString = [&](int t) {
|
|
|
|
return [self formatTime:(int)floor(t / kMs)];
|
|
|
|
};
|
|
|
|
curPosItem.stringValue = [NSString stringWithFormat:@"%@ / %@",
|
|
|
|
timeToString(self.position),
|
|
|
|
timeToString(self.duration)];
|
2019-04-29 16:41:51 +03:00
|
|
|
|
2019-05-02 13:00:05 +03:00
|
|
|
[self removeConstraintForIdentifier:currentPosition];
|
|
|
|
[self applyConstraintFromString:curPosItem.stringValue forIdentifier:currentPosition];
|
2019-04-29 16:41:51 +03:00
|
|
|
}
|
|
|
|
|
2019-05-02 13:25:05 +03:00
|
|
|
- (NSString *) getIdentifierFromView:(id)view {
|
2019-04-29 19:55:11 +03:00
|
|
|
NSString *identifier;
|
|
|
|
for (identifier in self.touchbarItems)
|
|
|
|
if([self.touchbarItems[identifier][@"view"] isEqual:view])
|
|
|
|
break;
|
|
|
|
return identifier;
|
2019-04-29 16:41:51 +03:00
|
|
|
}
|
|
|
|
|
2019-05-02 13:25:05 +03:00
|
|
|
- (void) buttonAction:(NSButton *)sender {
|
2019-04-29 19:55:11 +03:00
|
|
|
NSString *identifier = [self getIdentifierFromView:sender];
|
|
|
|
const auto command = [self.touchbarItems[identifier][@"cmd"] intValue];
|
|
|
|
|
|
|
|
Core::Sandbox::Instance().customEnterFromEventLoop([=] {
|
2019-05-02 13:25:05 +03:00
|
|
|
if (command == kPlayPause) {
|
2019-05-04 12:43:22 +03:00
|
|
|
Media::Player::instance()->playPause(kSongType);
|
2019-04-29 19:55:11 +03:00
|
|
|
} else if (command == kPlaylistPrevious) {
|
2019-05-04 12:43:22 +03:00
|
|
|
Media::Player::instance()->previous(kSongType);
|
2019-04-29 19:55:11 +03:00
|
|
|
} else if (command == kPlaylistNext) {
|
2019-05-04 12:43:22 +03:00
|
|
|
Media::Player::instance()->next(kSongType);
|
2019-04-30 21:31:42 +03:00
|
|
|
} else if (command == kClosePlayer) {
|
|
|
|
App::main()->closeBothPlayers();
|
|
|
|
}
|
2019-04-29 19:55:11 +03:00
|
|
|
});
|
2019-04-29 16:41:51 +03:00
|
|
|
}
|
|
|
|
|
2019-05-02 13:25:05 +03:00
|
|
|
- (void) seekbarChanged:(NSSliderTouchBarItem *)sender {
|
2019-05-04 10:01:10 +03:00
|
|
|
// https://stackoverflow.com/a/45891017
|
|
|
|
NSEvent *event = [[NSApplication sharedApplication] currentEvent];
|
|
|
|
bool touchUp = [event touchesMatchingPhase:NSTouchPhaseEnded inView:nil].count > 0;
|
|
|
|
Core::Sandbox::Instance().customEnterFromEventLoop([=] {
|
|
|
|
if (touchUp) {
|
|
|
|
Media::Player::instance()->finishSeeking(kSongType, sender.slider.doubleValue);
|
|
|
|
} else {
|
|
|
|
Media::Player::instance()->startSeeking(kSongType);
|
|
|
|
}
|
2019-04-29 19:55:11 +03:00
|
|
|
});
|
2019-04-29 16:41:51 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
@end
|