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> #include <utils/action.h>
using namespace Core;
using namespace ProjectExplorer;
using namespace Utils;
namespace MesonProjectManager::Internal { namespace MesonProjectManager::Internal {
class MesonActionsManager : public QObject void setupMesonActions(QObject *guard)
{ {
public: Action *buildTargetContextAction = nullptr;
MesonActionsManager();
Utils::Action buildTargetContextAction{ const Context projectContext{Constants::Project::ID};
Tr::tr("Build"),
Tr::tr("Build \"%1\""),
Utils::Action::AlwaysEnabled /*handled manually*/
};
QAction configureActionMenu;
QAction configureActionContextMenu;
void configureCurrentProject(); ActionBuilder(guard, "MesonProject.Configure")
void updateContextActions(); .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() ActionBuilder(guard, "Meson.BuildTargetContextMenu")
: configureActionMenu(Tr::tr("Configure")) .setParameterText(Tr::tr("Build \"%1\""), Tr::tr("Build"),
, configureActionContextMenu(Tr::tr("Configure")) ActionBuilder::AlwaysEnabled /*handled manually*/)
{ .bindContextAction(&buildTargetContextAction)
const Core::Context globalContext(Core::Constants::C_GLOBAL); .setContext(projectContext)
const Core::Context projectContext{Constants::Project::ID}; .setCommandAttribute(Core::Command::CA_Hide)
Core::ActionContainer *mproject = Core::ActionManager::actionContainer( .setCommandAttribute(Core::Command::CA_UpdateText)
ProjectExplorer::Constants::M_PROJECTCONTEXT); .setCommandDescription(Tr::tr("Build"))
Core::ActionContainer *msubproject = Core::ActionManager::actionContainer( .addToContainer(ProjectExplorer::Constants::M_SUBPROJECTCONTEXT,
ProjectExplorer::Constants::M_SUBPROJECTCONTEXT); ProjectExplorer::Constants::G_PROJECT_BUILD)
.addOnTriggered(guard, [] {
Core::Command *command; if (qobject_cast<MesonBuildSystem *>(ProjectTree::currentBuildSystem())) {
auto targetNode = dynamic_cast<MesonTargetNode *>(ProjectTree::currentNode());
{
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());
targetNode->build(); targetNode->build();
} }
}); });
}
}
void MesonActionsManager::configureCurrentProject() QObject::connect(ProjectTree::instance(), &ProjectTree::currentNodeChanged, guard, [&] {
{ auto targetNode = dynamic_cast<const MesonTargetNode *>(ProjectTree::currentNode());
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());
const QString targetDisplayName = targetNode ? targetNode->displayName() : QString(); const QString targetDisplayName = targetNode ? targetNode->displayName() : QString();
// Build Target: buildTargetContextAction->setParameter(targetDisplayName);
buildTargetContextAction.setParameter(targetDisplayName); buildTargetContextAction->setEnabled(targetNode);
buildTargetContextAction.setEnabled(targetNode); buildTargetContextAction->setVisible(targetNode);
buildTargetContextAction.setVisible(targetNode); });
}
void setupMesonActions()
{
static MesonActionsManager theMesonActionsManager;
} }
} // MesonProjectManager::Internal } // MesonProjectManager::Internal

View File

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

View File

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