helium/docs/core/Element.md
Elmārs Āboliņš f85b19c118 Documentation
2021-06-22 14:12:19 +03:00

1.7 KiB

Element

The element is helium's basic building block, in practice it consists of 2 nested functions, the basic pattern looks like this

element = function()
	return function()

	end
end

So, what's what?

Well this outer part is what i call the loader function, it runs once per created element

element = function()

end

It's where you'll set up variables for later, callbacks and state

element = function()
	local x = 'blah'
end

Of course, the variables in this first function become attached to the 'closure'

element = function()
	local x = 'blah'
	return function()
		--so you can use X in here
		print(x)
	end
end

So now we have a function, but this isn't an element yet, to create an element you need to import helium and call the function with helium

local helium = require('helium')

local elementFactory = helium(function()
	return function()

	end
end)

This returns a new 'factory', that is a function that will create a new element when called:

	local element = elementFactory(params, width, height, flags)

Param is an arbitrary table that you can pass in for the element to use, width and height are the 'minimum' dimensions of the new element and flags is an arbitrary table of flags, currently only used in layout.

Now that we've learned about params, the first function gets passed them and another table:

element = function(param, view)
	return function()

	end
end

The first parameter is what got passed to the factory, view table looks like : {x, y, w, h} and it contains the size and coordinates of the element.

so:

local elementFactory = helium(function(param, view)
	print(param.x)
	return function()

	end
end)

elementFactory({x = 10}, 10, 10)