elements.c 29 KB

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