Built-in variables: styles

The variable styles stores in a multi-variant application the names of all currently active style members. Please note, the variable is declared with the homonymous styles data type.

Declaration

styles styles

Discussion

In your project you can define several style members, one for every mode and device configuration your application should support. Then at the runtime, you activate individual styles by adding the corresponding style member names to the built-in variable styles. Similarly to deactivate styles, you remove the names of the corresponding style members from the styles variable. Assuming, in your project you have defined style members called HiRes, NightMode and DebugMode, then the following operation would deactivate the styles DebugMode and HiRes and activate the style NightMode:

// Assuming an initial situation styles = [ HiRes, DebugMode ]; // styles = [ HiRes, DebugMode ] // Deactivate the DebugMode and the HiRes style styles = styles - [ DebugMode, HiRes ]; // styles = [] // Activate the NightMode style styles = styles + [ NightMode ]; // styles = [ NightMode ]

The modification of the styles variable affects only the subsequent accesses to multi-variant classes, autoobjects, constants, font and bitmap resources. It has no effect on already evaluated expressions. In other words: the once instantiated object of a multi-variant class will not change its content nor its behavior when you afterwards switch the styles. Assuming, your project contains a multi-variant autoobject Drivers::DeviceObject depending on the style DebugMode, then the both variables object1 and object2 at the end of the following example will refer to different versions of the original autoobject:

styles = []; // styles = [] var Drivers::DeviceClass object1 = Drivers::DeviceObject; styles = styles + [ DebugMode ]; // styles = [ DebugMode ] var Drivers::DeviceClass object2 = Drivers::DeviceObject; styles = styles - [ DebugMode ]; // styles = [] (again) var Drivers::DeviceClass object3 = Drivers::DeviceObject; trace object1 == object2; // false trace object1 == object3; // true

You can also evaluate the variable styles within expressions wherever an operand of data type styles is allowed. For example, you can store the variable locally or use it in conditions. The following code demonstrates it on the example of how to toggle the NightMode style:

var styles tmp = styles; if ( tmp.contains([ NightMode ])) tmp = tmp - [ NightMode ]; else tmp = tmp + [ NightMode ]; styles = tmp;

Please note, starting with version 13, Chora provides powerful automatisms to allow existing objects to update themselves in response to the modification of the styles variable. First, when switching on/of styles, Chora automatically calls the ReInit() re-constructors of every currently existing object allowing the objects to explicitly handle the styles alternation. You can implement these re-constructors accordingly to your needs and so react to the styles alternation.

Furthermore, when the styles variable is changed, Embedded Wizard can automatically re-evaluate in all actually existing objects all initialization expressions, which involve multi-variant operands. For example, if an embedded object's property is initialized with a multi-variant constant, the initialization expression is re-evaluated and the resulting, eventually different constant value is re-assigned to the property automatically. Thereupon, the state and/or the appearance of the object changes.

A practical application for this automatism could be switching color themes in all GUI components. To achieve this, the components use color values stored in multi-variant constants. The constant variants, in turn, are controlled by a style (e.g. NightMode). Switching on/off this style will provoke a re-evaluation of all affected initialization expressions so new color values are used.

IMPORTANT

In many cases it is thus not necessary to implement any additional code to handle the styles alternation. The above described automatism takes care of the appropriate update operations after switching on/off a style. The automatic re-evaluation of initialization expressions, however, is performed only when the affected (switched on/off) style member has been configured with its attribute InstantUpdate equal true. See also the section Managing variants.