hex_viewer_scene_scene_1.c 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. #include "../hex_viewer.h"
  2. #include "../helpers/hex_viewer_custom_event.h"
  3. #include "../views/hex_viewer_scene_1.h"
  4. void hex_viewer_scene_scene_1_callback(void* context) {
  5. HexViewer* app = (HexViewer*)context;
  6. view_dispatcher_send_custom_event(
  7. app->view_dispatcher, HexViewerCustomEventMenuPercentEntered);
  8. }
  9. void hex_viewer_scene_scene_1_on_enter(void* context) {
  10. furi_assert(context);
  11. HexViewer* app = context;
  12. TextInput* text_input = app->text_input;
  13. text_input_set_header_text(text_input, "Scroll to percent (0..100)");
  14. text_input_set_result_callback(
  15. text_input,
  16. hex_viewer_scene_scene_1_callback,
  17. app,
  18. app->percent_buf,
  19. HEX_VIEWER_PERCENT_INPUT,
  20. false);
  21. // ValidatorIsFile* validator_is_file = validator_is_file_alloc_init(
  22. // IBUTTON_APP_FOLDER, IBUTTON_APP_FILENAME_EXTENSION, ibutton->key_name);
  23. // text_input_set_validator(text_input, validator_is_file_callback, validator_is_file);
  24. view_dispatcher_switch_to_view(app->view_dispatcher, HexViewerSceneScene_1);
  25. // if(success) {
  26. // //
  27. // }
  28. // if(success) {
  29. // // Load page to do something with result
  30. // //scene_manager_next_scene(app->scene_manager, HexViewerViewIdMenu);
  31. // //scene_manager_previous_scene(app->scene_manager); // temp for showcase
  32. // scene_manager_search_and_switch_to_previous_scene(
  33. // app->scene_manager, HexViewerViewIdStartscreen);
  34. // } else {
  35. // // This is basically if someone quites the browser
  36. // scene_manager_previous_scene(app->scene_manager);
  37. // }
  38. }
  39. bool hex_viewer_scene_scene_1_on_event(void* context, SceneManagerEvent event) {
  40. HexViewer* app = (HexViewer*)context;
  41. bool consumed = false;
  42. if(event.type == SceneManagerEventTypeCustom) {
  43. if(event.event == HexViewerCustomEventMenuPercentEntered) {
  44. int ipercent = atoi(app->percent_buf);
  45. // float percent = atof(app->percent_buf);
  46. ipercent = MIN(ipercent, 100);
  47. ipercent = MAX(ipercent, 0);
  48. float percent = ipercent / 100.0;
  49. uint32_t line_count = app->model->file_size / HEX_VIEWER_BYTES_PER_LINE;
  50. if(app->model->file_size % HEX_VIEWER_BYTES_PER_LINE != 0) line_count += 1;
  51. uint32_t scrollable_lines = line_count - HEX_VIEWER_LINES_ON_SCREEN;
  52. uint32_t target_line = (uint32_t)(percent * scrollable_lines);
  53. // uint32_t first_line_on_screen = model->file_offset / HEX_VIEWER_BYTES_PER_LINE;
  54. // if(line_count > HEX_VIEWER_LINES_ON_SCREEN) {
  55. // uint8_t width = canvas_width(canvas);
  56. // elements_scrollbar_pos(
  57. // canvas,
  58. // width,
  59. // 0,
  60. // ROW_HEIGHT * HEX_VIEWER_LINES_ON_SCREEN,
  61. // first_line_on_screen, // TODO
  62. // line_count - (HEX_VIEWER_LINES_ON_SCREEN - 1));
  63. // }
  64. uint32_t new_file_offset = target_line * HEX_VIEWER_BYTES_PER_LINE;
  65. if(app->model->file_size > new_file_offset) {
  66. app->model->file_offset = new_file_offset;
  67. if(!hex_viewer_read_file(app)) new_file_offset = new_file_offset; // TODO Do smth
  68. }
  69. scene_manager_search_and_switch_to_previous_scene(
  70. app->scene_manager, HexViewerViewIdStartscreen);
  71. consumed = true;
  72. }
  73. }
  74. return consumed;
  75. }
  76. void hex_viewer_scene_scene_1_on_exit(void* context) {
  77. UNUSED(context);
  78. }