Meson: Use ActionBuilder to set up actions

Change-Id: If84acd6cf89d9375714de703c2aed889bc2cb75b
Reviewed-by: Eike Ziller <eike.ziller@qt.io>
This commit is contained in:
hjk
2024-01-30 09:46:22 +01:00
parent 4cc80b6cd0
commit 4cbdd443fe
3 changed files with 44 additions and 86 deletions

View File

@@ -17,101 +17,57 @@
#include <utils/action.h>
using namespace Core;
using namespace ProjectExplorer;
using namespace Utils;
namespace MesonProjectManager::Internal {
class MesonActionsManager : public QObject
void setupMesonActions(QObject *guard)
{
public:
MesonActionsManager();
Action *buildTargetContextAction = nullptr;
Utils::Action buildTargetContextAction{
Tr::tr("Build"),
Tr::tr("Build \"%1\""),
Utils::Action::AlwaysEnabled /*handled manually*/
};
QAction configureActionMenu;
QAction configureActionContextMenu;
const Context projectContext{Constants::Project::ID};
void configureCurrentProject();
void updateContextActions();
};
ActionBuilder(guard, "MesonProject.Configure")
.setText(Tr::tr("Configure"))
.setContext(projectContext)
.addToContainer(ProjectExplorer::Constants::M_PROJECTCONTEXT,
ProjectExplorer::Constants::G_PROJECT_BUILD)
.addToContainer(ProjectExplorer::Constants::M_SUBPROJECTCONTEXT,
ProjectExplorer::Constants::G_PROJECT_BUILD)
.addOnTriggered(guard, [] {
auto bs = dynamic_cast<MesonBuildSystem *>(ProjectTree::currentBuildSystem());
QTC_ASSERT(bs, return);
if (ProjectExplorerPlugin::saveModifiedFiles())
bs->configure();
});
MesonActionsManager::MesonActionsManager()
: configureActionMenu(Tr::tr("Configure"))
, configureActionContextMenu(Tr::tr("Configure"))
{
const Core::Context globalContext(Core::Constants::C_GLOBAL);
const Core::Context projectContext{Constants::Project::ID};
Core::ActionContainer *mproject = Core::ActionManager::actionContainer(
ProjectExplorer::Constants::M_PROJECTCONTEXT);
Core::ActionContainer *msubproject = Core::ActionManager::actionContainer(
ProjectExplorer::Constants::M_SUBPROJECTCONTEXT);
Core::Command *command;
{
command = Core::ActionManager::registerAction(&configureActionMenu,
"MesonProject.Configure",
projectContext);
mproject->addAction(command, ProjectExplorer::Constants::G_PROJECT_BUILD);
msubproject->addAction(command, ProjectExplorer::Constants::G_PROJECT_BUILD);
connect(&configureActionMenu,
&QAction::triggered,
this,
&MesonActionsManager::configureCurrentProject);
}
{
command = Core::ActionManager::registerAction(&buildTargetContextAction,
"Meson.BuildTargetContextMenu",
projectContext);
command->setAttribute(Core::Command::CA_Hide);
command->setAttribute(Core::Command::CA_UpdateText);
command->setDescription(buildTargetContextAction.text());
Core::ActionManager::actionContainer(ProjectExplorer::Constants::M_SUBPROJECTCONTEXT)
->addAction(command, ProjectExplorer::Constants::G_PROJECT_BUILD);
// Wire up context menu updates:
connect(ProjectExplorer::ProjectTree::instance(),
&ProjectExplorer::ProjectTree::currentNodeChanged,
this,
&MesonActionsManager::updateContextActions);
connect(&buildTargetContextAction, &Utils::Action::triggered, this, [] {
auto bs = qobject_cast<MesonBuildSystem *>(
ProjectExplorer::ProjectTree::currentBuildSystem());
if (bs) {
auto targetNode = dynamic_cast<MesonTargetNode *>(
ProjectExplorer::ProjectTree::currentNode());
ActionBuilder(guard, "Meson.BuildTargetContextMenu")
.setParameterText(Tr::tr("Build \"%1\""), Tr::tr("Build"),
ActionBuilder::AlwaysEnabled /*handled manually*/)
.bindContextAction(&buildTargetContextAction)
.setContext(projectContext)
.setCommandAttribute(Core::Command::CA_Hide)
.setCommandAttribute(Core::Command::CA_UpdateText)
.setCommandDescription(Tr::tr("Build"))
.addToContainer(ProjectExplorer::Constants::M_SUBPROJECTCONTEXT,
ProjectExplorer::Constants::G_PROJECT_BUILD)
.addOnTriggered(guard, [] {
if (qobject_cast<MesonBuildSystem *>(ProjectTree::currentBuildSystem())) {
auto targetNode = dynamic_cast<MesonTargetNode *>(ProjectTree::currentNode());
targetNode->build();
}
});
}
}
void MesonActionsManager::configureCurrentProject()
{
auto bs = dynamic_cast<MesonBuildSystem *>(ProjectExplorer::ProjectTree::currentBuildSystem());
QTC_ASSERT(bs, return );
if (ProjectExplorer::ProjectExplorerPlugin::saveModifiedFiles())
bs->configure();
}
void MesonActionsManager::updateContextActions()
{
auto targetNode = dynamic_cast<const MesonTargetNode *>(
ProjectExplorer::ProjectTree::currentNode());
QObject::connect(ProjectTree::instance(), &ProjectTree::currentNodeChanged, guard, [&] {
auto targetNode = dynamic_cast<const MesonTargetNode *>(ProjectTree::currentNode());
const QString targetDisplayName = targetNode ? targetNode->displayName() : QString();
// Build Target:
buildTargetContextAction.setParameter(targetDisplayName);
buildTargetContextAction.setEnabled(targetNode);
buildTargetContextAction.setVisible(targetNode);
}
void setupMesonActions()
{
static MesonActionsManager theMesonActionsManager;
buildTargetContextAction->setParameter(targetDisplayName);
buildTargetContextAction->setEnabled(targetNode);
buildTargetContextAction->setVisible(targetNode);
});
}
} // MesonProjectManager::Internal

View File

@@ -3,8 +3,10 @@
#pragma once
#include <QObject>
namespace MesonProjectManager::Internal {
void setupMesonActions();
void setupMesonActions(QObject *guard);
} // Internal::MesonProjectManager

View File

@@ -38,7 +38,7 @@ class MesonProjectPlugin final : public ExtensionSystem::IPlugin
setupMesonRunConfiguration();
setupMesonRunAndDebugWorkers();
setupMesonActions();
setupMesonActions(this);
ProjectManager::registerProjectType<MesonProject>(Constants::Project::MIMETYPE);