Allow inserting actions in the middle of Menu.
This commit is contained in:
parent
4d45396174
commit
a5766cb1f6
4 changed files with 36 additions and 18 deletions
|
|
@ -94,21 +94,30 @@ not_null<QAction*> Menu::addAction(
|
|||
}
|
||||
|
||||
not_null<QAction*> Menu::addAction(base::unique_qptr<ItemBase> widget) {
|
||||
const auto action = widget->action();
|
||||
_actions.emplace_back(action);
|
||||
return insertAction(_actions.size(), std::move(widget));
|
||||
}
|
||||
|
||||
widget->setParent(this);
|
||||
not_null<QAction*> Menu::insertAction(
|
||||
int position,
|
||||
base::unique_qptr<ItemBase> widget) {
|
||||
Expects(position >= 0 && position <= _actions.size());
|
||||
Expects(position >= 0 && position <= _actionWidgets.size());
|
||||
|
||||
const auto top = _actionWidgets.empty()
|
||||
? 0
|
||||
: _actionWidgets.back()->y() + _actionWidgets.back()->height();
|
||||
const auto raw = widget.get();
|
||||
const auto action = raw->action();
|
||||
_actions.insert(begin(_actions) + position, action);
|
||||
|
||||
widget->moveToLeft(0, top);
|
||||
widget->show();
|
||||
raw->setParent(this);
|
||||
raw->show();
|
||||
raw->setIndex(position);
|
||||
for (auto i = position, to = int(_actionWidgets.size()); i != to; ++i) {
|
||||
_actionWidgets[i]->setIndex(i + 1);
|
||||
}
|
||||
_actionWidgets.insert(
|
||||
begin(_actionWidgets) + position,
|
||||
std::move(widget));
|
||||
|
||||
widget->setIndex(_actionWidgets.size());
|
||||
|
||||
widget->selects(
|
||||
raw->selects(
|
||||
) | rpl::start_with_next([=](const CallbackData &data) {
|
||||
if (!data.selected) {
|
||||
if (!findSelectedAction()
|
||||
|
|
@ -128,16 +137,16 @@ not_null<QAction*> Menu::addAction(base::unique_qptr<ItemBase> widget) {
|
|||
if (_activatedCallback) {
|
||||
_activatedCallback(data);
|
||||
}
|
||||
}, widget->lifetime());
|
||||
}, raw->lifetime());
|
||||
|
||||
widget->clicks(
|
||||
raw->clicks(
|
||||
) | rpl::start_with_next([=](const CallbackData &data) {
|
||||
if (_triggeredCallback) {
|
||||
_triggeredCallback(data);
|
||||
}
|
||||
}, widget->lifetime());
|
||||
}, raw->lifetime());
|
||||
|
||||
QObject::connect(action.get(), &QAction::changed, widget.get(), [=] {
|
||||
QObject::connect(action.get(), &QAction::changed, raw, [=] {
|
||||
// Select an item under mouse that was disabled and became enabled.
|
||||
if (_lastSelectedByMouse
|
||||
&& !findSelectedAction()
|
||||
|
|
@ -146,9 +155,6 @@ not_null<QAction*> Menu::addAction(base::unique_qptr<ItemBase> widget) {
|
|||
}
|
||||
});
|
||||
|
||||
const auto raw = widget.get();
|
||||
_actionWidgets.push_back(std::move(widget));
|
||||
|
||||
const auto recountWidth = [=] {
|
||||
return _forceWidth
|
||||
? _forceWidth
|
||||
|
|
|
|||
|
|
@ -53,6 +53,9 @@ public:
|
|||
const style::icon *iconOver = nullptr);
|
||||
not_null<QAction*> addSeparator(
|
||||
const style::MenuSeparator *st = nullptr);
|
||||
not_null<QAction*> insertAction(
|
||||
int position,
|
||||
base::unique_qptr<ItemBase> widget);
|
||||
void clearActions();
|
||||
void finishAnimating();
|
||||
|
||||
|
|
|
|||
|
|
@ -407,6 +407,12 @@ not_null<QAction*> PopupMenu::addSeparator(const style::MenuSeparator *st) {
|
|||
return _menu->addSeparator(st);
|
||||
}
|
||||
|
||||
not_null<QAction*> PopupMenu::insertAction(
|
||||
int position,
|
||||
base::unique_qptr<Menu::ItemBase> widget) {
|
||||
return _menu->insertAction(position, std::move(widget));
|
||||
}
|
||||
|
||||
void PopupMenu::clearActions() {
|
||||
_submenus.clear();
|
||||
return _menu->clearActions();
|
||||
|
|
|
|||
|
|
@ -51,6 +51,9 @@ public:
|
|||
const style::icon *iconOver = nullptr);
|
||||
not_null<QAction*> addSeparator(
|
||||
const style::MenuSeparator *st = nullptr);
|
||||
not_null<QAction*> insertAction(
|
||||
int position,
|
||||
base::unique_qptr<Menu::ItemBase> widget);
|
||||
void clearActions();
|
||||
|
||||
[[nodiscard]] const std::vector<not_null<QAction*>> &actions() const;
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue