-
-
Notifications
You must be signed in to change notification settings - Fork 449
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Clean up/refactor InputCompletionPopup #4242
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -13,35 +13,47 @@ | |||||
#include "widgets/listview/GenericListView.hpp" | ||||||
#include "widgets/splits/InputCompletionItem.hpp" | ||||||
|
||||||
namespace chatterino { | ||||||
namespace { | ||||||
|
||||||
struct _Emote { | ||||||
EmotePtr emote; | ||||||
QString displayName; | ||||||
QString providerName; | ||||||
}; | ||||||
using namespace chatterino; | ||||||
|
||||||
void addEmotes(std::vector<_Emote> &out, const EmoteMap &map, | ||||||
const QString &text, const QString &providerName) | ||||||
{ | ||||||
for (auto &&emote : map) | ||||||
if (emote.first.string.contains(text, Qt::CaseInsensitive)) | ||||||
out.push_back( | ||||||
{emote.second, emote.second->name.string, providerName}); | ||||||
} | ||||||
struct CompletionEmote { | ||||||
EmotePtr emote; | ||||||
QString displayName; | ||||||
QString providerName; | ||||||
}; | ||||||
|
||||||
void addEmojis(std::vector<_Emote> &out, const EmojiMap &map, | ||||||
const QString &text) | ||||||
void addEmotes(std::vector<CompletionEmote> &out, const EmoteMap &map, | ||||||
const QString &text, const QString &providerName) | ||||||
{ | ||||||
for (auto &&emote : map) | ||||||
{ | ||||||
map.each([&](const QString &, const std::shared_ptr<EmojiData> &emoji) { | ||||||
for (auto &&shortCode : emoji->shortCodes) | ||||||
if (shortCode.contains(text, Qt::CaseInsensitive)) | ||||||
out.push_back({emoji->emote, shortCode, "Emoji"}); | ||||||
}); | ||||||
if (emote.first.string.contains(text, Qt::CaseInsensitive)) | ||||||
{ | ||||||
out.push_back( | ||||||
{emote.second, emote.second->name.string, providerName}); | ||||||
} | ||||||
} | ||||||
} | ||||||
|
||||||
void addEmojis(std::vector<CompletionEmote> &out, const EmojiMap &map, | ||||||
const QString &text) | ||||||
{ | ||||||
map.each([&](const QString &, const std::shared_ptr<EmojiData> &emoji) { | ||||||
for (auto &&shortCode : emoji->shortCodes) | ||||||
{ | ||||||
if (shortCode.contains(text, Qt::CaseInsensitive)) | ||||||
{ | ||||||
out.push_back({emoji->emote, shortCode, "Emoji"}); | ||||||
} | ||||||
} | ||||||
}); | ||||||
} | ||||||
|
||||||
} // namespace | ||||||
|
||||||
namespace chatterino { | ||||||
|
||||||
InputCompletionPopup::InputCompletionPopup(QWidget *parent) | ||||||
: BasePopup({BasePopup::EnableCustomFrame, BasePopup::Frameless, | ||||||
BasePopup::DontFocus, BaseWindow::DisableLayoutSave}, | ||||||
|
@@ -52,30 +64,17 @@ InputCompletionPopup::InputCompletionPopup(QWidget *parent) | |||||
|
||||||
QObject::connect(&this->redrawTimer_, &QTimer::timeout, this, [this] { | ||||||
if (this->isVisible()) | ||||||
{ | ||||||
this->ui_.listView->doItemsLayout(); | ||||||
} | ||||||
}); | ||||||
this->redrawTimer_.setInterval(33); | ||||||
} | ||||||
|
||||||
void InputCompletionPopup::initLayout() | ||||||
{ | ||||||
LayoutCreator creator = {this}; | ||||||
|
||||||
auto listView = | ||||||
creator.emplace<GenericListView>().assign(&this->ui_.listView); | ||||||
listView->setInvokeActionOnTab(true); | ||||||
|
||||||
listView->setModel(&this->model_); | ||||||
QObject::connect(listView.getElement(), &GenericListView::closeRequested, | ||||||
this, [this] { | ||||||
this->close(); | ||||||
}); | ||||||
} | ||||||
|
||||||
void InputCompletionPopup::updateEmotes(const QString &text, ChannelPtr channel) | ||||||
{ | ||||||
std::vector<_Emote> emotes; | ||||||
auto tc = dynamic_cast<TwitchChannel *>(channel.get()); | ||||||
std::vector<CompletionEmote> emotes; | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. warning: variable 'emotes' is not initialized [cppcoreguidelines-init-variables]
Suggested change
|
||||||
auto *tc = dynamic_cast<TwitchChannel *>(channel.get()); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. warning: variable 'tc' is not initialized [cppcoreguidelines-init-variables]
Suggested change
|
||||||
// returns true also for special Twitch channels (/live, /mentions, /whispers, etc.) | ||||||
if (channel->isTwitchChannel()) | ||||||
{ | ||||||
|
@@ -90,7 +89,7 @@ void InputCompletionPopup::updateEmotes(const QString &text, ChannelPtr channel) | |||||
if (tc && | ||||||
localEmoteData->find(tc->roomId()) != localEmoteData->end()) | ||||||
{ | ||||||
if (auto localEmotes = &localEmoteData->at(tc->roomId())) | ||||||
if (const auto *localEmotes = &localEmoteData->at(tc->roomId())) | ||||||
{ | ||||||
addEmotes(emotes, *localEmotes, text, | ||||||
"Local Twitch Emotes"); | ||||||
|
@@ -102,19 +101,27 @@ void InputCompletionPopup::updateEmotes(const QString &text, ChannelPtr channel) | |||||
{ | ||||||
// TODO extract "Channel {BetterTTV,7TV,FrankerFaceZ}" text into a #define. | ||||||
if (auto bttv = tc->bttvEmotes()) | ||||||
{ | ||||||
addEmotes(emotes, *bttv, text, "Channel BetterTTV"); | ||||||
} | ||||||
if (auto ffz = tc->ffzEmotes()) | ||||||
{ | ||||||
addEmotes(emotes, *ffz, text, "Channel FrankerFaceZ"); | ||||||
} | ||||||
if (auto seventv = tc->seventvEmotes()) | ||||||
{ | ||||||
addEmotes(emotes, *seventv, text, "Channel 7TV"); | ||||||
} | ||||||
} | ||||||
|
||||||
if (auto bttvG = getApp()->twitch->getBttvEmotes().emotes()) | ||||||
{ | ||||||
addEmotes(emotes, *bttvG, text, "Global BetterTTV"); | ||||||
} | ||||||
if (auto ffzG = getApp()->twitch->getFfzEmotes().emotes()) | ||||||
{ | ||||||
addEmotes(emotes, *ffzG, text, "Global FrankerFaceZ"); | ||||||
} | ||||||
if (auto seventvG = getApp()->twitch->getSeventvEmotes().globalEmotes()) | ||||||
{ | ||||||
addEmotes(emotes, *seventvG, text, "Global 7TV"); | ||||||
|
@@ -148,8 +155,10 @@ void InputCompletionPopup::updateEmotes(const QString &text, ChannelPtr channel) | |||||
emote.emote, emote.displayName + " - " + emote.providerName, | ||||||
this->callback_)); | ||||||
|
||||||
if (count++ == maxEntryCount) | ||||||
if (count++ == MAX_ENTRY_COUNT) | ||||||
{ | ||||||
break; | ||||||
} | ||||||
} | ||||||
|
||||||
if (!emotes.empty()) | ||||||
|
@@ -160,45 +169,68 @@ void InputCompletionPopup::updateEmotes(const QString &text, ChannelPtr channel) | |||||
|
||||||
void InputCompletionPopup::updateUsers(const QString &text, ChannelPtr channel) | ||||||
{ | ||||||
auto twitchChannel = dynamic_cast<TwitchChannel *>(channel.get()); | ||||||
if (twitchChannel) | ||||||
auto *tc = dynamic_cast<TwitchChannel *>(channel.get()); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. warning: variable 'tc' is not initialized [cppcoreguidelines-init-variables]
Suggested change
|
||||||
if (!tc) | ||||||
{ | ||||||
auto chatters = twitchChannel->accessChatters()->filterByPrefix(text); | ||||||
this->model_.clear(); | ||||||
int count = 0; | ||||||
for (const auto &name : chatters) | ||||||
{ | ||||||
this->model_.addItem(std::make_unique<InputCompletionItem>( | ||||||
nullptr, name, this->callback_)); | ||||||
return; | ||||||
} | ||||||
|
||||||
if (count++ == maxEntryCount) | ||||||
break; | ||||||
} | ||||||
if (!chatters.empty()) | ||||||
auto chatters = tc->accessChatters()->filterByPrefix(text); | ||||||
this->model_.clear(); | ||||||
|
||||||
if (chatters.empty()) | ||||||
{ | ||||||
return; | ||||||
} | ||||||
|
||||||
int count = 0; | ||||||
for (const auto &name : chatters) | ||||||
{ | ||||||
this->model_.addItem(std::make_unique<InputCompletionItem>( | ||||||
nullptr, name, this->callback_)); | ||||||
|
||||||
if (count++ == MAX_ENTRY_COUNT) | ||||||
{ | ||||||
this->ui_.listView->setCurrentIndex(this->model_.index(0)); | ||||||
break; | ||||||
} | ||||||
} | ||||||
} | ||||||
|
||||||
bool InputCompletionPopup::eventFilter(QObject *watched, QEvent *event) | ||||||
{ | ||||||
return this->ui_.listView->eventFilter(watched, event); | ||||||
this->ui_.listView->setCurrentIndex(this->model_.index(0)); | ||||||
} | ||||||
|
||||||
void InputCompletionPopup::setInputAction(ActionCallback callback) | ||||||
{ | ||||||
this->callback_ = std::move(callback); | ||||||
} | ||||||
|
||||||
void InputCompletionPopup::showEvent(QShowEvent *) | ||||||
bool InputCompletionPopup::eventFilter(QObject *watched, QEvent *event) | ||||||
{ | ||||||
return this->ui_.listView->eventFilter(watched, event); | ||||||
} | ||||||
|
||||||
void InputCompletionPopup::showEvent(QShowEvent * /*event*/) | ||||||
{ | ||||||
this->redrawTimer_.start(); | ||||||
} | ||||||
|
||||||
void InputCompletionPopup::hideEvent(QHideEvent *) | ||||||
void InputCompletionPopup::hideEvent(QHideEvent * /*event*/) | ||||||
{ | ||||||
this->redrawTimer_.stop(); | ||||||
} | ||||||
|
||||||
void InputCompletionPopup::initLayout() | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. warning: method 'initLayout' can be made static [readability-convert-member-functions-to-static] src/widgets/splits/InputCompletionPopup.hpp:33: - void initLayout();
+ static void initLayout(); |
||||||
{ | ||||||
LayoutCreator creator = {this}; | ||||||
|
||||||
auto listView = | ||||||
creator.emplace<GenericListView>().assign(&this->ui_.listView); | ||||||
listView->setInvokeActionOnTab(true); | ||||||
|
||||||
listView->setModel(&this->model_); | ||||||
QObject::connect(listView.getElement(), &GenericListView::closeRequested, | ||||||
this, [this] { | ||||||
this->close(); | ||||||
}); | ||||||
} | ||||||
|
||||||
} // namespace chatterino |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -625,7 +625,7 @@ void SplitInput::installKeyPressedEvent() | |||||
{ | ||||||
this->ui_.textEdit->keyPressed.disconnectAll(); | ||||||
this->ui_.textEdit->keyPressed.connect([this](QKeyEvent *event) { | ||||||
if (auto popup = this->inputCompletionPopup_.get()) | ||||||
if (auto *popup = this->inputCompletionPopup_.get()) | ||||||
{ | ||||||
if (popup->isVisible()) | ||||||
{ | ||||||
|
@@ -674,11 +674,11 @@ void SplitInput::onCursorPositionChanged() | |||||
|
||||||
void SplitInput::updateCompletionPopup() | ||||||
{ | ||||||
auto channel = this->split_->getChannel().get(); | ||||||
auto tc = dynamic_cast<TwitchChannel *>(channel); | ||||||
auto *channel = this->split_->getChannel().get(); | ||||||
auto *tc = dynamic_cast<TwitchChannel *>(channel); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. warning: variable 'tc' is not initialized [cppcoreguidelines-init-variables]
Suggested change
|
||||||
bool showEmoteCompletion = getSettings()->emoteCompletionWithColon; | ||||||
bool showUsernameCompletion = | ||||||
tc && getSettings()->showUsernameCompletionMenu; | ||||||
tc != nullptr && getSettings()->showUsernameCompletionMenu; | ||||||
if (!showEmoteCompletion && !showUsernameCompletion) | ||||||
{ | ||||||
this->hideCompletionPopup(); | ||||||
|
@@ -704,22 +704,32 @@ void SplitInput::updateCompletionPopup() | |||||
this->hideCompletionPopup(); | ||||||
return; | ||||||
} | ||||||
else if (text[i] == ':' && showEmoteCompletion) | ||||||
|
||||||
if (text[i] == ':' && showEmoteCompletion) | ||||||
{ | ||||||
if (i == 0 || text[i - 1].isSpace()) | ||||||
{ | ||||||
this->showCompletionPopup(text.mid(i, position - i + 1).mid(1), | ||||||
true); | ||||||
} | ||||||
else | ||||||
{ | ||||||
this->hideCompletionPopup(); | ||||||
} | ||||||
return; | ||||||
} | ||||||
else if (text[i] == '@' && showUsernameCompletion) | ||||||
|
||||||
if (text[i] == '@' && showUsernameCompletion) | ||||||
{ | ||||||
if (i == 0 || text[i - 1].isSpace()) | ||||||
{ | ||||||
this->showCompletionPopup(text.mid(i, position - i + 1).mid(1), | ||||||
false); | ||||||
} | ||||||
else | ||||||
{ | ||||||
this->hideCompletionPopup(); | ||||||
} | ||||||
return; | ||||||
} | ||||||
} | ||||||
|
@@ -734,21 +744,25 @@ void SplitInput::showCompletionPopup(const QString &text, bool emoteCompletion) | |||||
this->inputCompletionPopup_ = new InputCompletionPopup(this); | ||||||
this->inputCompletionPopup_->setInputAction( | ||||||
[that = QObjectRef(this)](const QString &text) mutable { | ||||||
if (auto this2 = that.get()) | ||||||
if (auto *this2 = that.get()) | ||||||
{ | ||||||
this2->insertCompletionText(text); | ||||||
this2->hideCompletionPopup(); | ||||||
} | ||||||
}); | ||||||
} | ||||||
|
||||||
auto popup = this->inputCompletionPopup_.get(); | ||||||
auto *popup = this->inputCompletionPopup_.get(); | ||||||
assert(popup); | ||||||
|
||||||
if (emoteCompletion) // autocomplete emotes | ||||||
if (emoteCompletion) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. warning: if with identical then and else branches [bugprone-branch-clone] if (emoteCompletion)
^ src/widgets/splits/SplitInput.cpp:761: else branch starts here else
^ |
||||||
{ | ||||||
popup->updateEmotes(text, this->split_->getChannel()); | ||||||
else // autocomplete usernames | ||||||
} | ||||||
else | ||||||
{ | ||||||
popup->updateUsers(text, this->split_->getChannel()); | ||||||
} | ||||||
|
||||||
auto pos = this->mapToGlobal({0, 0}) - QPoint(0, popup->height()) + | ||||||
QPoint((this->width() - popup->width()) / 2, 0); | ||||||
|
@@ -759,11 +773,13 @@ void SplitInput::showCompletionPopup(const QString &text, bool emoteCompletion) | |||||
|
||||||
void SplitInput::hideCompletionPopup() | ||||||
{ | ||||||
if (auto popup = this->inputCompletionPopup_.get()) | ||||||
if (auto *popup = this->inputCompletionPopup_.get()) | ||||||
{ | ||||||
popup->hide(); | ||||||
} | ||||||
} | ||||||
|
||||||
void SplitInput::insertCompletionText(const QString &input_) | ||||||
void SplitInput::insertCompletionText(const QString &input_) const | ||||||
{ | ||||||
auto &edit = *this->ui_.textEdit; | ||||||
auto input = input_ + ' '; | ||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
warning: parameter 'out' is unused [misc-unused-parameters]