Refactored code.

This commit is contained in:
23rd 2019-05-02 13:25:05 +03:00 committed by John Preston
parent 9059b2e617
commit 92ff237c2d
2 changed files with 22 additions and 53 deletions

View File

@ -25,11 +25,7 @@ static NSTouchBarCustomizationIdentifier _Nullable customIDMain = @"telegram.tou
static NSTouchBarItemIdentifier _Nullable savedMessages = [NSString stringWithFormat:@"%@.savedMessages", customIDMain]; static NSTouchBarItemIdentifier _Nullable savedMessages = [NSString stringWithFormat:@"%@.savedMessages", customIDMain];
static NSTouchBarItemIdentifier _Nullable archiveFolder = [NSString stringWithFormat:@"%@.archiveFolder", customIDMain]; static NSTouchBarItemIdentifier _Nullable archiveFolder = [NSString stringWithFormat:@"%@.archiveFolder", customIDMain];
static NSTouchBarItemIdentifier _Nullable pinnedDialog1 = [NSString stringWithFormat:@"%@.pinnedDialog1", customIDMain]; static NSTouchBarItemIdentifier _Nullable pinnedPanel = [NSString stringWithFormat:@"%@.pinnedPanel", customIDMain];
static NSTouchBarItemIdentifier _Nullable pinnedDialog2 = [NSString stringWithFormat:@"%@.pinnedDialog2", customIDMain];
static NSTouchBarItemIdentifier _Nullable pinnedDialog3 = [NSString stringWithFormat:@"%@.pinnedDialog3", customIDMain];
static NSTouchBarItemIdentifier _Nullable pinnedDialog4 = [NSString stringWithFormat:@"%@.pinnedDialog4", customIDMain];
static NSTouchBarItemIdentifier _Nullable pinnedDialog5 = [NSString stringWithFormat:@"%@.pinnedDialog5", 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];

View File

@ -153,20 +153,19 @@ auto lifetime = rpl::lifetime();
return self; return self;
} }
- (void)updatePeerData { - (void) updatePeerData {
const auto &order = Auth().data().pinnedChatsOrder(nullptr); const auto &order = Auth().data().pinnedChatsOrder(nullptr);
if (self.number > order.size()) { if (self.number > order.size()) {
self.peer = nil; self.peer = nil;
return; return;
} }
// Order is reversed.
const auto pinned = order.at(self.number - 1); const auto pinned = order.at(self.number - 1);
if (const auto history = pinned.history()) { if (const auto history = pinned.history()) {
self.peer = history->peer; self.peer = history->peer;
} }
} }
- (void)buttonActionPin:(NSButton *)sender { - (void) buttonActionPin:(NSButton *)sender {
Core::Sandbox::Instance().customEnterFromEventLoop([=] { Core::Sandbox::Instance().customEnterFromEventLoop([=] {
App::main()->choosePeer(self.number == kSavedMessagesId || self.number == kArchiveId App::main()->choosePeer(self.number == kSavedMessagesId || self.number == kArchiveId
? Auth().userPeerId() ? Auth().userPeerId()
@ -214,37 +213,14 @@ auto lifetime = rpl::lifetime();
@implementation TouchBar @implementation TouchBar
- (id)init:(NSView *)view{ - (id) init:(NSView *)view {
self = [super init]; self = [super init];
if (self) { if (self) {
self.view = view; self.view = view;
self.touchbarItems = @{ self.touchbarItems = @{
// savedMessages: [NSMutableDictionary dictionaryWithDictionary:@{ pinnedPanel: [NSMutableDictionary dictionaryWithDictionary:@{
// @"type": @"button",
// @"name": @"Saved Messages",
// @"cmd": [NSNumber numberWithInt:kSavedMessages],
// @"image": static_cast<NSImage*>(qt_mac_create_nsimage(*pix)),
// }],
pinnedDialog1: [NSMutableDictionary dictionaryWithDictionary:@{
@"type": @"pinned", @"type": @"pinned",
@"num": @1,
}], }],
// pinnedDialog2: [NSMutableDictionary dictionaryWithDictionary:@{
// @"type": @"pinned",
// @"num": @2,
// }],
// pinnedDialog3: [NSMutableDictionary dictionaryWithDictionary:@{
// @"type": @"pinned",
// @"num": @3,
// }],
// pinnedDialog4: [NSMutableDictionary dictionaryWithDictionary:@{
// @"type": @"pinned",
// @"num": @4,
// }],
// pinnedDialog5: [NSMutableDictionary dictionaryWithDictionary:@{
// @"type": @"pinned",
// @"num": @5,
// }],
seekBar: [NSMutableDictionary dictionaryWithDictionary:@{ seekBar: [NSMutableDictionary dictionaryWithDictionary:@{
@"type": @"slider", @"type": @"slider",
@"name": @"Seek Bar" @"name": @"Seek Bar"
@ -295,13 +271,12 @@ auto lifetime = rpl::lifetime();
return self; return self;
} }
- (void) createTouchBar{ - (void) createTouchBar {
_touchBarMain = [[NSTouchBar alloc] init]; _touchBarMain = [[NSTouchBar alloc] init];
_touchBarMain.delegate = self; _touchBarMain.delegate = self;
_touchBarMain.customizationIdentifier = customIDMain; _touchBarMain.customizationIdentifier = customIDMain;
_touchBarMain.defaultItemIdentifiers = @[savedMessages, pinnedDialog1, pinnedDialog2, pinnedDialog3, pinnedDialog4, pinnedDialog5]; _touchBarMain.defaultItemIdentifiers = @[pinnedPanel];
_touchBarMain.customizationAllowedItemIdentifiers = @[savedMessages];
_touchBarAudioPlayer = [[NSTouchBar alloc] init]; _touchBarAudioPlayer = [[NSTouchBar alloc] init];
_touchBarAudioPlayer.delegate = self; _touchBarAudioPlayer.delegate = self;
@ -312,8 +287,8 @@ auto lifetime = rpl::lifetime();
nextItem, currentPosition, seekBar, closePlayer]; nextItem, currentPosition, seekBar, closePlayer];
} }
- (nullable NSTouchBarItem *)touchBar:(NSTouchBar *)touchBar - (nullable NSTouchBarItem *) touchBar:(NSTouchBar *)touchBar
makeItemForIdentifier:(NSTouchBarItemIdentifier)identifier { makeItemForIdentifier:(NSTouchBarItemIdentifier)identifier {
if ([self.touchbarItems[identifier][@"type"] isEqualToString:@"slider"]) { if ([self.touchbarItems[identifier][@"type"] isEqualToString:@"slider"]) {
NSSliderTouchBarItem *item = [[NSSliderTouchBarItem alloc] initWithIdentifier:identifier]; NSSliderTouchBarItem *item = [[NSSliderTouchBarItem alloc] initWithIdentifier:identifier];
@ -334,7 +309,7 @@ auto lifetime = rpl::lifetime();
return item; return item;
} else if ([self.touchbarItems[identifier][@"type"] isEqualToString:@"text"]) { } else if ([self.touchbarItems[identifier][@"type"] isEqualToString:@"text"]) {
NSCustomTouchBarItem *item = [[NSCustomTouchBarItem alloc] initWithIdentifier:identifier]; NSCustomTouchBarItem *item = [[NSCustomTouchBarItem alloc] initWithIdentifier:identifier];
NSTextField *text = [NSTextField labelWithString:@"0: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 = self.touchbarItems[identifier][@"name"];
@ -344,7 +319,7 @@ auto lifetime = rpl::lifetime();
NSCustomTouchBarItem *item = [[NSCustomTouchBarItem alloc] initWithIdentifier:identifier]; NSCustomTouchBarItem *item = [[NSCustomTouchBarItem alloc] initWithIdentifier:identifier];
NSMutableArray *pins = [[NSMutableArray alloc] init]; NSMutableArray *pins = [[NSMutableArray alloc] init];
for (auto i = -1; i <= 5; i++) { for (auto i = kArchiveId; i <= Global::PinnedDialogsCountMax(); i++) {
[pins addObject:[[PinnedDialogButton alloc] init:i].view]; [pins addObject:[[PinnedDialogButton alloc] init:i].view];
} }
NSStackView *stackView = [NSStackView stackViewWithViews:[pins copy]]; NSStackView *stackView = [NSStackView stackViewWithViews:[pins copy]];
@ -357,7 +332,7 @@ auto lifetime = rpl::lifetime();
return nil; return nil;
} }
- (void)setTouchBar:(TouchBarType)type { - (void) setTouchBar:(TouchBarType)type {
if (self.touchBarType == type) { if (self.touchBarType == type) {
return; return;
} }
@ -369,7 +344,7 @@ auto lifetime = rpl::lifetime();
} }
} }
- (void)handlePropertyChange:(Media::Player::TrackState)property { - (void) handlePropertyChange:(Media::Player::TrackState)property {
self.position = property.position < 0 ? 0 : property.position; self.position = property.position < 0 ? 0 : property.position;
self.duration = property.length; self.duration = property.length;
if (Media::Player::IsStoppedOrStopping(property.state)) { if (Media::Player::IsStoppedOrStopping(property.state)) {
@ -390,7 +365,7 @@ auto lifetime = rpl::lifetime();
setEnabled:Media::Player::instance()->previousAvailable(kSongType)]; setEnabled:Media::Player::instance()->previousAvailable(kSongType)];
} }
- (NSString *)formatTime:(int)time { - (NSString *) formatTime:(int)time {
const int seconds = time % 60; const int seconds = time % 60;
const int minutes = (time / 60) % 60; const int minutes = (time / 60) % 60;
const int hours = time / (60 * 60); const int hours = time / (60 * 60);
@ -404,13 +379,13 @@ auto lifetime = rpl::lifetime();
return stime; return stime;
} }
- (void)removeConstraintForIdentifier:(NSTouchBarItemIdentifier)identifier { - (void) removeConstraintForIdentifier:(NSTouchBarItemIdentifier)identifier {
NSTextField *field = self.touchbarItems[identifier][@"view"]; NSTextField *field = self.touchbarItems[identifier][@"view"];
[field removeConstraint:self.touchbarItems[identifier][@"constrain"]]; [field removeConstraint:self.touchbarItems[identifier][@"constrain"]];
} }
- (void)applyConstraintFromString:(NSString *)string - (void) applyConstraintFromString:(NSString *)string
forIdentifier:(NSTouchBarItemIdentifier)identifier { forIdentifier:(NSTouchBarItemIdentifier)identifier {
NSTextField *field = self.touchbarItems[identifier][@"view"]; NSTextField *field = self.touchbarItems[identifier][@"view"];
if (field) { if (field) {
NSString *fString = [[string componentsSeparatedByCharactersInSet: NSString *fString = [[string componentsSeparatedByCharactersInSet:
@ -431,7 +406,7 @@ auto lifetime = rpl::lifetime();
} }
} }
- (void)updateTouchBarTimeItems { - (void) updateTouchBarTimeItems {
NSSlider *seekSlider = self.touchbarItems[seekBar][@"view"]; NSSlider *seekSlider = self.touchbarItems[seekBar][@"view"];
NSTextField *curPosItem = self.touchbarItems[currentPosition][@"view"]; NSTextField *curPosItem = self.touchbarItems[currentPosition][@"view"];
@ -455,7 +430,7 @@ auto lifetime = rpl::lifetime();
[self applyConstraintFromString:curPosItem.stringValue forIdentifier:currentPosition]; [self applyConstraintFromString:curPosItem.stringValue forIdentifier:currentPosition];
} }
- (NSString *)getIdentifierFromView:(id)view { - (NSString *) getIdentifierFromView:(id)view {
NSString *identifier; NSString *identifier;
for (identifier in self.touchbarItems) for (identifier in self.touchbarItems)
if([self.touchbarItems[identifier][@"view"] isEqual:view]) if([self.touchbarItems[identifier][@"view"] isEqual:view])
@ -463,14 +438,12 @@ auto lifetime = rpl::lifetime();
return identifier; return identifier;
} }
- (void)buttonAction:(NSButton *)sender { - (void) buttonAction:(NSButton *)sender {
NSString *identifier = [self getIdentifierFromView:sender]; NSString *identifier = [self getIdentifierFromView:sender];
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 == kSavedMessages) { if (command == kPlayPause) {
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();
@ -482,7 +455,7 @@ auto lifetime = rpl::lifetime();
}); });
} }
- (void)seekbarChanged:(NSSliderTouchBarItem *)sender { - (void) seekbarChanged:(NSSliderTouchBarItem *)sender {
Core::Sandbox::Instance().customEnterFromEventLoop([&] { Core::Sandbox::Instance().customEnterFromEventLoop([&] {
Media::Player::instance()->finishSeeking(kSongType, sender.slider.doubleValue); Media::Player::instance()->finishSeeking(kSongType, sender.slider.doubleValue);
}); });