diff --git a/druid/druid.lua b/druid/druid.lua index 3fb795d..2bbcd43 100644 --- a/druid/druid.lua +++ b/druid/druid.lua @@ -22,6 +22,17 @@ local default_style = require("druid.styles.default.style") 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. -- After register you can create the component with @@ -47,11 +58,14 @@ function M.new(context, style) if settings.default_style == nil then M.set_default_style(default_style) end - return druid_instance(context, style) + + local new_instance = druid_instance(context, style) + table.insert(_instances, new_instance) + return new_instance end --- Set new default style. +--- Set new default style. -- @function druid.set_default_style -- @tparam table style Druid style module function M.set_default_style(style) @@ -59,7 +73,7 @@ function M.set_default_style(style) end --- Set text function. +--- Set text function. -- Druid locale component will call this function -- to get translated text. After set_text_funtion -- all existing locale component will be updated @@ -72,7 +86,7 @@ function M.set_text_function(callback) end --- Set sound function. +--- Set sound function. -- Component will call this function to -- play sound by sound_id -- @function druid.set_sound_function @@ -82,4 +96,48 @@ function M.set_sound_function(callback) 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 diff --git a/druid/system/druid_instance.lua b/druid/system/druid_instance.lua index f071608..5a5fb4a 100644 --- a/druid/system/druid_instance.lua +++ b/druid/system/druid_instance.lua @@ -111,6 +111,8 @@ end function Druid.initialize(self, context, style) self._context = context self._style = style or settings.default_style + self._deleted = false + self.url = msg.url() self.components = {} end @@ -141,6 +143,8 @@ function Druid.final(self) components[i]:on_remove() end end + + self._deleted = true end @@ -231,6 +235,45 @@ function Druid.on_message(self, message_id, message, sender) 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 -- @function druid:new_button -- @tparam args ... button init args diff --git a/example/gui/main/main.gui_script b/example/gui/main/main.gui_script index c08c365..275561f 100644 --- a/example/gui/main/main.gui_script +++ b/example/gui/main/main.gui_script @@ -51,10 +51,17 @@ local function init_swipe_control(self) end +local function on_window_callback(self, event, data) + druid.on_window_callback(event, data) +end + + function init(self) druid.set_default_style(default_style) self.druid = druid.new(self) + window.set_listener(on_window_callback) + init_top_panel(self) init_swipe_control(self) self.page = 1 diff --git a/example/init.script b/example/init.script index 87e6385..b2bfc47 100644 --- a/example/init.script +++ b/example/init.script @@ -1,5 +1,4 @@ local druid = require("druid.druid") -local const = require("druid.const") local lang = require("example.lang") @@ -12,9 +11,7 @@ local function setup_druid() return lang.get_locale(lang_id) end) - -- TODO: Call druid.finish_setup? - -- Need to update all gui, in case, when gui init was befure this init - msg.post("/gui#main", const.ON_CHANGE_LANGUAGE) + druid.on_language_change() end diff --git a/example/lang.lua b/example/lang.lua index addbb3d..890a64d 100644 --- a/example/lang.lua +++ b/example/lang.lua @@ -1,4 +1,4 @@ -local const = require("druid.const") +local druid = require("druid.druid") local M = {} @@ -47,7 +47,7 @@ end function M.toggle_locale() data = data == en and ru or en - msg.post("/gui#main", const.ON_CHANGE_LANGUAGE) + druid.on_language_change() end return M