From bb9e6e7b5f7d552c358a388be1dff1c35d91ee6a Mon Sep 17 00:00:00 2001 From: 23rd <23rd@vivaldi.net> Date: Sat, 29 Jun 2019 13:53:00 +0300 Subject: [PATCH] Added ability to display media preview from touchbar. --- .../SourceFiles/platform/mac/mac_touchbar.mm | 60 ++++++++++++++++++- 1 file changed, 59 insertions(+), 1 deletion(-) diff --git a/Telegram/SourceFiles/platform/mac/mac_touchbar.mm b/Telegram/SourceFiles/platform/mac/mac_touchbar.mm index d73c821963..f2ecd1ac5e 100644 --- a/Telegram/SourceFiles/platform/mac/mac_touchbar.mm +++ b/Telegram/SourceFiles/platform/mac/mac_touchbar.mm @@ -653,6 +653,9 @@ void AppendEmojiPacks(std::vector &to) { @implementation PickerCustomTouchBarItem { std::vector _stickers; NSPopoverTouchBarItem *_parentPopover; + std::unique_ptr _previewTimer; + int _highlightedIndex; + bool _previewShown; } - (id) init:(ScrubberItemType)type popover:(NSPopoverTouchBarItem *)popover { @@ -678,6 +681,14 @@ void AppendEmojiPacks(std::vector &to) { [scrubber registerClass:[NSScrubberTextItemView class] forItemIdentifier:kPickerTitleItemIdentifier]; [scrubber registerClass:[NSScrubberImageItemView class] forItemIdentifier:kEmojiItemIdentifier]; + _previewShown = false; + _highlightedIndex = 0; + _previewTimer = !IsSticker(type) + ? nullptr + : std::make_unique([=] { + [self showPreview]; + }); + self.view = scrubber; return self; } @@ -721,6 +732,13 @@ void AppendEmojiPacks(std::vector &to) { if (!CanWriteToActiveChat()) { return; } + scrubber.selectedIndex = -1; + if (_previewShown && [self hidePreview]) { + return; + } + if (_previewTimer) { + _previewTimer->cancel(); + } const auto chat = GetActiveChat(); const auto callback = [&]() -> bool { @@ -752,7 +770,47 @@ void AppendEmojiPacks(std::vector &to) { if (_parentPopover) { [_parentPopover dismissPopover:nil]; } - scrubber.selectedIndex = -1; +} + +- (void)scrubber:(NSScrubber *)scrubber didHighlightItemAtIndex:(NSInteger)index { + if (_previewTimer) { + _previewTimer->callOnce(QApplication::startDragTime()); + _highlightedIndex = index; + } +} + +- (void)scrubber:(NSScrubber *)scrubber didChangeVisibleRange:(NSRange)visibleRange { + [self didCancelInteractingWithScrubber:scrubber]; +} + +- (void)didCancelInteractingWithScrubber:(NSScrubber *)scrubber { + if (_previewTimer) { + _previewTimer->cancel(); + } + if (_previewShown) { + [self hidePreview]; + } +} + +- (void)showPreview { + if (const auto document = _stickers[_highlightedIndex].document) { + if (const auto w = App::wnd()) { + w->showMediaPreview(document->stickerSetOrigin(), document); + _previewShown = true; + } + } +} + +- (bool)hidePreview { + if (const auto w = App::wnd()) { + Core::Sandbox::Instance().customEnterFromEventLoop([=] { + w->hideMediaPreview(); + }); + _previewShown = false; + _highlightedIndex = 0; + return true; + } + return false; } - (void)updateStickers {