Added ability to display media preview from touchbar.

This commit is contained in:
23rd 2019-06-29 13:53:00 +03:00 committed by John Preston
parent 7c98f64cdb
commit bb9e6e7b5f
1 changed files with 59 additions and 1 deletions

View File

@ -653,6 +653,9 @@ void AppendEmojiPacks(std::vector<PickerScrubberItem> &to) {
@implementation PickerCustomTouchBarItem {
std::vector<PickerScrubberItem> _stickers;
NSPopoverTouchBarItem *_parentPopover;
std::unique_ptr<base::Timer> _previewTimer;
int _highlightedIndex;
bool _previewShown;
}
- (id) init:(ScrubberItemType)type popover:(NSPopoverTouchBarItem *)popover {
@ -678,6 +681,14 @@ void AppendEmojiPacks(std::vector<PickerScrubberItem> &to) {
[scrubber registerClass:[NSScrubberTextItemView class] forItemIdentifier:kPickerTitleItemIdentifier];
[scrubber registerClass:[NSScrubberImageItemView class] forItemIdentifier:kEmojiItemIdentifier];
_previewShown = false;
_highlightedIndex = 0;
_previewTimer = !IsSticker(type)
? nullptr
: std::make_unique<base::Timer>([=] {
[self showPreview];
});
self.view = scrubber;
return self;
}
@ -721,6 +732,13 @@ void AppendEmojiPacks(std::vector<PickerScrubberItem> &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<PickerScrubberItem> &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 {