elements.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757
  1. #include "elements.h"
  2. #include "m-core.h"
  3. #include <assets_icons.h>
  4. #include "furi_hal_resources.h"
  5. #include <furi_hal.h>
  6. #include "gui/canvas.h"
  7. #include <gui/icon_i.h>
  8. #include <gui/icon_animation_i.h>
  9. #include <furi.h>
  10. #include "canvas_i.h"
  11. #include <string.h>
  12. #include <stdint.h>
  13. #include <stdbool.h>
  14. typedef struct {
  15. uint8_t x;
  16. uint8_t y;
  17. uint8_t leading_min;
  18. uint8_t leading_default;
  19. uint8_t height;
  20. uint8_t descender;
  21. uint8_t len;
  22. const char* text;
  23. } ElementTextBoxLine;
  24. void elements_progress_bar(Canvas* canvas, uint8_t x, uint8_t y, uint8_t width, float progress) {
  25. furi_assert(canvas);
  26. furi_assert((progress >= 0) && (progress <= 1.0));
  27. uint8_t height = 9;
  28. uint8_t progress_length = roundf(progress * (width - 2));
  29. canvas_set_color(canvas, ColorWhite);
  30. canvas_draw_box(canvas, x + 1, y + 1, width - 2, height - 2);
  31. canvas_set_color(canvas, ColorBlack);
  32. canvas_draw_rframe(canvas, x, y, width, height, 3);
  33. canvas_draw_box(canvas, x + 1, y + 1, progress_length, height - 2);
  34. }
  35. void elements_scrollbar_pos(
  36. Canvas* canvas,
  37. uint8_t x,
  38. uint8_t y,
  39. uint8_t height,
  40. uint16_t pos,
  41. uint16_t total) {
  42. furi_assert(canvas);
  43. // prevent overflows
  44. canvas_set_color(canvas, ColorWhite);
  45. canvas_draw_box(canvas, x - 3, y, 3, height);
  46. // dot line
  47. canvas_set_color(canvas, ColorBlack);
  48. for(uint8_t i = y; i < height + y; i += 2) {
  49. canvas_draw_dot(canvas, x - 2, i);
  50. }
  51. // Position block
  52. if(total) {
  53. float block_h = ((float)height) / total;
  54. canvas_draw_box(canvas, x - 3, y + (block_h * pos), 3, MAX(block_h, 1));
  55. }
  56. }
  57. void elements_scrollbar(Canvas* canvas, uint16_t pos, uint16_t total) {
  58. furi_assert(canvas);
  59. uint8_t width = canvas_width(canvas);
  60. uint8_t height = canvas_height(canvas);
  61. // prevent overflows
  62. canvas_set_color(canvas, ColorWhite);
  63. canvas_draw_box(canvas, width - 3, 0, 3, height);
  64. // dot line
  65. canvas_set_color(canvas, ColorBlack);
  66. for(uint8_t i = 0; i < height; i += 2) {
  67. canvas_draw_dot(canvas, width - 2, i);
  68. }
  69. // Position block
  70. if(total) {
  71. float block_h = ((float)height) / total;
  72. canvas_draw_box(canvas, width - 3, block_h * pos, 3, MAX(block_h, 1));
  73. }
  74. }
  75. void elements_frame(Canvas* canvas, uint8_t x, uint8_t y, uint8_t width, uint8_t height) {
  76. furi_assert(canvas);
  77. canvas_draw_line(canvas, x + 2, y, x + width - 2, y);
  78. canvas_draw_line(canvas, x + 1, y + height - 1, x + width, y + height - 1);
  79. canvas_draw_line(canvas, x + 2, y + height, x + width - 1, y + height);
  80. canvas_draw_line(canvas, x, y + 2, x, y + height - 2);
  81. canvas_draw_line(canvas, x + width - 1, y + 1, x + width - 1, y + height - 2);
  82. canvas_draw_line(canvas, x + width, y + 2, x + width, y + height - 2);
  83. canvas_draw_dot(canvas, x + 1, y + 1);
  84. }
  85. void elements_button_left(Canvas* canvas, const char* str) {
  86. const uint8_t button_height = 12;
  87. const uint8_t vertical_offset = 3;
  88. const uint8_t horizontal_offset = 3;
  89. const uint8_t string_width = canvas_string_width(canvas, str);
  90. const Icon* icon = &I_ButtonLeft_4x7;
  91. const uint8_t icon_h_offset = 3;
  92. const uint8_t icon_width_with_offset = icon->width + icon_h_offset;
  93. const uint8_t icon_v_offset = icon->height + vertical_offset;
  94. const uint8_t button_width = string_width + horizontal_offset * 2 + icon_width_with_offset;
  95. const uint8_t x = 0;
  96. const uint8_t y = canvas_height(canvas);
  97. canvas_draw_box(canvas, x, y - button_height, button_width, button_height);
  98. canvas_draw_line(canvas, x + button_width + 0, y, x + button_width + 0, y - button_height + 0);
  99. canvas_draw_line(canvas, x + button_width + 1, y, x + button_width + 1, y - button_height + 1);
  100. canvas_draw_line(canvas, x + button_width + 2, y, x + button_width + 2, y - button_height + 2);
  101. canvas_invert_color(canvas);
  102. canvas_draw_icon(canvas, x + horizontal_offset, y - icon_v_offset, &I_ButtonLeft_4x7);
  103. canvas_draw_str(
  104. canvas, x + horizontal_offset + icon_width_with_offset, y - vertical_offset, str);
  105. canvas_invert_color(canvas);
  106. }
  107. void elements_button_right(Canvas* canvas, const char* str) {
  108. const uint8_t button_height = 12;
  109. const uint8_t vertical_offset = 3;
  110. const uint8_t horizontal_offset = 3;
  111. const uint8_t string_width = canvas_string_width(canvas, str);
  112. const Icon* icon = &I_ButtonRight_4x7;
  113. const uint8_t icon_h_offset = 3;
  114. const uint8_t icon_width_with_offset = icon->width + icon_h_offset;
  115. const uint8_t icon_v_offset = icon->height + vertical_offset;
  116. const uint8_t button_width = string_width + horizontal_offset * 2 + icon_width_with_offset;
  117. const uint8_t x = canvas_width(canvas);
  118. const uint8_t y = canvas_height(canvas);
  119. canvas_draw_box(canvas, x - button_width, y - button_height, button_width, button_height);
  120. canvas_draw_line(canvas, x - button_width - 1, y, x - button_width - 1, y - button_height + 0);
  121. canvas_draw_line(canvas, x - button_width - 2, y, x - button_width - 2, y - button_height + 1);
  122. canvas_draw_line(canvas, x - button_width - 3, y, x - button_width - 3, y - button_height + 2);
  123. canvas_invert_color(canvas);
  124. canvas_draw_str(canvas, x - button_width + horizontal_offset, y - vertical_offset, str);
  125. canvas_draw_icon(
  126. canvas, x - horizontal_offset - icon->width, y - icon_v_offset, &I_ButtonRight_4x7);
  127. canvas_invert_color(canvas);
  128. }
  129. void elements_button_center(Canvas* canvas, const char* str) {
  130. const uint8_t button_height = 12;
  131. const uint8_t vertical_offset = 3;
  132. const uint8_t horizontal_offset = 1;
  133. const uint8_t string_width = canvas_string_width(canvas, str);
  134. const Icon* icon = &I_ButtonCenter_7x7;
  135. const uint8_t icon_h_offset = 3;
  136. const uint8_t icon_width_with_offset = icon->width + icon_h_offset;
  137. const uint8_t icon_v_offset = icon->height + vertical_offset;
  138. const uint8_t button_width = string_width + horizontal_offset * 2 + icon_width_with_offset;
  139. const uint8_t x = (canvas_width(canvas) - button_width) / 2;
  140. const uint8_t y = canvas_height(canvas);
  141. canvas_draw_box(canvas, x, y - button_height, button_width, button_height);
  142. canvas_draw_line(canvas, x - 1, y, x - 1, y - button_height + 0);
  143. canvas_draw_line(canvas, x - 2, y, x - 2, y - button_height + 1);
  144. canvas_draw_line(canvas, x - 3, y, x - 3, y - button_height + 2);
  145. canvas_draw_line(canvas, x + button_width + 0, y, x + button_width + 0, y - button_height + 0);
  146. canvas_draw_line(canvas, x + button_width + 1, y, x + button_width + 1, y - button_height + 1);
  147. canvas_draw_line(canvas, x + button_width + 2, y, x + button_width + 2, y - button_height + 2);
  148. canvas_invert_color(canvas);
  149. canvas_draw_icon(canvas, x + horizontal_offset, y - icon_v_offset, &I_ButtonCenter_7x7);
  150. canvas_draw_str(
  151. canvas, x + horizontal_offset + icon_width_with_offset, y - vertical_offset, str);
  152. canvas_invert_color(canvas);
  153. }
  154. static size_t
  155. elements_get_max_chars_to_fit(Canvas* canvas, Align horizontal, const char* text, uint8_t x) {
  156. const char* end = strchr(text, '\n');
  157. if(end == NULL) {
  158. end = text + strlen(text);
  159. }
  160. size_t text_size = end - text;
  161. FuriString* str;
  162. str = furi_string_alloc_set(text);
  163. furi_string_left(str, text_size);
  164. size_t result = 0;
  165. uint16_t len_px = canvas_string_width(canvas, furi_string_get_cstr(str));
  166. uint8_t px_left = 0;
  167. if(horizontal == AlignCenter) {
  168. if(x > (canvas_width(canvas) / 2)) {
  169. px_left = (canvas_width(canvas) - x) * 2;
  170. } else {
  171. px_left = x * 2;
  172. }
  173. } else if(horizontal == AlignLeft) {
  174. px_left = canvas_width(canvas) - x;
  175. } else if(horizontal == AlignRight) {
  176. px_left = x;
  177. } else {
  178. furi_assert(0);
  179. }
  180. if(len_px > px_left) {
  181. uint8_t excess_symbols_approximately =
  182. roundf((float)(len_px - px_left) / ((float)len_px / (float)text_size));
  183. // reduce to 5 to be sure dash fit, and next line will be at least 5 symbols long
  184. if(excess_symbols_approximately > 0) {
  185. excess_symbols_approximately = MAX(excess_symbols_approximately, 5);
  186. result = text_size - excess_symbols_approximately - 1;
  187. } else {
  188. result = text_size;
  189. }
  190. } else {
  191. result = text_size;
  192. }
  193. furi_string_free(str);
  194. return result;
  195. }
  196. void elements_multiline_text_aligned(
  197. Canvas* canvas,
  198. uint8_t x,
  199. uint8_t y,
  200. Align horizontal,
  201. Align vertical,
  202. const char* text) {
  203. furi_assert(canvas);
  204. furi_assert(text);
  205. uint8_t lines_count = 0;
  206. uint8_t font_height = canvas_current_font_height(canvas);
  207. FuriString* line;
  208. /* go through text line by line and count lines */
  209. for(const char* start = text; start[0];) {
  210. size_t chars_fit = elements_get_max_chars_to_fit(canvas, horizontal, start, x);
  211. ++lines_count;
  212. start += chars_fit;
  213. start += start[0] == '\n' ? 1 : 0;
  214. }
  215. if(vertical == AlignBottom) {
  216. y -= font_height * (lines_count - 1);
  217. } else if(vertical == AlignCenter) {
  218. y -= (font_height * (lines_count - 1)) / 2;
  219. }
  220. /* go through text line by line and print them */
  221. for(const char* start = text; start[0];) {
  222. size_t chars_fit = elements_get_max_chars_to_fit(canvas, horizontal, start, x);
  223. if((start[chars_fit] == '\n') || (start[chars_fit] == 0)) {
  224. line = furi_string_alloc_printf("%.*s", chars_fit, start);
  225. } else if((y + font_height) > canvas_height(canvas)) {
  226. line = furi_string_alloc_printf("%.*s...\n", chars_fit, start);
  227. } else {
  228. line = furi_string_alloc_printf("%.*s-\n", chars_fit, start);
  229. }
  230. canvas_draw_str_aligned(canvas, x, y, horizontal, vertical, furi_string_get_cstr(line));
  231. furi_string_free(line);
  232. y += font_height;
  233. if(y > canvas_height(canvas)) {
  234. break;
  235. }
  236. start += chars_fit;
  237. start += start[0] == '\n' ? 1 : 0;
  238. }
  239. }
  240. void elements_multiline_text(Canvas* canvas, uint8_t x, uint8_t y, const char* text) {
  241. furi_assert(canvas);
  242. furi_assert(text);
  243. uint8_t font_height = canvas_current_font_height(canvas);
  244. FuriString* str;
  245. str = furi_string_alloc();
  246. const char* start = text;
  247. char* end;
  248. do {
  249. end = strchr(start, '\n');
  250. if(end) {
  251. furi_string_set_strn(str, start, end - start);
  252. } else {
  253. furi_string_set(str, start);
  254. }
  255. canvas_draw_str(canvas, x, y, furi_string_get_cstr(str));
  256. start = end + 1;
  257. y += font_height;
  258. } while(end && y < 64);
  259. furi_string_free(str);
  260. }
  261. void elements_multiline_text_framed(Canvas* canvas, uint8_t x, uint8_t y, const char* text) {
  262. furi_assert(canvas);
  263. furi_assert(text);
  264. uint8_t font_y = canvas_current_font_height(canvas);
  265. uint16_t str_width = canvas_string_width(canvas, text);
  266. // count \n's
  267. uint8_t lines = 1;
  268. const char* t = text;
  269. while(*t != '\0') {
  270. if(*t == '\n') {
  271. lines++;
  272. uint16_t temp_width = canvas_string_width(canvas, t + 1);
  273. str_width = temp_width > str_width ? temp_width : str_width;
  274. }
  275. t++;
  276. }
  277. canvas_set_color(canvas, ColorWhite);
  278. canvas_draw_box(canvas, x, y - font_y, str_width + 8, font_y * lines + 4);
  279. canvas_set_color(canvas, ColorBlack);
  280. elements_multiline_text(canvas, x + 4, y - 1, text);
  281. elements_frame(canvas, x, y - font_y, str_width + 8, font_y * lines + 4);
  282. }
  283. void elements_slightly_rounded_frame(
  284. Canvas* canvas,
  285. uint8_t x,
  286. uint8_t y,
  287. uint8_t width,
  288. uint8_t height) {
  289. furi_assert(canvas);
  290. canvas_draw_rframe(canvas, x, y, width, height, 1);
  291. }
  292. void elements_slightly_rounded_box(
  293. Canvas* canvas,
  294. uint8_t x,
  295. uint8_t y,
  296. uint8_t width,
  297. uint8_t height) {
  298. furi_assert(canvas);
  299. canvas_draw_rbox(canvas, x, y, width, height, 1);
  300. }
  301. void elements_bold_rounded_frame(
  302. Canvas* canvas,
  303. uint8_t x,
  304. uint8_t y,
  305. uint8_t width,
  306. uint8_t height) {
  307. furi_assert(canvas);
  308. canvas_set_color(canvas, ColorWhite);
  309. canvas_draw_box(canvas, x + 2, y + 2, width - 3, height - 3);
  310. canvas_set_color(canvas, ColorBlack);
  311. canvas_draw_line(canvas, x + 3, y, x + width - 3, y);
  312. canvas_draw_line(canvas, x + 2, y + 1, x + width - 2, y + 1);
  313. canvas_draw_line(canvas, x, y + 3, x, y + height - 3);
  314. canvas_draw_line(canvas, x + 1, y + 2, x + 1, y + height - 2);
  315. canvas_draw_line(canvas, x + width, y + 3, x + width, y + height - 3);
  316. canvas_draw_line(canvas, x + width - 1, y + 2, x + width - 1, y + height - 2);
  317. canvas_draw_line(canvas, x + 3, y + height, x + width - 3, y + height);
  318. canvas_draw_line(canvas, x + 2, y + height - 1, x + width - 2, y + height - 1);
  319. canvas_draw_dot(canvas, x + 2, y + 2);
  320. canvas_draw_dot(canvas, x + 3, y + 2);
  321. canvas_draw_dot(canvas, x + 2, y + 3);
  322. canvas_draw_dot(canvas, x + width - 2, y + 2);
  323. canvas_draw_dot(canvas, x + width - 3, y + 2);
  324. canvas_draw_dot(canvas, x + width - 2, y + 3);
  325. canvas_draw_dot(canvas, x + 2, y + height - 2);
  326. canvas_draw_dot(canvas, x + 3, y + height - 2);
  327. canvas_draw_dot(canvas, x + 2, y + height - 3);
  328. canvas_draw_dot(canvas, x + width - 2, y + height - 2);
  329. canvas_draw_dot(canvas, x + width - 3, y + height - 2);
  330. canvas_draw_dot(canvas, x + width - 2, y + height - 3);
  331. }
  332. void elements_bubble(Canvas* canvas, uint8_t x, uint8_t y, uint8_t width, uint8_t height) {
  333. furi_assert(canvas);
  334. canvas_draw_rframe(canvas, x + 4, y, width, height, 3);
  335. uint8_t y_corner = y + height * 2 / 3;
  336. canvas_draw_line(canvas, x, y_corner, x + 4, y_corner - 4);
  337. canvas_draw_line(canvas, x, y_corner, x + 4, y_corner + 4);
  338. canvas_set_color(canvas, ColorWhite);
  339. canvas_draw_line(canvas, x + 4, y_corner - 3, x + 4, y_corner + 3);
  340. canvas_set_color(canvas, ColorBlack);
  341. }
  342. void elements_bubble_str(
  343. Canvas* canvas,
  344. uint8_t x,
  345. uint8_t y,
  346. const char* text,
  347. Align horizontal,
  348. Align vertical) {
  349. furi_assert(canvas);
  350. furi_assert(text);
  351. uint8_t font_y = canvas_current_font_height(canvas);
  352. uint16_t str_width = canvas_string_width(canvas, text);
  353. // count \n's
  354. uint8_t lines = 1;
  355. const char* t = text;
  356. while(*t != '\0') {
  357. if(*t == '\n') {
  358. lines++;
  359. uint16_t temp_width = canvas_string_width(canvas, t + 1);
  360. str_width = temp_width > str_width ? temp_width : str_width;
  361. }
  362. t++;
  363. }
  364. uint8_t frame_x = x;
  365. uint8_t frame_y = y;
  366. uint8_t frame_width = str_width + 8;
  367. uint8_t frame_height = font_y * lines + 4;
  368. canvas_set_color(canvas, ColorWhite);
  369. canvas_draw_box(canvas, frame_x + 1, frame_y + 1, frame_width - 2, frame_height - 2);
  370. canvas_set_color(canvas, ColorBlack);
  371. canvas_draw_rframe(canvas, frame_x, frame_y, frame_width, frame_height, 1);
  372. elements_multiline_text(canvas, x + 4, y - 1 + font_y, text);
  373. uint8_t x1 = 0;
  374. uint8_t x2 = 0;
  375. uint8_t x3 = 0;
  376. uint8_t y1 = 0;
  377. uint8_t y2 = 0;
  378. uint8_t y3 = 0;
  379. if((horizontal == AlignLeft) && (vertical == AlignTop)) {
  380. x1 = frame_x;
  381. y1 = frame_y;
  382. x2 = frame_x - 4;
  383. y2 = frame_y;
  384. x3 = frame_x;
  385. y3 = frame_y + 4;
  386. canvas_set_color(canvas, ColorWhite);
  387. canvas_draw_box(canvas, x2 + 2, y2 + 1, 2, 2);
  388. canvas_set_color(canvas, ColorBlack);
  389. } else if((horizontal == AlignLeft) && (vertical == AlignCenter)) {
  390. x1 = frame_x;
  391. y1 = frame_y + (frame_height - 1) / 2 - 4;
  392. x2 = frame_x - 4;
  393. y2 = frame_y + (frame_height - 1) / 2;
  394. x3 = frame_x;
  395. y3 = frame_y + (frame_height - 1) / 2 + 4;
  396. canvas_set_color(canvas, ColorWhite);
  397. canvas_draw_box(canvas, x2 + 2, y2 - 2, 2, 5);
  398. canvas_draw_dot(canvas, x2 + 1, y2);
  399. canvas_set_color(canvas, ColorBlack);
  400. } else if((horizontal == AlignLeft) && (vertical == AlignBottom)) {
  401. x1 = frame_x;
  402. y1 = frame_y + (frame_height - 1) - 4;
  403. x2 = frame_x - 4;
  404. y2 = frame_y + (frame_height - 1);
  405. x3 = frame_x;
  406. y3 = frame_y + (frame_height - 1);
  407. canvas_set_color(canvas, ColorWhite);
  408. canvas_draw_box(canvas, x2 + 2, y2 - 2, 2, 2);
  409. canvas_set_color(canvas, ColorBlack);
  410. } else if((horizontal == AlignRight) && (vertical == AlignTop)) {
  411. x1 = frame_x + (frame_width - 1);
  412. y1 = frame_y;
  413. x2 = frame_x + (frame_width - 1) + 4;
  414. y2 = frame_y;
  415. x3 = frame_x + (frame_width - 1);
  416. y3 = frame_y + 4;
  417. canvas_set_color(canvas, ColorWhite);
  418. canvas_draw_box(canvas, x2 - 3, y2 + 1, 2, 2);
  419. canvas_set_color(canvas, ColorBlack);
  420. } else if((horizontal == AlignRight) && (vertical == AlignCenter)) {
  421. x1 = frame_x + (frame_width - 1);
  422. y1 = frame_y + (frame_height - 1) / 2 - 4;
  423. x2 = frame_x + (frame_width - 1) + 4;
  424. y2 = frame_y + (frame_height - 1) / 2;
  425. x3 = frame_x + (frame_width - 1);
  426. y3 = frame_y + (frame_height - 1) / 2 + 4;
  427. canvas_set_color(canvas, ColorWhite);
  428. canvas_draw_box(canvas, x2 - 3, y2 - 2, 2, 5);
  429. canvas_draw_dot(canvas, x2 - 1, y2);
  430. canvas_set_color(canvas, ColorBlack);
  431. } else if((horizontal == AlignRight) && (vertical == AlignBottom)) {
  432. x1 = frame_x + (frame_width - 1);
  433. y1 = frame_y + (frame_height - 1) - 4;
  434. x2 = frame_x + (frame_width - 1) + 4;
  435. y2 = frame_y + (frame_height - 1);
  436. x3 = frame_x + (frame_width - 1);
  437. y3 = frame_y + (frame_height - 1);
  438. canvas_set_color(canvas, ColorWhite);
  439. canvas_draw_box(canvas, x2 - 3, y2 - 2, 2, 2);
  440. canvas_set_color(canvas, ColorBlack);
  441. } else if((horizontal == AlignCenter) && (vertical == AlignTop)) {
  442. x1 = frame_x + (frame_width - 1) / 2 - 4;
  443. y1 = frame_y;
  444. x2 = frame_x + (frame_width - 1) / 2;
  445. y2 = frame_y - 4;
  446. x3 = frame_x + (frame_width - 1) / 2 + 4;
  447. y3 = frame_y;
  448. canvas_set_color(canvas, ColorWhite);
  449. canvas_draw_box(canvas, x2 - 2, y2 + 2, 5, 2);
  450. canvas_draw_dot(canvas, x2, y2 + 1);
  451. canvas_set_color(canvas, ColorBlack);
  452. } else if((horizontal == AlignCenter) && (vertical == AlignBottom)) {
  453. x1 = frame_x + (frame_width - 1) / 2 - 4;
  454. y1 = frame_y + (frame_height - 1);
  455. x2 = frame_x + (frame_width - 1) / 2;
  456. y2 = frame_y + (frame_height - 1) + 4;
  457. x3 = frame_x + (frame_width - 1) / 2 + 4;
  458. y3 = frame_y + (frame_height - 1);
  459. canvas_set_color(canvas, ColorWhite);
  460. canvas_draw_box(canvas, x2 - 2, y2 - 3, 5, 2);
  461. canvas_draw_dot(canvas, x2, y2 - 1);
  462. canvas_set_color(canvas, ColorBlack);
  463. }
  464. canvas_set_color(canvas, ColorWhite);
  465. canvas_draw_line(canvas, x3, y3, x1, y1);
  466. canvas_set_color(canvas, ColorBlack);
  467. canvas_draw_line(canvas, x1, y1, x2, y2);
  468. canvas_draw_line(canvas, x2, y2, x3, y3);
  469. }
  470. void elements_string_fit_width(Canvas* canvas, FuriString* string, uint8_t width) {
  471. furi_assert(canvas);
  472. furi_assert(string);
  473. uint16_t len_px = canvas_string_width(canvas, furi_string_get_cstr(string));
  474. if(len_px > width) {
  475. width -= canvas_string_width(canvas, "...");
  476. do {
  477. furi_string_left(string, furi_string_size(string) - 1);
  478. len_px = canvas_string_width(canvas, furi_string_get_cstr(string));
  479. } while(len_px > width);
  480. furi_string_cat(string, "...");
  481. }
  482. }
  483. void elements_text_box(
  484. Canvas* canvas,
  485. uint8_t x,
  486. uint8_t y,
  487. uint8_t width,
  488. uint8_t height,
  489. Align horizontal,
  490. Align vertical,
  491. const char* text,
  492. bool strip_to_dots) {
  493. furi_assert(canvas);
  494. ElementTextBoxLine line[ELEMENTS_MAX_LINES_NUM];
  495. bool bold = false;
  496. bool mono = false;
  497. bool inversed = false;
  498. bool inversed_present = false;
  499. Font current_font = FontSecondary;
  500. Font prev_font = FontSecondary;
  501. CanvasFontParameters* font_params = canvas_get_font_params(canvas, current_font);
  502. // Fill line parameters
  503. uint8_t line_leading_min = font_params->leading_min;
  504. uint8_t line_leading_default = font_params->leading_default;
  505. uint8_t line_height = font_params->height;
  506. uint8_t line_descender = font_params->descender;
  507. uint8_t line_num = 0;
  508. uint8_t line_width = 0;
  509. uint8_t line_len = 0;
  510. uint8_t total_height_min = 0;
  511. uint8_t total_height_default = 0;
  512. uint16_t i = 0;
  513. bool full_text_processed = false;
  514. uint16_t dots_width = canvas_string_width(canvas, "...");
  515. canvas_set_font(canvas, FontSecondary);
  516. // Fill all lines
  517. line[0].text = text;
  518. for(i = 0; !full_text_processed; i++) {
  519. line_len++;
  520. // Identify line height
  521. if(prev_font != current_font) {
  522. font_params = canvas_get_font_params(canvas, current_font);
  523. line_leading_min = MAX(line_leading_min, font_params->leading_min);
  524. line_leading_default = MAX(line_leading_default, font_params->leading_default);
  525. line_height = MAX(line_height, font_params->height);
  526. line_descender = MAX(line_descender, font_params->descender);
  527. prev_font = current_font;
  528. }
  529. // Set the font
  530. if(text[i] == '\e' && text[i + 1]) {
  531. i++;
  532. line_len++;
  533. if(text[i] == ELEMENTS_BOLD_MARKER) {
  534. if(bold) {
  535. current_font = FontSecondary;
  536. } else {
  537. current_font = FontPrimary;
  538. }
  539. canvas_set_font(canvas, current_font);
  540. bold = !bold;
  541. }
  542. if(text[i] == ELEMENTS_MONO_MARKER) {
  543. if(mono) {
  544. current_font = FontSecondary;
  545. } else {
  546. current_font = FontKeyboard;
  547. }
  548. canvas_set_font(canvas, FontKeyboard);
  549. mono = !mono;
  550. }
  551. if(text[i] == ELEMENTS_INVERSED_MARKER) {
  552. inversed_present = true;
  553. }
  554. continue;
  555. }
  556. if(text[i] != '\n') {
  557. line_width += canvas_glyph_width(canvas, text[i]);
  558. }
  559. // Process new line
  560. if(text[i] == '\n' || text[i] == '\0' || line_width > width) {
  561. if(line_width > width) {
  562. line_width -= canvas_glyph_width(canvas, text[i--]);
  563. line_len--;
  564. }
  565. if(text[i] == '\0') {
  566. full_text_processed = true;
  567. }
  568. if(inversed_present) {
  569. line_leading_min += 1;
  570. line_leading_default += 1;
  571. inversed_present = false;
  572. }
  573. line[line_num].leading_min = line_leading_min;
  574. line[line_num].leading_default = line_leading_default;
  575. line[line_num].height = line_height;
  576. line[line_num].descender = line_descender;
  577. if(total_height_min + line_leading_min > height) {
  578. break;
  579. }
  580. total_height_min += line_leading_min;
  581. total_height_default += line_leading_default;
  582. line[line_num].len = line_len;
  583. if(horizontal == AlignCenter) {
  584. line[line_num].x = x + (width - line_width) / 2;
  585. } else if(horizontal == AlignRight) {
  586. line[line_num].x = x + (width - line_width);
  587. } else {
  588. line[line_num].x = x;
  589. }
  590. line[line_num].y = total_height_min;
  591. line_num++;
  592. if(text[i + 1]) {
  593. line[line_num].text = &text[i + 1];
  594. }
  595. line_leading_min = font_params->leading_min;
  596. line_height = font_params->height;
  597. line_descender = font_params->descender;
  598. line_width = 0;
  599. line_len = 0;
  600. }
  601. }
  602. // Set vertical alignment for all lines
  603. if(total_height_default < height) {
  604. if(vertical == AlignTop) {
  605. line[0].y = y + line[0].height;
  606. } else if(vertical == AlignCenter) {
  607. line[0].y = y + line[0].height + (height - total_height_default) / 2;
  608. } else if(vertical == AlignBottom) {
  609. line[0].y = y + line[0].height + (height - total_height_default);
  610. }
  611. if(line_num > 1) {
  612. for(uint8_t i = 1; i < line_num; i++) {
  613. line[i].y = line[i - 1].y + line[i - 1].leading_default;
  614. }
  615. }
  616. } else if(line_num > 1) {
  617. uint8_t free_pixel_num = height - total_height_min;
  618. uint8_t fill_pixel = 0;
  619. uint8_t j = 1;
  620. line[0].y = y + line[0].height;
  621. while(fill_pixel < free_pixel_num) {
  622. line[j].y = line[j - 1].y + line[j - 1].leading_min + 1;
  623. fill_pixel++;
  624. j = j % (line_num - 1) + 1;
  625. }
  626. }
  627. // Draw line by line
  628. canvas_set_font(canvas, FontSecondary);
  629. bold = false;
  630. mono = false;
  631. inversed = false;
  632. for(uint8_t i = 0; i < line_num; i++) {
  633. for(uint8_t j = 0; j < line[i].len; j++) {
  634. // Process format symbols
  635. if(line[i].text[j] == ELEMENTS_BOLD_MARKER) {
  636. if(bold) {
  637. current_font = FontSecondary;
  638. } else {
  639. current_font = FontPrimary;
  640. }
  641. canvas_set_font(canvas, current_font);
  642. bold = !bold;
  643. continue;
  644. }
  645. if(line[i].text[j] == ELEMENTS_MONO_MARKER) {
  646. if(mono) {
  647. current_font = FontSecondary;
  648. } else {
  649. current_font = FontKeyboard;
  650. }
  651. canvas_set_font(canvas, current_font);
  652. mono = !mono;
  653. continue;
  654. }
  655. if(line[i].text[j] == ELEMENTS_INVERSED_MARKER) {
  656. inversed = !inversed;
  657. continue;
  658. }
  659. if(inversed) {
  660. canvas_draw_box(
  661. canvas,
  662. line[i].x - 1,
  663. line[i].y - line[i].height - 1,
  664. canvas_glyph_width(canvas, line[i].text[j]) + 1,
  665. line[i].height + line[i].descender + 2);
  666. canvas_invert_color(canvas);
  667. canvas_draw_glyph(canvas, line[i].x, line[i].y, line[i].text[j]);
  668. canvas_invert_color(canvas);
  669. } else {
  670. if((i == line_num - 1) && strip_to_dots) {
  671. uint8_t next_symbol_width = canvas_glyph_width(canvas, line[i].text[j]);
  672. if(line[i].x + next_symbol_width + dots_width > x + width) {
  673. canvas_draw_str(canvas, line[i].x, line[i].y, "...");
  674. break;
  675. }
  676. }
  677. canvas_draw_glyph(canvas, line[i].x, line[i].y, line[i].text[j]);
  678. }
  679. line[i].x += canvas_glyph_width(canvas, line[i].text[j]);
  680. }
  681. }
  682. canvas_set_font(canvas, FontSecondary);
  683. }