Added buttons of pinned dialogs in main touch bar.

This commit is contained in:
23rd 2019-05-01 16:18:31 +03:00 committed by John Preston
parent 3eadc62cd5
commit 8099305c53
5 changed files with 161 additions and 14 deletions

View File

@ -1363,6 +1363,14 @@ rpl::producer<> Session::savedGifsUpdated() const {
return _savedGifsUpdated.events(); return _savedGifsUpdated.events();
} }
void Session::notifyPinnedDialogsOrderUpdated() {
_pinnedDialogsOrderUpdated.fire({});
}
rpl::producer<> Session::pinnedDialogsOrderUpdated() const {
return _pinnedDialogsOrderUpdated.events();
}
void Session::userIsContactUpdated(not_null<UserData*> user) { void Session::userIsContactUpdated(not_null<UserData*> user) {
const auto i = _contactViews.find(peerToUser(user->id)); const auto i = _contactViews.find(peerToUser(user->id));
if (i != _contactViews.end()) { if (i != _contactViews.end()) {
@ -1422,6 +1430,7 @@ void Session::setPinnedFromDialog(const Dialogs::Key &key, bool pinned) {
void Session::applyPinnedChats( void Session::applyPinnedChats(
Data::Folder *folder, Data::Folder *folder,
const QVector<MTPDialogPeer> &list) { const QVector<MTPDialogPeer> &list) {
notifyPinnedDialogsOrderUpdated()
for (const auto &peer : list) { for (const auto &peer : list) {
peer.match([&](const MTPDdialogPeer &data) { peer.match([&](const MTPDdialogPeer &data) {
const auto history = this->history(peerFromMTP(data.vpeer)); const auto history = this->history(peerFromMTP(data.vpeer));
@ -1481,6 +1490,7 @@ void Session::applyDialog(
void Session::applyDialog( void Session::applyDialog(
Data::Folder *requestFolder, Data::Folder *requestFolder,
const MTPDdialogFolder &data) { const MTPDdialogFolder &data) {
notifyPinnedDialogsOrderUpdated()
if (requestFolder) { if (requestFolder) {
LOG(("API Error: requestFolder != nullptr for dialogFolder.")); LOG(("API Error: requestFolder != nullptr for dialogFolder."));
} }

View File

@ -228,6 +228,8 @@ public:
[[nodiscard]] rpl::producer<> stickersUpdated() const; [[nodiscard]] rpl::producer<> stickersUpdated() const;
void notifySavedGifsUpdated(); void notifySavedGifsUpdated();
[[nodiscard]] rpl::producer<> savedGifsUpdated() const; [[nodiscard]] rpl::producer<> savedGifsUpdated() const;
void notifyPinnedDialogsOrderUpdated();
[[nodiscard]] rpl::producer<> pinnedDialogsOrderUpdated() const;
bool stickersUpdateNeeded(crl::time now) const { bool stickersUpdateNeeded(crl::time now) const {
return stickersUpdateNeeded(_lastStickersUpdate, now); return stickersUpdateNeeded(_lastStickersUpdate, now);
@ -830,6 +832,7 @@ private:
rpl::event_stream<> _stickersUpdated; rpl::event_stream<> _stickersUpdated;
rpl::event_stream<> _savedGifsUpdated; rpl::event_stream<> _savedGifsUpdated;
rpl::event_stream<> _pinnedDialogsOrderUpdated;
crl::time _lastStickersUpdate = 0; crl::time _lastStickersUpdate = 0;
crl::time _lastRecentStickersUpdate = 0; crl::time _lastRecentStickersUpdate = 0;
crl::time _lastFavedStickersUpdate = 0; crl::time _lastFavedStickersUpdate = 0;

View File

@ -33,6 +33,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
#include "media/player/media_player_instance.h" #include "media/player/media_player_instance.h"
#include "media/audio/media_audio.h" #include "media/audio/media_audio.h"
#include "platform/mac/touchbar.h" #include "platform/mac/touchbar.h"
#include "data/data_session.h"
@interface MainWindowObserver : NSObject { @interface MainWindowObserver : NSObject {
} }
@ -395,6 +396,20 @@ MainWindow::MainWindow()
} }
}); });
subscribe(Core::App().authSessionChanged(), [this] {
if (AuthSession::Exists()) {
Auth().data().pinnedDialogsOrderUpdated(
) | rpl::start_with_next([this] {
if (auto view = reinterpret_cast<NSView*>(winId())) {
// Create TouchBar.
[NSApplication sharedApplication].automaticCustomizeTouchBarMenuItemEnabled = YES;
_private->_touchBar = [[TouchBar alloc] init:view];
}
}, lifetime());
}
});
subscribe(Media::Player::instance()->updatedNotifier(), subscribe(Media::Player::instance()->updatedNotifier(),
[=](const Media::Player::TrackState &state) { [=](const Media::Player::TrackState &state) {
[_private->_touchBar handlePropertyChange:state]; [_private->_touchBar handlePropertyChange:state];
@ -429,9 +444,6 @@ 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];
} }
} }

View File

@ -23,6 +23,13 @@ static NSString * _Nullable BASE_ID = @"telegram.touchbar";
static NSTouchBarCustomizationIdentifier _Nullable customID = @"telegram.touchbar"; static NSTouchBarCustomizationIdentifier _Nullable customID = @"telegram.touchbar";
static NSTouchBarCustomizationIdentifier _Nullable customIDMain = @"telegram.touchbarMain"; static NSTouchBarCustomizationIdentifier _Nullable customIDMain = @"telegram.touchbarMain";
static NSTouchBarItemIdentifier _Nullable savedMessages = [NSString stringWithFormat:@"%@.savedMessages", customIDMain]; static NSTouchBarItemIdentifier _Nullable savedMessages = [NSString stringWithFormat:@"%@.savedMessages", customIDMain];
static NSTouchBarItemIdentifier _Nullable pinnedDialog1 = [NSString stringWithFormat:@"%@.pinnedDialog1", 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];
static NSTouchBarItemIdentifier _Nullable nextItem = [NSString stringWithFormat:@"%@.nextItem", BASE_ID]; static NSTouchBarItemIdentifier _Nullable nextItem = [NSString stringWithFormat:@"%@.nextItem", BASE_ID];

View File

@ -24,6 +24,9 @@
#include "lang/lang_keys.h" #include "lang/lang_keys.h"
#include "base/timer.h" #include "base/timer.h"
#include "styles/style_window.h" #include "styles/style_window.h"
#include "auth_session.h"
#include "data/data_session.h"
#include "history/history.h"
namespace { namespace {
constexpr auto kSavedMessages = 0x001; constexpr auto kSavedMessages = 0x001;
@ -38,12 +41,96 @@ constexpr auto kMs = 1000;
constexpr auto kSongType = AudioMsgId::Type::Song; constexpr auto kSongType = AudioMsgId::Type::Song;
} // namespace } // namespace
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;
- (id) init:(int)num;
- (NSImage *) getPinImage;
- (void)buttonActionPin:(NSButton *)sender;
- (void)updatePeerData;
@end // @interface PinnedDialogButton
@implementation PinnedDialogButton : NSCustomTouchBarItem
- (id) init:(int)num {
NSString *identifier = [NSString stringWithFormat:@"%@.pinnedDialog%d", customIDMain, num];
self = [super initWithIdentifier:identifier];
if (!self) {
return nil;
}
self.number = num;
self.waiting = true;
[self updatePeerData];
NSButton *button = [NSButton buttonWithImage:[self getPinImage] target:self action:@selector(buttonActionPin:)];
[button setBordered:NO];
[button sizeToFit];
[button setHidden:(num > Auth().data().pinnedDialogsOrder().size())];
self.view = button;
self.customizationLabel = [NSString stringWithFormat:@"Pinned Dialog %d", num];
base::ObservableViewer(
Auth().downloaderTaskFinished()
) | rpl::start_with_next([self] {
if (self.waiting) {
NSButton *button = self.view;
button.image = [self getPinImage];
}
}, Auth().lifetime());
return self;
}
- (void)updatePeerData {
const auto &order = Auth().data().pinnedDialogsOrder();
if (self.number > order.size()) {
self.peer = nil;
return;
}
// Order is reversed.
const auto pinned = order.at(order.size() - self.number);
if (const auto history = pinned.history()) {
self.peer = history->peer;
}
}
- (void)buttonActionPin:(NSButton *)sender {
Core::Sandbox::Instance().customEnterFromEventLoop([=] {
App::main()->choosePeer(self.peer->id, ShowAtUnreadMsgId);
});
}
- (NSImage *) getPinImage {
if (!self.peer) {
return nil;
}
self.waiting = !self.peer->userpicLoaded();
auto pixmap = self.peer->genUserpic(20);
pixmap.setDevicePixelRatio(cRetinaFactor());
return static_cast<NSImage*>(qt_mac_create_nsimage(pixmap));
}
@end
@interface TouchBar()<NSTouchBarDelegate>
@end // @interface TouchBar
@interface TouchBar()<NSTouchBarDelegate> @interface TouchBar()<NSTouchBarDelegate>
@end @end
@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;
@ -52,7 +139,27 @@ constexpr auto kSongType = AudioMsgId::Type::Song;
@"type": @"button", @"type": @"button",
@"name": @"Saved Messages", @"name": @"Saved Messages",
@"cmd": [NSNumber numberWithInt:kSavedMessages], @"cmd": [NSNumber numberWithInt:kSavedMessages],
@"image": [NSImage imageNamed:NSImageNameTouchBarBookmarksTemplate], @"image": [NSImage imageNamed:NSImageNameTouchBarBookmarksTemplate],
}],
pinnedDialog1: [NSMutableDictionary dictionaryWithDictionary:@{
@"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",
@ -89,8 +196,8 @@ constexpr auto kSongType = AudioMsgId::Type::Song;
}] }]
}; };
} }
[self createTouchBar]; [self createTouchBar];
[self setTouchBar:TouchBarType::Main]; [self setTouchBar:TouchBarType::Main];
return self; return self;
} }
@ -100,20 +207,21 @@ constexpr auto kSongType = AudioMsgId::Type::Song;
_touchBarMain.delegate = self; _touchBarMain.delegate = self;
_touchBarMain.customizationIdentifier = customIDMain; _touchBarMain.customizationIdentifier = customIDMain;
_touchBarMain.defaultItemIdentifiers = @[savedMessages]; _touchBarMain.defaultItemIdentifiers = @[savedMessages, pinnedDialog1, pinnedDialog2, pinnedDialog3, pinnedDialog4, pinnedDialog5];
_touchBarMain.customizationAllowedItemIdentifiers = @[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 = @[play, previousItem, nextItem, seekBar, closePlayer]; _touchBarAudioPlayer.defaultItemIdentifiers = @[play, previousItem, nextItem, seekBar, closePlayer];
_touchBarAudioPlayer.customizationAllowedItemIdentifiers = @[play, previousItem, _touchBarAudioPlayer.customizationAllowedItemIdentifiers = @[play, previousItem,
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];
item.slider.minValue = 0.0f; item.slider.minValue = 0.0f;
@ -139,6 +247,13 @@ constexpr auto kSongType = AudioMsgId::Type::Song;
item.customizationLabel = self.touchbarItems[identifier][@"name"]; item.customizationLabel = self.touchbarItems[identifier][@"name"];
[self.touchbarItems[identifier] setObject:text forKey:@"view"]; [self.touchbarItems[identifier] setObject:text forKey:@"view"];
return item; return item;
} else if ([self.touchbarItems[identifier][@"type"] isEqualToString:@"pinned"]) {
const auto number = [self.touchbarItems[identifier][@"num"] intValue];
PinnedDialogButton *item = [[PinnedDialogButton alloc] init:number];
NSImage *image = self.touchbarItems[identifier][@"image"];
[self.touchbarItems[identifier] setObject:item.view forKey:@"view"];
return item;
} }
return nil; return nil;