Skip to content

Commit 98eaa57

Browse files
Merge pull request #812 from LedgerHQ/nbgl-improvements-to-factorize-code
NBGL improvements to factorize code
2 parents 96fbd22 + 58b0ac0 commit 98eaa57

File tree

3 files changed

+122
-9
lines changed

3 files changed

+122
-9
lines changed

lib_nbgl/include/nbgl_layout.h

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,17 @@ typedef nbgl_contentTagValueList_t nbgl_layoutTagValueList_t;
255255
*/
256256
typedef nbgl_contentCenteredInfo_t nbgl_layoutCenteredInfo_t;
257257

258+
/**
259+
* @brief This structure contains info to build a left content area.
260+
*
261+
*/
262+
typedef struct {
263+
uint8_t nbRows; ///< number of rows in the area
264+
const char *title; ///< title of area in bold
265+
const char **rowTexts; ///< array of nbRows texts (displayed in regular)
266+
const nbgl_icon_details_t **rowIcons; ///< array of nbRows icon
267+
} nbgl_layoutLeftContent_t;
268+
258269
#ifdef HAVE_SE_TOUCH
259270

260271
/**
@@ -583,6 +594,7 @@ typedef struct {
583594
nbgl_layout_t *nbgl_layoutGet(const nbgl_layoutDescription_t *description);
584595
int nbgl_layoutAddCenteredInfo(nbgl_layout_t *layout, const nbgl_layoutCenteredInfo_t *info);
585596
int nbgl_layoutAddContentCenter(nbgl_layout_t *layout, const nbgl_contentCenter_t *info);
597+
int nbgl_layoutAddLeftContent(nbgl_layout_t *layout, const nbgl_layoutLeftContent_t *info);
586598
int nbgl_layoutAddProgressBar(nbgl_layout_t *layout, const nbgl_layoutProgressBar_t *barLayout);
587599

588600
#ifdef HAVE_SE_TOUCH
@@ -605,7 +617,7 @@ int nbgl_layoutAddChoiceButtons(nbgl_layout_t *layout, const nbgl_layoutChoiceBu
605617
int nbgl_layoutAddHorizontalButtons(nbgl_layout_t *layout,
606618
const nbgl_layoutHorizontalButtons_t *info);
607619
int nbgl_layoutAddTagValueList(nbgl_layout_t *layout, const nbgl_layoutTagValueList_t *list);
608-
int nbgl_layoutAddLargeCaseText(nbgl_layout_t *layout, const char *text);
620+
int nbgl_layoutAddLargeCaseText(nbgl_layout_t *layout, const char *text, bool grayedOut);
609621
int nbgl_layoutAddTextContent(nbgl_layout_t *layout,
610622
const char *title,
611623
const char *description,

lib_nbgl/src/nbgl_layout.c

Lines changed: 106 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1367,13 +1367,14 @@ int nbgl_layoutAddSubHeaderText(nbgl_layout_t *layout, const char *text)
13671367
}
13681368

13691369
/**
1370-
* @brief Creates an area with given text in 32px font (in Black)
1370+
* @brief Creates an area with given text in 32px font (in Black or Light Gray)
13711371
*
13721372
* @param layout the current layout
13731373
* @param text text to be displayed (auto-wrap)
1374+
* @param grayedOut if true, use light-gray instead of black
13741375
* @return >= 0 if OK
13751376
*/
1376-
int nbgl_layoutAddLargeCaseText(nbgl_layout_t *layout, const char *text)
1377+
int nbgl_layoutAddLargeCaseText(nbgl_layout_t *layout, const char *text, bool grayedOut)
13771378
{
13781379
nbgl_layoutInternal_t *layoutInt = (nbgl_layoutInternal_t *) layout;
13791380
nbgl_text_area_t *textArea;
@@ -1384,7 +1385,7 @@ int nbgl_layoutAddLargeCaseText(nbgl_layout_t *layout, const char *text)
13841385
}
13851386
textArea = (nbgl_text_area_t *) nbgl_objPoolGet(TEXT_AREA, layoutInt->layer);
13861387

1387-
textArea->textColor = BLACK;
1388+
textArea->textColor = grayedOut ? LIGHT_GRAY : BLACK;
13881389
textArea->text = PIC(text);
13891390
textArea->textAlignment = MID_LEFT;
13901391
textArea->fontId = LARGE_MEDIUM_FONT;
@@ -1395,12 +1396,19 @@ int nbgl_layoutAddLargeCaseText(nbgl_layout_t *layout, const char *text)
13951396
textArea->style = NO_STYLE;
13961397
textArea->obj.alignment = NO_ALIGNMENT;
13971398
textArea->obj.alignmentMarginX = BORDER_MARGIN;
1398-
#ifdef TARGET_STAX
1399-
// if first object of container, increase the margin from top
14001399
if (layoutInt->container->nbChildren == 0) {
1400+
#ifdef TARGET_STAX
1401+
// if first object of container, increase the margin from top
14011402
textArea->obj.alignmentMarginY += BORDER_MARGIN;
1402-
}
14031403
#endif // TARGET_STAX
1404+
}
1405+
else {
1406+
#ifdef TARGET_STAX
1407+
textArea->obj.alignmentMarginY = 40; // 40px between paragraphs
1408+
#else // TARGET_STAX
1409+
textArea->obj.alignmentMarginY = 24; // 24px between paragraphs
1410+
#endif // TARGET_STAX
1411+
}
14041412

14051413
// set this new obj as child of main container
14061414
layoutAddObject(layoutInt, (nbgl_obj_t *) textArea);
@@ -1677,6 +1685,98 @@ int nbgl_layoutAddContentCenter(nbgl_layout_t *layout, const nbgl_contentCenter_
16771685
return container->obj.area.height;
16781686
}
16791687

1688+
/**
1689+
* @brief Creates an area with a title, and rows of icon + text, left aligned
1690+
*
1691+
* @param layout the current layout
1692+
* @param info structure giving the description of rows (number of rows, title, icons, texts)
1693+
* @return >= 0 if OK
1694+
*/
1695+
int nbgl_layoutAddLeftContent(nbgl_layout_t *layout, const nbgl_layoutLeftContent_t *info)
1696+
{
1697+
nbgl_layoutInternal_t *layoutInt = (nbgl_layoutInternal_t *) layout;
1698+
nbgl_container_t *container;
1699+
nbgl_text_area_t *textArea;
1700+
uint8_t row;
1701+
1702+
LOG_DEBUG(LAYOUT_LOGGER, "nbgl_layoutAddLeftContent():\n");
1703+
if (layout == NULL) {
1704+
return -1;
1705+
}
1706+
container = (nbgl_container_t *) nbgl_objPoolGet(CONTAINER, layoutInt->layer);
1707+
1708+
// get container children
1709+
container->nbChildren = info->nbRows + 1;
1710+
container->children = nbgl_containerPoolGet(container->nbChildren, layoutInt->layer);
1711+
container->layout = VERTICAL;
1712+
container->obj.area.width = AVAILABLE_WIDTH;
1713+
container->obj.alignmentMarginX = BORDER_MARGIN;
1714+
1715+
// create title
1716+
textArea = (nbgl_text_area_t *) nbgl_objPoolGet(TEXT_AREA, layoutInt->layer);
1717+
textArea->textColor = BLACK;
1718+
textArea->text = PIC(info->title);
1719+
textArea->textAlignment = MID_LEFT;
1720+
textArea->fontId = LARGE_MEDIUM_FONT;
1721+
textArea->wrapping = true;
1722+
textArea->obj.area.width = AVAILABLE_WIDTH;
1723+
textArea->obj.area.height = nbgl_getTextHeightInWidth(
1724+
textArea->fontId, textArea->text, textArea->obj.area.width, textArea->wrapping);
1725+
1726+
container->obj.area.height += textArea->obj.area.height;
1727+
1728+
container->children[0] = (nbgl_obj_t *) textArea;
1729+
1730+
for (row = 0; row < info->nbRows; row++) {
1731+
nbgl_container_t *rowContainer;
1732+
nbgl_image_t *image;
1733+
1734+
// create a grid with 1 icon on the left column and 1 text on the right one
1735+
rowContainer = (nbgl_container_t *) nbgl_objPoolGet(CONTAINER, 0);
1736+
rowContainer->nbChildren = 2; // 1 icon + 1 text
1737+
rowContainer->children = nbgl_containerPoolGet(rowContainer->nbChildren, 0);
1738+
rowContainer->obj.area.width = AVAILABLE_WIDTH;
1739+
1740+
image = (nbgl_image_t *) nbgl_objPoolGet(IMAGE, 0);
1741+
image->foregroundColor = BLACK;
1742+
image->buffer = info->rowIcons[row];
1743+
rowContainer->children[0] = (nbgl_obj_t *) image;
1744+
1745+
textArea = (nbgl_text_area_t *) nbgl_objPoolGet(TEXT_AREA, 0);
1746+
textArea->textColor = BLACK;
1747+
textArea->text = info->rowTexts[row];
1748+
textArea->textAlignment = MID_LEFT;
1749+
textArea->fontId = SMALL_REGULAR_FONT;
1750+
textArea->wrapping = true;
1751+
textArea->obj.alignmentMarginX = 16;
1752+
textArea->obj.area.width
1753+
= AVAILABLE_WIDTH - image->buffer->width - textArea->obj.alignmentMarginX;
1754+
textArea->obj.area.height = nbgl_getTextHeightInWidth(
1755+
textArea->fontId, textArea->text, textArea->obj.area.width, textArea->wrapping);
1756+
textArea->obj.alignment = RIGHT_TOP;
1757+
textArea->obj.alignTo = (nbgl_obj_t *) image;
1758+
rowContainer->children[1] = (nbgl_obj_t *) textArea;
1759+
rowContainer->obj.area.height = MAX(image->buffer->height, textArea->obj.area.height);
1760+
1761+
if (row == 0) {
1762+
rowContainer->obj.alignmentMarginY = 32;
1763+
}
1764+
else {
1765+
#ifdef TARGET_STAX
1766+
rowContainer->obj.alignmentMarginY = 16;
1767+
#else // TARGET_STAX
1768+
rowContainer->obj.alignmentMarginY = 26;
1769+
#endif // TARGET_STAX
1770+
}
1771+
container->children[1 + row] = (nbgl_obj_t *) rowContainer;
1772+
container->obj.area.height
1773+
+= rowContainer->obj.area.height + rowContainer->obj.alignmentMarginY;
1774+
}
1775+
layoutAddObject(layoutInt, (nbgl_obj_t *) container);
1776+
1777+
return container->obj.area.height;
1778+
}
1779+
16801780
#ifdef NBGL_QRCODE
16811781
/**
16821782
* @brief Creates an area on the center of the main panel, with a QRCode,

lib_nbgl/src/nbgl_touch.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,7 @@ void nbgl_touchHandler(bool fromUx,
330330
nbgl_touchType_t swipe = nbgl_detectSwipe(touchStatePosition, &ctx->firstTouchedPosition);
331331
bool consumed = false;
332332

333+
ctx->lastState = touchStatePosition->state;
333334
if (swipe != NB_TOUCH_TYPES) {
334335
// Swipe detected
335336
nbgl_obj_t *swipedObj = getSwipableObject(
@@ -359,6 +360,7 @@ void nbgl_touchHandler(bool fromUx,
359360
}
360361
else { // PRESSED
361362
if ((ctx->lastState == PRESSED) && (ctx->lastPressedObj != NULL)) {
363+
ctx->lastState = touchStatePosition->state;
362364
if (foundObj != ctx->lastPressedObj) {
363365
// finger has moved out of an object
364366
// make sure lastPressedObj still belongs to current screen before warning it
@@ -373,15 +375,14 @@ void nbgl_touchHandler(bool fromUx,
373375
}
374376
}
375377
else if (ctx->lastState == RELEASED) {
378+
ctx->lastState = touchStatePosition->state;
376379
// newly touched object
377380
ctx->lastPressedObj = foundObj;
378381
ctx->lastPressedTime = currentTime;
379382
applytouchStatePosition(foundObj, TOUCH_PRESSED);
380383
applytouchStatePosition(foundObj, TOUCHING);
381384
}
382385
}
383-
384-
ctx->lastState = touchStatePosition->state;
385386
}
386387

387388
bool nbgl_touchGetTouchedPosition(nbgl_obj_t *obj,

0 commit comments

Comments
 (0)