View ID

It is quite often that we need to update values of the view properties but to do this we need to have a reference to the view or know it's ID. View ID can be set by "id" property, it is an optional textual identifier of the view. Usually we use that property to be able to find a particular view of the application user interface to read or set its properties. To find the view by its ID we use a global rui.ViewByID() function which will return a view interface.

Example

RUI

// Root container
ListLayout {
    id = root-layout,
    orientation = up-down,
    width = 100%,
    height = 100%,

    // Children
    content = [
        ListLayout {
            id = buttons-layout,
            orientation = up-down,
            horizontal-align = center,
            content = Button {
                id = button,
                content = "Push me!",
            },
        },
    ],
}

Go

view := rui.ViewByID(session.RootView(), "button")

In the example above, the function is used to get the reference of the view with the ID "button". The search starts from the root view which is a top most ListLayout element. If view has not been found, the function returns nil and an error message is written to the log.

When searching, we can specify a chain of identifiers. In this case, they must be separated by the / character.

Example

Go

view := rui.ViewByID(session.RootView(), "buttons-layout/button")

This code is equivalent to

Go

var button rui.View = nil
if buttonsLayout := rui.ViewByID(rootView, "buttons-layout"); buttonsLayout != nil {
    button = rui.ViewByID(buttonsLayout, "button")
}

We can also provide several view ID's to the ViewByID() function, which might be convenient for some users

Go

if button := rui.ViewByID(rootView, "buttons-layout", "button"); button != nil {
    // ...
}

Usually ID is set when the view is created and is not changed afterwards. But it can be changed at any time if we need it.

Example

Go

view.Set(rui.ID, "myView")

where "view" is a reference to the View interface.

To get the "id" property value of the view we can use rui.Get() or ID() functions.

Examples

Go

if value := view.Get(rui.ID); value != nil {
    id = value.(string)
}

Go

id = view.ID()

When we got the reference to the view we can further manipulate it's properties. As an example the code to hide the button may look like this:

Go

if view := rui.ViewByID(session.RootView(), "button"); view != nil {
    view.Set(rui.Visibility, rui.Gone)
}

It is not necessary to provide a whole path of the IDs to get the desired view reference. We can make the path which will clearly point to a specific view of the hierarchy and does not contain all the IDs. Lets explain this using a more complex example:

Go

view := rui.CreateListLayout {
    rui.ID     : "list1",
    rui.Content: []rui.View {
        rui.CreateListLayout {
            rui.ID     : "list2",
            rui.Content: []rui.View {
                rui.CreateListLayout {
                    rui.ID     : "list3_1",
                    rui.Content: []rui.View {
                        rui.CreateListLayout {
                            rui.ID     : "list4",
                            rui.Content: []rui.View {
                                rui.CreateListLayout {
                                    rui.ID: "list5",
                                },
                            },
                        },
                    },  
                },
                rui.CreateListLayout {
                    rui.ID     : "list3_2",
                    rui.Content: []rui.View {
                        rui.CreateListLayout {
                            rui.ID     : "list4",
                            rui.Content: []rui.View {
                                rui.CreateListLayout {
                                    rui.ID: "list5",
                                },
                            },
                        },
                    },  
                },
            },
        },
    },
}

To get the reference of the view from the second branch we may use the path "list2/list3_2/list4/list5" but at the same time it will be sufficient for the library to find that view reference by providing a more simpler "list3_2/list5" path.