Skip to content
Closed
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
7 changes: 7 additions & 0 deletions src/Wt/WMenuItem.C
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,10 @@ void WMenuItem::setContents(std::unique_ptr<WWidget> contents,
int menuIdx = -1;
WMenu *menu = menu_;
std::unique_ptr<WMenuItem> self;
bool wasCurrent = false;
if (menu) {
menuIdx = menu->indexOf(this);
wasCurrent = menu->currentItem() == this;
self = menu->removeItem(this);
}

Expand All @@ -133,6 +135,11 @@ void WMenuItem::setContents(std::unique_ptr<WWidget> contents,

if (menu) {
menu->insertItem(menuIdx, std::move(self));
if (wasCurrent) {
// Restore the selection without re-emitting triggered() / itemSelected()
menu->setCurrent(menuIdx);
menu->select(menuIdx, true);
}
}
}

Expand Down
23 changes: 23 additions & 0 deletions test/widgets/WMenuTest.C
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,26 @@ BOOST_AUTO_TEST_CASE(WMenu_addItem_change_index_for_internal_path_match)
BOOST_TEST(menu->currentIndex() == previousIndex);
}

// Regression test: setContents() on the currently-selected item must not
// change the active selection.
BOOST_AUTO_TEST_CASE(WMenuItem_setContents_keeps_current_selection)
{
Wt::Test::WTestEnvironment testEnv;
Wt::WApplication app(testEnv);

Wt::WStackedWidget *stack = app.root()->addNew<Wt::WStackedWidget>();
Wt::WMenu *menu = app.root()->addNew<Wt::WMenu>(stack);

menu->addItem("Item 0", std::make_unique<Wt::WText>("Content 0"));
Wt::WMenuItem *item1 = menu->addItem("Item 1", std::make_unique<Wt::WText>("Content 1"));
menu->addItem("Item 2", std::make_unique<Wt::WText>("Content 2"));

menu->select(item1);
BOOST_REQUIRE(menu->currentIndex() == 1);

item1->setContents(std::make_unique<Wt::WText>("New content 1"));

BOOST_TEST(menu->currentItem() == item1);
BOOST_TEST(menu->currentIndex() == 1);
}