Tutorial: Control application UI

In this tutorial, you learn how to use conditions to control the application UI. You learn how to control the activation state of Activities that make up the UI by creating popup windows that an application shows only when a condition in a Data Trigger is met. In the Data Trigger, you use an Apply Activation Action to activate a specific Activity.

A Data Trigger with an Apply Activation Action keeps an Activity active for as long as the condition in that Data Trigger is met.

You set the condition in a Data Trigger in the form of a binding expression that evaluates to either True or False.

This video shows the result of the tutorial.

../../_images/dt-complete.gif

This tutorial assumes that you understand the basics of working with the Activity system. The best entry point for that is the Tutorial: Structure application UI.

Assets for the tutorial

The starting point of this tutorial is the <KanziWorkspace>/Tutorials/Data trigger/Start/Tool_project/Data trigger.kzproj Kanzi Studio project. To learn about the content of this project and how to create such a project, see Tutorial: Structure application UI.

You can find the completed tutorial in the <KanziWorkspace>/Tutorials/Data trigger/Completed directory.

The starting point project contains the assets that you need to complete this tutorial:

  • These prefabs:

    • The images of UI elements that you use in this tutorial

      ../../_images/start-prefabs1.png
    • UI controls that you use to control the activation of Activities that you create in this tutorial

      ../../_images/prefabs-activity-control-for-preview.png
  • The Datatrigger.ActivityProperty.Cluster.MainView property type that you use to control and check the state of the cluster. In Data Triggers, you use the value of this property to decide which Activity you want to activate.

    ../../_images/library-mainview.png ../../_images/properties-data-trigger-mainview-property.png

Create the structure

In this section, you create the structure for the popup windows.

To create the structure:

  1. In Kanzi Studio, open the <KanziWorkspace>/Tutorials/Data trigger/Start/Tool_project/Data trigger.kzproj project.

  2. Create the Activity structure for the popup windows when they are maximized:

    1. In the Prefabs > MaximizedLeft prefab, create a Parallel Activity Host named PopupMaximized and drag the MaximizedWidget Exclusive Activity Host to the PopupMaximized Activity Host.

      With a Parallel Activity Host, you can activate multiple popup windows at a time and show the popup windows on top of the currently activated widget.

      ../../_images/prefabs-maximizedleft-pah.png ../../_images/prefabs-maximizedleft-popupmaximized.png
    2. In the Activity Browser, below the PopupMaximized Activity Host, click add-button, select Create Activity, and name the Activity and its prefab PopupIncomingCallMaximized.

      ../../_images/create-incomingcallmaximized.png ../../_images/ab-incomingcallmaximized.png
    3. In the Prefabs, drag the PopupIncomingCallMaximizedImage to the PopupIncomingCallMaximized Activity.

      ../../_images/prefabs-popupincomingcall.gif
    4. In the Activity Browser, in the PopupIncomingCallMaximized Activity, click activate.

      This way you simulate the activation of this Activity so that you can see its content in the Preview.

      ../../_images/ab-activate-popupincomingcallmaximized.png ../../_images/preview-popupincomingcallmaximized.png
    5. In the Prefabs, select the MaximizedLeft > PopupMaximized > MaximizedWidget Activity Host. From the Properties, drag the Horizontal Padding property to the Prefabs > MaximizedLeft > PopupMaximized Activity Host.

      This way you set the padding for all Activities of the PopupMaximized Activity Host.

      ../../_images/popupmaximized-padding.gif
    6. In the Prefabs, select the MaximizedWidget Activity Host. In the Properties, remove the Horizontal Padding property.

      ../../_images/prefabs-maximizedwidget1.png ../../_images/properties-maximizedwidget-remove-padding.png ../../_images/preview-popupmaximized-positioned.png
  3. Create the Activity structure for the popups when they are minimized:

    1. In the Prefabs > MinimizedLeft prefab, create a Parallel Activity Host named PopupMinimized and drag the MinimizedWidget Activity to the PopupMinimized Activity Host.

      ../../_images/prefabs-minimizedleft-popupminimized.png
    2. In the Activity Browser, in the PopupMinimized Activity Host, add an Activity, and name it and its prefab PopupIncomingCallMinimized.

      ../../_images/create-incomingcallminimized.png ../../_images/ab-incomingcallminimized.png
    3. In the Prefabs, drag the PopupIncomingCallMinimizedImage to the PopupIncomingCallMinimized Activity.

      ../../_images/prefabs-popupincomingcallminimized.png
    4. In the Activity Browser, in the PopupIncomingCallMinimized Activity, click activate.

      This way you simulate the activation of this Activity so that you can see its content in the Preview.

      ../../_images/ab-activate-popupincomingcallminimized.png ../../_images/preview-popupincomingcallminimized.png
    5. In the Prefabs, select the MinimizedLeft > PopupMinimized > MinimizedWidget Activity Host. From the Properties, drag the Horizontal Padding and Vertical Padding properties to the Prefabs > MinimizedLeft > PopupMinimized Activity Host.

      ../../_images/prefabs-minimizedleft-popupminimized.png ../../_images/properties-popupminimized.png
    6. In the Prefabs, select the MinimizedWidget Activity Host. In the Properties, remove the Horizontal Padding and Vertical Padding properties.

      ../../_images/prefabs-minimizedwidget1.png ../../_images/properties-minimizedwidget-remove-padding.png ../../_images/preview-popupminimized-positioned.png

Use conditions to control the activation state of Activities

In this section, you learn how to use a Data Trigger with an Apply Activation Action to control the activation state of an Activity when a condition in that Data Trigger is met.

To use conditions to control the activation state of Activities:

  1. Create a property type that you use to set the state of an Activity:

    1. In the Library > Property Types > Datatrigger > ActivityProperty, press Alt and right-click, and select Property Type. In the New Property Type, window set:

      • Name to Datatrigger.ActivityProperty.IncomingCall

      • Category to Controller Properties

      • Data Type to Boolean

      ../../_images/library-incomingcall.png ../../_images/incomingcall-property.png
    2. In the Node Tree, select the Cluster node. In the Properties, add the Controller Properties > Datatrigger.ActivityProperty.IncomingCall property.

      You add the Datatrigger.ActivityProperty.IncomingCall property to the Cluster Activity Host because you want to control the activation state of the IncomingCall Activities from this node.

      ../../_images/node-tree-root-pah.png ../../_images/properties-cluster1.png
  2. Create a Data Trigger that controls the activation state of an Activity based on a condition:

    1. In the Node Tree, select the Cluster node. In the Node Components, press Alt and right-click Triggers, select Data Trigger, and name it PopupIncomingCallMaximized.

      ../../_images/apply-activation-create-data-trigger.png ../../_images/node-components-popupincomingcallmaximized.png
    2. In the Data Trigger, in the Condition Expression, click expression-open, select the Properties window, drag the Datatrigger.ActivityProperty.Cluster.MainView and Datatrigger.ActivityProperty.IncomingCall properties to the Expression field, and edit the expression to

      {@./Datatrigger.ActivityProperty.Cluster.MainView}==0 && {@./Datatrigger.ActivityProperty.IncomingCall}==true
      

      A condition expression in a Data Trigger must evaluate to either True or False.

      Click Save.

      This way, you set the Data Trigger to keep an Apply Action in that Data Trigger applied only when these conditions are met:

      • The Datatrigger.ActivityProperty.Cluster.MainView is set to Drive (enumeration value 0).

      • The Datatrigger.ActivityProperty.IncomingCall is enabled (Boolean value True).

      You add an Apply Activation Action in the next step.

      ../../_images/expression-mainview-incomingcall.png
  3. Create an Apply Activation Action that activates and deactivates an Activity:

    1. In the PopupIncomingCallMaximized Data Trigger, press Alt and right-click Actions, and select Apply Activation Action.

      ../../_images/popupincomingcallmaximized-create-apply-activation-action.png ../../_images/popupincomingcallmaximized-apply-activation-action.png
    2. In the Activity Browser, right-click the PopupIncomingCallMaximized Activity and select Copy Activation Path.

      ../../_images/popupincomingcallmaximized-copy-activation-path.png
    3. In the Node Tree, select the Cluster node. In the Node Components, in the Apply Activation Action, set:

      • Activity Host Path to < Relative > and .

      • Activity Activation Path to the path that you copied from the Activity Browser and from the beginning of the path remove Cluster/

      The path to the Activity that you set in the Activity Activation Path property is relative to the Activity Host node that you set in the Activity Host Path property.

      The Data Trigger and Apply Activation Action keep this Activity active for as long as the condition in the Data Trigger is met.

      The Activity Browser indicates that an Activity Host has a Data Trigger with the data-trigger icon.

      ../../_images/popupincomingcallmaximized-dt-set.png ../../_images/ab-cluster-w-dt.png
  4. Set the Cluster Activity Host to activate the PopupIncomingCallMinimized Activity when the Cluster is in Browse mode and the Datatrigger.ActivityProperty.IncomingCall is enabled:

    1. In the Node Components, right-click the PopupIncomingCallMaximized Data Trigger, select Duplicate, and rename the Data Trigger to PopupIncomingCallMinimized.

      ../../_images/popupincomingcallmaximized-duplicate.png ../../_images/popupincomingcallminimized-dt.png
    2. In the Condition Expression, edit the expression to

      {@./Datatrigger.ActivityProperty.Cluster.MainView}==1 && {@./Datatrigger.ActivityProperty.IncomingCall}==true
      

      Click Apply.

      ../../_images/popupincomingcallminimized-expression.png
    3. In the Activity Browser, right-click the PopupIncomingCallMinimized Activity and select Copy Activation Path. In the PopupIncomingCallMinimized Data Trigger Apply Activation Action, paste the copied path, and remove Cluster/ from the beginning of the path.

      ../../_images/popupincomingcallminimized-copy-activation-path.png ../../_images/popupincomingcallminimized-dt-set.png
  5. Create a UI element to control the activation of the IncomingCall Activities.

    1. In the Prefabs, drag the PopupControl prefab to the MainGauge prefab.

      The PopupControl prefab contains the Toggle Buttons that you can use to control the activation of popup Activities.

      ../../_images/prefabs-maingauge-popupcontrol.png
    2. In the Prefabs, select the PopupControl > IncomingCall node. In the Properties, click + Add Binding and in the Binding Editor set:

      • Binding Mode to Two way

      • Property to Toggle State

      • Expression to

      {#Cluster/Datatrigger.ActivityProperty.IncomingCall}
      

      Click Save.

      This binding enables the IncomingCall Toggle Button to get and set the value of the Datatrigger.ActivityProperty.IncomingCall property in the Cluster Activity Host. This way the Toggle Button controls the state of the IncomingCall Activity.

      ../../_images/prefabs-popupcontrol-incomingcall.png ../../_images/binding-editor-incomingcall.png

Tip

During the application development, use these Data Trigger tools in the Activity Browser side panel:

Tool

Description

../../_images/show-data-trigger-icon.png

Shows where a Data Trigger is and where the source and target Activitites of that Data Trigger are.

../../_images/apply-data-trigger-icon.png

Simulates the state when you apply a Data Trigger.

This enables you to see the state of your application when the conditions for a Data Trigger are met without having to change the property values in your application.

../../_images/data-trigger-node-components-icon.png

Shows the selected Data Trigger in the Node Components window.

../../_images/ab-helpers.png

You can now control the activation state of the IncomingCall popup in these ways:

  • In the Preview, click the incoming-call Toggle Button.

  • In the Activity Browser, select the Cluster Activity Host and in the side panel, set the IncomingCall property.

../../_images/popupincomingcallmaximized.gif

Create additional popup windows

To create popup windows for a new message notification and control that notification with a Data Trigger, repeat the procedures above with these differences:

  1. In the Activity Browser, add these Activities:

    • In the PopupMaximized, add an Activity named PopupNewMessageMaximized whose prefab uses the PopupNewMessageMaximizedImage.

      ../../_images/ab-popupnewmessagemaximized.png
    • In the PopupMinimized, add an Activity named PopupNewMessageMinimized whose prefab uses the PopupNewMessageMinimizedImage.

      ../../_images/ab-popupnewmessageminimized.png
    ../../_images/prefabs-popupnewmessage.png
  2. To control the activation state, create a controller Boolean property type called Datatrigger.ActivityProperty.NewMessage and add it to the Cluster Activity Host.

    ../../_images/properties-newmessage.png ../../_images/properties-newmessage-bool.png ../../_images/node-tree-root-pah.png ../../_images/properties-cluster-newmessage.png
  3. In the Cluster Activity Host, in the Node Components, duplicate the PopupIncomingCallMaximized Data Trigger, rename it to PopupNewMessageMaximized and:

    • Set Expression to

      {@./Datatrigger.ActivityProperty.Cluster.MainView}==0 && {@./Datatrigger.ActivityProperty.NewMessage}==true
      

      Click Apply.

    • In the Apply Activation Action, set the Activity Activation Path property to the activation path of the PopupNewMessageMaximized Activity.

    ../../_images/node-components-popupnewmessagemaximized.png
  4. In the Cluster Activity Host, in the Node Components, duplicate the PopupNewMessageMaximized Data Trigger, rename it to PopupNewMessageMinimized and:

    • Set Condition Expression to

      {@./Datatrigger.ActivityProperty.Cluster.MainView}==1 && {@./Datatrigger.ActivityProperty.NewMessage}==true
      

      Click Apply.

    • In the Apply Activation Action, set the Activity Activation Path property to the activation path of the PopupNewMessageMinimized Activity.

    ../../_images/node-components-popupnewmessageminimized.png
  5. In the Prefabs, select the PopupControl > NewMessage node. In the Properties, click + Add Binding and in the Binding Editor set:

    • Binding Mode to Two way

    • Property to Toggle State

    • Expression to

    {#Cluster/Datatrigger.ActivityProperty.NewMessage}
    

    Click Save.

    This binding enables the NewMessage Toggle Button to get and set the value of the Datatrigger.ActivityProperty.NewMessage property in the Cluster Activity Host. This way the Toggle Button controls the state of the NewMessage Activity.

    ../../_images/prefabs-popupcontrol-newmessage.png ../../_images/binding-editor-newmessage.png
  6. In the Activity Browser, select the PopupIncomingCallMaximized Activity and in the side panel, set the Priority to 1.

    This way you set the priority of the PopupIncomingCallMaximized Activity to be higher than the priority of the PopupNewMessageMaximized Activity. The Activity with higher priority shows on top.

    ../../_images/ab-popupincomingcallmaximized-priority.png
  7. Repeat the previous step for the PopupIncomingCallMinimized Activity.

    ../../_images/ab-popupincomingcallminimized-priority.png

You can now control the activation state of the NewMessage popup in these ways:

  • In the Preview, click the new-message Toggle Button.

  • In the Activity Browser, select the Cluster Activity Host and in the side panel set the NewMessage property.

../../_images/leftpopups-complete.gif

What’s next?

In this tutorial, you learned how to control the activation state of Activities with condition expressions. The application shows popup windows only when a condition in a Data Trigger is met.

To continue practicing the use of Data Triggers and Apply Activation Actions, you can repeat the entire tutorial to create the structure for the popup windows in the right-hand area of the cluster UI. Here are some useful tips for creating that structure:

  1. In the Prefabs:

    1. In the MaximizedRight Activity, create a Parallel Activity Host named PopupMaximized, and drag the MaximizedWidget Activity Host to the PopupMaximized Activity Host.

    2. From the MaximizedWidget Activity Host, drag the Horizontal Padding property to the PopupMaximized Activity Host. Remove the Horizontal Padding property from the MaximizedWidget Activity.

    ../../_images/prefabs-popupmaximized-right.png
  2. Repeat the previous step for the MinimizedRight Activity, but name the Parallel Activity Host PopupMinimized and drag the Horizontal Padding and Vertical Padding properties. Remove the Horizontal Padding and Vertical Padding properties from the MinimizedWidget Activity Host.

    ../../_images/prefabs-popupminimized-right.png
  3. In the Activity Browser:

    • In the PopupMaximized Activity Host, create Activities:

      • PopupAccidentAheadMaximized

      • PopupBatteryLowMaximized

      ../../_images/ab-maximizedright-accident-battery.png
    • In the PopupMinimized Activity Host, create Activities:

      • PopupAccidentAheadMinimized

      • PopupBatteryLowMinimized

      ../../_images/ab-minimizedright-accident-battery.png
  4. In the Prefabs, drag:

    • PopupAccidentAheadMaximizedImage to PopupAccidentAheadMaximized

    • PopupAccidentAheadMinimizedImage to PopupAccidentAheadMinimized

    • PopupBatteryLowMaximizedImage to PopupBatteryLowMaximized

    • PopupBatteryLowMinimizedImage to PopupBatteryLowMinimized

    ../../_images/prefabs-right-area-popups.png
  5. In the Library > Property Types > Datatrigger > ActivityProperty, create Datatrigger.ActivityProperty.AccidentAhead and Datatrigger.ActivityProperty.BatteryLow property types, and set:

    • Category to Controller Properties

    • Data Type to Boolean

    ../../_images/properties-accidentahead.png ../../_images/properties-batterylow.png
  6. Add to the Cluster Activity Host the Datatrigger.ActivityProperty.AccidentAhead and Datatrigger.ActivityProperty.BatteryLow property types.

    ../../_images/properties-cluster-final.png
  7. In the Node Components, duplicate an existing Data Trigger and set:

    • Name to PopupAccidentAheadMaximized

    • Condition Expression to

      {@./Datatrigger.ActivityProperty.Cluster.MainView}==0 && {@./Datatrigger.ActivityProperty.AccidentAhead}==true
      

      Click Apply.

    • Activity Activation Path to the path of the PopupAccidentAheadMaximized Activity

    ../../_images/node-components-popupaccidentaheadmaximized.png
  8. Repeat the previous step for the PopupAccidentAheadMinimized, PopupBatteryLowMaximized, and PopupBatteryLowMinimized, but set the relevant name, expression, and Activity that the corresponding Data Trigger activates.

    ../../_images/node-components-popupaccidentahead-batterylow.png
  9. In the Prefabs, select the PopupControl > BatteryLow node. In the Properties, click + Add Binding and in the Binding Editor set:

    • Binding Mode to Two way

    • Property to Toggle State

    • Expression to

    {#Cluster/Datatrigger.ActivityProperty.BatteryLow}
    

    Click Save.

    ../../_images/prefabs-popupcontrol-batterylow.png ../../_images/binding-editor-batterylow.png
  10. Repeat the previous step for the PopupControl > AccidentAhead node, but set the binding expression to:

    {#Cluster/Datatrigger.ActivityProperty.AccidentAhead}
    
    ../../_images/prefabs-popupcontrol-accidentahead.png ../../_images/binding-editor-accidentahead.png

The completed tutorial project has the popup window functionality that you can control from the Cluster Activity Host for both the left- and right-hand areas. You can find the completed Kanzi Studio project in the <KanziWorkspace>/Tutorials/Data trigger/Completed directory.

../../_images/dt-complete.gif

See also

To learn more about how to use the Activity Browser, see Using the Activity Browser.

To learn more about the Activity system, see Activities.