From 1cfb16d9a706f47f377971c81c4310d368000c5d Mon Sep 17 00:00:00 2001 From: its your bedtime <23366927+itsyourbedtime@users.noreply.github.com> Date: Wed, 26 May 2021 19:01:59 +0300 Subject: [PATCH] [FL-1235] Cut long names (#494) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * fix typo, elements_string_fit_width added * add string_fit_witdt to fileselect module Co-authored-by: あく --- applications/archive/archive.c | 8 ++++---- applications/archive/archive_i.h | 4 ++-- applications/archive/archive_views.c | 20 ++++---------------- applications/gui/elements.c | 18 ++++++++++++++++-- applications/gui/elements.h | 8 ++++++++ applications/gui/modules/file_select.c | 11 +++++++---- 6 files changed, 41 insertions(+), 28 deletions(-) diff --git a/applications/archive/archive.c b/applications/archive/archive.c index 4dc7b55c..53fe414f 100644 --- a/applications/archive/archive.c +++ b/applications/archive/archive.c @@ -200,7 +200,7 @@ static uint32_t archive_previous_callback(void* context) { } /* file menu */ -static void archive_add_to_favorites(ArchiveApp* archive) { +static void archive_add_to_favourites(ArchiveApp* archive) { furi_assert(archive); FS_Common_Api* common_api = &archive->fs_api->common; @@ -212,7 +212,7 @@ static void archive_add_to_favorites(ArchiveApp* archive) { string_cat(buffer_src, "/"); string_cat(buffer_src, archive->browser.name); - string_init_set_str(buffer_dst, "/favorites/"); + string_init_set_str(buffer_dst, "/favourites/"); string_cat(buffer_dst, archive->browser.name); common_api->rename(string_get_cstr(buffer_src), string_get_cstr(buffer_dst)); @@ -345,7 +345,7 @@ static void archive_file_menu_callback(ArchiveApp* archive) { case 1: string_set(archive->browser.name, selected->name); - archive_add_to_favorites(archive); + archive_add_to_favourites(archive); archive_close_file_menu(archive); break; case 2: @@ -521,7 +521,7 @@ ArchiveApp* archive_alloc() { view_dispatcher_attach_to_gui( archive->view_dispatcher, archive->gui, ViewDispatcherTypeFullscreen); - view_dispatcher_switch_to_view(archive->view_dispatcher, ArchiveTabFavorites); + view_dispatcher_switch_to_view(archive->view_dispatcher, ArchiveTabFavourites); return archive; } diff --git a/applications/archive/archive_i.h b/applications/archive/archive_i.h index 574e333a..d1d5c88d 100644 --- a/applications/archive/archive_i.h +++ b/applications/archive/archive_i.h @@ -23,7 +23,7 @@ typedef enum { } ArchiveViewEnum; typedef enum { - ArchiveTabFavorites, + ArchiveTabFavourites, ArchiveTabIButton, ArchiveTabNFC, ArchiveTabSubOne, @@ -42,7 +42,7 @@ static const char* known_ext[] = { }; static const char* tab_default_paths[] = { - [ArchiveTabFavorites] = "favorites", + [ArchiveTabFavourites] = "favourites", [ArchiveTabIButton] = "ibutton", [ArchiveTabNFC] = "nfc", [ArchiveTabSubOne] = "subone", diff --git a/applications/archive/archive_views.c b/applications/archive/archive_views.c index 60935413..2dd22190 100644 --- a/applications/archive/archive_views.c +++ b/applications/archive/archive_views.c @@ -1,7 +1,7 @@ #include "archive_views.h" static const char* ArchiveTabNames[] = - {"Favorites", "iButton", "NFC", "SubOne", "Rfid", "Infared", "Browser"}; + {"Favourites", "iButton", "NFC", "SubOne", "Rfid", "Infared", "Browser"}; static const IconName ArchiveItemIcons[] = { [ArchiveFileTypeIButton] = I_ibutt_10px, @@ -59,20 +59,6 @@ static void trim_file_ext(string_t name) { } } -static void format_filename_buffer(Canvas* canvas, string_t name, ArchiveFileTypeEnum type) { - furi_assert(name); - - size_t s_len = strlen(string_get_cstr(name)); - uint16_t len_px = canvas_string_width(canvas, string_get_cstr(name)); - - if(is_known_app(type)) trim_file_ext(name); - - if(len_px > MAX_LEN_PX) { - string_mid(name, 0, s_len - (size_t)((len_px - MAX_LEN_PX) / ((len_px / s_len) + 2) + 2)); - string_cat(name, "..."); - } -} - static void archive_draw_frame(Canvas* canvas, uint16_t idx, bool scrollbar) { canvas_set_color(canvas, ColorBlack); canvas_draw_box(canvas, 0, 15 + idx * FRAME_HEIGHT, scrollbar ? 122 : 127, FRAME_HEIGHT); @@ -101,7 +87,9 @@ static void draw_list(Canvas* canvas, ArchiveViewModel* model) { ArchiveFile_t* file = files_array_get(model->files, CLAMP(idx, array_size - 1, 0)); string_set(str_buff, file->name); - format_filename_buffer(canvas, str_buff, file->type); + + if(is_known_app(file->type)) trim_file_ext(str_buff); + elements_string_fit_width(canvas, str_buff, MAX_LEN_PX); if(model->idx == idx) { archive_draw_frame(canvas, i, scrollbar); diff --git a/applications/gui/elements.c b/applications/gui/elements.c index be7115a8..f3762418 100644 --- a/applications/gui/elements.c +++ b/applications/gui/elements.c @@ -1,7 +1,6 @@ #include "elements.h" #include #include -#include #include #include "canvas_i.h" #include @@ -257,4 +256,19 @@ void elements_slightly_rounded_frame( canvas_draw_dot(canvas, x + width - 1, y); canvas_draw_dot(canvas, x, y + height - 1); canvas_invert_color(canvas); -} \ No newline at end of file +} + +void elements_string_fit_width(Canvas* canvas, string_t string, uint8_t width) { + furi_assert(canvas); + furi_assert(string); + + uint16_t len_px = canvas_string_width(canvas, string_get_cstr(string)); + + if(len_px > width) { + size_t s_len = strlen(string_get_cstr(string)); + uint8_t end_pos = s_len - ((len_px - width) / ((len_px / s_len) + 2) + 2); + + string_mid(string, 0, end_pos); + string_cat(string, "..."); + } +} diff --git a/applications/gui/elements.h b/applications/gui/elements.h index 04c1f236..76e43cff 100644 --- a/applications/gui/elements.h +++ b/applications/gui/elements.h @@ -1,6 +1,7 @@ #pragma once #include +#include #include "canvas.h" #ifdef __cplusplus @@ -105,6 +106,13 @@ void elements_slightly_rounded_frame( uint8_t width, uint8_t height); +/* + * Trim string buffer to fit width in pixels + * @param string - string to trim + * @param width - max width + */ +void elements_string_fit_width(Canvas* canvas, string_t string, uint8_t width); + #ifdef __cplusplus } #endif diff --git a/applications/gui/modules/file_select.c b/applications/gui/modules/file_select.c index 32319b04..f9e1d2cd 100644 --- a/applications/gui/modules/file_select.c +++ b/applications/gui/modules/file_select.c @@ -37,8 +37,10 @@ static bool file_select_init_inner(FileSelect* file_select); static void file_select_draw_callback(Canvas* canvas, void* _model) { FileSelectModel* model = _model; + string_t string_buff; const uint8_t item_height = 16; const uint8_t item_width = 123; + const uint8_t max_width = 100; canvas_clear(canvas); canvas_set_font(canvas, FontSecondary); @@ -58,11 +60,12 @@ static void file_select_draw_callback(Canvas* canvas, void* _model) { canvas_set_color(canvas, ColorBlack); } + string_init_set(string_buff, model->filename[i]); + elements_string_fit_width(canvas, string_buff, max_width); canvas_draw_str( - canvas, - 6, - (i * item_height) + item_height - 4, - string_get_cstr(model->filename[i])); + canvas, 6, (i * item_height) + item_height - 4, string_get_cstr(string_buff)); + + string_clear(string_buff); } } else { canvas_draw_str(canvas, 6, item_height, "Empty folder");