hex_viewer_scene_scroll.c 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. #include "../hex_viewer.h"
  2. #include "../helpers/hex_viewer_custom_event.h"
  3. void hex_viewer_scene_scroll_callback(void* context) {
  4. HexViewer* app = (HexViewer*)context;
  5. view_dispatcher_send_custom_event(
  6. app->view_dispatcher, HexViewerCustomEventMenuPercentEntered);
  7. }
  8. void hex_viewer_scene_scroll_on_enter(void* context) {
  9. furi_assert(context);
  10. HexViewer* app = context;
  11. TextInput* text_input = app->text_input;
  12. text_input_set_header_text(text_input, "Scroll to percentage (0..100)");
  13. text_input_set_result_callback(
  14. text_input,
  15. hex_viewer_scene_scroll_callback,
  16. app,
  17. app->percent_buf,
  18. HEX_VIEWER_PERCENT_INPUT,
  19. false);
  20. view_dispatcher_switch_to_view(app->view_dispatcher, HexViewerSceneScroll);
  21. }
  22. bool hex_viewer_scene_scroll_on_event(void* context, SceneManagerEvent event) {
  23. HexViewer* app = (HexViewer*)context;
  24. bool consumed = false;
  25. if(event.type == SceneManagerEventTypeCustom) {
  26. if(event.event == HexViewerCustomEventMenuPercentEntered) {
  27. int ipercent = atoi(app->percent_buf);
  28. ipercent = MIN(ipercent, 100);
  29. ipercent = MAX(ipercent, 0);
  30. float percent = ipercent / 100.0;
  31. uint32_t line_count = app->model->file_size / HEX_VIEWER_BYTES_PER_LINE;
  32. if(app->model->file_size % HEX_VIEWER_BYTES_PER_LINE != 0) line_count += 1;
  33. uint32_t scrollable_lines = line_count - HEX_VIEWER_LINES_ON_SCREEN;
  34. uint32_t target_line = (uint32_t)(percent * scrollable_lines);
  35. uint32_t new_file_offset = target_line * HEX_VIEWER_BYTES_PER_LINE;
  36. if(app->model->file_size > new_file_offset) {
  37. app->model->file_offset = new_file_offset;
  38. if(!hex_viewer_read_file(app)) new_file_offset = new_file_offset; // TODO Do smth
  39. }
  40. scene_manager_search_and_switch_to_previous_scene(
  41. app->scene_manager, HexViewerViewIdStartscreen);
  42. consumed = true;
  43. }
  44. }
  45. return consumed;
  46. }
  47. void hex_viewer_scene_scroll_on_exit(void* context) {
  48. UNUSED(context);
  49. }