tab_browser.ts 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. let tab_browser_hpath: zui_handle_t = zui_handle_create();
  2. let tab_browser_hsearch: zui_handle_t = zui_handle_create();
  3. let tab_browser_known: bool = false;
  4. let tab_browser_last_path: string = "";
  5. function tab_browser_show_directory(directory: string) {
  6. tab_browser_hpath.text = directory;
  7. tab_browser_hsearch.text = "";
  8. ui_base_htabs[tab_area_t.STATUS].position = 0;
  9. }
  10. let _tab_browser_draw_file: string;
  11. let _tab_browser_draw_b: string;
  12. function tab_browser_draw(htab: zui_handle_t) {
  13. let ui: zui_t = ui_base_ui;
  14. let statush: i32 = config_raw.layout[layout_size_t.STATUS_H];
  15. if (zui_tab(htab, tr("Browser")) && statush > ui_status_default_status_h * zui_SCALE(ui)) {
  16. if (config_raw.bookmarks == null) {
  17. config_raw.bookmarks = [];
  18. }
  19. let bookmarks_w: i32 = math_floor(100 * zui_SCALE(ui));
  20. if (tab_browser_hpath.text == "" && config_raw.bookmarks.length > 0) { // Init to first bookmark
  21. tab_browser_hpath.text = config_raw.bookmarks[0];
  22. }
  23. zui_begin_sticky();
  24. let step: i32 = (1 - bookmarks_w / ui._w);
  25. if (tab_browser_hsearch.text != "") {
  26. let row: f32[] = [bookmarks_w / ui._w, step * 0.73, step * 0.07, step * 0.17, step * 0.03];
  27. zui_row(row);
  28. }
  29. else {
  30. let row: f32[] = [bookmarks_w / ui._w, step * 0.73, step * 0.07, step * 0.2];
  31. zui_row(row);
  32. }
  33. if (zui_button("+")) {
  34. array_push(config_raw.bookmarks, tab_browser_hpath.text);
  35. config_save();
  36. }
  37. if (ui.is_hovered) {
  38. zui_tooltip(tr("Add bookmark"));
  39. }
  40. ///if krom_android
  41. let stripped: bool = false;
  42. let strip: string = "/storage/emulated/0/";
  43. if (starts_with(tab_browser_hpath.text, strip)) {
  44. tab_browser_hpath.text = substring(tab_browser_hpath.text, strip.length - 1, tab_browser_hpath.text.length);
  45. stripped = true;
  46. }
  47. ///end
  48. tab_browser_hpath.text = zui_text_input(tab_browser_hpath, tr("Path"));
  49. ///if krom_android
  50. if (stripped) {
  51. tab_browser_hpath.text = "/storage/emulated/0" + tab_browser_hpath.text;
  52. }
  53. ///end
  54. let refresh: bool = false;
  55. let in_focus: bool = ui.input_x > ui._window_x && ui.input_x < ui._window_x + ui._window_w &&
  56. ui.input_y > ui._window_y && ui.input_y < ui._window_y + ui._window_h;
  57. if (zui_button(tr("Refresh")) || (in_focus && ui.is_key_pressed && ui.key == key_code_t.F5)) {
  58. refresh = true;
  59. }
  60. tab_browser_hsearch.text = zui_text_input(tab_browser_hsearch, tr("Search"), zui_align_t.LEFT, true, true);
  61. if (ui.is_hovered) {
  62. zui_tooltip(tr("ctrl+f to search") + "\n" + tr("esc to cancel"));
  63. }
  64. if (ui.is_ctrl_down && ui.is_key_pressed && ui.key == key_code_t.F) { // Start searching via ctrl+f
  65. zui_start_text_edit(tab_browser_hsearch);
  66. }
  67. if (tab_browser_hsearch.text != "" && (zui_button(tr("X")) || ui.is_escape_down)) {
  68. tab_browser_hsearch.text = "";
  69. }
  70. zui_end_sticky();
  71. if (tab_browser_last_path != tab_browser_hpath.text) {
  72. tab_browser_hsearch.text = "";
  73. }
  74. tab_browser_last_path = tab_browser_hpath.text;
  75. let _y: f32 = ui._y;
  76. ui._x = bookmarks_w;
  77. ui._w -= bookmarks_w;
  78. ui_files_file_browser(ui, tab_browser_hpath, false, true, tab_browser_hsearch.text, refresh, function (file: string) {
  79. let file_name: string = substring(file, string_last_index_of(file, path_sep) + 1, file.length);
  80. if (file_name == "..") {
  81. return;
  82. }
  83. _tab_browser_draw_file = file;
  84. // Context menu
  85. ui_menu_draw(function (ui: zui_t) {
  86. let file: string = _tab_browser_draw_file;
  87. if (ui_menu_button(ui, tr("Import"))) {
  88. import_asset_run(file);
  89. }
  90. if (path_is_texture(file)) {
  91. if (ui_menu_button(ui, tr("Set as Envmap"))) {
  92. import_asset_run(file, -1.0, -1.0, true, true, function () {
  93. app_notify_on_next_frame(function () {
  94. let file: string = _tab_browser_draw_file;
  95. let asset_index: i32 = -1;
  96. for (let i: i32 = 0; i < project_assets.length; ++i) {
  97. if (project_assets[i].file == file) {
  98. asset_index = i;
  99. break;
  100. }
  101. }
  102. if (asset_index != -1) {
  103. import_envmap_run(file, project_get_image(project_assets[asset_index]));
  104. }
  105. });
  106. });
  107. }
  108. ///if (is_paint || is_sculpt)
  109. if (ui_menu_button(ui, tr("Set as Mask"))) {
  110. import_asset_run(file, -1.0, -1.0, true, true, function () {
  111. app_notify_on_next_frame(function () {
  112. let file: string = _tab_browser_draw_file;
  113. let asset_index: i32 = -1;
  114. for (let i: i32 = 0; i < project_assets.length; ++i) {
  115. if (project_assets[i].file == file) {
  116. asset_index = i;
  117. break;
  118. }
  119. }
  120. if (asset_index != -1) {
  121. base_create_image_mask(project_assets[asset_index]);
  122. }
  123. });
  124. });
  125. }
  126. ///end
  127. ///if is_paint
  128. if (ui_menu_button(ui, tr("Set as Color ID Map"))) {
  129. import_asset_run(file, -1.0, -1.0, true, true, function () {
  130. app_notify_on_next_frame(function () {
  131. let file: string = _tab_browser_draw_file;
  132. let asset_index: i32 = -1;
  133. for (let i: i32 = 0; i < project_assets.length; ++i) {
  134. if (project_assets[i].file == file) {
  135. asset_index = i;
  136. break;
  137. }
  138. }
  139. if (asset_index != -1) {
  140. context_raw.colorid_handle.position = asset_index;
  141. context_raw.colorid_picked = false;
  142. ui_toolbar_handle.redraws = 1;
  143. if (context_raw.tool == workspace_tool_t.COLORID) {
  144. ui_header_handle.redraws = 2;
  145. context_raw.ddirty = 2;
  146. }
  147. }
  148. });
  149. });
  150. }
  151. ///end
  152. }
  153. if (ui_menu_button(ui, tr("Open Externally"))) {
  154. file_start(file);
  155. }
  156. }, path_is_texture(file) ? 5 : 2);
  157. });
  158. if (tab_browser_known) {
  159. let path: string = tab_browser_hpath.text;
  160. app_notify_on_init(function (path: string) {
  161. import_asset_run(path);
  162. }, path);
  163. tab_browser_hpath.text = substring(tab_browser_hpath.text, 0, string_last_index_of(tab_browser_hpath.text, path_sep));
  164. }
  165. tab_browser_known = string_index_of(substring(tab_browser_hpath.text, string_last_index_of(tab_browser_hpath.text, path_sep), tab_browser_hpath.text.length), ".") > 0;
  166. ///if krom_android
  167. if (ends_with(tab_browser_hpath.text, "." + to_lower_case(manifest_title))) {
  168. tab_browser_known = false;
  169. }
  170. ///end
  171. let bottom_y: i32 = ui._y;
  172. ui._x = 0;
  173. ui._y = _y;
  174. ui._w = bookmarks_w;
  175. if (zui_button(tr("Cloud"), zui_align_t.LEFT)) {
  176. tab_browser_hpath.text = "cloud";
  177. }
  178. if (zui_button(tr("Disk"), zui_align_t.LEFT)) {
  179. ///if krom_android
  180. ui_menu_draw(function (ui: zui_t) {
  181. if (ui_menu_button(ui, tr("Download"))) {
  182. tab_browser_hpath.text = ui_files_default_path;
  183. }
  184. if (ui_menu_button(ui, tr("Pictures"))) {
  185. tab_browser_hpath.text = "/storage/emulated/0/Pictures";
  186. }
  187. if (ui_menu_button(ui, tr("Camera"))) {
  188. tab_browser_hpath.text = "/storage/emulated/0/DCIM/Camera";
  189. }
  190. if (ui_menu_button(ui, tr("Projects"))) {
  191. tab_browser_hpath.text = krom_save_path();
  192. }
  193. }, 4);
  194. ///else
  195. tab_browser_hpath.text = ui_files_default_path;
  196. ///end
  197. }
  198. for (let i: i32 = 0; i < config_raw.bookmarks.length; ++i) {
  199. let b: string = config_raw.bookmarks[i];
  200. let folder: string = substring(b, string_last_index_of(b, path_sep) + 1, b.length);
  201. if (zui_button(folder, zui_align_t.LEFT)) {
  202. tab_browser_hpath.text = b;
  203. }
  204. if (ui.is_hovered && ui.input_released_r) {
  205. _tab_browser_draw_b = b;
  206. ui_menu_draw(function (ui: zui_t) {
  207. if (ui_menu_button(ui, tr("Delete"))) {
  208. array_remove(config_raw.bookmarks, _tab_browser_draw_b);
  209. config_save();
  210. }
  211. }, 1);
  212. }
  213. }
  214. if (ui._y < bottom_y) {
  215. ui._y = bottom_y;
  216. }
  217. }
  218. }