Lightning Framework
repository·master·Indexed 41 days ago
https://github.com/rdkcentral/lightningA high-performance, portable framework optimized for TV application development. It provides a core engine for building efficient television apps, featuring a WebGL renderer, a comprehensive animation system with progress-to-value mapping, and developer tools like the Lightning Inspector for DOM-based debugging. Includes support for TypeScript via the @lightningjs/core package (v2.20.1).
What's inside Lightning
- Lightning is a high-performance, portable framework designed specifically for (TV) app development. It provides the core engine for building applications that require high efficiency on television hardware.
Supported input methods in Lightning
masterLightning provides built-in support for three primary input methods. Depending on your target hardware and user interface requirements, you can implement:
- Remote / Keyboard Controls: For traditional TV remote navigation or standard keyboard input.
- Mouse: For pointer-based interaction.
- Touch: For touch-screen enabled devices.
Refer to the specific documentation for each method to implement specialized input handling logic.
Configure Element appearance and positioning
masterYou can control an element's presence and visual state using several categories of properties:
- Positioning: Set coordinates, dimensions, and mountpoints. All positioning is relative to its parent.
- Transform: Use transform properties to scale or rotate an element.
- Rendering: Use rendering properties to define if and how the element is rendered.
- Children: Use children properties to modify the configuration of child nodes.
Enable rectangular clipping
masterSetting the
clippingproperty totrueprevents anything outside the element's dimensions from being rendered (similar to CSSoverflow:hidden). This can improve performance by detecting and skipping descendants outside the clipping region.Limitations:
- Clipping is implemented using the WebGL
scissoroperation and only works for rectangular areas. - If an element (or any of its ancestors) is rotated, clipping will not be applied.
- For non-rectangular clipping or rotated elements, use
renderToTextureinstead.
- Clipping is implemented using the WebGL
How the focus path works in Lightning
masterTo handle key events, Lightning identifies an active component through a concept called the focus path. The focus path is a hierarchy consisting of the active component and its descendants (including the
Appitself).Lightning determines the focus path by recursively calling the
_getFocused()method starting from theAppobject:- If
_getFocused()returnsundefined, the focus path stops at the current component. - If
_getFocused()returns a child component, that child is added to the path, and its own_getFocused()method is invoked. - This recursion continues until a component returns
undefinedor the end of the tree is reached.
The focus path is only recalculated when:
- A component's state changes.
- A key is pressed.
_refocus()is called on any component.
- If
Create a Loose Component
masterIf you need to opt-out of strict type safety—for example, to allow arbitrary properties or child keys not explicitly declared in your spec—you can create a Loose Component.
To do this, extend your Template Spec from
Lightning.Component.TemplateSpecLooseinstead ofLightning.Component.TemplateSpec.By default, any component that does not define its own Template Spec is considered a Loose Component. This is useful when integrating third-party libraries that may not have TypeScript support.
export interface MyComponentTemplateSpec extends Lightning.Component.TemplateSpecLoose { // Properties and children defined here are still typed, // but additional arbitrary keys are allowed. }Understand the Lightning Render Tree concept
masterThe Lightning Render Tree is the structure used to define, position, and render elements in a Lightning application. While it shares conceptual similarities with the HTML DOM Render Tree (such as defining elements and setting properties to change display), it is fundamentally different in implementation:
- JavaScript-based: The entire tree is written in JavaScript.
- Basic API: It uses a more fundamental and minimal set of methods compared to the DOM.
- Template-driven: The tree is defined using Templates.
- Dynamic: It is designed to be highly dynamic during runtime.
To define and use specific elements within this tree, refer to the Elements documentation.
Manage render priority with Z-indexing
masterLightning supports
zIndexto alter the order in which textures are rendered, similar to CSS.A z-index context is created when you use:
- A non-zero
zIndexvalue. - The
forceZIndexContextproperty (useful when combined withzIndex: 0). renderToTexture.
- A non-zero
Use Loose Type Configs for hybrid type safety
masterBy default, if no Type Config is provided, Lightning uses a Loose Type Config, which allows any Event or Signal to be handled with
anyparameters.If you want the benefit of type-checking for specific events/signals while still allowing arbitrary ones, you can extend
Lightning.Component.TypeConfigLooseinstead of the standardTypeConfig.export interface TypeConfigLoose extends Lightning.Component.TypeConfigLoose { SignalMapType: MyComponentSignalMap, EventMapType: MyComponentEventMap }Performance optimizations in the Render Engine
masterThe Lightning Render Engine implements several strategies to maintain high performance, especially in large applications:
- Incremental Updates: By tagging changed branches with
hasUpdates, the engine only recalculates coordinates for modified sections, avoiding a full tree traversal. - Visibility Culling: The engine does not render invisible parts of the tree.
- Out-of-screen Detection: Branches that are off-screen are detected and skipped, which enables high-performance, nearly infinite scrolling lists.
- Zero-change Idling: If no changes are detected in the Render Tree, no re-rendering occurs, which minimizes resource usage and power consumption.
- Incremental Updates: By tagging changed branches with
Understand the role of Elements in the Lightning Render Tree
masterIn Lightning, the Render Tree is composed of Elements (of type
lng.Element). An element acts as a render primitive that determines how a texture is displayed.Crucial Note: An element will only be visible on screen if it has a texture attached to it (e.g., text, an image, a solid color, or a gradient).
How `withAnnouncer` traverses the focus path
masterThe
withAnnouncermixin automates speech announcements by inspecting the component hierarchy during focus changes. It follows a two-step process:Information Collection: It travels through the
_focusPathlooking for properties in this specific order of precedence:component.announcecomponent.titlecomponent.label
Contextualization: After collecting the properties, it reverses the
_focusPathto look forcomponent.announceContextproperties.
This mechanism allows developers to provide both the primary identity of a component (e.g., a movie title) and the contextual information (e.g., "1 of 5 items") to create a coherent spoken experience.