3.9 KiB
3.9 KiB
Bangle.js Layout Library
Take a look at README.md for hints on developing with this library.
Usage
var Layout = require("Layout");
var layout = new Layout(layoutObject, options)
layout.render(optionalObject);
For example:
var Layout = require("Layout");
var layout = new Layout({
type:"v",
c: [
{ type: "txt", font: "20%", label: "12:00" },
{ type: "txt", font: "6x8", label: "The Date" }
]
});
g.clear();
layout.render();
layoutObject (first argument) has:
- A
typefield of:undefined- blank, can be used for padding"txt"- a text label, with valuelabel.fontis required"btn"- a button, with valuelabeland callbackcb. Optionalsrcspecifies an image (like img) in which case label is ignored. Default font is6x8, scale 2. This can be overridden with thefontorscalefields."img"- an image wheresrcis an image, or a function which is called to return an image to draw"custom"- a custom block whererender(layoutObj)is called to render"h"- Horizontal layout,cis an array of morelayoutObject"v"- Vertical layout,cis an array of morelayoutObject
- A
idfield. If specified the object is added with this name to the returnedlayoutobject, so can be referenced aslayout.foo - A
fontfield, eg6x8or30%to use a percentage of screen height. Set scale with :, e.g.6x8:2. - A
scalefield, eg2to set scale of an image - A
rfield to set rotation of text or images (0: 0°, 1: 90°, 2: 180°, 3: 270°). - A
wrapfield to enable line wrapping. Requires some combination ofwidth/heightandfillx/fillyto be set. Not compatible with text rotation. - A
colfield, eg#f00for red - A
bgColfield for background color (will automatically fill on render). Whentype:"btn", this sets the background color of the button, and will not change color on press - A
btnBorderfield for button border color (will default to theme if not set) - A
halignfield to set horizontal alignment WITHIN avcontainer.-1=left,1=right,0=center - A
valignfield to set vertical alignment WITHIN ahcontainer.-1=top,1=bottom,0=center - A
padinteger field to set pixels padding - A
fillxint to choose if the object should fill available space in x. 0=no, 1=yes, 2=2x more space - A
fillyint to choose if the object should fill available space in y. 0=no, 1=yes, 2=2x more space widthandheightfields to optionally specify minimum size
options (second argument) is an object containing:
lazy- a boolean specifying whether to enable automatic lazy renderingbtns- array of objects containing:label- the text on the buttoncb- a callback functioncbl- a callback function for long presses
back- a callback function, passed asbackinto Bangle.setUI (which usually adds an icon in the top left)remove- a cleanup function, passed asremoveinto Bangle.setUI (allows to cleanly remove the app from memory)
If automatic lazy rendering is enabled, calls to layout.render() will attempt to automatically determine what objects have changed or moved, clear their previous locations, and re-render just those objects.
Once layout.update() is called, the following fields are added to each object:
xandyfor the top left positionwandhfor the width and height_wand_hfor the minimum width and height
Other functions:
layout.update()- update positions of everything if contents have changedlayout.debug(obj)- draw outlines for objects on screenlayout.clear(obj)- clear the given object (you can also just specifybgColto clear before each render)layout.forgetLazyState()- if lazy rendering is enabled, makes the next call torender()perform a full re-render