Files
playlet-piped/playlet-lib/src/components/SettingsScreen/SettingsScreen.bs
T

364 lines
9.7 KiB
Plaintext

import "pkg:/source/utils/StringUtils.bs"
import "pkg:/source/asyncTask/asyncTask.bs"
import "pkg:/source/services/PreferencesService.bs"
import "pkg:/components/VideoPlayer/Video.bs"
function Init()
m.service = new PreferencesService()
m.menu = m.top.findNode("PreferencesMenu")
m.hint = m.top.findNode("HintLabel")
LoadPreferencesTopLevel(m.service.prefs_model)
RefreshUI(0)
m.top.ObserveField("focus", "OnFocusChange")
m.top.ObserveField("visible", "OnVisibilityChange")
end function
function LoadPreferencesTopLevel(model as object)
m.uiElements = {}
LoadPreferencesAtLevel(model, 0, "")
end function
function LoadPreferencesAtLevel(model as object, level as integer, key as string)
if m.menu.getChildCount() - 1 < level
container = m.menu.createChild("Group")
container.id = `Container${level}`
else
container = m.menu.getChild(level)
end if
buttonGroup = CreateSubmenu(container, model, level, key)
buttons = []
for each item in model
if item.visibility = "web"
continue for
end if
buttons.push(item.displayText)
if item.children <> invalid
LoadPreferencesAtLevel(item.children, level + 1, item.key)
else if item.type <> invalid
CreateControl(item, level + 1, item.key)
else if item.rokuComponent <> invalid
CreateRokuComponent(item, level + 1, item.key)
end if
end for
buttonGroup.buttons = buttons
buttonGroup.observeField("buttonFocused", "OnButtonFocused")
end function
function CreateSubmenu(container as object, model as object, level as integer, key as string) as object
buttonGroup = container.createChild("ButtonGroup")
buttonGroup.minWidth = 300
buttonGroup.iconUri = ""
buttonGroup.id = `Control${level}`
buttonGroup.visible = false
m.uiElements[key] = buttonGroup
buttonGroup.addFields({
"level": level,
"key": key
})
return buttonGroup
end function
function CreateControl(model as object, level as integer, key as string) as object
menu = m.top.findNode("PreferencesMenu")
if menu.getChildCount() - 1 < level
container = menu.createChild("Group")
container.id = `Container${level}`
else
container = menu.getChild(level)
end if
if model.type = "string"
return CreateStringControl(container, model, level, key)
else if model.type = "boolean"
return CreateBooleanControl(container, model, level, key)
else if model.type = "radio"
return CreateRadioControl(container, model, level, key)
end if
return invalid
end function
function CreateBooleanControl(container as object, model as object, level as integer, key as string) as object
control = container.createChild("BooleanControl")
control.id = `Control${level}`
control.visible = false
control.key = key
control.level = level
m.uiElements[key] = control
control.settings = m.top
return control
end function
function CreateStringControl(container as object, model as object, level as integer, key as string) as object
control = container.createChild("StringControl")
control.id = `Control${level}`
control.visible = false
control.key = key
control.level = level
control.displayText = model.displayText
control.description = model.description
control.settings = m.top
m.uiElements[key] = control
return control
end function
function CreateRadioControl(container as object, model as object, level as integer, key as string) as object
control = container.createChild("RadioControl")
control.id = `Control${level}`
control.visible = false
control.key = key
control.level = level
control.options = model.options
control.settings = m.top
m.uiElements[key] = control
return control
end function
function CreateRokuComponent(model as object, level as integer, key as string) as object
menu = m.top.findNode("PreferencesMenu")
if menu.getChildCount() - 1 < level
container = menu.createChild("Group")
container.id = `Container${level}`
else
container = menu.getChild(level)
end if
component = container.createChild(model.rokuComponent)
component.id = `Control${level}`
component.settings = m.top
component.visible = false
component.key = key
component.level = level
m.uiElements[key] = component
return invalid
end function
function OnButtonFocused(event as object)
buttonGroup = event.getRoSGNode()
RefreshUI(buttonGroup.level + 1)
end function
function RefreshUI(maxLevel = 0 as integer)
for each key in m.uiElements
m.uiElements[key].visible = false
end for
topLevel = m.top.findNode("Control0")
topLevel.visible = true
ShowButtonGroup(m.service.prefs_model, topLevel.buttonFocused, maxLevel)
RefreshHint()
end function
function ShowButtonGroup(model as object, index as integer, maxLevel as integer) as void
item = model[index]
element = m.uiElements[item.key]
if element = invalid
return
end if
if element.level > maxLevel
return
end if
element.visible = true
if item.children <> invalid
ShowButtonGroup(item.children, element.buttonFocused, maxLevel)
end if
end function
function RefreshHint() as void
focusedButtonGroup = GetFocusedControl()
if focusedButtonGroup = invalid
return
end if
selectedIndex = focusedButtonGroup.buttonFocused
if selectedIndex = invalid
return
end if
children = FindModelChildrenByKey(focusedButtonGroup.key, m.service.prefs_model)
if children = invalid
return
end if
if children.Count() <= selectedIndex
return
end if
displayText = children[selectedIndex].displayText
description = children[selectedIndex].description
if description = invalid or description = invalid
return
end if
m.hint.text = `${displayText}: ${description}`
end function
function FindModelChildrenByKey(key as string, model as object) as object
if key = ""
return m.service.prefs_model
end if
for each item in model
if item.key = key
return item.children
end if
if item.children <> invalid
result = FindModelChildrenByKey(key, item.children)
if result <> invalid
return result
end if
end if
end for
return invalid
end function
function OnFocusChange() as void
if m.top.focus <> true
return
end if
topLevelButtonGroup = m.top.findNode("Control0")
if topLevelButtonGroup <> invalid
topLevelButtonGroup.setFocus(true)
RefreshUI(1)
end if
end function
function OnVisibilityChange() as void
if m.top.visible <> true
return
end if
topLevelButtonGroup = m.top.findNode("Control0")
if topLevelButtonGroup <> invalid
topLevelButtonGroup.visible = true
end if
end function
function OnkeyEvent(key as string, press as boolean) as boolean
if press = false
return false
end if
if key = "options"
if ToggleVideoPictureInPicture(m.top)
return true
end if
end if
if key = "back"
m.top.escape = key
return true
end if
if key = "left"
if not MenuGoLeft()
RefreshUI(0)
m.top.escape = key
end if
return true
end if
if key = "right"
if MenuGoRight()
return true
end if
end if
return false
end function
function GetFocusedControl() as object
node = m.top.focusedChild
if node = invalid
return invalid
end if
while not node.id.StartsWith("Control")
child = node.focusedChild
if child = invalid
return invalid
end if
if node.isSameNode(child)
return invalid
end if
node = child
end while
return node
end function
function MenuGoLeft() as boolean
return MenuNavigate(-1)
end function
function MenuGoRight() as boolean
return MenuNavigate(1)
end function
function MenuNavigate(direction as integer) as boolean
focusedControl = GetFocusedControl()
if focusedControl = invalid
return false
end if
container = focusedControl.getParent()
containerParent = container.getParent()
containerCount = containerParent.getChildCount()
index = GetChildIndex(containerParent, container)
index += direction
if index >= containerCount or index < 0
return false
end if
newFocusedContainer = containerParent.getChild(index)
childCount = newFocusedContainer.getChildCount()
for i = 0 to childCount - 1
child = newFocusedContainer.getChild(i)
if child.visible
child.setFocus(true)
RefreshUI(index + 1)
return true
end if
end for
return false
end function
function GetChildIndex(node as object, child as object) as integer
childCount = node.getChildCount()
for i = 0 to childCount - 1
c = node.getChild(i)
if child.id = c.id
return i
end if
end for
return -1
end function
function GetPreferenceValue(key as string) as dynamic
return m.service.GetValue(key)
end function
function SetPreferenceValue(key as string, value as dynamic)
m.service.SetValue(key, value)
end function
function GetUserPreferences(unused as object) as object
return m.service.GetUserPreferences()
end function
function ApplyUserPreferences(preferences as object) as void
m.service.ApplyUserPreferences(preferences)
end function
function ClearUserPreferences(unused as object) as void
m.service.ClearUserPreferences()
end function