Added ability to set text color of action menu item via QAction.

This commit is contained in:
23rd 2022-03-30 20:02:03 +03:00
parent c035b78f87
commit 8d181e53c5
3 changed files with 20 additions and 2 deletions

View file

@ -105,7 +105,13 @@ void Action::paint(Painter &p) {
if (const auto icon = (selected ? _iconOver : _icon)) { if (const auto icon = (selected ? _iconOver : _icon)) {
icon->paint(p, _st.itemIconPosition, width()); icon->paint(p, _st.itemIconPosition, width());
} }
p.setPen(selected ? _st.itemFgOver : (enabled ? _st.itemFg : _st.itemFgDisabled)); p.setPen(_fgOverride
? QPen(*_fgOverride)
: selected
? _st.itemFgOver
: enabled
? _st.itemFg
: _st.itemFgDisabled);
paintText(p); paintText(p);
if (hasSubmenu()) { if (hasSubmenu()) {
const auto left = width() - _st.itemPadding.right() - _st.arrow.width(); const auto left = width() - _st.itemPadding.right() - _st.arrow.width();
@ -168,6 +174,14 @@ void Action::processAction() {
_shortcut = actionShortcut; _shortcut = actionShortcut;
setMinWidth(w); setMinWidth(w);
update(); update();
// TODO better way.
if (const auto variant = _action->data(); variant.isValid()) {
const auto overrideColor = variant.value<QColor>();
if (overrideColor.isValid()) {
_fgOverride = overrideColor;
}
}
} }
bool Action::isEnabled() const { bool Action::isEnabled() const {

View file

@ -54,6 +54,7 @@ private:
const style::icon *_iconOver; const style::icon *_iconOver;
// std::unique_ptr<ToggleView> _toggle; // std::unique_ptr<ToggleView> _toggle;
int _textWidth = 0; int _textWidth = 0;
std::optional<QColor> _fgOverride;
const int _height; const int _height;
}; };

View file

@ -6,6 +6,8 @@
// //
#include "ui/widgets/menu/menu_common.h" #include "ui/widgets/menu/menu_common.h"
#include "ui/ui_utility.h"
#include <QAction> #include <QAction>
namespace Ui::Menu { namespace Ui::Menu {
@ -14,7 +16,8 @@ not_null<QAction*> CreateAction(
QWidget *parent, QWidget *parent,
const QString &text, const QString &text,
Fn<void()> &&callback) { Fn<void()> &&callback) {
const auto action = new QAction(text, parent); const auto action = Ui::CreateChild<QAction>(parent);
action->setText(text);
parent->connect( parent->connect(
action, action,
&QAction::triggered, &QAction::triggered,