From aa2f01f4b250a122f2531c0586287e69d1ddd704 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jaakko=20Kera=CC=88nen?= Date: Wed, 23 Nov 2022 21:41:04 +0200 Subject: [PATCH 1/1] LabelWidget: Flag for truncating to fit Useful for over-long labels like the identity dropdown button. --- src/ui/labelwidget.c | 26 +++++++++++++++++++------- src/ui/labelwidget.h | 1 + src/ui/uploadwidget.c | 1 + 3 files changed, 21 insertions(+), 7 deletions(-) diff --git a/src/ui/labelwidget.c b/src/ui/labelwidget.c index f5c0dad2..69b9092a 100644 --- a/src/ui/labelwidget.c +++ b/src/ui/labelwidget.c @@ -55,6 +55,7 @@ struct Impl_LabelWidget { uint16_t removeTrailingColon : 1; uint16_t chevron : 1; uint16_t checkMark : 1; + uint16_t truncateToFit : 1; } flags; }; @@ -460,8 +461,19 @@ static void draw_LabelWidget_(const iLabelWidget *d) { draw_WrapText(&wt, d->font, topLeft_Rect(cont), fg); } else if (flags & alignLeft_WidgetFlag) { - draw_Text(d->font, add_I2(bounds.pos, addX_I2(padding_LabelWidget_(d, 0), iconPad)), - fg, "%s", cstr_String(&d->label)); + const iInt2 topLeft = add_I2(bounds.pos, addX_I2(padding_LabelWidget_(d, 0), iconPad)); + if (d->flags.truncateToFit) { + const char *endPos; + tryAdvanceNoWrap_Text(d->font, + range_String(&d->label), + width_Rect(rect) - padding_LabelWidget_(d, 0).x - + padding_LabelWidget_(d, 1).x - iconPad, + &endPos); + drawRange_Text(d->font, topLeft, fg, (iRangecc){ constBegin_String(&d->label), endPos }); + } + else { + draw_Text(d->font, topLeft, fg, "%s", cstr_String(&d->label)); + } if ((flags & drawKey_WidgetFlag) && d->key) { iString str; init_String(&str); @@ -470,11 +482,7 @@ static void draw_LabelWidget_(const iLabelWidget *d) { add_I2(topRight_Rect(bounds), addX_I2(negX_I2(padding_LabelWidget_(d, 1)), deviceType_App() == tablet_AppDeviceType ? gap_UI : 0)), - metaColor,/* - isHover || flags & pressed_WidgetFlag ? fg -// : isCaution ? uiTextCaution_ColorId - : colorEscape != none_ColorId ? colorEscape - : uiTextShortcut_ColorId,*/ + metaColor, right_Alignment, "%s", cstr_String(&str)); @@ -683,6 +691,10 @@ void setWrap_LabelWidget(iLabelWidget *d, iBool wrap) { d->flags.wrap = wrap; } +void setTruncateToFit_LabelWidget (iLabelWidget *d, iBool truncateToFit) { + d->flags.truncateToFit = truncateToFit; +} + void setOutline_LabelWidget(iLabelWidget *d, iBool drawAsOutline) { if (d) { d->flags.drawAsOutline = drawAsOutline; diff --git a/src/ui/labelwidget.h b/src/ui/labelwidget.h index bc861834..fbc83d2d 100644 --- a/src/ui/labelwidget.h +++ b/src/ui/labelwidget.h @@ -36,6 +36,7 @@ void setNoBottomFrame_LabelWidget(iLabelWidget *, iBool noBottomFrame); void setChevron_LabelWidget (iLabelWidget *, iBool chevron); void setCheckMark_LabelWidget (iLabelWidget *, iBool checkMark); void setWrap_LabelWidget (iLabelWidget *, iBool wrap); +void setTruncateToFit_LabelWidget(iLabelWidget *, iBool truncateToFit); void setOutline_LabelWidget (iLabelWidget *, iBool drawAsOutline); void setAllCaps_LabelWidget (iLabelWidget *, iBool allCaps); void setRemoveTrailingColon_LabelWidget (iLabelWidget *, iBool removeTrailingColon); diff --git a/src/ui/uploadwidget.c b/src/ui/uploadwidget.c index c6d6cb27..bf840d7b 100644 --- a/src/ui/uploadwidget.c +++ b/src/ui/uploadwidget.c @@ -177,6 +177,7 @@ iLabelWidget *makeIdentityDropdown_LabelWidget(iWidget *headings, iWidget *value iLabelWidget *ident = makeMenuButton_LabelWidget(label, items, numItems); setFixedSize_Widget(as_Widget(ident), init_I2(-1, lineHeight_Text(uiLabel_FontId) + 2 * gap_UI)); setTextCStr_LabelWidget(ident, items[findWidestLabel_MenuItem(items, numItems)].label); + setTruncateToFit_LabelWidget(ident, iTrue); iWidget *identHeading = addChild_Widget(headings, iClob(makeHeading_Widget(label))); identHeading->sizeRef = as_Widget(ident); setId_Widget(addChildFlags_Widget(values, iClob(ident), alignLeft_WidgetFlag), id); -- 2.34.1