Refactored code in touchbar.

- Refactored variables.
 - Moved TouchBarType to Platform namespace.
This commit is contained in:
23rd 2019-05-27 15:39:19 +03:00 committed by John Preston
parent de3cc76468
commit bafa838afc
3 changed files with 181 additions and 163 deletions

View File

@ -113,7 +113,7 @@ public:
bool clipboardHasText(); bool clipboardHasText();
TouchBar *_touchBar; TouchBar *_touchBar = nullptr;
~Private(); ~Private();
@ -408,7 +408,7 @@ MainWindow::MainWindow()
} }
} else { } else {
if (_private->_touchBar) { if (_private->_touchBar) {
[_private->_touchBar setTouchBar:TouchBarType::None]; [_private->_touchBar setTouchBar:Platform::TouchBarType::None];
} }
_private->_touchBar = nullptr; _private->_touchBar = nullptr;
} }

View File

@ -11,7 +11,7 @@
#include "media/player/media_player_instance.h" #include "media/player/media_player_instance.h"
#import <Cocoa/Cocoa.h> #import <Cocoa/Cocoa.h>
namespace { namespace Platform {
enum class TouchBarType { enum class TouchBarType {
None, None,
Main, Main,
@ -21,22 +21,26 @@ enum class TouchBarType {
} // namespace } // namespace
@interface TouchBar : NSTouchBar { @interface TouchBar : NSTouchBar {
rpl::lifetime lifetime; @private
NSView *_parentView;
NSMutableArray *_mainPinnedButtons;
NSTouchBar *_touchBarMain;
NSTouchBar *_touchBarAudioPlayer;
Platform::TouchBarType _touchBarType;
Platform::TouchBarType _touchBarTypeBeforeLock;
double _duration;
double _position;
rpl::lifetime _lifetime;
} }
@property TouchBarType touchBarType;
@property TouchBarType touchBarTypeBeforeLock;
@property(retain) NSDictionary * _Nullable touchbarItems; @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;
@property(retain) NSMutableArray * _Nullable mainPinnedButtons;
- (id _Nonnull) init:(NSView * _Nonnull)view; - (id _Nonnull) init:(NSView * _Nonnull)view;
- (void) handleTrackStateChange:(Media::Player::TrackState)property; - (void) handleTrackStateChange:(Media::Player::TrackState)state;
- (void) setTouchBar:(TouchBarType)type; - (void) setTouchBar:(Platform::TouchBarType)type;
@end @end

View File

@ -67,7 +67,7 @@ inline bool IsCurrentSongExists() {
return Media::Player::instance()->current(kSongType).audio() != nullptr; return Media::Player::instance()->current(kSongType).audio() != nullptr;
} }
NSString* FormatTime(int time) { NSString *FormatTime(int time) {
const auto seconds = time % 60; const auto seconds = time % 60;
const auto minutes = (time / 60) % 60; const auto minutes = (time / 60) % 60;
const auto hours = time / (60 * 60); const auto hours = time / (60 * 60);
@ -88,13 +88,13 @@ NSString* FormatTime(int time) {
} // namespace } // namespace
@interface PinnedDialogButton : NSCustomTouchBarItem { @interface PinnedDialogButton : NSCustomTouchBarItem {
rpl::lifetime lifetime; rpl::lifetime _lifetime;
rpl::lifetime userpicChangedLifetime; rpl::lifetime _userpicChangedLifetime;
bool isWaitingUserpicLoad;
} }
@property(nonatomic, assign) int number; @property(nonatomic, assign) int number;
@property(nonatomic, assign) bool waiting; @property(nonatomic, assign) PeerData *peer;
@property(nonatomic, assign) PeerData * peer;
@property(nonatomic, assign) bool isDeletedFromView; @property(nonatomic, assign) bool isDeletedFromView;
- (id) init:(int)num; - (id) init:(int)num;
@ -109,16 +109,16 @@ NSString* FormatTime(int time) {
- (id) init:(int)num { - (id) init:(int)num {
if (num == kSavedMessagesId) { if (num == kSavedMessagesId) {
self = [super initWithIdentifier:kSavedMessagesItemIdentifier]; self = [super initWithIdentifier:kSavedMessagesItemIdentifier];
self.waiting = false; isWaitingUserpicLoad = false;
self.customizationLabel = [NSString stringWithFormat:@"Pinned Dialog %d", num]; self.customizationLabel = [NSString stringWithFormat:@"Pinned Dialog %d", num];
} else if (num == kArchiveId) { } else if (num == kArchiveId) {
self = [super initWithIdentifier:kArchiveFolderItemIdentifier]; self = [super initWithIdentifier:kArchiveFolderItemIdentifier];
self.waiting = false; isWaitingUserpicLoad = false;
self.customizationLabel = @"Archive Folder"; self.customizationLabel = @"Archive Folder";
} else { } else {
NSString *identifier = [NSString stringWithFormat:@"%@.pinnedDialog%d", kCustomizationIdMain, num]; NSString *identifier = [NSString stringWithFormat:@"%@.pinnedDialog%d", kCustomizationIdMain, num];
self = [super initWithIdentifier:identifier]; self = [super initWithIdentifier:identifier];
self.waiting = true; isWaitingUserpicLoad = true;
self.customizationLabel = @"Saved Messages"; self.customizationLabel = @"Saved Messages";
} }
if (!self) { if (!self) {
@ -138,10 +138,10 @@ NSString* FormatTime(int time) {
base::ObservableViewer( base::ObservableViewer(
Auth().downloaderTaskFinished() Auth().downloaderTaskFinished()
) | rpl::start_with_next([=] { ) | rpl::start_with_next([=] {
if (self.waiting) { if (isWaitingUserpicLoad) {
[self updatePinnedDialog]; [self updatePinnedDialog];
} }
}, self->lifetime); }, _lifetime);
return self; return self;
} }
@ -152,7 +152,7 @@ NSString* FormatTime(int time) {
return; return;
} }
_peer = newPeer; _peer = newPeer;
self->userpicChangedLifetime.destroy(); _userpicChangedLifetime.destroy();
if (!_peer) { if (!_peer) {
return; return;
} }
@ -160,9 +160,9 @@ NSString* FormatTime(int time) {
_peer, _peer,
Notify::PeerUpdate::Flag::PhotoChanged Notify::PeerUpdate::Flag::PhotoChanged
) | rpl::start_with_next([=] { ) | rpl::start_with_next([=] {
self.waiting = true; isWaitingUserpicLoad = true;
[self updatePinnedDialog]; [self updatePinnedDialog];
}, self->userpicChangedLifetime); }, _userpicChangedLifetime);
} }
- (void) updatePinnedDialog { - (void) updatePinnedDialog {
@ -214,7 +214,7 @@ NSString* FormatTime(int time) {
// Random picture. // Random picture.
return [NSImage imageNamed:NSImageNameTouchBarAddTemplate]; return [NSImage imageNamed:NSImageNameTouchBarAddTemplate];
} }
self.waiting = !self.peer->userpicLoaded(); isWaitingUserpicLoad = !self.peer->userpicLoaded();
auto pixmap = self.peer->genUserpic(kIdealIconSize); auto pixmap = self.peer->genUserpic(kIdealIconSize);
pixmap.setDevicePixelRatio(cRetinaFactor()); pixmap.setDevicePixelRatio(cRetinaFactor());
return [qt_mac_create_nsimage(pixmap) autorelease]; return [qt_mac_create_nsimage(pixmap) autorelease];
@ -230,86 +230,93 @@ NSString* FormatTime(int time) {
- (id) init:(NSView *)view { - (id) init:(NSView *)view {
self = [super init]; self = [super init];
if (self) { if (!self) {
const auto iconSize = kIdealIconSize / 3; return nil;
self.view = view;
self.touchbarItems = @{
kPinnedPanelItemIdentifier: [NSMutableDictionary dictionaryWithDictionary:@{
@"type": @"pinned",
}],
kSeekBarItemIdentifier: [NSMutableDictionary dictionaryWithDictionary:@{
@"type": @"slider",
@"name": @"Seek Bar"
}],
kPlayItemIdentifier: [NSMutableDictionary dictionaryWithDictionary:@{
@"type": @"button",
@"name": @"Play Button",
@"cmd": [NSNumber numberWithInt:kCommandPlayPause],
@"image": CreateNSImageFromStyleIcon(st::touchBarIconPlayerPause, iconSize),
@"imageAlt": CreateNSImageFromStyleIcon(st::touchBarIconPlayerPlay, iconSize),
}],
kPreviousItemIdentifier: [NSMutableDictionary dictionaryWithDictionary:@{
@"type": @"button",
@"name": @"Previous Playlist Item",
@"cmd": [NSNumber numberWithInt:kCommandPlaylistPrevious],
@"image": CreateNSImageFromStyleIcon(st::touchBarIconPlayerPrevious, iconSize),
}],
kNextItemIdentifier: [NSMutableDictionary dictionaryWithDictionary:@{
@"type": @"button",
@"name": @"Next Playlist Item",
@"cmd": [NSNumber numberWithInt:kCommandPlaylistNext],
@"image": CreateNSImageFromStyleIcon(st::touchBarIconPlayerNext, iconSize),
}],
kCommandClosePlayerItemIdentifier: [NSMutableDictionary dictionaryWithDictionary:@{
@"type": @"button",
@"name": @"Close Player",
@"cmd": [NSNumber numberWithInt:kCommandClosePlayer],
@"image": CreateNSImageFromStyleIcon(st::touchBarIconPlayerClose, iconSize),
}],
kCurrentPositionItemIdentifier: [NSMutableDictionary dictionaryWithDictionary:@{
@"type": @"text",
@"name": @"Current Position"
}]
};
} }
const auto iconSize = kIdealIconSize / 3;
_position = 0;
_duration = 0;
_parentView = view;
self.touchBarItems = @{
kPinnedPanelItemIdentifier: [NSMutableDictionary dictionaryWithDictionary:@{
@"type": @"pinned",
}],
kSeekBarItemIdentifier: [NSMutableDictionary dictionaryWithDictionary:@{
@"type": @"slider",
@"name": @"Seek Bar"
}],
kPlayItemIdentifier: [NSMutableDictionary dictionaryWithDictionary:@{
@"type": @"button",
@"name": @"Play Button",
@"cmd": [NSNumber numberWithInt:kCommandPlayPause],
@"image": CreateNSImageFromStyleIcon(st::touchBarIconPlayerPause, iconSize),
@"imageAlt": CreateNSImageFromStyleIcon(st::touchBarIconPlayerPlay, iconSize),
}],
kPreviousItemIdentifier: [NSMutableDictionary dictionaryWithDictionary:@{
@"type": @"button",
@"name": @"Previous Playlist Item",
@"cmd": [NSNumber numberWithInt:kCommandPlaylistPrevious],
@"image": CreateNSImageFromStyleIcon(st::touchBarIconPlayerPrevious, iconSize),
}],
kNextItemIdentifier: [NSMutableDictionary dictionaryWithDictionary:@{
@"type": @"button",
@"name": @"Next Playlist Item",
@"cmd": [NSNumber numberWithInt:kCommandPlaylistNext],
@"image": CreateNSImageFromStyleIcon(st::touchBarIconPlayerNext, iconSize),
}],
kCommandClosePlayerItemIdentifier: [NSMutableDictionary dictionaryWithDictionary:@{
@"type": @"button",
@"name": @"Close Player",
@"cmd": [NSNumber numberWithInt:kCommandClosePlayer],
@"image": CreateNSImageFromStyleIcon(st::touchBarIconPlayerClose, iconSize),
}],
kCurrentPositionItemIdentifier: [NSMutableDictionary dictionaryWithDictionary:@{
@"type": @"text",
@"name": @"Current Position"
}]
};
[self createTouchBar]; [self createTouchBar];
[self setTouchBar:TouchBarType::Main]; [self setTouchBar:Platform::TouchBarType::Main];
Media::Player::instance()->playerWidgetToggled( Media::Player::instance()->playerWidgetToggled(
) | rpl::start_with_next([=](bool toggled) { ) | rpl::start_with_next([=](bool toggled) {
if (!toggled) { if (!toggled) {
[self setTouchBar:TouchBarType::Main]; [self setTouchBar:Platform::TouchBarType::Main];
} else { } else {
[self setTouchBar:TouchBarType::AudioPlayer]; [self setTouchBar:Platform::TouchBarType::AudioPlayer];
} }
}, self->lifetime); }, _lifetime);
Media::Player::instance()->updatedNotifier( Media::Player::instance()->updatedNotifier(
) | rpl::start_with_next([=](const Media::Player::TrackState &state) { ) | rpl::start_with_next([=](const Media::Player::TrackState &state) {
[self handleTrackStateChange:state]; [self handleTrackStateChange:state];
}, self->lifetime); }, _lifetime);
Core::App().passcodeLockChanges( Core::App().passcodeLockChanges(
) | rpl::start_with_next([=](bool locked) { ) | rpl::start_with_next([=](bool locked) {
if (locked) { if (locked) {
self.touchBarTypeBeforeLock = self.touchBarType; _touchBarTypeBeforeLock = _touchBarType;
[self setTouchBar:TouchBarType::None]; [self setTouchBar:Platform::TouchBarType::None];
} else { } else {
[self setTouchBar:self.touchBarTypeBeforeLock]; [self setTouchBar:_touchBarTypeBeforeLock];
} }
}, self->lifetime); }, _lifetime);
Auth().data().pinnedDialogsOrderUpdated( Auth().data().pinnedDialogsOrderUpdated(
) | rpl::start_with_next([self] { ) | rpl::start_with_next([self] {
[self updatePinnedButtons]; [self updatePinnedButtons];
}, self->lifetime); }, _lifetime);
Auth().data().chatsListChanges( Auth().data().chatsListChanges(
) | rpl::filter([](Data::Folder *folder) { ) | rpl::filter([](Data::Folder *folder) {
return folder && folder->chatsList(); return folder
&& folder->chatsList()
&& folder->id() == Data::Folder::kId;
}) | rpl::start_with_next([=](Data::Folder *folder) { }) | rpl::start_with_next([=](Data::Folder *folder) {
[self toggleArchiveButton:folder->chatsList()->empty()]; [self toggleArchiveButton:folder->chatsList()->empty()];
}, self->lifetime); }, _lifetime);
[self updatePinnedButtons]; [self updatePinnedButtons];
@ -318,40 +325,42 @@ NSString* FormatTime(int time) {
- (nullable NSTouchBarItem *) touchBar:(NSTouchBar *)touchBar - (nullable NSTouchBarItem *) touchBar:(NSTouchBar *)touchBar
makeItemForIdentifier:(NSTouchBarItemIdentifier)identifier { makeItemForIdentifier:(NSTouchBarItemIdentifier)identifier {
if ([self.touchbarItems[identifier][@"type"] isEqualToString:@"slider"]) { const id dictionaryItem = self.touchBarItems[identifier];
const id type = dictionaryItem[@"type"];
if ([type isEqualToString:@"slider"]) {
NSSliderTouchBarItem *item = [[NSSliderTouchBarItem alloc] initWithIdentifier:identifier]; NSSliderTouchBarItem *item = [[NSSliderTouchBarItem alloc] initWithIdentifier:identifier];
item.slider.minValue = 0.0f; item.slider.minValue = 0.0f;
item.slider.maxValue = 1.0f; item.slider.maxValue = 1.0f;
item.target = self; item.target = self;
item.action = @selector(seekbarChanged:); item.action = @selector(seekbarChanged:);
item.customizationLabel = self.touchbarItems[identifier][@"name"]; item.customizationLabel = dictionaryItem[@"name"];
[self.touchbarItems[identifier] setObject:item.slider forKey:@"view"]; [dictionaryItem setObject:item.slider forKey:@"view"];
return item; return item;
} else if ([self.touchbarItems[identifier][@"type"] isEqualToString:@"button"]) { } else if ([type isEqualToString:@"button"]) {
NSCustomTouchBarItem *item = [[NSCustomTouchBarItem alloc] initWithIdentifier:identifier]; NSCustomTouchBarItem *item = [[NSCustomTouchBarItem alloc] initWithIdentifier:identifier];
NSImage *image = self.touchbarItems[identifier][@"image"]; NSImage *image = dictionaryItem[@"image"];
NSButton *button = [NSButton buttonWithImage:image target:self action:@selector(buttonAction:)]; NSButton *button = [NSButton buttonWithImage:image target:self action:@selector(buttonAction:)];
button.tag = [self.touchbarItems[identifier][@"cmd"] intValue]; button.tag = [dictionaryItem[@"cmd"] intValue];
item.view = button; item.view = button;
item.customizationLabel = self.touchbarItems[identifier][@"name"]; item.customizationLabel = dictionaryItem[@"name"];
[self.touchbarItems[identifier] setObject:button forKey:@"view"]; [dictionaryItem setObject:button forKey:@"view"];
return item; return item;
} else if ([self.touchbarItems[identifier][@"type"] isEqualToString:@"text"]) { } else if ([type isEqualToString:@"text"]) {
NSCustomTouchBarItem *item = [[NSCustomTouchBarItem alloc] initWithIdentifier:identifier]; NSCustomTouchBarItem *item = [[NSCustomTouchBarItem alloc] initWithIdentifier:identifier];
NSTextField *text = [NSTextField labelWithString:@"00:00 / 00:00"]; NSTextField *text = [NSTextField labelWithString:@"00:00 / 00:00"];
text.alignment = NSTextAlignmentCenter; text.alignment = NSTextAlignmentCenter;
item.view = text; item.view = text;
item.customizationLabel = self.touchbarItems[identifier][@"name"]; item.customizationLabel = dictionaryItem[@"name"];
[self.touchbarItems[identifier] setObject:text forKey:@"view"]; [dictionaryItem setObject:text forKey:@"view"];
return item; return item;
} else if ([self.touchbarItems[identifier][@"type"] isEqualToString:@"pinned"]) { } else if ([type isEqualToString:@"pinned"]) {
NSCustomTouchBarItem *item = [[NSCustomTouchBarItem alloc] initWithIdentifier:identifier]; NSCustomTouchBarItem *item = [[NSCustomTouchBarItem alloc] initWithIdentifier:identifier];
self.mainPinnedButtons = [[NSMutableArray alloc] init]; _mainPinnedButtons = [[NSMutableArray alloc] init];
NSStackView *stackView = [[NSStackView alloc] init]; NSStackView *stackView = [[NSStackView alloc] init];
for (auto i = kArchiveId; i <= Global::PinnedDialogsCountMax(); i++) { for (auto i = kArchiveId; i <= Global::PinnedDialogsCountMax(); i++) {
PinnedDialogButton *button = [[PinnedDialogButton alloc] init:i]; PinnedDialogButton *button = [[PinnedDialogButton alloc] init:i];
[self.mainPinnedButtons addObject:button]; [_mainPinnedButtons addObject:button];
if (i == kArchiveId) { if (i == kArchiveId) {
button.isDeletedFromView = true; button.isDeletedFromView = true;
continue; continue;
@ -361,7 +370,7 @@ NSString* FormatTime(int time) {
[stackView setSpacing:-15]; [stackView setSpacing:-15];
item.view = stackView; item.view = stackView;
[self.touchbarItems[identifier] setObject:item.view forKey:@"view"]; [dictionaryItem setObject:item.view forKey:@"view"];
return item; return item;
} }
@ -391,34 +400,34 @@ NSString* FormatTime(int time) {
kCommandClosePlayerItemIdentifier]; kCommandClosePlayerItemIdentifier];
} }
- (void) setTouchBar:(TouchBarType)type { - (void) setTouchBar:(Platform::TouchBarType)type {
if (self.touchBarType == type) { if (_touchBarType == type) {
return; return;
} }
if (type == TouchBarType::Main) { if (type == Platform::TouchBarType::Main) {
[self.view setTouchBar:_touchBarMain]; [_parentView setTouchBar:_touchBarMain];
} else if (type == TouchBarType::AudioPlayer) { } else if (type == Platform::TouchBarType::AudioPlayer) {
if (!IsCurrentSongExists() if (!IsCurrentSongExists()
|| Media::Player::instance()->getActiveType() != kSongType) { || Media::Player::instance()->getActiveType() != kSongType) {
return; return;
} }
[self.view setTouchBar:_touchBarAudioPlayer]; [_parentView setTouchBar:_touchBarAudioPlayer];
} else if (type == TouchBarType::AudioPlayerForce) { } else if (type == Platform::TouchBarType::AudioPlayerForce) {
[self.view setTouchBar:_touchBarAudioPlayer]; [_parentView setTouchBar:_touchBarAudioPlayer];
self.touchBarType = TouchBarType::AudioPlayer; _touchBarType = Platform::TouchBarType::AudioPlayer;
return; return;
} else if (type == TouchBarType::None) { } else if (type == Platform::TouchBarType::None) {
[self.view setTouchBar:nil]; [_parentView setTouchBar:nil];
} }
self.touchBarType = type; _touchBarType = type;
} }
// Main Touchbar. // Main Touchbar.
- (void) toggleArchiveButton:(bool)hide { - (void) toggleArchiveButton:(bool)hide {
for (PinnedDialogButton *button in self.mainPinnedButtons) { for (PinnedDialogButton *button in _mainPinnedButtons) {
if (button.number == kArchiveId) { if (button.number == kArchiveId) {
NSCustomTouchBarItem *item = [self.touchBarMain itemForIdentifier:kPinnedPanelItemIdentifier]; NSCustomTouchBarItem *item = [_touchBarMain itemForIdentifier:kPinnedPanelItemIdentifier];
NSStackView *stack = item.view; NSStackView *stack = item.view;
[button updatePinnedDialog]; [button updatePinnedDialog];
if (hide && !button.isDeletedFromView) { if (hide && !button.isDeletedFromView) {
@ -440,10 +449,10 @@ NSString* FormatTime(int time) {
auto isSelfPeerPinned = false; auto isSelfPeerPinned = false;
auto isArchivePinned = false; auto isArchivePinned = false;
PinnedDialogButton *selfChatButton; PinnedDialogButton *selfChatButton;
NSCustomTouchBarItem *item = [self.touchBarMain itemForIdentifier:kPinnedPanelItemIdentifier]; NSCustomTouchBarItem *item = [_touchBarMain itemForIdentifier:kPinnedPanelItemIdentifier];
NSStackView *stack = item.view; NSStackView *stack = item.view;
for (PinnedDialogButton *button in self.mainPinnedButtons) { for (PinnedDialogButton *button in _mainPinnedButtons) {
const auto num = button.number; const auto num = button.number;
if (num <= kSavedMessagesId) { if (num <= kSavedMessagesId) {
if (num == kSavedMessagesId) { if (num == kSavedMessagesId) {
@ -483,64 +492,64 @@ NSString* FormatTime(int time) {
// Audio Player Touchbar. // Audio Player Touchbar.
- (void) handleTrackStateChange:(Media::Player::TrackState)property { - (void) handleTrackStateChange:(Media::Player::TrackState)state {
if (property.id.type() == kSongType) { if (state.id.type() == kSongType) {
[self setTouchBar:TouchBarType::AudioPlayerForce]; [self setTouchBar:Platform::TouchBarType::AudioPlayerForce];
} else { } else {
return; return;
} }
self.position = property.position < 0 ? 0 : property.position; _position = state.position < 0 ? 0 : state.position;
self.duration = property.length; _duration = state.length;
if (Media::Player::IsStoppedOrStopping(property.state)) { if (Media::Player::IsStoppedOrStopping(state.state)) {
self.position = 0; _position = 0;
self.duration = 0; _duration = 0;
} }
[self updateTouchBarTimeItem]; [self updateTouchBarTimeItem];
NSButton *playButton = self.touchbarItems[kPlayItemIdentifier][@"view"]; NSButton *playButton = self.touchBarItems[kPlayItemIdentifier][@"view"];
const auto imgButton = (property.state == Media::Player::State::Playing) const auto imgButton = (state.state == Media::Player::State::Playing)
? @"image" ? @"image"
: @"imageAlt"; : @"imageAlt";
playButton.image = self.touchbarItems[kPlayItemIdentifier][imgButton]; playButton.image = self.touchBarItems[kPlayItemIdentifier][imgButton];
[self.touchbarItems[kNextItemIdentifier][@"view"] [self.touchBarItems[kNextItemIdentifier][@"view"]
setEnabled:Media::Player::instance()->nextAvailable(kSongType)]; setEnabled:Media::Player::instance()->nextAvailable(kSongType)];
[self.touchbarItems[kPreviousItemIdentifier][@"view"] [self.touchBarItems[kPreviousItemIdentifier][@"view"]
setEnabled:Media::Player::instance()->previousAvailable(kSongType)]; setEnabled:Media::Player::instance()->previousAvailable(kSongType)];
} }
- (void) updateTouchBarTimeItem { - (void) updateTouchBarTimeItem {
NSSlider *seekSlider = self.touchbarItems[kSeekBarItemIdentifier][@"view"]; const id item = self.touchBarItems[kCurrentPositionItemIdentifier];
NSTextField *curPosItem = self.touchbarItems[kCurrentPositionItemIdentifier][@"view"]; NSSlider *seekSlider = self.touchBarItems[kSeekBarItemIdentifier][@"view"];
NSTextField *textField = item[@"view"];
if (self.duration <= 0) { if (_duration <= 0) {
seekSlider.enabled = NO; seekSlider.enabled = NO;
seekSlider.doubleValue = 0; seekSlider.doubleValue = 0;
} else { } else {
seekSlider.enabled = YES; seekSlider.enabled = YES;
if (!seekSlider.highlighted) { if (!seekSlider.highlighted) {
seekSlider.doubleValue = (self.position / self.duration) * seekSlider.maxValue; seekSlider.doubleValue = (_position / _duration) * seekSlider.maxValue;
} }
} }
const auto timeToString = [&](int t) { const auto timeToString = [&](int t) {
return FormatTime((int)floor(t / kMs)); return FormatTime((int)floor(t / kMs));
}; };
curPosItem.stringValue = [NSString stringWithFormat:@"%@ / %@", textField.stringValue = [NSString stringWithFormat:@"%@ / %@",
timeToString(self.position), timeToString(_position),
timeToString(self.duration)]; timeToString(_duration)];
NSTextField *field = self.touchbarItems[kCurrentPositionItemIdentifier][@"view"]; NSTextField *field = item[@"view"];
if (!field) { if (!field) {
return; return;
} }
[field removeConstraint:self.touchbarItems[kCurrentPositionItemIdentifier][@"constrain"]]; [field removeConstraint:item[@"constrain"]];
NSString *fString = [[curPosItem.stringValue componentsSeparatedByCharactersInSet: NSString *fString = [[textField.stringValue componentsSeparatedByCharactersInSet:
[NSCharacterSet decimalDigitCharacterSet]] componentsJoinedByString:@"0"]; [NSCharacterSet decimalDigitCharacterSet]] componentsJoinedByString:@"0"];
NSTextField *tempField = [NSTextField labelWithString:fString]; NSSize size = [[NSTextField labelWithString:fString] frame].size;
NSSize size = [tempField frame].size;
NSLayoutConstraint *con = NSLayoutConstraint *con =
[NSLayoutConstraint constraintWithItem:field [NSLayoutConstraint constraintWithItem:field
@ -551,21 +560,26 @@ NSString* FormatTime(int time) {
multiplier:1.0 multiplier:1.0
constant:(int)ceil(size.width) * 1.2]; constant:(int)ceil(size.width) * 1.2];
[field addConstraint:con]; [field addConstraint:con];
[self.touchbarItems[kCurrentPositionItemIdentifier] setObject:con forKey:@"constrain"]; [item setObject:con forKey:@"constrain"];
} }
- (void) buttonAction:(NSButton *)sender { - (void) buttonAction:(NSButton *)sender {
const auto command = sender.tag; const auto command = sender.tag;
Core::Sandbox::Instance().customEnterFromEventLoop([=] { Core::Sandbox::Instance().customEnterFromEventLoop([=] {
if (command == kCommandPlayPause) { switch (command) {
case kCommandPlayPause:
Media::Player::instance()->playPause(kSongType); Media::Player::instance()->playPause(kSongType);
} else if (command == kCommandPlaylistPrevious) { break;
case kCommandPlaylistPrevious:
Media::Player::instance()->previous(kSongType); Media::Player::instance()->previous(kSongType);
} else if (command == kCommandPlaylistNext) { break;
case kCommandPlaylistNext:
Media::Player::instance()->next(kSongType); Media::Player::instance()->next(kSongType);
} else if (command == kCommandClosePlayer) { break;
case kCommandClosePlayer:
App::main()->closeBothPlayers(); App::main()->closeBothPlayers();
break;
} }
}); });
} }