aboutsummaryrefslogtreecommitdiff
path: root/launcher/ui/widgets/WideBar.cpp
diff options
context:
space:
mode:
authorflow <flowlnlnln@gmail.com>2022-11-19 13:39:43 -0300
committerflow <flowlnlnln@gmail.com>2022-11-19 18:41:30 -0300
commit6e1639551bbe98b32e9abef2d816e8abe01789e4 (patch)
tree85c22fa4df754a866f10de21d4f16c90dd060b28 /launcher/ui/widgets/WideBar.cpp
parent2367903ac6c6f6778935ed1bbab88fd8342dffa0 (diff)
downloadPrismLauncher-6e1639551bbe98b32e9abef2d816e8abe01789e4.tar.gz
PrismLauncher-6e1639551bbe98b32e9abef2d816e8abe01789e4.tar.bz2
PrismLauncher-6e1639551bbe98b32e9abef2d816e8abe01789e4.zip
feat(WideBar): allow hiding buttons with right-click
Signed-off-by: flow <flowlnlnln@gmail.com>
Diffstat (limited to 'launcher/ui/widgets/WideBar.cpp')
-rw-r--r--launcher/ui/widgets/WideBar.cpp58
1 files changed, 57 insertions, 1 deletions
diff --git a/launcher/ui/widgets/WideBar.cpp b/launcher/ui/widgets/WideBar.cpp
index ed5c5bc8..ed7dc5fa 100644
--- a/launcher/ui/widgets/WideBar.cpp
+++ b/launcher/ui/widgets/WideBar.cpp
@@ -1,4 +1,6 @@
#include "WideBar.h"
+
+#include <QContextMenuEvent>
#include <QToolButton>
class ActionButton : public QToolButton {
@@ -13,7 +15,7 @@ class ActionButton : public QToolButton {
actionChanged();
};
- private slots:
+ public slots:
void actionChanged()
{
setEnabled(m_action->isEnabled());
@@ -34,12 +36,16 @@ WideBar::WideBar(const QString& title, QWidget* parent) : QToolBar(title, parent
{
setFloatable(false);
setMovable(false);
+
+ m_bar_menu = std::make_unique<QMenu>(this);
}
WideBar::WideBar(QWidget* parent) : QToolBar(parent)
{
setFloatable(false);
setMovable(false);
+
+ m_bar_menu = std::make_unique<QMenu>(this);
}
void WideBar::addAction(QAction* action)
@@ -50,6 +56,8 @@ void WideBar::addAction(QAction* action)
entry.type = BarEntry::Type::Action;
m_entries.push_back(entry);
+
+ m_menu_state = MenuState::Dirty;
}
void WideBar::addSeparator()
@@ -80,6 +88,8 @@ void WideBar::insertActionBefore(QAction* before, QAction* action)
entry.type = BarEntry::Type::Action;
m_entries.insert(iter, entry);
+
+ m_menu_state = MenuState::Dirty;
}
void WideBar::insertActionAfter(QAction* after, QAction* action)
@@ -94,6 +104,8 @@ void WideBar::insertActionAfter(QAction* after, QAction* action)
entry.type = BarEntry::Type::Action;
m_entries.insert(iter + 1, entry);
+
+ m_menu_state = MenuState::Dirty;
}
void WideBar::insertSpacer(QAction* action)
@@ -144,4 +156,48 @@ QMenu* WideBar::createContextMenu(QWidget* parent, const QString& title)
return contextMenu;
}
+static void copyAction(QAction* from, QAction* to)
+{
+ Q_ASSERT(from);
+ Q_ASSERT(to);
+
+ to->setText(from->text());
+ to->setIcon(from->icon());
+ to->setToolTip(from->toolTip());
+}
+
+void WideBar::contextMenuEvent(QContextMenuEvent* event)
+{
+ if (m_menu_state == MenuState::Dirty) {
+ for (auto* old_action : m_bar_menu->actions())
+ old_action->deleteLater();
+
+ m_bar_menu->clear();
+
+ for (auto& entry : m_entries) {
+ if (entry.type != BarEntry::Type::Action)
+ continue;
+
+ auto act = new QAction();
+ copyAction(entry.menu_action, act);
+
+ act->setCheckable(true);
+ act->setChecked(entry.bar_action->isVisible());
+
+ connect(act, &QAction::toggled, entry.bar_action, [this, &entry](bool toggled){
+ entry.bar_action->setVisible(toggled);
+
+ // NOTE: This is needed so that disabled actions get reflected on the button when it is made visible.
+ static_cast<ActionButton*>(widgetForAction(entry.bar_action))->actionChanged();
+ });
+
+ m_bar_menu->addAction(act);
+ }
+
+ m_menu_state = MenuState::Fresh;
+ }
+
+ m_bar_menu->popup(event->globalPos());
+}
+
#include "WideBar.moc"