summaryrefslogtreecommitdiff
path: root/src/config/lua/layout/LuaLayoutTarget.cpp
blob: 9113f0fd5a6d610e1111b4a5dc2951c357d10e57 (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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#include "LuaLayoutTarget.hpp"

#include "LuaLayoutContext.hpp"
#include "../bindings/LuaBindingsInternal.hpp"
#include "../objects/LuaObjectHelpers.hpp"
#include "../objects/LuaWindow.hpp"

#include "../../../layout/target/Target.hpp"

#include <string_view>

using namespace Config::Lua;
using namespace Config::Lua::Layouts;

static constexpr const char* TARGET_MT = "HL.LayoutTarget";

namespace {
    struct SLuaLayoutTargetRef {
        WP<Layout::ITarget> target;
        size_t              index = 0;
    };
}

static int layoutTargetPlace(lua_State* L) {
    auto* ref    = sc<SLuaLayoutTargetRef*>(luaL_checkudata(L, 1, TARGET_MT));
    auto  target = ref->target.lock();
    if (!target)
        return 0;

    CBox box;
    if (!boxFromTable(L, 2, box))
        return Bindings::Internal::configError(L, "HL.LayoutTarget: place expects a box table { x, y, w, h }");

    target->setPositionGlobal(box.noNegativeSize());
    return 0;
}

static int layoutTargetIndex(lua_State* L) {
    auto*                  ref = sc<SLuaLayoutTargetRef*>(luaL_checkudata(L, 1, TARGET_MT));
    const std::string_view key = luaL_checkstring(L, 2);

    auto                   target = ref->target.lock();
    if (!target) {
        lua_pushnil(L);
        return 1;
    }

    if (key == "index")
        lua_pushinteger(L, sc<lua_Integer>(ref->index));
    else if (key == "window") {
        const auto window = target->window();
        if (window)
            Objects::CLuaWindow::push(L, window);
        else
            lua_pushnil(L);
    } else if (key == "box")
        pushBox(L, target->position());
    else if (key == "place" || key == "set_box")
        lua_pushcfunction(L, layoutTargetPlace);
    else
        lua_pushnil(L);

    return 1;
}

static int layoutTargetGc(lua_State* L) {
    sc<SLuaLayoutTargetRef*>(lua_touserdata(L, 1))->~SLuaLayoutTargetRef();
    return 0;
}

static int layoutTargetToString(lua_State* L) {
    auto* ref    = sc<SLuaLayoutTargetRef*>(luaL_checkudata(L, 1, TARGET_MT));
    auto  target = ref->target.lock();
    if (!target)
        lua_pushstring(L, "HL.LayoutTarget(expired)");
    else
        lua_pushfstring(L, "HL.LayoutTarget(%d)", sc<int>(ref->index));
    return 1;
}

void Config::Lua::Layouts::setupLayoutTarget(lua_State* L) {
    luaL_newmetatable(L, TARGET_MT);
    lua_pushcfunction(L, layoutTargetIndex);
    lua_setfield(L, -2, "__index");
    lua_pushcfunction(L, Objects::readOnlyNewIndex);
    lua_setfield(L, -2, "__newindex");
    lua_pushcfunction(L, layoutTargetGc);
    lua_setfield(L, -2, "__gc");
    lua_pushcfunction(L, layoutTargetToString);
    lua_setfield(L, -2, "__tostring");
    lua_pop(L, 1);
}

void Config::Lua::Layouts::pushLayoutTarget(lua_State* L, const SP<Layout::ITarget>& target, size_t index) {
    new (lua_newuserdata(L, sizeof(SLuaLayoutTargetRef))) SLuaLayoutTargetRef{target, index};
    luaL_getmetatable(L, TARGET_MT);
    lua_setmetatable(L, -2);
}