Skip to content

Commit

Permalink
refactor: Change lifetime of context menus (#4924)
Browse files Browse the repository at this point in the history
  • Loading branch information
Nerixyz authored Oct 29, 2023
1 parent 7ecbfa0 commit c811e2d
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 27 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
- Dev: Improve performance by reducing repaints caused by selections. (#4889)
- Dev: Removed direct dependency on Qt 5 compatibility module. (#4906)
- Dev: Refactor `DebugCount` and add copy button to debug popup. (#4921)
- Dev: Changed lifetime of context menus. (#4924)

## 2.4.6

Expand Down
11 changes: 2 additions & 9 deletions src/widgets/dialogs/UserInfoPopup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -278,15 +278,8 @@ UserInfoPopup::UserInfoPopup(bool closeAutomatically, QWidget *parent,
return;
}

static QMenu *previousMenu = nullptr;
if (previousMenu != nullptr)
{
previousMenu->deleteLater();
previousMenu = nullptr;
}

auto menu = new QMenu;
previousMenu = menu;
auto *menu = new QMenu(this);
menu->setAttribute(Qt::WA_DeleteOnClose);

auto avatarUrl = this->avatarUrl_;

Expand Down
23 changes: 5 additions & 18 deletions src/widgets/helper/ChannelView.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,19 +73,13 @@ namespace {
MessageElementFlags creatorFlags, QMenu &menu)
{
auto *openAction = menu.addAction("&Open");
auto openMenu = new QMenu;
auto *openMenu = new QMenu(&menu);
openAction->setMenu(openMenu);

auto *copyAction = menu.addAction("&Copy");
auto copyMenu = new QMenu;
auto *copyMenu = new QMenu(&menu);
copyAction->setMenu(copyMenu);

// see if the QMenu actually gets destroyed
QObject::connect(openMenu, &QMenu::destroyed, [] {
QMessageBox(QMessageBox::Information, "xD", "the menu got deleted")
.exec();
});

// Add copy and open links for 1x, 2x, 3x
auto addImageLink = [&](const ImagePtr &image, char scale) {
if (!image->isEmpty())
Expand Down Expand Up @@ -2099,15 +2093,8 @@ void ChannelView::addContextMenuItems(
const MessageLayoutElement *hoveredElement, MessageLayoutPtr layout,
QMouseEvent *event)
{
static QMenu *previousMenu = nullptr;
if (previousMenu != nullptr)
{
previousMenu->deleteLater();
previousMenu = nullptr;
}

auto menu = new QMenu;
previousMenu = menu;
auto *menu = new QMenu(this);
menu->setAttribute(Qt::WA_DeleteOnClose);

// Add image options if the element clicked contains an image (e.g. a badge or an emote)
this->addImageContextMenuItems(hoveredElement, layout, event, *menu);
Expand Down Expand Up @@ -2416,7 +2403,7 @@ void ChannelView::addCommandExecutionContextMenuItems(

menu.addSeparator();
auto *executeAction = menu.addAction("&Execute command");
auto cmdMenu = new QMenu;
auto *cmdMenu = new QMenu(&menu);
executeAction->setMenu(cmdMenu);

for (auto &cmd : cmds)
Expand Down

0 comments on commit c811e2d

Please sign in to comment.