Add list of druid instances, add global events: on_window_callback, on_layout_change and on_language_change

This commit is contained in:
Insality 2020-04-18 01:41:28 +03:00
parent b9b67c55d6
commit f02c68242a
5 changed files with 115 additions and 10 deletions

View File

@ -22,6 +22,17 @@ local default_style = require("druid.styles.default.style")
local M = {} local M = {}
local _instances = {}
local function get_druid_instances()
for i = #_instances, 1, -1 do
if _instances[i]._deleted then
table.remove(_instances, i)
end
end
return _instances
end
--- Register external druid component. --- Register external druid component.
-- After register you can create the component with -- After register you can create the component with
@ -47,11 +58,14 @@ function M.new(context, style)
if settings.default_style == nil then if settings.default_style == nil then
M.set_default_style(default_style) M.set_default_style(default_style)
end end
return druid_instance(context, style)
local new_instance = druid_instance(context, style)
table.insert(_instances, new_instance)
return new_instance
end end
-- Set new default style. --- Set new default style.
-- @function druid.set_default_style -- @function druid.set_default_style
-- @tparam table style Druid style module -- @tparam table style Druid style module
function M.set_default_style(style) function M.set_default_style(style)
@ -59,7 +73,7 @@ function M.set_default_style(style)
end end
-- Set text function. --- Set text function.
-- Druid locale component will call this function -- Druid locale component will call this function
-- to get translated text. After set_text_funtion -- to get translated text. After set_text_funtion
-- all existing locale component will be updated -- all existing locale component will be updated
@ -72,7 +86,7 @@ function M.set_text_function(callback)
end end
-- Set sound function. --- Set sound function.
-- Component will call this function to -- Component will call this function to
-- play sound by sound_id -- play sound by sound_id
-- @function druid.set_sound_function -- @function druid.set_sound_function
@ -82,4 +96,48 @@ function M.set_sound_function(callback)
end end
--- Callback on global window event.
-- Used to trigger on_focus_lost and on_focus_gain
-- @function druid.on_window_callback
-- @tparam string event Event param from window listener
-- @tparam table data Data param from window listener
function M.on_window_callback(event)
local instances = get_druid_instances()
if event == window.WINDOW_EVENT_FOCUS_LOST then
for i = 1, #instances do
msg.post(instances[i].url, const.ON_FOCUS_LOST)
end
end
if event == window.WINDOW_EVENT_FOCUS_GAINED then
for i = 1, #instances do
msg.post(instances[i].url, const.ON_FOCUS_GAINED)
end
end
end
--- Callback on global layout change event.
-- @function druid.on_layout_change
function M.on_layout_change()
local instances = get_druid_instances()
for i = 1, #instances do
msg.post(instances[i].url, const.ON_LAYOUT_CHANGE)
end
end
--- Callback on global language change event.
-- Used to update all lang texts
-- @function druid.on_language_change
function M.on_language_change()
local instances = get_druid_instances()
for i = 1, #instances do
msg.post(instances[i].url, const.ON_LANGUAGE_CHANGE)
end
end
return M return M

View File

@ -111,6 +111,8 @@ end
function Druid.initialize(self, context, style) function Druid.initialize(self, context, style)
self._context = context self._context = context
self._style = style or settings.default_style self._style = style or settings.default_style
self._deleted = false
self.url = msg.url()
self.components = {} self.components = {}
end end
@ -141,6 +143,8 @@ function Druid.final(self)
components[i]:on_remove() components[i]:on_remove()
end end
end end
self._deleted = true
end end
@ -231,6 +235,45 @@ function Druid.on_message(self, message_id, message, sender)
end end
function Druid.on_focus_lost(self)
local components = self.components[const.ON_FOCUS_LOST]
if components then
for i = 1, #components do
components[i]:on_focus_lost()
end
end
end
function Druid.on_focus_gained(self)
local components = self.components[const.ON_FOCUS_GAINED]
if components then
for i = 1, #components do
components[i]:on_focus_gained()
end
end
end
function Druid.on_layout_change(self)
local components = self.components[const.ON_LAYOUT_CHANGE]
if components then
for i = 1, #components do
components[i]:on_layout_change()
end
end
end
function Druid.on_language_change(self)
local components = self.components[const.ON_LANGUAGE_CHANGE]
if components then
for i = 1, #components do
components[i]:on_language_change()
end
end
end
--- Create button basic component --- Create button basic component
-- @function druid:new_button -- @function druid:new_button
-- @tparam args ... button init args -- @tparam args ... button init args

View File

@ -51,10 +51,17 @@ local function init_swipe_control(self)
end end
local function on_window_callback(self, event, data)
druid.on_window_callback(event, data)
end
function init(self) function init(self)
druid.set_default_style(default_style) druid.set_default_style(default_style)
self.druid = druid.new(self) self.druid = druid.new(self)
window.set_listener(on_window_callback)
init_top_panel(self) init_top_panel(self)
init_swipe_control(self) init_swipe_control(self)
self.page = 1 self.page = 1

View File

@ -1,5 +1,4 @@
local druid = require("druid.druid") local druid = require("druid.druid")
local const = require("druid.const")
local lang = require("example.lang") local lang = require("example.lang")
@ -12,9 +11,7 @@ local function setup_druid()
return lang.get_locale(lang_id) return lang.get_locale(lang_id)
end) end)
-- TODO: Call druid.finish_setup? druid.on_language_change()
-- Need to update all gui, in case, when gui init was befure this init
msg.post("/gui#main", const.ON_CHANGE_LANGUAGE)
end end

View File

@ -1,4 +1,4 @@
local const = require("druid.const") local druid = require("druid.druid")
local M = {} local M = {}
@ -47,7 +47,7 @@ end
function M.toggle_locale() function M.toggle_locale()
data = data == en and ru or en data = data == en and ru or en
msg.post("/gui#main", const.ON_CHANGE_LANGUAGE) druid.on_language_change()
end end
return M return M