From 540dbf85fa37848eb9d21968cbab60428e5d2855 Mon Sep 17 00:00:00 2001 From: Charles-Edouard de la Vergne Date: Mon, 26 Feb 2024 14:48:25 +0100 Subject: [PATCH] Update nbgl_layoutAddTouchableBar for subtext rendering --- lib_nbgl/src/nbgl_layout.c | 134 +++++++++++++++++++------------------ 1 file changed, 70 insertions(+), 64 deletions(-) diff --git a/lib_nbgl/src/nbgl_layout.c b/lib_nbgl/src/nbgl_layout.c index 3b1688d12..e6a858f05 100644 --- a/lib_nbgl/src/nbgl_layout.c +++ b/lib_nbgl/src/nbgl_layout.c @@ -937,9 +937,9 @@ int nbgl_layoutAddTouchableBar(nbgl_layout_t *layout, const nbgl_layoutBar_t *ba nbgl_layoutInternal_t *layoutInt = (nbgl_layoutInternal_t *) layout; layoutObj_t *obj; nbgl_text_area_t *textArea; - nbgl_image_t *imageLeft = NULL, *imageRight = NULL; nbgl_container_t *container; - color_t color = (barLayout->inactive != true) ? BLACK : LIGHT_GRAY; + color_t color = (barLayout->inactive != true) ? BLACK : LIGHT_GRAY; + int16_t usedHeight = 0; LOG_DEBUG(LAYOUT_LOGGER, "nbgl_layoutAddTouchableBar():\n"); if (layout == NULL) { @@ -952,7 +952,7 @@ int nbgl_layoutAddTouchableBar(nbgl_layout_t *layout, const nbgl_layoutBar_t *ba return -1; } - // get container children (up to 4) + // get container children (up to 4: text + left+right icons + sub text) container->children = nbgl_containerPoolGet(4, layoutInt->layer); container->nbChildren = 0; @@ -971,82 +971,88 @@ int nbgl_layoutAddTouchableBar(nbgl_layout_t *layout, const nbgl_layoutBar_t *ba nbTouchableControls++; } + // allocate main text because always present + textArea = (nbgl_text_area_t *) nbgl_objPoolGet(TEXT_AREA, layoutInt->layer); + textArea->textColor = color; + textArea->text = (barLayout->text != NULL) ? PIC(barLayout->text) : ""; + textArea->onDrawCallback = NULL; + textArea->fontId = SMALL_BOLD_FONT; + textArea->wrapping = true; + textArea->obj.area.width = container->obj.area.width; if (barLayout->iconLeft != NULL) { - imageLeft = (nbgl_image_t *) nbgl_objPoolGet(IMAGE, layoutInt->layer); + // reduce text width accordingly + textArea->obj.area.width -= ((nbgl_icon_details_t *) PIC(barLayout->iconLeft))->width + 16; + } + if (barLayout->iconRight != NULL) { + // reduce text width accordingly + textArea->obj.area.width -= ((nbgl_icon_details_t *) PIC(barLayout->iconRight))->width; + } + textArea->obj.area.height = nbgl_getTextHeightInWidth( + textArea->fontId, textArea->text, textArea->obj.area.width, textArea->wrapping); + usedHeight = textArea->obj.area.height; + textArea->style = NO_STYLE; + textArea->obj.alignment = MID_LEFT; + textArea->textAlignment = MID_LEFT; + container->children[container->nbChildren] = (nbgl_obj_t *) textArea; + container->nbChildren++; + + // allocate left icon if present + if (barLayout->iconLeft != NULL) { + nbgl_image_t *imageLeft = (nbgl_image_t *) nbgl_objPoolGet(IMAGE, layoutInt->layer); imageLeft->foregroundColor = color; imageLeft->buffer = PIC(barLayout->iconLeft); - imageLeft->obj.alignment = MID_LEFT; - imageLeft->obj.alignTo = (nbgl_obj_t *) NULL; + // align at the left of text + imageLeft->obj.alignment = MID_LEFT; + imageLeft->obj.alignTo = (nbgl_obj_t *) textArea; + imageLeft->obj.alignmentMarginX = 16; container->children[container->nbChildren] = (nbgl_obj_t *) imageLeft; container->nbChildren++; - } - if (barLayout->text != NULL) { - textArea = (nbgl_text_area_t *) nbgl_objPoolGet(TEXT_AREA, layoutInt->layer); - textArea->textColor = color; - textArea->text = PIC(barLayout->text); - textArea->onDrawCallback = NULL; - textArea->fontId = SMALL_BOLD_FONT; - textArea->obj.area.width = container->obj.area.width; - if (barLayout->iconLeft != NULL) { - textArea->obj.area.width -= imageLeft->buffer->width + 12; - } - if (barLayout->iconRight != NULL) { - textArea->obj.area.width -= ((nbgl_icon_details_t *) PIC(barLayout->iconRight))->width; - } - textArea->obj.area.height = container->obj.area.height; - textArea->style = NO_STYLE; - if ((barLayout->iconLeft != NULL) && (barLayout->centered != true)) { - textArea->obj.alignmentMarginX = 12; - } - if (barLayout->iconLeft != NULL) { - textArea->obj.alignTo = (nbgl_obj_t *) imageLeft; - textArea->obj.alignment = MID_RIGHT; - } - else { - textArea->obj.alignTo = (nbgl_obj_t *) NULL; - textArea->obj.alignment = NO_ALIGNMENT; - } - if (barLayout->centered != true) { - textArea->textAlignment = MID_LEFT; - } - else { - textArea->textAlignment = CENTER; + + textArea->obj.alignmentMarginX = imageLeft->buffer->width + 16; + + if (imageLeft->buffer->height > usedHeight) { + usedHeight = imageLeft->buffer->height; } - textArea->wrapping = true; - container->children[container->nbChildren] = (nbgl_obj_t *) textArea; - container->nbChildren++; } + // allocate right icon if present if (barLayout->iconRight != NULL) { - imageRight = (nbgl_image_t *) nbgl_objPoolGet(IMAGE, layoutInt->layer); + nbgl_image_t *imageRight = (nbgl_image_t *) nbgl_objPoolGet(IMAGE, layoutInt->layer); imageRight->foregroundColor = color; imageRight->buffer = PIC(barLayout->iconRight); - imageRight->obj.alignment = MID_RIGHT; - if (barLayout->text == NULL) { - imageRight->obj.alignTo = (nbgl_obj_t *) NULL; - } - else { - imageRight->obj.alignTo = (nbgl_obj_t *) container->children[container->nbChildren - 1]; - } + // align at the right of text + imageRight->obj.alignment = MID_RIGHT; + imageRight->obj.alignTo = (nbgl_obj_t *) textArea; + container->children[container->nbChildren] = (nbgl_obj_t *) imageRight; container->nbChildren++; + + if (imageRight->buffer->height > usedHeight) { + usedHeight = imageRight->buffer->height; + } } if (barLayout->subText != NULL) { - textArea = (nbgl_text_area_t *) nbgl_objPoolGet(TEXT_AREA, layoutInt->layer); - - textArea->textColor = BLACK; - textArea->text = PIC(barLayout->subText); - textArea->textAlignment = MID_LEFT; - textArea->fontId = SMALL_REGULAR_FONT; - textArea->style = NO_STYLE; - textArea->wrapping = true; - textArea->obj.alignment = BOTTOM_LEFT; - textArea->obj.alignmentMarginY = BORDER_MARGIN; - textArea->obj.area.width = container->obj.area.width; - textArea->obj.area.height = nbgl_getTextHeightInWidth( - textArea->fontId, textArea->text, textArea->obj.area.width, textArea->wrapping); - container->children[container->nbChildren] = (nbgl_obj_t *) textArea; + nbgl_text_area_t *subTextArea + = (nbgl_text_area_t *) nbgl_objPoolGet(TEXT_AREA, layoutInt->layer); + + subTextArea->textColor = color; + subTextArea->text = PIC(barLayout->subText); + subTextArea->textAlignment = MID_LEFT; + subTextArea->fontId = SMALL_REGULAR_FONT; + subTextArea->style = NO_STYLE; + subTextArea->wrapping = true; + subTextArea->obj.alignment = MID_LEFT; + subTextArea->obj.area.width = container->obj.area.width; + subTextArea->obj.area.height = nbgl_getTextHeightInWidth(subTextArea->fontId, + subTextArea->text, + subTextArea->obj.area.width, + subTextArea->wrapping); + container->children[container->nbChildren] = (nbgl_obj_t *) subTextArea; container->nbChildren++; - container->obj.area.height += textArea->obj.area.height + 16; + container->obj.area.height += subTextArea->obj.area.height + 4; + + // modify alignments to have sub-text under (icon left - text - icon right) + textArea->obj.alignmentMarginY = -(subTextArea->obj.area.height + 4) / 2; + subTextArea->obj.alignmentMarginY = (usedHeight + 4) / 2; } // set this new container as child of main container