Skip to content
Open
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
9 changes: 6 additions & 3 deletions src/Wt/WMenu.C
Original file line number Diff line number Diff line change
Expand Up @@ -283,10 +283,13 @@ std::unique_ptr<WMenuItem> WMenu::removeItem(WMenuItem *item)

item->setParentMenu(nullptr);

if (itemIndex <= current_ && current_ >= 0)
if (itemIndex == current_) {
setCurrent(-1);
select(-1, true);
} else if (itemIndex < current_ && current_ > 0) {
--current_;

select(current_, true);
select(current_, true);
}
}

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

// Regression test: removeItem() must clear selection (not select the previous
// item) when the currently-selected item is the one being removed.
BOOST_AUTO_TEST_CASE(WMenu_removeItem_clears_selection_of_current_item)
{
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);

Wt::WMenuItem *item0 = 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"));
Wt::WMenuItem *item2 = menu->addItem("Item 2", std::make_unique<Wt::WText>("Content 2"));

// Select the middle item
menu->select(item1);
BOOST_REQUIRE(menu->currentIndex() == 1);

// Remove the currently-selected item — selection must be cleared, not moved
auto removed = menu->removeItem(item1);
BOOST_TEST(menu->currentIndex() == -1);
BOOST_TEST(menu->currentItem() == nullptr);

// Remaining items must be accessible at correct indices
BOOST_TEST(menu->count() == 2);
BOOST_TEST(menu->itemAt(0) == item0);
BOOST_TEST(menu->itemAt(1) == item2);
}

// Regression test: removeItem() must decrement current_ (not clear) when
// an item *before* the currently-selected item is removed.
BOOST_AUTO_TEST_CASE(WMenu_removeItem_before_current_keeps_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);

Wt::WMenuItem *item0 = 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"));
Wt::WMenuItem *item2 = menu->addItem("Item 2", std::make_unique<Wt::WText>("Content 2"));

// Select item2 (index 2)
menu->select(item2);
BOOST_REQUIRE(menu->currentIndex() == 2);

// Remove item0 (before current) — item2 must remain selected at new index 1
auto removed = menu->removeItem(item0);
BOOST_TEST(menu->currentItem() == item2);
BOOST_TEST(menu->currentIndex() == 1);
}