Feature: execute commands on chat messages using the context menu (#3738)

This commit is contained in:
xel86
2022-05-22 10:45:54 -04:00
committed by GitHub
parent 7d9c3c65da
commit 4239666934
8 changed files with 103 additions and 9 deletions
+60
View File
@@ -1885,6 +1885,10 @@ void ChannelView::addContextMenuItems(
// Add hidden options (e.g. copy message ID) if the user held down Shift
this->addHiddenContextMenuItems(hoveredElement, layout, event, *menu);
// Add executable command options
this->addCommandExecutionContextMenuItems(hoveredElement, layout, event,
*menu);
menu->popup(QCursor::pos());
menu->raise();
}
@@ -2081,6 +2085,62 @@ void ChannelView::addHiddenContextMenuItems(
});
}
}
void ChannelView::addCommandExecutionContextMenuItems(
const MessageLayoutElement * /*hoveredElement*/, MessageLayoutPtr layout,
QMouseEvent * /*event*/, QMenu &menu)
{
/* Get commands to be displayed in context menu;
* only those that had the showInMsgContextMenu check box marked in the Commands page */
std::vector<Command> cmds;
for (auto &cmd : getApp()->commands->items)
{
if (cmd.showInMsgContextMenu)
{
cmds.push_back(cmd);
}
}
if (cmds.empty())
{
return;
}
menu.addSeparator();
auto executeAction = menu.addAction("Execute command");
auto cmdMenu = new QMenu;
executeAction->setMenu(cmdMenu);
for (auto &cmd : cmds)
{
QString inputText = this->selection_.isEmpty()
? layout->getMessage()->messageText
: this->getSelectedText();
inputText.push_front(cmd.name + " ");
cmdMenu->addAction(cmd.name, [this, inputText] {
ChannelPtr channel;
/* Search popups and user message history's underlyingChannels aren't of type TwitchChannel, but
* we would still like to execute commands from them. Use their source channel instead if applicable. */
if (this->hasSourceChannel())
{
channel = this->sourceChannel();
}
else
{
channel = this->underlyingChannel_;
}
QString value =
getApp()->commands->execCommand(inputText, channel, false);
channel->sendMessage(value);
});
}
}
void ChannelView::mouseDoubleClickEvent(QMouseEvent *event)
{
if (event->button() != Qt::LeftButton)