aboutsummaryrefslogtreecommitdiffstats
path: root/src/plugins/lua/bindings/menu.cpp
blob: af5c86fcaf7049c55ec791e58eaed3662b75a041 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
// Copyright (C) 2024 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0

#include "../luaengine.h"

#include "utils.h"

#include <coreplugin/actionmanager/actionmanager.h>
#include <coreplugin/modemanager.h>

using namespace Utils;
using namespace Core;

namespace Lua::Internal {

void setupMenuModule()
{
    registerProvider("Menu", [](sol::state_view lua) -> sol::object {
        sol::table result = lua.create_table();

        mirrorEnum(result, QMetaEnum::fromType<ActionContainer::OnAllDisabledBehavior>());

        result["create"] = [](const std::string &menuId, const sol::table &options) {
            MenuBuilder builder(Id::fromString(QString::fromStdString(menuId)));

            for (const auto &[k, v] : options) {
                const QString key = QString::fromStdString(k.as<std::string>());
                if (key == "title") {
                    builder.setTitle(v.as<QString>());
                } else if (key == "icon") {
                    builder.setIcon(toIcon(v.as<IconFilePathOrString>())->icon());
                } else if (key == "onAllDisabledBehavior") {
                    int behavior = v.as<int>();
                    builder.setOnAllDisabledBehavior(
                        static_cast<ActionContainer::OnAllDisabledBehavior>(behavior));
                } else if (key == "containers") {
                    v.as<sol::table>().for_each([&builder](sol::object, sol::object value) {
                        if (value.is<sol::table>()) {
                            const sol::table t = value.as<sol::table>();
                            const auto containerId = t.get<std::string>("containerId");
                            const auto groupId = t.get_or<std::string>("groupId", {});
                            builder.addToContainer(
                                Id::fromString(QString::fromStdString(containerId)),
                                Id::fromString(QString::fromStdString(groupId)));
                        } else if (value.is<QString>()) {
                            builder.addToContainer(Id::fromString(value.as<QString>()));
                        }
                    });
                } else {
                    throw std::runtime_error("Unknown key: " + key.toStdString());
                }
            }
        };

        result["addSeparator"] = [](const std::string &menuId) {
            ActionContainer *container = ActionManager::actionContainer(
                Id::fromString(QString::fromStdString(menuId)));
            if (!container)
                throw std::runtime_error("Menu not found: " + menuId);
            container->addSeparator();
        };

        return result;
    });
}

} // namespace Lua::Internal