204 views
in Embedded Wizard Studio by
Hi,

Do u have any idea how day/night background will work?

If yes, could u please provide solution.

Thanks.

1 Answer

0 votes
by

Hello Manvitha,

if I understood your question correctly, you want the GUI in two variants day and night. For this purpose please see the chapter Managing variants. Since you want the variants to change dynamically at the runtime, your use case would be Use case: dynamic variants.

With variants you can manage multiple values for color constants and bitmap resources. The right value is selected dynamically depending on the actually active Style. See also Control the selection of dynamic variants.

Important aspect: when the Style is switched on/off, this alternation does not affect already evaluated expressions. It means, a bitmap displayed already on the screen is not automatically switched to its variant. You would trigger this operation manually. Following options exist:

Option 1: Recreate the affected GUI components. This will have the effect of all expressions referring color constants and resources being evaluated again. 

Option 2: Force all components to re-initialize the expressions. For example, to assign the Bitmap resource to an Image view again so that its current variant is estimated.

I hope it helps you further.

Best regards

Paul Banach

by
Hi Paul,

I have created a simple screen for dark mode and I want to create light mode screen also. Is it enough to create a variant from the dark mode screen? or else do I need to create variants which are in the dark mode screen. I couldn't get a clear understanding by referring example project you have given under dynamic variants. Could you please explain this use case in detail?

Regards,

Sazna.
by

Hello Sazna,

it depends on where the colors are determined. Consequently create variants of the affected GUI components and change there the colors from dark to light. Also possible, instead of specifying the colors directly in GUI components you use constants containing the color values. Then you don't need to create variants of the GUI components. Instead you create variants of the constants. Following could be the approach:

1. You store all light mode colors in constants. For example, a constant for the background and another constant for the foreground color. If you plan to use 100 colors, you create 100 constants.

2. Now, wherever a GUI component needs the color value, you use the respective constant.

3. To support dark mode, you override the constants by so-called 'variants'. In the variant you can specify another color. Additionally, you specify in the variant a condition when the variant is active.

4. Since you want the variants to be activated dynamically at the runtime, the above mentioned variant condition should depend on a Style member. At the runtime the style can be switched on and off. Accordingly, the variant become active and inactive.

5. When the variant is actually active, all accesses to the original constant result in the value found in the variant. When the variant is inactive, the color value from the original constant is used.

The chapter Managing variants explains the technical aspects more in detail.

This approach has however a peculiarity: the expression containing the multi-variant constant needs to be re-evaluated in order to become a new value (dark or light). The simplest to force this re-evaluation is a new instantiation of all actually visible GUI components. In other words, after you switch on/off the style controlling the color constant variants, you dismiss the existing screen (or screens) and present them again. This should cause the components to be created and initialized again now with new colors.

If you don't want (or you can't) recreate the existing components, I would recommend following trick:

6. Wherever you assign a multi-variant color constant to an initialization expression in Inspector window, prefix this initialization by the %+ operator (see also React automatically to language selection. This operator forces Embedded Wizard to generate additional re-initialization code for the affected initialization. The following screenshot demonstrates the usage of this %+ operator, here in the initialization of the property Color of a Rectangle view with a constant named Application::BkColor:

7. The above mentioned code generation depends on an additional Multilingual attribute, you have to set it true for each GUI component class, you want the automatic color alternation:

As you may have noticed, the trick explained here uses Embedded Wizard automatic language actualization. To force the execution of the code containing the initialization expressions, you have thus to switch the language temporarily.

8. Add a new language member to your project and name it e.g. DummyLanguage.

9. Now, when the user switches the dark/light modes, switch on/off the the style controlling the variants and switch temporarily the language:

For demonstration purpose I have prepared a small example. You will find it in the attachment at the end of this comment named theme-v12.zip. This example is created explicitly for Embedded Wizard Studio version 12. For the next version 13, we have improved this approach so the above mentioned trick with language switch will not be necessary anymore.

In the example, please note the constants BkColor and FgColor as well as their variants for the dark theme. Also note the Style DarkTheme and the dummy language:

In practice you can add further constants and override them by variants. Then you can use the constants in your project. When the user switches the color theme, the above mentioned trick with language selection triggers all initialization expressions and the GUI should appear updated.

Please note, if colors are determined within the logic of a component (e.g. in some method executed in response to an event), this code is not automatically executed when the the trick with language selection is performed. Only initialization expressions specified in Inspector are using this approach.

Therefore, if you have some color selected within a method, you will need to re-evaluate the corresponding expressions explicitly. For this purpose you copy/paste the affected expression inside the ReInit() method. The ReInit() method is executed when the language is switched. It is thus well suitable to perform the necessary color updates. For demonstration purpose, the example uses such a ReInit() method to update the color of a small rectangle in the upper-left screen corner.

The project demonstrating the application case.

I hope it helps you further.

Best regards

Paul Banach

by
Hi Paul,

Thanks for your explanation. It is more understandable and the trick with language option seems convenient. I implemented both themes. Thank You.

Regards,

Sazna.

Ask Embedded Wizard

Welcome to the question and answer site for Embedded Wizard users and UI developers.

Ask your question and receive answers from the Embedded Wizard support team or from other members of the community!

Embedded Wizard Website | Privacy Policy | Imprint

...