From 8b2bb722de283716a6c95e7a60cf9852de6d8fea Mon Sep 17 00:00:00 2001 From: 23rd <23rd@vivaldi.net> Date: Sat, 7 Nov 2020 22:38:03 +0300 Subject: [PATCH] Added ability to send and cancel recorded voice data with keys. --- .../history_view_voice_record_bar.cpp | 111 +++++++++++++----- .../controls/history_view_voice_record_bar.h | 1 + 2 files changed, 81 insertions(+), 31 deletions(-) diff --git a/Telegram/SourceFiles/history/view/controls/history_view_voice_record_bar.cpp b/Telegram/SourceFiles/history/view/controls/history_view_voice_record_bar.cpp index 4f3ac5146a..d5ef93345c 100644 --- a/Telegram/SourceFiles/history/view/controls/history_view_voice_record_bar.cpp +++ b/Telegram/SourceFiles/history/view/controls/history_view_voice_record_bar.cpp @@ -622,37 +622,7 @@ void VoiceRecordBar::init() { _listen->lifetime().add([=] { _listenChanges.fire({}); }); - auto filterCallback = [=](not_null e) { - using Result = base::EventFilterResult; - if (_send->type() != Ui::SendButton::Type::Send - && _send->type() != Ui::SendButton::Type::Schedule) { - return Result::Continue; - } - switch(e->type()) { - case QEvent::MouseButtonRelease: { - auto callback = [=] { - const auto data = _listen->data(); - _sendVoiceRequests.fire({ - data->bytes, - data->waveform, - Duration(data->samples) }); - hide(); - }; - visibilityAnimate(false, std::move(callback)); - _send->clearState(); - return Result::Cancel; - } - default: return Result::Continue; - } - }; - - auto filter = base::install_event_filter( - _send.get(), - std::move(filterCallback)); - - _listen->lifetime().make_state>( - std::move(filter)); - + installListenStateFilter(); }, lifetime()); } @@ -1045,4 +1015,83 @@ void VoiceRecordBar::installClickOutsideFilter() { std::move(filter)); } +void VoiceRecordBar::installListenStateFilter() { + const auto close = [=](bool send) { + auto callback = [=] { + if (send) { + const auto data = _listen->data(); + _sendVoiceRequests.fire({ + data->bytes, + data->waveform, + Duration(data->samples) }); + } + hide(); + }; + visibilityAnimate(false, std::move(callback)); + }; + + const auto isSend = [=] { + return _send->type() == Ui::SendButton::Type::Send + || _send->type() == Ui::SendButton::Type::Schedule; + }; + + auto mouseFilterCallback = [=](not_null e) { + using Result = base::EventFilterResult; + if (!isSend()) { + return Result::Continue; + } + switch(e->type()) { + case QEvent::MouseButtonRelease: { + close(true); + _send->clearState(); + return Result::Cancel; + } + default: return Result::Continue; + } + }; + + auto sendFilter = base::install_event_filter( + _send.get(), + std::move(mouseFilterCallback)); + + _listen->lifetime().make_state>( + std::move(sendFilter)); + + auto keyFilterCallback = [=](not_null e) { + using Result = base::EventFilterResult; + if (!isSend()) { + return Result::Continue; + } + switch(e->type()) { + case QEvent::KeyPress: { + const auto key = static_cast(e.get())->key(); + const auto isEsc = (key == Qt::Key_Escape); + const auto isEnter = (key == Qt::Key_Enter + || key == Qt::Key_Return); + if (isEnter) { + close(true); + return Result::Cancel; + } + if (isEsc) { + if (_escFilter && _escFilter()) { + return Result::Continue; + } else { + close(false); + return Result::Cancel; + } + } + return Result::Continue; + } + default: return Result::Continue; + } + }; + + auto keyFilter = base::install_event_filter( + QCoreApplication::instance(), + std::move(keyFilterCallback)); + + _listen->lifetime().make_state>( + std::move(keyFilter)); +} + } // namespace HistoryView::Controls diff --git a/Telegram/SourceFiles/history/view/controls/history_view_voice_record_bar.h b/Telegram/SourceFiles/history/view/controls/history_view_voice_record_bar.h index 1df1477756..0968e6e520 100644 --- a/Telegram/SourceFiles/history/view/controls/history_view_voice_record_bar.h +++ b/Telegram/SourceFiles/history/view/controls/history_view_voice_record_bar.h @@ -95,6 +95,7 @@ private: void startRedCircleAnimation(); void installClickOutsideFilter(); + void installListenStateFilter(); bool isTypeRecord() const; bool hasDuration() const;