summaryrefslogtreecommitdiffstats
path: root/lua-bindings/diaphragm.lua
diff options
context:
space:
mode:
authorMinijackson <minijackson@riseup.net>2023-01-24 22:40:41 +0100
committerMinijackson <minijackson@riseup.net>2023-01-24 22:40:41 +0100
commit24a9ce31acf489b56682efdefed15c10dd4c308a (patch)
tree2657be06919e872b1d1dbab69271deb7219a79f8 /lua-bindings/diaphragm.lua
parent8378d14477e0c79129b36a98e349d27091a2054a (diff)
downloaddiaphragm-24a9ce31acf489b56682efdefed15c10dd4c308a.tar.gz
diaphragm-24a9ce31acf489b56682efdefed15c10dd4c308a.zip
lua-bindings: assigned reserved attr depending on type of object
Diffstat (limited to 'lua-bindings/diaphragm.lua')
-rw-r--r--lua-bindings/diaphragm.lua93
1 files changed, 52 insertions, 41 deletions
diff --git a/lua-bindings/diaphragm.lua b/lua-bindings/diaphragm.lua
index 57f065f..8632e51 100644
--- a/lua-bindings/diaphragm.lua
+++ b/lua-bindings/diaphragm.lua
@@ -72,39 +72,6 @@ function M.util.tbl_contains(t, value)
72 return false 72 return false
73end 73end
74 74
75-- TODO: depending on objects
76function M.util.is_reserved(s)
77 return M.util.tbl_contains({
78 "top",
79 "left",
80 "width",
81 "height",
82 "right",
83 "bottom",
84 "vert_center",
85 "horiz_center",
86
87 "top_left",
88 "top_right",
89 "bottom_left",
90 "bottom_right",
91 "middle_left",
92 "middle_right",
93 "top_middle",
94 "bottom_middle",
95 "center",
96
97 "stroke_color",
98 "stroke_width",
99 "fill_color",
100
101 -- "x",
102 -- "y",
103 --
104 "size",
105 }, s)
106end
107
108function M.util.tbl_filter_by_key(func, t) 75function M.util.tbl_filter_by_key(func, t)
109 local result = {} 76 local result = {}
110 for k, v in pairs(t) do 77 for k, v in pairs(t) do
@@ -121,9 +88,39 @@ function M.util.tbl_filter_reserved(t)
121 end, t) 88 end, t)
122end 89end
123 90
124function M.util.tbl_assign_reserved(t, rhs) 91local bounds_reserved = {
92 "top",
93 "bottom",
94 "left",
95 "right",
96 "width",
97 "height",
98 "vert_center",
99 "horiz_center",
100
101 "top_left",
102 "top_right",
103 "bottom_left",
104 "bottom_right",
105 "middle_left",
106 "middle_right",
107 "top_middle",
108 "bottom_middle",
109 "center",
110}
111
112local shape_reserved = {
113 "stroke_color",
114 "stroke_width",
115 "fill_color",
116}
117
118local rectangle_reserved = M.util.tbl_extend(bounds_reserved, shape_reserved)
119local text_reserved = M.util.tbl_extend(bounds_reserved, { "size" })
120
121function M.util.tbl_assign_reserved(t, rhs, whitelist)
125 for k, v in pairs(t) do 122 for k, v in pairs(t) do
126 if M.util.is_reserved(k) then 123 if M.util.tbl_contains(whitelist, k) then
127 rhs[k] = v 124 rhs[k] = v
128 end 125 end
129 end 126 end
@@ -159,7 +156,7 @@ M.text.font = M.core.font
159function M.text.new(params) 156function M.text.new(params)
160 params = params or {} 157 params = params or {}
161 local result = M.core.text(params) 158 local result = M.core.text(params)
162 M.util.tbl_assign_reserved(params, result) 159 M.util.tbl_assign_reserved(params, result, text_reserved)
163 return result 160 return result
164end 161end
165 162
@@ -168,7 +165,7 @@ M.rectangle = {}
168function M.rectangle.new(params) 165function M.rectangle.new(params)
169 params = params or {} 166 params = params or {}
170 local result = M.core.rectangle() 167 local result = M.core.rectangle()
171 M.util.tbl_assign_reserved(params, result) 168 M.util.tbl_assign_reserved(params, result, rectangle_reserved)
172 return result 169 return result
173end 170end
174 171
@@ -183,23 +180,37 @@ M.straight_path = {}
183function M.straight_path.new(params) 180function M.straight_path.new(params)
184 params = params or {} 181 params = params or {}
185 local result = M.core.straight_path(params) 182 local result = M.core.straight_path(params)
186 M.util.tbl_assign_reserved(params, result) 183 M.util.tbl_assign_reserved(params, result, rectangle_reserved)
187 return result 184 return result
188end 185end
189 186
190function M.shape(params) 187function M.shape(params)
191 params = params or {} 188 params = params or {}
192 local result = M.util.tbl_filter_reserved(params) 189
190 local function is_reserved(k)
191 return M.util.tbl_contains(bounds_reserved, k)
192 end
193
194 local result = M.util.tbl_filter_by_key(function(k)
195 return not is_reserved(k)
196 end, params)
193 197
194 local shape = M.core.complex_shape() 198 local shape = M.core.complex_shape()
195 setmetatable(result, { 199 setmetatable(result, {
196 __index = function(_, k) 200 __index = function(_, k)
197 if M.util.is_reserved(k) then 201 if is_reserved(k) then
198 return shape[k] 202 return shape[k]
199 end 203 end
200 end, 204 end,
205 __newindex = function(self, k, v)
206 if is_reserved(k) then
207 shape[k] = v
208 else
209 rawset(self, k, v)
210 end
211 end,
201 }) 212 })
202 M.util.tbl_assign_reserved(params, shape) 213 M.util.tbl_assign_reserved(params, shape, bounds_reserved)
203 214
204 return result 215 return result
205end 216end