diff options
author | Nat Goodspeed <nat@lindenlab.com> | 2024-08-23 17:18:14 -0400 |
---|---|---|
committer | Nat Goodspeed <nat@lindenlab.com> | 2024-08-23 17:18:14 -0400 |
commit | a80b9487dc7c893f5e96f48f15140a5f82b99e30 (patch) | |
tree | d6f048acac23e5f5cdb72920587f0ca240dbb1ca /indra/newview/scripts/lua/require/UI.lua | |
parent | 7ee93ea34dfc41640f852279888bbee0cafcecbe (diff) |
Allow UI to have lazily-loaded submodules.
Equip UI with an __index metamethod. When someone references an unknown
key/field in UI, require() that module and cache it for future reference.
Add util.setmetamethods() as a way to find or create a metatable on a
specified table containing specified metamethods.
Exercise the new functionality by referencing UI.popup in test_popup.lua.
Diffstat (limited to 'indra/newview/scripts/lua/require/UI.lua')
-rw-r--r-- | indra/newview/scripts/lua/require/UI.lua | 22 |
1 files changed, 19 insertions, 3 deletions
diff --git a/indra/newview/scripts/lua/require/UI.lua b/indra/newview/scripts/lua/require/UI.lua index 464e6547ea..969a2cbded 100644 --- a/indra/newview/scripts/lua/require/UI.lua +++ b/indra/newview/scripts/lua/require/UI.lua @@ -1,10 +1,26 @@ -- Engage the viewer's UI local leap = require 'leap' -local Timer = (require 'timers').Timer local mapargs = require 'mapargs' - -local UI = {} +local Timer = (require 'timers').Timer +local util = require 'util' + +-- Allow lazily accessing certain other modules on demand, e.g. a reference to +-- UI.Floater lazily loads the Floater module. Use of UI's __index metamethod +-- theoretically permits any other module you can require() to appear as a +-- submodule of UI, but it doesn't make sense to support (e.g.) UI.Queue. +local submods = { 'Floater', 'popup' } +local UI = util.setmetamethods{ + __index=function(t, key) + if not table.find(submods, key) then + error(`Invalid UI submodule {key}`, 2) + end + local mod = require(key) + -- cache the submodule + t[key] = mod + return mod + end +} -- *************************************************************************** -- registered menu actions |