tab_script.ts 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. let tab_script_hscript: ui_handle_t = ui_handle_create();
  2. let tab_script_text_coloring: ui_text_coloring_t = null;
  3. function tab_script_draw(htab: ui_handle_t) {
  4. let ui: ui_t = ui_base_ui;
  5. let statush: i32 = config_raw.layout[layout_size_t.STATUS_H];
  6. if (ui_tab(htab, tr("Script")) && statush > ui_status_default_status_h * ui_SCALE(ui)) {
  7. ui_begin_sticky();
  8. if (config_raw.touch_ui) {
  9. ui_row4();
  10. }
  11. else {
  12. let row: f32[] = [1 / 14, 1 / 14, 1 / 14, 1 / 14];
  13. ui_row(row);
  14. }
  15. if (ui_button(tr("Run"))) {
  16. js_eval(tab_script_hscript.text);
  17. }
  18. if (ui_button(tr("Clear"))) {
  19. tab_script_hscript.text = "";
  20. }
  21. if (ui_button(tr("Import"))) {
  22. ui_files_show("js", false, false, function (path: string) {
  23. let b: buffer_t = data_get_blob(path);
  24. tab_script_hscript.text = sys_buffer_to_string(b);
  25. data_delete_blob(path);
  26. });
  27. }
  28. if (ui_button(tr("Export"))) {
  29. ui_files_show("js", true, false, function (path: string) {
  30. let str: string = tab_script_hscript.text;
  31. let f: string = ui_files_filename;
  32. if (f == "") {
  33. f = tr("untitled");
  34. }
  35. path = path + path_sep + f;
  36. if (!ends_with(path, ".js")) {
  37. path += ".js";
  38. }
  39. iron_file_save_bytes(path, sys_string_to_buffer(str), 0);
  40. });
  41. }
  42. ui_end_sticky();
  43. let _font: draw_font_t = ui.ops.font;
  44. let _font_size: i32 = ui.font_size;
  45. let f: draw_font_t = data_get_font("font_mono.ttf");
  46. ui_set_font(ui, f);
  47. ui.font_size = math_floor(15 * ui_SCALE(ui));
  48. ui_text_area_line_numbers = true;
  49. ui_text_area_scroll_past_end = true;
  50. ui_text_area_coloring = tab_script_get_text_coloring();
  51. ui_text_area(tab_script_hscript);
  52. ui_text_area_line_numbers = false;
  53. ui_text_area_scroll_past_end = false;
  54. ui_text_area_coloring = null;
  55. ui_set_font(ui, _font);
  56. ui.font_size = _font_size;
  57. }
  58. }
  59. function tab_script_get_text_coloring(): ui_text_coloring_t {
  60. if (tab_script_text_coloring == null) {
  61. let blob: buffer_t = data_get_blob("text_coloring.json");
  62. tab_script_text_coloring = json_parse(sys_buffer_to_string(blob));
  63. }
  64. return tab_script_text_coloring;
  65. }