Add test "new_widget"

This commit is contained in:
Insality 2024-10-30 01:29:46 +02:00
parent 91bdcb6678
commit 00b8b192a7
3 changed files with 74 additions and 6 deletions

View File

@ -79,7 +79,6 @@ M.SPECIFIC_UI_MESSAGES = {
local uid = 0 local uid = 0
---@private
function M.create_uid() function M.create_uid()
uid = uid + 1 uid = uid + 1
return uid return uid

View File

@ -7,7 +7,7 @@
local component = require("druid.component") local component = require("druid.component")
---@class {COMPONENT_TYPE}: druid.component ---@class {COMPONENT_TYPE}: druid.base_component
---@field druid druid_instance{COMPONENT_ANNOTATIONS} ---@field druid druid_instance{COMPONENT_ANNOTATIONS}
local M = component.create("{COMPONENT_TYPE}") local M = component.create("{COMPONENT_TYPE}")

View File

@ -67,6 +67,7 @@
-- @see Text -- @see Text
-- @see Timer -- @see Timer
local const = require("druid.const")
local helper = require("druid.helper") local helper = require("druid.helper")
local settings = require("druid.system.settings") local settings = require("druid.system.settings")
local base_component = require("druid.component") local base_component = require("druid.component")
@ -160,6 +161,51 @@ local function create(self, instance_class)
end end
local WIDGET_METATABLE = { __index = base_component }
-- Create the Druid component instance
local function create_widget(self, widget_class)
local instance = setmetatable({}, {
__index = setmetatable(widget_class, WIDGET_METATABLE)
})
local uid = base_component.create_uid()
instance._components = {
_uid = uid,
name = "Druid Widget #" .. uid,
input_priority = const.PRIORITY_INPUT,
default_input_priority = const.PRIORITY_INPUT,
_is_input_priority_changed = true, -- Default true for sort once time after GUI init
}
instance._meta = {
template = "",
context = self._context,
nodes = nil,
style = nil,
druid = self,
input_enabled = true,
children = {},
parent = type(self._context) ~= "userdata" and self._context,
instance_class = widget_class
}
-- Register
if instance._meta.parent then
instance._meta.parent:__add_child(instance)
end
table.insert(self.components_all, instance)
local register_to = instance:__get_interests()
for i = 1, #register_to do
local interest = register_to[i]
table.insert(self.components_interest[interest], instance)
end
return instance
end
-- Before processing any input check if we need to update input stack -- Before processing any input check if we need to update input stack
local function check_sort_input_stack(self, components) local function check_sort_input_stack(self, components)
if not components or #components == 0 then if not components or #components == 0 then
@ -255,10 +301,11 @@ function M:initialize(context, style)
end end
-- Create new component. ---Create new Druid component instance
-- @tparam BaseComponent component Component module ---@generic T: druid.base_component
-- @tparam any ... Other component params to pass it to component:init function ---@param component T
-- @treturn BaseComponent Component instance ---@vararg any
---@return T
function M:new(component, ...) function M:new(component, ...)
local instance = create(self, component) local instance = create(self, component)
@ -524,6 +571,28 @@ function M:_clear_late_remove()
end end
---Create new Druid widget instance
---@generic T: druid.base_component
---@param widget T
---@param template string|nil The template name used by widget
---@param nodes table|nil The nodes table from gui.clone_tree
---@vararg any
---@return T
function M:new_widget(widget, template, nodes, ...)
local instance = create_widget(self, widget)
instance.druid = instance:get_druid(template, nodes)
if instance.init then
instance:init(...)
end
if instance.on_late_init or (not self.input_inited and instance.on_input) then
schedule_late_init(self)
end
return instance
end
--- Create Button component --- Create Button component
---@param node string|node The node_id or gui.get_node(node_id) ---@param node string|node The node_id or gui.get_node(node_id)
---@param callback function|nil Button callback ---@param callback function|nil Button callback