Added ability to set index to menu item.

This commit is contained in:
23rd 2021-01-13 18:04:05 +03:00
parent df0a8ff589
commit 497452250a
7 changed files with 14 additions and 13 deletions

View file

@ -86,7 +86,6 @@ not_null<QAction*> Menu::addAction(
auto item = base::make_unique_q<Action>( auto item = base::make_unique_q<Action>(
this, this,
_st, _st,
0,
std::move(action), std::move(action),
icon, icon,
iconOver ? iconOver : icon, iconOver ? iconOver : icon,
@ -105,6 +104,8 @@ not_null<QAction*> Menu::addAction(base::unique_qptr<ItemBase> widget) {
widget->moveToLeft(0, top); widget->moveToLeft(0, top);
widget->show(); widget->show();
widget->setIndex(_actionWidgets.size());
widget->selects( widget->selects(
) | rpl::start_with_next([=](const CallbackData &data) { ) | rpl::start_with_next([=](const CallbackData &data) {
if (!data.selected) { if (!data.selected) {
@ -156,7 +157,7 @@ not_null<QAction*> Menu::addAction(base::unique_qptr<ItemBase> widget) {
not_null<QAction*> Menu::addSeparator() { not_null<QAction*> Menu::addSeparator() {
const auto separator = new QAction(this); const auto separator = new QAction(this);
separator->setSeparator(true); separator->setSeparator(true);
auto item = base::make_unique_q<Separator>(this, _st, 0, separator); auto item = base::make_unique_q<Separator>(this, _st, separator);
return addAction(std::move(item)); return addAction(std::move(item));
} }

View file

@ -49,12 +49,11 @@ TextParseOptions MenuTextOptions = {
Action::Action( Action::Action(
not_null<RpWidget*> parent, not_null<RpWidget*> parent,
const style::Menu &st, const style::Menu &st,
int index,
not_null<QAction*> action, not_null<QAction*> action,
const style::icon *icon, const style::icon *icon,
const style::icon *iconOver, const style::icon *iconOver,
bool hasSubmenu) bool hasSubmenu)
: ItemBase(parent, st, index) : ItemBase(parent, st)
, _action(action) , _action(action)
, _st(st) , _st(st)
, _icon(icon) , _icon(icon)

View file

@ -19,7 +19,6 @@ public:
Action( Action(
not_null<RpWidget*> parent, not_null<RpWidget*> parent,
const style::Menu &st, const style::Menu &st,
int index,
not_null<QAction*> action, not_null<QAction*> action,
const style::icon *icon, const style::icon *icon,
const style::icon *iconOver, const style::icon *iconOver,

View file

@ -10,9 +10,8 @@ namespace Ui::Menu {
ItemBase::ItemBase( ItemBase::ItemBase(
not_null<RpWidget*> parent, not_null<RpWidget*> parent,
const style::Menu &st, int index) const style::Menu &st)
: RippleButton(parent, st.ripple) : RippleButton(parent, st.ripple) {
, _index(index) {
init(); init();
} }
@ -48,6 +47,10 @@ int ItemBase::index() const {
return _index; return _index;
} }
void ItemBase::setIndex(int index) {
_index = index;
}
void ItemBase::setClicked(TriggeredSource source) { void ItemBase::setClicked(TriggeredSource source) {
if (isEnabled()) { if (isEnabled()) {
_lastTriggeredSource = source; _lastTriggeredSource = source;

View file

@ -15,7 +15,7 @@ namespace Ui::Menu {
class ItemBase : public RippleButton { class ItemBase : public RippleButton {
public: public:
ItemBase(not_null<RpWidget*> parent, const style::Menu &st, int index); ItemBase(not_null<RpWidget*> parent, const style::Menu &st);
TriggeredSource lastTriggeredSource() const; TriggeredSource lastTriggeredSource() const;
@ -26,6 +26,7 @@ public:
bool isSelected() const; bool isSelected() const;
int index() const; int index() const;
void setIndex(int index);
void setClicked(TriggeredSource source = TriggeredSource::Mouse); void setClicked(TriggeredSource source = TriggeredSource::Mouse);
@ -48,7 +49,7 @@ protected:
virtual int contentHeight() const = 0; virtual int contentHeight() const = 0;
private: private:
const int _index; int _index = -1;
bool _hasSubmenu = false; bool _hasSubmenu = false;

View file

@ -13,9 +13,8 @@ namespace Ui::Menu {
Separator::Separator( Separator::Separator(
not_null<RpWidget*> parent, not_null<RpWidget*> parent,
const style::Menu &st, const style::Menu &st,
int index,
not_null<QAction*> action) not_null<QAction*> action)
: ItemBase(parent, st, index) : ItemBase(parent, st)
, _lineWidth(st.separatorWidth) , _lineWidth(st.separatorWidth)
, _padding(st.separatorPadding) , _padding(st.separatorPadding)
, _fg(st.separatorFg) , _fg(st.separatorFg)

View file

@ -18,7 +18,6 @@ public:
Separator( Separator(
not_null<RpWidget*> parent, not_null<RpWidget*> parent,
const style::Menu &st, const style::Menu &st,
int index,
not_null<QAction*> action); not_null<QAction*> action);
QAction *action() const override; QAction *action() const override;