Slightly refactored touchbar.

This commit is contained in:
23rd 2019-06-21 16:24:19 +03:00 committed by John Preston
parent bebf58ea8d
commit 3ea0247a3e
1 changed files with 55 additions and 51 deletions

View File

@ -172,6 +172,17 @@ inline int UnreadCount(not_null<PeerData*> peer) {
return 0; return 0;
} }
inline bool CanWriteToActiveChat() {
if (const auto window = App::wnd()) {
if (const auto controller = window->sessionController()) {
if (const auto chat = controller->activeChatCurrent()) {
return chat.history()->peer->canWrite();
}
}
}
return false;
}
QString TitleRecentlyUsed() { QString TitleRecentlyUsed() {
const auto &sets = Auth().data().stickerSets(); const auto &sets = Auth().data().stickerSets();
const auto it = sets.constFind(Stickers::CloudRecentSetId); const auto it = sets.constFind(Stickers::CloudRecentSetId);
@ -677,37 +688,39 @@ void AppendEmojiPacks(std::vector<PickerScrubberItem> &to) {
} }
- (void)scrubber:(NSScrubber *)scrubber didSelectItemAtIndex:(NSInteger)index { - (void)scrubber:(NSScrubber *)scrubber didSelectItemAtIndex:(NSInteger)index {
const auto controller = App::wnd()->sessionController(); if (!CanWriteToActiveChat()) {
if (const auto history = controller->activeChatCurrent().history()) { return;
Fn<void()> callback; }
const auto history = App::wnd()->sessionController()->activeChatCurrent()
.history();
const auto callback = [&]() -> bool {
if (const auto document = _stickers[index].document) { if (const auto document = _stickers[index].document) {
callback = [=] {
Auth().api().sendExistingDocument( Auth().api().sendExistingDocument(
document, document,
document->stickerSetOrigin(), document->stickerSetOrigin(),
{}, {},
ApiWrap::SendOptions(history)); ApiWrap::SendOptions(history));
}; return true;
} } else if (const auto emoji = _stickers[index].emoji) {
if (const auto emoji = _stickers[index].emoji) { if (const auto inputField = qobject_cast<QTextEdit*>(
callback = [=] { QApplication::focusWidget())) {
if (const auto inputField = qobject_cast<Ui::InputField*>(
QApplication::focusWidget()->parentWidget())) {
Ui::InsertEmojiAtCursor(inputField->textCursor(), emoji); Ui::InsertEmojiAtCursor(inputField->textCursor(), emoji);
Ui::Emoji::AddRecent(emoji); Ui::Emoji::AddRecent(emoji);
return true;
} }
}
return false;
}; };
}
if (!callback) { if (!Core::Sandbox::Instance().customEnterFromEventLoop(callback)) {
return; return;
} }
Core::Sandbox::Instance().customEnterFromEventLoop(
std::move(callback));
if (_parentPopover) { if (_parentPopover) {
[_parentPopover dismissPopover:nil]; [_parentPopover dismissPopover:nil];
} }
scrubber.selectedIndex = -1; scrubber.selectedIndex = -1;
}
} }
- (void)updateStickers { - (void)updateStickers {
@ -1075,33 +1088,24 @@ void AppendEmojiPacks(std::vector<PickerScrubberItem> &to) {
} }
- (void) showInputFieldItem:(bool)show { - (void) showInputFieldItem:(bool)show {
[self showItem:kPopoverInputItemIdentifier show:show]; [self showItemInMain: show
if (show) { ? kPopoverInputItemIdentifier
[self showItem:kPickerPopoverItemIdentifier show:false]; : CanWriteToActiveChat()
} else { ? kPickerPopoverItemIdentifier
const auto chat = App::wnd()->sessionController()->activeChatCurrent(); : nil];
[self showItem:kPickerPopoverItemIdentifier show:(!show
&& chat
&& chat.history()->peer->canWrite())];
}
} }
- (void) showPickerItem:(bool)show { - (void) showPickerItem:(bool)show {
[self showItem:kPickerPopoverItemIdentifier show:show]; [self showItemInMain: show
if (show) { ? kPickerPopoverItemIdentifier
[self showItem:kPopoverInputItemIdentifier show:false]; : nil];
}
} }
- (void) showItem:(NSTouchBarItemIdentifier)item show:(bool)show { - (void) showItemInMain:(NSTouchBarItemIdentifier)item {
NSMutableArray *items = [NSMutableArray arrayWithArray:_touchBarMain.defaultItemIdentifiers]; NSMutableArray *items = [NSMutableArray arrayWithArray:@[kPinnedPanelItemIdentifier]];
if (show) { if (item) {
if (![items containsObject:item]) {
[items addObject:item]; [items addObject:item];
} }
} else if ([items containsObject:item]) {
[items removeObject:item];
}
_touchBarMain.defaultItemIdentifiers = items; _touchBarMain.defaultItemIdentifiers = items;
} }