Slightly refactored creating new items in menu.
This commit is contained in:
parent
98fa99ae42
commit
df0a8ff589
4 changed files with 95 additions and 63 deletions
|
|
@ -53,80 +53,94 @@ void Menu::init() {
|
||||||
}, lifetime());
|
}, lifetime());
|
||||||
}
|
}
|
||||||
|
|
||||||
not_null<QAction*> Menu::addAction(const QString &text, Fn<void()> callback, const style::icon *icon, const style::icon *iconOver) {
|
not_null<QAction*> Menu::addAction(
|
||||||
|
const QString &text,
|
||||||
|
Fn<void()> callback,
|
||||||
|
const style::icon *icon,
|
||||||
|
const style::icon *iconOver) {
|
||||||
const auto action = addAction(new QAction(text, this), icon, iconOver);
|
const auto action = addAction(new QAction(text, this), icon, iconOver);
|
||||||
connect(action, &QAction::triggered, action, std::move(callback), Qt::QueuedConnection);
|
connect(
|
||||||
|
action,
|
||||||
|
&QAction::triggered,
|
||||||
|
action,
|
||||||
|
std::move(callback),
|
||||||
|
Qt::QueuedConnection);
|
||||||
return action;
|
return action;
|
||||||
}
|
}
|
||||||
|
|
||||||
not_null<QAction*> Menu::addAction(const QString &text, std::unique_ptr<QMenu> submenu) {
|
not_null<QAction*> Menu::addAction(
|
||||||
|
const QString &text,
|
||||||
|
std::unique_ptr<QMenu> submenu) {
|
||||||
const auto action = new QAction(text, this);
|
const auto action = new QAction(text, this);
|
||||||
action->setMenu(submenu.release());
|
action->setMenu(submenu.release());
|
||||||
return addAction(action, nullptr, nullptr);
|
return addAction(action, nullptr, nullptr);
|
||||||
}
|
}
|
||||||
|
|
||||||
not_null<QAction*> Menu::addAction(not_null<QAction*> action, const style::icon *icon, const style::icon *iconOver) {
|
not_null<QAction*> Menu::addAction(
|
||||||
|
not_null<QAction*> action,
|
||||||
|
const style::icon *icon,
|
||||||
|
const style::icon *iconOver) {
|
||||||
|
if (action->isSeparator()) {
|
||||||
|
return addSeparator();
|
||||||
|
}
|
||||||
|
auto item = base::make_unique_q<Action>(
|
||||||
|
this,
|
||||||
|
_st,
|
||||||
|
0,
|
||||||
|
std::move(action),
|
||||||
|
icon,
|
||||||
|
iconOver ? iconOver : icon,
|
||||||
|
(action->menu() != nullptr));
|
||||||
|
return addAction(std::move(item));
|
||||||
|
}
|
||||||
|
|
||||||
|
not_null<QAction*> Menu::addAction(base::unique_qptr<ItemBase> widget) {
|
||||||
|
const auto action = widget->action();
|
||||||
_actions.emplace_back(action);
|
_actions.emplace_back(action);
|
||||||
|
|
||||||
const auto top = _actionWidgets.empty()
|
const auto top = _actionWidgets.empty()
|
||||||
? 0
|
? 0
|
||||||
: _actionWidgets.back()->y() + _actionWidgets.back()->height();
|
: _actionWidgets.back()->y() + _actionWidgets.back()->height();
|
||||||
const auto index = _actionWidgets.size();
|
|
||||||
if (action->isSeparator()) {
|
|
||||||
auto widget = base::make_unique_q<Separator>(this, _st, index);
|
|
||||||
widget->moveToLeft(0, top);
|
|
||||||
widget->show();
|
|
||||||
_actionWidgets.push_back(std::move(widget));
|
|
||||||
} else {
|
|
||||||
auto widget = base::make_unique_q<Action>(
|
|
||||||
this,
|
|
||||||
_st,
|
|
||||||
index,
|
|
||||||
action,
|
|
||||||
icon,
|
|
||||||
iconOver ? iconOver : icon,
|
|
||||||
(action->menu() != nullptr));
|
|
||||||
widget->moveToLeft(0, top);
|
|
||||||
widget->show();
|
|
||||||
|
|
||||||
widget->selects(
|
widget->moveToLeft(0, top);
|
||||||
) | rpl::start_with_next([=](const CallbackData &data) {
|
widget->show();
|
||||||
if (!data.selected) {
|
|
||||||
return;
|
widget->selects(
|
||||||
|
) | rpl::start_with_next([=](const CallbackData &data) {
|
||||||
|
if (!data.selected) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
for (auto i = 0; i < _actionWidgets.size(); i++) {
|
||||||
|
if (i != data.index) {
|
||||||
|
_actionWidgets[i]->setSelected(false);
|
||||||
}
|
}
|
||||||
for (auto i = 0; i < _actionWidgets.size(); i++) {
|
}
|
||||||
if (i != data.index) {
|
if (_activatedCallback) {
|
||||||
_actionWidgets[i]->setSelected(false);
|
_activatedCallback(data);
|
||||||
}
|
}
|
||||||
}
|
}, widget->lifetime());
|
||||||
if (_activatedCallback) {
|
|
||||||
_activatedCallback(data);
|
|
||||||
}
|
|
||||||
}, widget->lifetime());
|
|
||||||
|
|
||||||
widget->clicks(
|
widget->clicks(
|
||||||
) | rpl::start_with_next([=](const CallbackData &data) {
|
) | rpl::start_with_next([=](const CallbackData &data) {
|
||||||
if (_triggeredCallback) {
|
if (_triggeredCallback) {
|
||||||
_triggeredCallback(data);
|
_triggeredCallback(data);
|
||||||
}
|
}
|
||||||
}, widget->lifetime());
|
}, widget->lifetime());
|
||||||
|
|
||||||
widget->contentWidthValue(
|
widget->contentWidthValue(
|
||||||
) | rpl::start_with_next([=] {
|
) | rpl::start_with_next([=] {
|
||||||
const auto newWidth = _forceWidth
|
const auto newWidth = _forceWidth
|
||||||
? _forceWidth
|
? _forceWidth
|
||||||
: _actionWidgets.empty()
|
: _actionWidgets.empty()
|
||||||
? _st.widthMin
|
? _st.widthMin
|
||||||
: (*ranges::max_element(
|
: (*ranges::max_element(
|
||||||
_actionWidgets,
|
_actionWidgets,
|
||||||
std::greater<>(),
|
std::greater<>(),
|
||||||
&ItemBase::width))->contentWidth();
|
&ItemBase::width))->contentWidth();
|
||||||
resize(newWidth, height());
|
resize(newWidth, height());
|
||||||
}, widget->lifetime());
|
}, widget->lifetime());
|
||||||
|
|
||||||
_actionWidgets.push_back(std::move(widget));
|
|
||||||
}
|
|
||||||
|
|
||||||
|
_actionWidgets.push_back(std::move(widget));
|
||||||
|
|
||||||
const auto newHeight = ranges::accumulate(
|
const auto newHeight = ranges::accumulate(
|
||||||
_actionWidgets,
|
_actionWidgets,
|
||||||
|
|
@ -142,7 +156,8 @@ not_null<QAction*> Menu::addAction(not_null<QAction*> action, const style::icon
|
||||||
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);
|
||||||
return addAction(separator);
|
auto item = base::make_unique_q<Separator>(this, _st, 0, separator);
|
||||||
|
return addAction(std::move(item));
|
||||||
}
|
}
|
||||||
|
|
||||||
void Menu::clearActions() {
|
void Menu::clearActions() {
|
||||||
|
|
|
||||||
|
|
@ -25,8 +25,14 @@ public:
|
||||||
Menu(QWidget *parent, QMenu *menu, const style::Menu &st = st::defaultMenu);
|
Menu(QWidget *parent, QMenu *menu, const style::Menu &st = st::defaultMenu);
|
||||||
~Menu();
|
~Menu();
|
||||||
|
|
||||||
not_null<QAction*> addAction(const QString &text, Fn<void()> callback, const style::icon *icon = nullptr, const style::icon *iconOver = nullptr);
|
not_null<QAction*> addAction(
|
||||||
not_null<QAction*> addAction(const QString &text, std::unique_ptr<QMenu> submenu);
|
const QString &text,
|
||||||
|
Fn<void()> callback,
|
||||||
|
const style::icon *icon = nullptr,
|
||||||
|
const style::icon *iconOver = nullptr);
|
||||||
|
not_null<QAction*> addAction(
|
||||||
|
const QString &text,
|
||||||
|
std::unique_ptr<QMenu> submenu);
|
||||||
not_null<QAction*> addSeparator();
|
not_null<QAction*> addSeparator();
|
||||||
void clearActions();
|
void clearActions();
|
||||||
void finishAnimating();
|
void finishAnimating();
|
||||||
|
|
@ -80,7 +86,11 @@ private:
|
||||||
void updateSelected(QPoint globalPosition);
|
void updateSelected(QPoint globalPosition);
|
||||||
void init();
|
void init();
|
||||||
|
|
||||||
not_null<QAction*> addAction(not_null<QAction*> action, const style::icon *icon = nullptr, const style::icon *iconOver = nullptr);
|
not_null<QAction*> addAction(
|
||||||
|
not_null<QAction*> action,
|
||||||
|
const style::icon *icon = nullptr,
|
||||||
|
const style::icon *iconOver = nullptr);
|
||||||
|
not_null<QAction*> addAction(base::unique_qptr<ItemBase> widget);
|
||||||
|
|
||||||
void setSelected(int selected);
|
void setSelected(int selected);
|
||||||
void clearMouseSelection();
|
void clearMouseSelection();
|
||||||
|
|
|
||||||
|
|
@ -13,13 +13,15 @@ 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)
|
int index,
|
||||||
|
not_null<QAction*> action)
|
||||||
: ItemBase(parent, st, index)
|
: ItemBase(parent, st, index)
|
||||||
, _lineWidth(st.separatorWidth)
|
, _lineWidth(st.separatorWidth)
|
||||||
, _padding(st.separatorPadding)
|
, _padding(st.separatorPadding)
|
||||||
, _fg(st.separatorFg)
|
, _fg(st.separatorFg)
|
||||||
, _bg(st.itemBg)
|
, _bg(st.itemBg)
|
||||||
, _height(_padding.top() + _lineWidth + _padding.bottom()) {
|
, _height(_padding.top() + _lineWidth + _padding.bottom())
|
||||||
|
, _action(action) {
|
||||||
|
|
||||||
initResizeHook(parent->sizeValue());
|
initResizeHook(parent->sizeValue());
|
||||||
paintRequest(
|
paintRequest(
|
||||||
|
|
@ -37,7 +39,7 @@ Separator::Separator(
|
||||||
}
|
}
|
||||||
|
|
||||||
QAction *Separator::action() const {
|
QAction *Separator::action() const {
|
||||||
return nullptr;
|
return _action;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool Separator::isEnabled() const {
|
bool Separator::isEnabled() const {
|
||||||
|
|
|
||||||
|
|
@ -15,7 +15,11 @@ namespace Ui::Menu {
|
||||||
|
|
||||||
class Separator : public ItemBase {
|
class Separator : public ItemBase {
|
||||||
public:
|
public:
|
||||||
Separator(not_null<RpWidget*> parent, const style::Menu &st, int index);
|
Separator(
|
||||||
|
not_null<RpWidget*> parent,
|
||||||
|
const style::Menu &st,
|
||||||
|
int index,
|
||||||
|
not_null<QAction*> action);
|
||||||
|
|
||||||
QAction *action() const override;
|
QAction *action() const override;
|
||||||
bool isEnabled() const override;
|
bool isEnabled() const override;
|
||||||
|
|
@ -29,6 +33,7 @@ private:
|
||||||
const style::color &_fg;
|
const style::color &_fg;
|
||||||
const style::color &_bg;
|
const style::color &_bg;
|
||||||
const int _height;
|
const int _height;
|
||||||
|
const not_null<QAction*> _action;
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue