[FL-2475] Text Box add three dots trim option (#1136)

* introduce text box debug application
* text box: add strip to dots option
* applications: update text box usage

Co-authored-by: あく <alleteam@gmail.com>
This commit is contained in:
gornekich
2022-04-19 22:23:50 +03:00
committed by GitHub
parent 57312961e8
commit 9351076c89
17 changed files with 226 additions and 62 deletions

View File

@@ -547,7 +547,8 @@ void elements_text_box(
uint8_t height,
Align horizontal,
Align vertical,
const char* text) {
const char* text,
bool strip_to_dots) {
furi_assert(canvas);
ElementTextBoxLine line[ELEMENTS_MAX_LINES_NUM];
@@ -571,6 +572,7 @@ void elements_text_box(
uint8_t total_height_default = 0;
uint16_t i = 0;
bool full_text_processed = false;
uint16_t dots_width = canvas_string_width(canvas, "...");
canvas_set_font(canvas, FontSecondary);
@@ -663,31 +665,29 @@ void elements_text_box(
}
// Set vertical alignment for all lines
if(full_text_processed) {
if(total_height_default < height) {
if(vertical == AlignTop) {
line[0].y = y + line[0].height;
} else if(vertical == AlignCenter) {
line[0].y = y + line[0].height + (height - total_height_default) / 2;
} else if(vertical == AlignBottom) {
line[0].y = y + line[0].height + (height - total_height_default);
}
if(line_num > 1) {
for(uint8_t i = 1; i < line_num; i++) {
line[i].y = line[i - 1].y + line[i - 1].leading_default;
}
}
} else if(line_num > 1) {
uint8_t free_pixel_num = height - total_height_min;
uint8_t fill_pixel = 0;
uint8_t j = 1;
if(total_height_default < height) {
if(vertical == AlignTop) {
line[0].y = y + line[0].height;
while(fill_pixel < free_pixel_num) {
line[j].y = line[j - 1].y + line[j - 1].leading_min + 1;
fill_pixel++;
j = j % (line_num - 1) + 1;
} else if(vertical == AlignCenter) {
line[0].y = y + line[0].height + (height - total_height_default) / 2;
} else if(vertical == AlignBottom) {
line[0].y = y + line[0].height + (height - total_height_default);
}
if(line_num > 1) {
for(uint8_t i = 1; i < line_num; i++) {
line[i].y = line[i - 1].y + line[i - 1].leading_default;
}
}
} else if(line_num > 1) {
uint8_t free_pixel_num = height - total_height_min;
uint8_t fill_pixel = 0;
uint8_t j = 1;
line[0].y = y + line[0].height;
while(fill_pixel < free_pixel_num) {
line[j].y = line[j - 1].y + line[j - 1].leading_min + 1;
fill_pixel++;
j = j % (line_num - 1) + 1;
}
}
// Draw line by line
@@ -733,10 +733,17 @@ void elements_text_box(
canvas_draw_glyph(canvas, line[i].x, line[i].y, line[i].text[j]);
canvas_invert_color(canvas);
} else {
if((i == line_num - 1) && strip_to_dots) {
uint8_t next_symbol_width = canvas_glyph_width(canvas, line[i].text[j]);
if(line[i].x + next_symbol_width + dots_width > x + width) {
canvas_draw_str(canvas, line[i].x, line[i].y, "...");
break;
}
}
canvas_draw_glyph(canvas, line[i].x, line[i].y, line[i].text[j]);
}
line[i].x += canvas_glyph_width(canvas, line[i].text[j]);
}
}
canvas_set_font(canvas, FontSecondary);
}
}

View File

@@ -194,17 +194,18 @@ void elements_string_fit_width(Canvas* canvas, string_t string, uint8_t width);
/** Draw text box element
*
* @param canvas Canvas instance
* @param x x coordinate
* @param y y coordinate
* @param width width to fit text
* @param height height to fit text
* @param horizontal Align instance
* @param vertical Align instance
* @param[in] text Formatted text. The following formats are available:
* "\e#Bold text\e#" - bold font is used
* "\e*Monospaced text\e*" - monospaced font is used
* "\e#Inversed text\e#" - white text on black background
* @param canvas Canvas instance
* @param x x coordinate
* @param y y coordinate
* @param width width to fit text
* @param height height to fit text
* @param horizontal Align instance
* @param vertical Align instance
* @param[in] text Formatted text. The following formats are available:
* "\e#Bold text\e#" - bold font is used
* "\e*Monospaced text\e*" - monospaced font is used
* "\e#Inversed text\e#" - white text on black background
* @param strip_to_dots Strip text to ... if does not fit to width
*/
void elements_text_box(
Canvas* canvas,
@@ -214,7 +215,8 @@ void elements_text_box(
uint8_t height,
Align horizontal,
Align vertical,
const char* text);
const char* text,
bool strip_to_dots);
#ifdef __cplusplus
}

7
applications/gui/modules/widget.c Executable file → Normal file
View File

@@ -154,10 +154,11 @@ void widget_add_text_box_element(
uint8_t height,
Align horizontal,
Align vertical,
const char* text) {
const char* text,
bool strip_to_dots) {
furi_assert(widget);
WidgetElement* text_box_element =
widget_element_text_box_create(x, y, width, height, horizontal, vertical, text);
WidgetElement* text_box_element = widget_element_text_box_create(
x, y, width, height, horizontal, vertical, text, strip_to_dots);
widget_add_element(widget, text_box_element);
}

View File

@@ -81,17 +81,18 @@ void widget_add_string_element(
/** Add Text Box Element
*
* @param widget Widget instance
* @param x x coordinate
* @param y y coordinate
* @param width width to fit text
* @param height height to fit text
* @param horizontal Align instance
* @param vertical Align instance
* @param[in] text Formatted text. The following formats are available:
* "\e#Bold text\e#" - bold font is used
* "\e*Monospaced text\e*" - monospaced font is used
* "\e#Inversed text\e#" - white text on black background
* @param widget Widget instance
* @param x x coordinate
* @param y y coordinate
* @param width width to fit text
* @param height height to fit text
* @param horizontal Align instance
* @param vertical Align instance
* @param[in] text Formatted text. The following formats are available:
* "\e#Bold text\e#" - bold font is used
* "\e*Monospaced text\e*" - monospaced font is used
* "\e#Inversed text\e#" - white text on black background
* @param strip_to_dots Strip text to ... if does not fit to width
*/
void widget_add_text_box_element(
Widget* widget,
@@ -101,7 +102,8 @@ void widget_add_text_box_element(
uint8_t height,
Align horizontal,
Align vertical,
const char* text);
const char* text,
bool strip_to_dots);
/** Add Button Element
*

View File

@@ -60,7 +60,8 @@ WidgetElement* widget_element_text_box_create(
uint8_t height,
Align horizontal,
Align vertical,
const char* text);
const char* text,
bool strip_to_dots);
/** Create button element */
WidgetElement* widget_element_button_create(

View File

@@ -10,6 +10,7 @@ typedef struct {
Align horizontal;
Align vertical;
string_t text;
bool strip_to_dots;
} GuiTextBoxModel;
static void gui_text_box_draw(Canvas* canvas, WidgetElement* element) {
@@ -26,7 +27,8 @@ static void gui_text_box_draw(Canvas* canvas, WidgetElement* element) {
model->height,
model->horizontal,
model->vertical,
string_get_cstr(model->text));
string_get_cstr(model->text),
model->strip_to_dots);
}
}
@@ -46,7 +48,8 @@ WidgetElement* widget_element_text_box_create(
uint8_t height,
Align horizontal,
Align vertical,
const char* text) {
const char* text,
bool strip_to_dots) {
furi_assert(text);
// Allocate and init model
@@ -58,6 +61,7 @@ WidgetElement* widget_element_text_box_create(
model->horizontal = horizontal;
model->vertical = vertical;
string_init_set_str(model->text, text);
model->strip_to_dots = strip_to_dots;
// Allocate and init Element
WidgetElement* gui_string = malloc(sizeof(WidgetElement));