hex_viewer_scene_scene_1.c 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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. view_dispatcher_switch_to_view(app->view_dispatcher, HexViewerSceneScene_1);
  22. }
  23. bool hex_viewer_scene_scene_1_on_event(void* context, SceneManagerEvent event) {
  24. HexViewer* app = (HexViewer*)context;
  25. bool consumed = false;
  26. if(event.type == SceneManagerEventTypeCustom) {
  27. if(event.event == HexViewerCustomEventMenuPercentEntered) {
  28. int ipercent = atoi(app->percent_buf);
  29. ipercent = MIN(ipercent, 100);
  30. ipercent = MAX(ipercent, 0);
  31. float percent = ipercent / 100.0;
  32. uint32_t line_count = app->model->file_size / HEX_VIEWER_BYTES_PER_LINE;
  33. if(app->model->file_size % HEX_VIEWER_BYTES_PER_LINE != 0) line_count += 1;
  34. uint32_t scrollable_lines = line_count - HEX_VIEWER_LINES_ON_SCREEN;
  35. uint32_t target_line = (uint32_t)(percent * scrollable_lines);
  36. uint32_t new_file_offset = target_line * HEX_VIEWER_BYTES_PER_LINE;
  37. if(app->model->file_size > new_file_offset) {
  38. app->model->file_offset = new_file_offset;
  39. if(!hex_viewer_read_file(app)) new_file_offset = new_file_offset; // TODO Do smth
  40. }
  41. scene_manager_search_and_switch_to_previous_scene(
  42. app->scene_manager, HexViewerViewIdStartscreen);
  43. consumed = true;
  44. }
  45. }
  46. return consumed;
  47. }
  48. void hex_viewer_scene_scene_1_on_exit(void* context) {
  49. UNUSED(context);
  50. }