elements.c 29 KB

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