Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Gui: Submenu and VarItemList API additions and improvements #3621

Draft
wants to merge 12 commits into
base: dev
Choose a base branch
from
52 changes: 50 additions & 2 deletions applications/services/gui/elements.c
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,32 @@ void elements_scrollbar_pos(
}
}

void elements_scrollbar_horizontal(
Canvas* canvas,
int32_t x,
int32_t y,
size_t width,
size_t pos,
size_t total) {
furi_check(canvas);

// prevent overflows
canvas_set_color(canvas, ColorWhite);
canvas_draw_box(canvas, x, y - 3, width, 3);

// dot line
canvas_set_color(canvas, ColorBlack);
for(size_t i = x; i < width + x; i += 2) {
canvas_draw_dot(canvas, i, y - 2);
}

// Position block
if(total) {
float block_w = ((float)width) / total;
canvas_draw_box(canvas, x + (block_w * pos), y - 3, MAX(block_w, 1), 3);
}
}

void elements_scrollbar(Canvas* canvas, size_t pos, size_t total) {
furi_check(canvas);

Expand Down Expand Up @@ -590,21 +616,38 @@ void elements_scrollable_text_line(
FuriString* string,
size_t scroll,
bool ellipsis) {
elements_scrollable_text_line_centered(canvas, x, y, width, string, scroll, ellipsis, false);
}

void elements_scrollable_text_line_centered(
Canvas* canvas,
int32_t x,
int32_t y,
size_t width,
FuriString* string,
size_t scroll,
bool ellipsis,
bool centered) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about calling it elements_scrollable_text_line_centered and instead of bool centered accept Align?

furi_check(canvas);
furi_check(string);

FuriString* line = furi_string_alloc_set(string);

size_t len_px = canvas_string_width(canvas, furi_string_get_cstr(line));
if(len_px > width) {
if(centered) {
centered = false;
x -= width / 2;
}

if(ellipsis) {
width -= canvas_string_width(canvas, "...");
}

// Calculate scroll size
size_t scroll_size = furi_string_size(line);
size_t right_width = 0;
for(size_t i = scroll_size; i > 0; i--) {
for(size_t i = scroll_size - 1; i > 0; i--) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and if line is empty?

right_width += canvas_glyph_width(canvas, furi_string_get_char(line, i));
if(right_width > width) break;
scroll_size--;
Expand All @@ -628,7 +671,12 @@ void elements_scrollable_text_line(
}
}

canvas_draw_str(canvas, x, y, furi_string_get_cstr(line));
if(centered) {
canvas_draw_str_aligned(
canvas, x, y, AlignCenter, AlignBottom, furi_string_get_cstr(line));
} else {
canvas_draw_str(canvas, x, y, furi_string_get_cstr(line));
}
furi_string_free(line);
}

Expand Down
38 changes: 38 additions & 0 deletions applications/services/gui/elements.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,23 @@ void elements_scrollbar_pos(
size_t pos,
size_t total);

/** Draw horizontal scrollbar on canvas at specific position.
*
* @param canvas Canvas instance
* @param x scrollbar position on X axis
* @param y scrollbar position on Y axis
* @param width scrollbar width
* @param pos current element
* @param total total elements
*/
void elements_scrollbar_horizontal(
Canvas* canvas,
int32_t x,
int32_t y,
size_t width,
size_t pos,
size_t total);

/** Draw scrollbar on canvas.
* @note width 3px, height equal to canvas height
*
Expand Down Expand Up @@ -230,6 +247,27 @@ void elements_scrollable_text_line(
size_t scroll,
bool ellipsis);

/** Draw scrollable text line, optionally centered
*
* @param canvas The canvas
* @param[in] x X coordinate
* @param[in] y Y coordinate
* @param[in] width The width
* @param string The string
* @param[in] scroll The scroll counter: 0 - no scroll, any other number - scroll. Just count up, everything else will be calculated on the inside.
* @param[in] ellipsis The ellipsis flag: true to add ellipse
* @param[in] centered The centered flag: true to center text horizontally, x coordinate will indicate the middle
*/
void elements_scrollable_text_line_centered(
Canvas* canvas,
int32_t x,
int32_t y,
size_t width,
FuriString* string,
size_t scroll,
bool ellipsis,
bool centered);

/** Draw text box element
*
* @param canvas Canvas instance
Expand Down
Loading