-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinit.lua
More file actions
178 lines (151 loc) · 5.7 KB
/
Copy pathinit.lua
File metadata and controls
178 lines (151 loc) · 5.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
local json = require("rapidjson")
local Module = {}
-- Valid transition strings
Module.Transitions = {
Left = 'left',
Right = 'right',
Top = 'top',
Bottom = 'bottom',
Fade = 'fade',
None = 'none'
}
--------------------------------Layer Groups and Controllers-----------------------------------------------
Module.Layer = {}
Module.Layer.__index = Module.Layer
--- Create a layer object.
--- @param name string The UCI layer name.
--- @param stateFunction function Function which returns a boolean value corresponding to the the layer's visibility.
--- @param transition string? The desired transition if not inheriting the controller's default.
--- @return table
function Module.Layer.New(name, stateFunction, transition)
local self = {}
self.Name = name
self.State = stateFunction or function()
return true
end
self.Transition = transition
setmetatable(self, Module.Layer)
return self
end
Module.LayerController = {}
Module.LayerController.__index = Module.LayerController
--- Update a layer's visibility. Optionally set hide to true to override a layer's `stateFunction` and hide the layer.
--- @param layer table The layer object to update visibility for.
--- @param hide boolean? Optionally set to true when a layer's stateFunction should be overridden.
function Module.LayerController:Update(layer, hide)
local newState = layer.State() and not hide
if newState ~= layer.PreviousState then
Uci.SetLayerVisibility(self.Page, layer.Name, newState, layer.Transition or self.DefaultTransition)
if self.Debug then
print("LayerControllerUpdate:", layer.Name, 'New State = ', tostring(newState))
end
end
layer.PreviousState = newState
end
--- Update the visibility of all layers in the layer controller.
--- @param hide boolean? Optionally set to true to override all stateFunctions and hide all layers.
function Module.LayerController:UpdateAll(hide)
for _, layer in pairs(self.List) do
self:Update(layer, hide)
end
end
--- Hide all layers in the Controller.
function Module.LayerController:Hide()
self:UpdateAll(true)
end
--- Return the state of another layer and update it.
--- @param layerName string The name of the layer to get stat from.
--- @return boolean? The state of the layer if it exists, else nil.
function Module.LayerController:GetState(layerName)
for _, layer in pairs(self.List) do
if layer.Name == layerName then
self:Update(layer)
return layer.State()
end
end
end
--- Add layer objects to this controller.
--- @param layer table The layer object to add.
function Module.LayerController:Add(layer)
if self.Debug then
print("Layer added:", layer.Name)
end
table.insert(self.List, layer)
self:Update(layer)
end
--- Initialize this controller's layer list using a boolean lookup table. The lookup table's keys must match layer names
--- while the value must be a boolean type corresponding to the visibility.
--- @param layerLookup table Table containing key value pairs
--- @param transition string? The desired transition for all layers if not the controller's default
function Module.LayerController:InitializeList(layerLookup, transition)
if self.Debug then
print("LayerController: Initialize List from lookup table")
end
self.List = {}
for layer, _ in pairs(layerLookup) do
self:Add(
Module.Layer.New(
layer,
function() return layerLookup[layer] end,
transition
))
end
end
--- Modify a controls event handler so that it also triggers the layer controller to update visibilities of all layers.
--- @param control table A control which should also trigger visibility checks.
function Module.LayerController:UpdateOnEvent(control)
local oldEH = control.EventHandler or function()
end
control.EventHandler = function(ctrl)
oldEH(ctrl)
self:UpdateAll()
end
end
--- Create a new layer controller object. Set .Debug to true if you want to see a print of what is happening.
--- @param page string? The UCI page name that this layer controller should act on. Defaults to "Page 1".
--- @param list table? An optional list of layer objects.
--- @param transition string? An optional default transition to use when layers do not specify a transition.
--- @return table # A new layer controller object.
function Module.LayerController.New(page, list, transition)
local self = {}
self.Page = page or "Page 1"
self.List = list or {}
self.Debug = false
self.DefaultTransition = transition or Module.Transitions.None
setmetatable(self, Module.LayerController)
return self
end
----------------------------------------------------------------------------------------
--- Load UCI Information / UCI Names.
---@param UCIName string The UCI name to get the layout for.
---@return table? # A list of pages contained in the UCI.
function Module.GetLayout(UCIName)
-- Load UCI information into table to parse below
local thefile = io.open("design/ucis.json")
local text = thefile:read("a")
local UCIs = json.decode(text).Ucis
thefile:close()
for _, UCI in pairs(UCIs) do
if UCI.Name == UCIName then
return UCI.Pages
end
end
end
--- Check if a string represents a valid transition and return it. This will always return a valid transition type.
--- @param transition string A transition string to check
--- @return string # The provided transition type if `transition` was valid, else 'none'
function Module.ParseTransition(transition)
transition = transition:lower()
for _, t in pairs(Module.Transitions) do
if transition == t then
return transition
end
end
return Module.Transitions.None
end
---------------------------------------------------------------
-- Add the standard Uci library interface to this library
for key, fun in pairs(Uci) do
Module[key] = fun
end
return Module