Class: Toybox::WatchUi::View

Inherits:
Lang::Object show all

Overview

A View is an object that represents a page within an app.

An app may have multiple View objects representing things like menus and other app states. Each View contains a Layout, which in turn contain Drawable objects, such as Bitmaps and Text. View objects also handle the lifecycle of each app, which varies depending on the app type:

Widgets and Watch Apps

onLayout()onShow()onUpdate()onHide()

Watch Faces

onLayout()onShow()onUpdate()

Data Fields

onLayout()onShow()onUpdate()

If the size of the data field has changed since the last onUpdate(), onLayout() will be called prior to onUpdate(). However, onLayout(), onShow(), and onUpdate() are not called for SimpleDataField objects.

Examples:

A basic widget View class definition

using Toybox.WatchUi;

class MyWidgetView extends WatchUi.View {
    function initialize() {
        View.initialize();
    }

    // Resources are loaded here
    function onLayout(dc) {
        setLayout(Rez.Layouts.MainLayout(dc));
    }

    // onShow() is called when this View is brought to the foreground
    function onShow() {
    }

    // onUpdate() is called periodically to update the View
    function onUpdate(dc) {
        View.onUpdate(dc);
    }

    // onHide() is called when this View is removed from the screen
    function onHide() {
    }
}

See Also:

Since:

  • 1.0.0

Direct Known Subclasses

DataField, Picker, WatchFace

Direct Known Subclasses

DataField, Picker, WatchFace

Instance Method Summary collapse

Methods inherited from Lang::Object

equals, hashCode, method, toString, weak

Constructor Details

initializeView

The View constructor.

Since:

  • 2.1.0



9284
9285
# File 'Monkeybrains.rb', line 9284

def initialize()
end

Instance Method Details

findDrawableById(id) ⇒ Toybox::WatchUi::Drawable

Find a Drawable by its ID.

A common use for this method is to get layout information to format dynamic content, such as a string that updates at runtime.

Examples:

Formatting the clock time as centered, blue text

// The layout.xml file contents:
// <layout id="WatchFace">
//      <label id="TimeLabel" x="center" y="center" font="Graphics.FONT_LARGE" justification="Graphics.TEXT_JUSTIFY_CENTER" color="Graphics.COLOR_BLUE" />
// </layout>

using Toybox.Graphics;
using Toybox.Lang;
using Toybox.System;
using Toybox.WatchUi.View;

var clockTime = System.getClockTime();
var timeString = Lang.format(
    "$1$:$2$",
    [clockTime.hour, clockTime.min.format("%02d")]
);
var view = View.findDrawableById("TimeLabel");
view.setText(timeString);

Parameters:

Returns:

See Also:

Since:

  • 1.0.0



9314
9315
# File 'Monkeybrains.rb', line 9314

def findDrawableById(id)
end

onHideObject

Hide the View.

This is called before the View is removed from the foreground. This occurs when a new View object is pushed on top of the current one, when the current View is popped, or when the app is closed. Resources should be freed from memory at this point if the current View will be left on the page stack.

Since:

  • 1.0.0



9363
9364
# File 'Monkeybrains.rb', line 9363

def onHide()
end

onLayout(dc) ⇒ Toybox::Lang::Boolean

The entry point for the View.

onLayout() is called before the View is shown to load resources and set up the layout of the View.

Parameters:

Returns:

Since:

  • 1.0.0



9324
9325
# File 'Monkeybrains.rb', line 9324

def onLayout(dc)
end

onShowToybox::Lang::Boolean

Show the View.

This is called when the View is brought into the foreground. Resources should be loaded into system memory for use in the View at this point.

Returns:

Since:

  • 1.0.0



9333
9334
# File 'Monkeybrains.rb', line 9333

def onShow()
end

onUpdate(dc) ⇒ Toybox::Lang::Boolean

Update the View.

This is called when a View is brought to the foreground, after the call to onShow(). While a View is active, this method is generally used to update dynamic content in the View. There are also some special cases when it will be invoked:

  • On WatchUi.requestUpdate() calls within Widgets and Watch Apps

  • Once per minute in Watch Faces when in low power mode

  • Once per second in Watch Faces when in high power mode

  • Once per second in Data Fields

If a class that extends View does not implement this function then any Drawable objects contained in the View will automatically be drawn.

Parameters:

Returns:

See Also:

Since:

  • 1.0.0



9352
9353
# File 'Monkeybrains.rb', line 9352

def onUpdate(dc)
end

setKeyToSelectableInteraction(enable) ⇒ Object

Enter Selectable interaction mode.

When enabled, physical buttons may be used to cycle through on-screen Selectable objects. The first registered Selectable in the current layout will be highlighted initially.

Examples:

Toggle the Selectable interaction mode with the Menu button

var selectableMode = false;
function onMenu() {
    selectableMode = !selectableMode;
    // currentView is a View containing Selectable objects
    currentView.setKeyToSelectableInteraction(selectableMode);
    return true;
}

Parameters:

Since:

  • 2.1.0



9391
9392
# File 'Monkeybrains.rb', line 9391

def setKeyToSelectableInteraction(enable)
end

setLayout(layout) ⇒ Object

Set the layout for the View.

If a class that extends View does not implement this function then any Drawable objects contained in the View will automatically be drawn.

Parameters:

Since:

  • 1.0.0



9372
9373
# File 'Monkeybrains.rb', line 9372

def setLayout(layout)
end