Skip to content
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

Merged
merged 1 commit into from
Dec 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .clang-tidy
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,5 @@ CheckOptions:
value: UPPER_CASE
- key: readability-identifier-naming.VariableCase
value: camelBack
- key: readability-implicit-bool-conversion.AllowPointerConditions
value: true
152 changes: 92 additions & 60 deletions src/widgets/splits/InputCompletionPopup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Copy link
Contributor

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]

Suggested change
void addEmojis(std::vector<CompletionEmote> &out, const EmojiMap &map,
void addEmojis(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},
Expand All @@ -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;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: variable 'emotes' is not initialized [cppcoreguidelines-init-variables]

Suggested change
std::vector<CompletionEmote> emotes;
std::vector<CompletionEmote> emotes = 0;

auto *tc = dynamic_cast<TwitchChannel *>(channel.get());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: variable 'tc' is not initialized [cppcoreguidelines-init-variables]

Suggested change
auto *tc = dynamic_cast<TwitchChannel *>(channel.get());
auto *tc = nullptr = dynamic_cast<TwitchChannel *>(channel.get());

// returns true also for special Twitch channels (/live, /mentions, /whispers, etc.)
if (channel->isTwitchChannel())
{
Expand All @@ -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");
Expand All @@ -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");
Expand Down Expand Up @@ -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())
Expand All @@ -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());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: variable 'tc' is not initialized [cppcoreguidelines-init-variables]

Suggested change
auto *tc = dynamic_cast<TwitchChannel *>(channel.get());
auto *tc = nullptr = dynamic_cast<TwitchChannel *>(channel.get());

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()
Copy link
Contributor

Choose a reason for hiding this comment

The 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
5 changes: 3 additions & 2 deletions src/widgets/splits/InputCompletionPopup.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,18 @@ class InputCompletionPopup : public BasePopup
{
using ActionCallback = std::function<void(const QString &)>;

constexpr static int maxEntryCount = 200;
constexpr static int MAX_ENTRY_COUNT = 200;

public:
InputCompletionPopup(QWidget *parent = nullptr);

void updateEmotes(const QString &text, ChannelPtr channel);
void updateUsers(const QString &text, ChannelPtr channel);
virtual bool eventFilter(QObject *, QEvent *event) override;

void setInputAction(ActionCallback callback);

bool eventFilter(QObject *watched, QEvent *event) override;

protected:
void showEvent(QShowEvent *event) override;
void hideEvent(QHideEvent *event) override;
Expand Down
40 changes: 28 additions & 12 deletions src/widgets/splits/SplitInput.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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())
{
Expand Down Expand Up @@ -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);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: variable 'tc' is not initialized [cppcoreguidelines-init-variables]

Suggested change
auto *tc = dynamic_cast<TwitchChannel *>(channel);
auto *tc = nullptr = dynamic_cast<TwitchChannel *>(channel);

bool showEmoteCompletion = getSettings()->emoteCompletionWithColon;
bool showUsernameCompletion =
tc && getSettings()->showUsernameCompletionMenu;
tc != nullptr && getSettings()->showUsernameCompletionMenu;
if (!showEmoteCompletion && !showUsernameCompletion)
{
this->hideCompletionPopup();
Expand All @@ -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;
}
}
Expand All @@ -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)
Copy link
Contributor

Choose a reason for hiding this comment

The 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);
Expand All @@ -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_ + ' ';
Expand Down
2 changes: 1 addition & 1 deletion src/widgets/splits/SplitInput.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ class SplitInput : public BaseWidget
void updateCompletionPopup();
void showCompletionPopup(const QString &text, bool emoteCompletion);
void hideCompletionPopup();
void insertCompletionText(const QString &text);
void insertCompletionText(const QString &input_) const;
void openEmotePopup();

void updateCancelReplyButton();
Expand Down