project_browser.vala 50 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594
  1. /*
  2. * Copyright (c) 2012-2025 Daniele Bartolini et al.
  3. * SPDX-License-Identifier: GPL-3.0-or-later
  4. */
  5. namespace Crown
  6. {
  7. public const Gtk.TargetEntry[] dnd_targets =
  8. {
  9. { "RESOURCE_PATH", Gtk.TargetFlags.SAME_APP, 0 },
  10. };
  11. private string project_path(string type, string name)
  12. {
  13. if (type == "<folder>")
  14. return name;
  15. return ResourceId.path(type, name);
  16. }
  17. // Menu to open when clicking on project's files and folders.
  18. private GLib.Menu? project_entry_menu_create(string type, string name)
  19. {
  20. GLib.Menu menu = new GLib.Menu();
  21. GLib.MenuItem mi;
  22. if (type == "<folder>") {
  23. if (name == "..")
  24. return null;
  25. GLib.Menu import_menu = new GLib.Menu();
  26. mi = new GLib.MenuItem("Import...", null);
  27. mi.set_action_and_target_value("app.import", new GLib.Variant.tuple({(string)name, new string[] {}}));
  28. import_menu.append_item(mi);
  29. menu.append_section(null, import_menu);
  30. GLib.Menu create_menu = new GLib.Menu();
  31. mi = new GLib.MenuItem("New Script...", null);
  32. mi.set_action_and_target_value("app.create-script", new GLib.Variant.tuple({(string)name, "", true}));
  33. create_menu.append_item(mi);
  34. mi = new GLib.MenuItem("New Script (Unit)...", null);
  35. mi.set_action_and_target_value("app.create-script", new GLib.Variant.tuple({(string)name, "", false}));
  36. create_menu.append_item(mi);
  37. mi = new GLib.MenuItem("New Unit...", null);
  38. mi.set_action_and_target_value("app.create-unit", new GLib.Variant.tuple({(string)name, ""}));
  39. create_menu.append_item(mi);
  40. mi = new GLib.MenuItem("New Material...", null);
  41. mi.set_action_and_target_value("app.create-material", new GLib.Variant.tuple({(string)name, ""}));
  42. create_menu.append_item(mi);
  43. mi = new GLib.MenuItem("New Folder...", null);
  44. mi.set_action_and_target_value("app.create-directory", new GLib.Variant.tuple({(string)name, ""}));
  45. create_menu.append_item(mi);
  46. menu.append_section(null, create_menu);
  47. GLib.Menu destroy_menu = new GLib.Menu();
  48. if ((string)name != ProjectStore.ROOT_FOLDER) {
  49. mi = new GLib.MenuItem("Delete Folder", null);
  50. mi.set_action_and_target_value("app.delete-directory", new GLib.Variant.string((string)name));
  51. destroy_menu.append_item(mi);
  52. }
  53. menu.append_section(null, destroy_menu);
  54. } else { // If file
  55. menu = new GLib.Menu();
  56. mi = new GLib.MenuItem("Delete File", null);
  57. mi.set_action_and_target_value("app.delete-file", new GLib.Variant.string(project_path(type, name)));
  58. menu.append_item(mi);
  59. if (type == OBJECT_TYPE_MESH_SKELETON || type == OBJECT_TYPE_SPRITE) {
  60. mi = new GLib.MenuItem("New State Machine...", null);
  61. string skeleton_name;
  62. if (type == OBJECT_TYPE_SPRITE)
  63. skeleton_name = "";
  64. else
  65. skeleton_name = name;
  66. mi.set_action_and_target_value("app.create-state-machine", new GLib.Variant.tuple({ResourceId.parent_folder(name), "", skeleton_name}));
  67. menu.append_item(mi);
  68. }
  69. }
  70. // Add common menu items.
  71. GLib.Menu common_menu = new GLib.Menu();
  72. mi = new GLib.MenuItem("Copy Path", null);
  73. mi.set_action_and_target_value("app.copy-path", new GLib.Variant.string(project_path(type, name)));
  74. common_menu.append_item(mi);
  75. mi = new GLib.MenuItem("Copy Name", null);
  76. mi.set_action_and_target_value("app.copy-name", new GLib.Variant.string(name));
  77. common_menu.append_item(mi);
  78. mi = new GLib.MenuItem("Open Containing Folder...", null);
  79. mi.set_action_and_target_value("app.open-containing", new GLib.Variant.string(name));
  80. common_menu.append_item(mi);
  81. if (type != "<folder>" || name != "") {
  82. mi = new GLib.MenuItem("Add to Favorites", null);
  83. mi.set_action_and_target_value("app.favorite-resource", new GLib.Variant.tuple({type, name}));
  84. common_menu.append_item(mi);
  85. }
  86. menu.append_section(null, common_menu);
  87. return menu;
  88. }
  89. // Menu to open when clicking on favorites' entries.
  90. private GLib.Menu? favorites_entry_menu_create(string type, string name)
  91. {
  92. GLib.Menu menu = new GLib.Menu();
  93. GLib.MenuItem mi;
  94. mi = new GLib.MenuItem("Open Containing Folder...", null);
  95. mi.set_action_and_target_value("app.open-containing", new GLib.Variant.string(name));
  96. menu.append_item(mi);
  97. GLib.Menu common_menu = new GLib.Menu();
  98. mi = new GLib.MenuItem("Copy Path", null);
  99. string path = project_path(type, name);
  100. mi.set_action_and_target_value("app.copy-path", new GLib.Variant.string(path));
  101. common_menu.append_item(mi);
  102. mi = new GLib.MenuItem("Copy Name", null);
  103. mi.set_action_and_target_value("app.copy-name", new GLib.Variant.string(name));
  104. common_menu.append_item(mi);
  105. mi = new GLib.MenuItem("Remove from Favorites", null);
  106. mi.set_action_and_target_value("app.unfavorite-resource", new GLib.Variant.tuple({type, name}));
  107. common_menu.append_item(mi);
  108. mi = new GLib.MenuItem("Reveal", null);
  109. mi.set_action_and_target_value("app.reveal-resource", new GLib.Variant.tuple({type, name}));
  110. common_menu.append_item(mi);
  111. menu.append_section(null, common_menu);
  112. return menu;
  113. }
  114. private void set_thumbnail(Gtk.CellRenderer cell, string type, string name, int icon_size, ThumbnailCache thumbnail_cache)
  115. {
  116. // https://specifications.freedesktop.org/icon-naming-spec/icon-naming-spec-latest.html
  117. if (type == "<folder>")
  118. cell.set_property("icon-name", "browser-folder-symbolic");
  119. else if ((string)type == "<favorites>")
  120. cell.set_property("icon-name", "browser-favorites");
  121. else if ((string)type == OBJECT_TYPE_STATE_MACHINE)
  122. cell.set_property("icon-name", "object-state-machine");
  123. else if ((string)type == "config")
  124. cell.set_property("icon-name", "object-config");
  125. else if ((string)type == OBJECT_TYPE_FONT)
  126. cell.set_property("icon-name", "object-font");
  127. else if ((string)type == OBJECT_TYPE_LEVEL)
  128. cell.set_property("icon-name", "object-level");
  129. else if ((string)type == OBJECT_TYPE_MATERIAL)
  130. cell.set_property("pixbuf", thumbnail_cache.get(type, name, icon_size));
  131. else if ((string)type == OBJECT_TYPE_MESH)
  132. cell.set_property("icon-name", "object-mesh");
  133. else if ((string)type == "package")
  134. cell.set_property("icon-name", "object-package");
  135. else if ((string)type == "physics_config")
  136. cell.set_property("icon-name", "object-config");
  137. else if ((string)type == "render_config")
  138. cell.set_property("icon-name", "object-config");
  139. else if ((string)type == "lua")
  140. cell.set_property("icon-name", "object-script");
  141. else if ((string)type == OBJECT_TYPE_UNIT)
  142. cell.set_property("pixbuf", thumbnail_cache.get(type, name, icon_size));
  143. else if ((string)type == "shader")
  144. cell.set_property("icon-name", "object-shader");
  145. else if ((string)type == OBJECT_TYPE_SOUND)
  146. cell.set_property("pixbuf", thumbnail_cache.get(type, name, icon_size));
  147. else if ((string)type == OBJECT_TYPE_SPRITE_ANIMATION)
  148. cell.set_property("icon-name", "object-animation");
  149. else if ((string)type == OBJECT_TYPE_SPRITE)
  150. cell.set_property("icon-name", "object-sprite");
  151. else if ((string)type == OBJECT_TYPE_TEXTURE)
  152. cell.set_property("pixbuf", thumbnail_cache.get(type, name, icon_size));
  153. else if ((string)type == OBJECT_TYPE_MESH_ANIMATION)
  154. cell.set_property("icon-name", "object-animation");
  155. else if ((string)type == OBJECT_TYPE_MESH_SKELETON)
  156. cell.set_property("icon-name", "object-skeleton");
  157. else
  158. cell.set_property("icon-name", "text-x-generic-symbolic");
  159. }
  160. public class ProjectFolderView : Gtk.Box
  161. {
  162. public enum Column
  163. {
  164. TYPE,
  165. NAME,
  166. PIXBUF,
  167. SIZE,
  168. MTIME,
  169. COUNT
  170. }
  171. public string _selected_type;
  172. public string _selected_name;
  173. public ProjectStore _project_store;
  174. public ThumbnailCache _thumbnail_cache;
  175. public Gtk.ListStore _list_store;
  176. public Gtk.IconView _icon_view;
  177. public Gtk.TreeView _list_view;
  178. public Gtk.CellRendererPixbuf _cell_renderer_pixbuf;
  179. public Gtk.CellRendererText _cell_renderer_text;
  180. public Gdk.Pixbuf _empty_pixbuf;
  181. public bool _showing_project_folder;
  182. public Gtk.ScrolledWindow _icon_view_window;
  183. public Gtk.ScrolledWindow _list_view_window;
  184. public Gtk.GestureMultiPress _icon_view_gesture_click;
  185. public Gtk.GestureMultiPress _list_view_gesture_click;
  186. public Gtk.Stack _stack;
  187. public ProjectFolderView(ProjectStore project_store, ThumbnailCache thumbnail_cache)
  188. {
  189. Object(orientation: Gtk.Orientation.VERTICAL);
  190. _project_store = project_store;
  191. _thumbnail_cache = thumbnail_cache;
  192. _list_store = new Gtk.ListStore(Column.COUNT
  193. , typeof(string) // Column.TYPE
  194. , typeof(string) // Column.NAME
  195. , typeof(Gdk.Pixbuf) // Column.PIXBUF
  196. , typeof(uint64) // Column.SIZE
  197. , typeof(uint64) // Column.MTIME
  198. );
  199. _icon_view = new Gtk.IconView();
  200. _icon_view.set_model(_list_store);
  201. _icon_view.set_item_width(80);
  202. _icon_view.enable_model_drag_source(Gdk.ModifierType.BUTTON1_MASK, dnd_targets, Gdk.DragAction.COPY);
  203. _icon_view.drag_data_get.connect(on_drag_data_get);
  204. _icon_view.drag_begin.connect_after(on_drag_begin);
  205. _icon_view.drag_end.connect(on_drag_end);
  206. _icon_view.drag_data_received.connect(on_drag_data_received);
  207. _icon_view.has_tooltip = true;
  208. _icon_view.query_tooltip.connect(on_icon_view_query_tooltip);
  209. /*
  210. _icon_view_gesture_click = new Gtk.GestureMultiPress(_icon_view);
  211. _icon_view_gesture_click.set_button(0);
  212. _icon_view_gesture_click.pressed.connect((n_press, x, y) => {
  213. on_button_pressed(_icon_view_gesture_click.get_current_button(), n_press, x, y);
  214. });
  215. */
  216. _icon_view.button_press_event.connect((ev) => {
  217. int n_press = 1;
  218. if (ev.type == Gdk.EventType.@2BUTTON_PRESS)
  219. n_press = 2;
  220. return on_button_pressed(ev.button, n_press, ev.x, ev.y);
  221. });
  222. const Gtk.TargetEntry targets[] =
  223. {
  224. { "text/uri-list", 0, 0 },
  225. };
  226. _icon_view.enable_model_drag_dest(targets
  227. , Gdk.DragAction.COPY
  228. | Gdk.DragAction.MOVE
  229. );
  230. // https://gitlab.gnome.org/GNOME/gtk/-/blob/3.24.43/gtk/gtkiconview.c#L5147
  231. _cell_renderer_text = new Gtk.CellRendererText();
  232. _cell_renderer_text.set("wrap-mode", Pango.WrapMode.WORD_CHAR);
  233. _cell_renderer_text.set("alignment", Pango.Alignment.CENTER);
  234. _cell_renderer_text.set("xalign", 0.5);
  235. _cell_renderer_text.set("yalign", 0.0);
  236. int wrap_width = _icon_view.item_width;
  237. wrap_width -= 2 * _icon_view.item_padding * 2;
  238. _cell_renderer_text.set("wrap-width", wrap_width);
  239. _cell_renderer_text.set("width", wrap_width);
  240. _icon_view.pack_end(_cell_renderer_text, false);
  241. _icon_view.set_cell_data_func(_cell_renderer_text, icon_view_text_func);
  242. _cell_renderer_pixbuf = new Gtk.CellRendererPixbuf();
  243. _cell_renderer_pixbuf.stock_size = Gtk.IconSize.DIALOG;
  244. _icon_view.pack_start(_cell_renderer_pixbuf, false);
  245. _icon_view.set_cell_data_func(_cell_renderer_pixbuf, icon_view_pixbuf_func);
  246. _list_view = new Gtk.TreeView();
  247. _list_view.set_model(_list_store);
  248. _list_view.enable_model_drag_source(Gdk.ModifierType.BUTTON1_MASK, dnd_targets, Gdk.DragAction.COPY);
  249. _list_view.drag_data_get.connect(on_drag_data_get);
  250. _list_view.drag_begin.connect_after(on_drag_begin);
  251. _list_view.drag_end.connect(on_drag_end);
  252. _list_view_gesture_click = new Gtk.GestureMultiPress(_list_view);
  253. _list_view_gesture_click.set_button(0);
  254. _list_view_gesture_click.pressed.connect((n_press, x, y) => {
  255. on_button_pressed(_list_view_gesture_click.get_current_button(), n_press, x, y);
  256. });
  257. var cell_pixbuf = new Gtk.CellRendererPixbuf();
  258. cell_pixbuf.stock_size = Gtk.IconSize.DND;
  259. var cell_text = new Gtk.CellRendererText();
  260. Gtk.TreeViewColumn column = null;
  261. column = new Gtk.TreeViewColumn();
  262. // column.title = "Thumbnail";
  263. column.pack_start(cell_pixbuf, false);
  264. column.set_cell_data_func(cell_pixbuf, list_view_pixbuf_func);
  265. _list_view.append_column(column);
  266. column = new Gtk.TreeViewColumn();
  267. column.title = "Basename";
  268. column.pack_start(cell_text, true);
  269. column.set_cell_data_func(cell_text, list_view_basename_text_func);
  270. _list_view.append_column(column);
  271. column = new Gtk.TreeViewColumn();
  272. column.title = "Type";
  273. column.pack_start(cell_text, true);
  274. column.set_cell_data_func(cell_text, list_view_type_text_func);
  275. _list_view.append_column(column);
  276. column = new Gtk.TreeViewColumn();
  277. column.title = "Size";
  278. column.pack_start(cell_text, true);
  279. column.set_cell_data_func(cell_text, list_view_size_text_func);
  280. _list_view.append_column(column);
  281. column = new Gtk.TreeViewColumn();
  282. column.title = "Modified";
  283. column.pack_start(cell_text, true);
  284. column.set_cell_data_func(cell_text, list_view_mtime_text_func);
  285. _list_view.append_column(column);
  286. column = new Gtk.TreeViewColumn();
  287. column.title = "Name";
  288. column.pack_start(cell_text, true);
  289. column.set_cell_data_func(cell_text, list_view_name_text_func);
  290. _list_view.append_column(column);
  291. _empty_pixbuf = new Gdk.Pixbuf.from_data({ 0x00, 0x00, 0x00, 0x00 }, Gdk.Colorspace.RGB, true, 8, 1, 1, 4);
  292. _showing_project_folder = true;
  293. _icon_view_window = new Gtk.ScrolledWindow(null, null);
  294. _icon_view_window.add(_icon_view);
  295. _list_view_window = new Gtk.ScrolledWindow(null, null);
  296. _list_view_window.add(_list_view);
  297. _stack = new Gtk.Stack();
  298. _stack.add_named(_icon_view_window, "icon-view");
  299. _stack.add_named(_list_view_window, "list-view");
  300. _stack.set_visible_child_full("icon-view", Gtk.StackTransitionType.NONE);
  301. this.pack_start(_stack);
  302. }
  303. private void on_drag_data_get(Gdk.DragContext context, Gtk.SelectionData data, uint info, uint time_)
  304. {
  305. // https://valadoc.org/gtk+-3.0/Gtk.Widget.drag_data_get.html
  306. Gtk.TreePath path;
  307. if (!selected_path(out path))
  308. return;
  309. Gtk.TreeIter iter;
  310. _list_store.get_iter(out iter, path);
  311. Value val;
  312. string type;
  313. string name;
  314. _list_store.get_value(iter, Column.TYPE, out val);
  315. type = (string)val;
  316. _list_store.get_value(iter, Column.NAME, out val);
  317. name = (string)val;
  318. string resource_path = ResourceId.path(type, name);
  319. data.set(Gdk.Atom.intern_static_string("RESOURCE_PATH"), 8, resource_path.data);
  320. }
  321. private void on_drag_begin(Gdk.DragContext context)
  322. {
  323. // https://valadoc.org/gtk+-3.0/Gtk.Widget.drag_begin.html
  324. Gtk.drag_set_icon_pixbuf(context, _empty_pixbuf, 0, 0);
  325. }
  326. private void on_drag_end(Gdk.DragContext context)
  327. {
  328. // https://valadoc.org/gtk+-3.0/Gtk.Widget.drag_end.html
  329. GLib.Application.get_default().activate_action("cancel-place", null);
  330. }
  331. private void on_drag_data_received(Gdk.DragContext context, int x, int y, Gtk.SelectionData selection_data, uint info, uint time_)
  332. {
  333. Gtk.TreePath? path = path_at_pos(x, y);
  334. if (path != null) {
  335. _icon_view.select_path(path);
  336. _icon_view.scroll_to_path(path, false, 0.0f, 0.0f);
  337. }
  338. string type;
  339. string name;
  340. resource_at_path(out type, out name, path);
  341. if (type == "<folder>") {
  342. string[] uris = selection_data.get_uris();
  343. string[] filenames = new string[uris.length];
  344. // Convert URIs to filenames.
  345. for (int i = 0; i < uris.length; ++i)
  346. filenames[i] = GLib.Filename.from_uri(uris[i]);
  347. GLib.Application.get_default().activate_action("import", new GLib.Variant.tuple({name, filenames}));
  348. }
  349. Gtk.drag_finish(context, true, false, time_);
  350. }
  351. private bool on_button_pressed(uint button, int n_press, double x, double y)
  352. {
  353. Gtk.TreePath? path = path_at_pos((int)x, (int)y);
  354. if (button == Gdk.BUTTON_SECONDARY) {
  355. string type;
  356. string name;
  357. if (path != null) {
  358. _icon_view.select_path(path);
  359. _icon_view.scroll_to_path(path, false, 0.0f, 0.0f);
  360. }
  361. resource_at_path(out type, out name, path);
  362. GLib.Menu? menu_model;
  363. if (_showing_project_folder)
  364. menu_model = project_entry_menu_create(type, name);
  365. else
  366. menu_model = favorites_entry_menu_create(type, name);
  367. if (menu_model != null) {
  368. Gtk.Popover menu = new Gtk.Popover.from_model(this, menu_model);
  369. if (_stack.get_visible_child() == _icon_view_window) {
  370. // Adjust for scroll offset since IconView fails to do it itself.
  371. var new_x = x - _icon_view_window.get_hadjustment().get_value();
  372. var new_y = y - _icon_view_window.get_vadjustment().get_value();
  373. menu.set_pointing_to({ (int)new_x, (int)new_y, 1, 1 });
  374. } else {
  375. menu.set_pointing_to({ (int)x, (int)y, 1, 1 });
  376. }
  377. menu.set_position(Gtk.PositionType.BOTTOM);
  378. menu.popup();
  379. }
  380. return Gdk.EVENT_STOP; // Stop the event. Otherwise, popover menu won't show on _icon_view.
  381. } else if (button == Gdk.BUTTON_PRIMARY && n_press == 2) {
  382. if (path != null) {
  383. Gtk.TreeIter iter;
  384. _list_store.get_iter(out iter, path);
  385. Value type;
  386. Value name;
  387. _list_store.get_value(iter, Column.TYPE, out type);
  388. _list_store.get_value(iter, Column.NAME, out name);
  389. if ((string)type == "<folder>") {
  390. string dir_name;
  391. if ((string)name == "..")
  392. dir_name = ResourceId.parent_folder((string)_selected_name);
  393. else
  394. dir_name = (string)name;
  395. GLib.Application.get_default().activate_action("open-directory", new GLib.Variant.string(dir_name));
  396. } else {
  397. GLib.Application.get_default().activate_action("open-resource", ResourceId.path((string)type, (string)name));
  398. }
  399. }
  400. }
  401. return Gdk.EVENT_PROPAGATE;
  402. }
  403. private void icon_view_pixbuf_func(Gtk.CellLayout cell_layout, Gtk.CellRenderer cell, Gtk.TreeModel model, Gtk.TreeIter iter)
  404. {
  405. Value val;
  406. string type;
  407. string name;
  408. model.get_value(iter, Column.TYPE, out val);
  409. type = (string)val;
  410. model.get_value(iter, Column.NAME, out val);
  411. name = (string)val;
  412. set_thumbnail(cell, type, name, 64, _thumbnail_cache);
  413. }
  414. private void icon_view_text_func(Gtk.CellLayout cell_layout, Gtk.CellRenderer cell, Gtk.TreeModel model, Gtk.TreeIter iter)
  415. {
  416. Value type;
  417. Value name;
  418. model.get_value(iter, Column.TYPE, out type);
  419. model.get_value(iter, Column.NAME, out name);
  420. if (name == "..")
  421. cell.set_property("text", name);
  422. else
  423. cell.set_property("text", GLib.Path.get_basename((string)name));
  424. }
  425. private void list_view_pixbuf_func(Gtk.CellLayout cell_layout, Gtk.CellRenderer cell, Gtk.TreeModel model, Gtk.TreeIter iter)
  426. {
  427. Value val;
  428. string type;
  429. string name;
  430. model.get_value(iter, Column.TYPE, out val);
  431. type = (string)val;
  432. model.get_value(iter, Column.NAME, out val);
  433. name = (string)val;
  434. set_thumbnail(cell, type, name, 32, _thumbnail_cache);
  435. }
  436. private void list_view_basename_text_func(Gtk.CellLayout cell_layout, Gtk.CellRenderer cell, Gtk.TreeModel model, Gtk.TreeIter iter)
  437. {
  438. Value name;
  439. model.get_value(iter, Column.NAME, out name);
  440. if (name == "..")
  441. cell.set_property("text", name);
  442. else
  443. cell.set_property("text", GLib.Path.get_basename((string)name));
  444. }
  445. private void list_view_type_text_func(Gtk.CellLayout cell_layout, Gtk.CellRenderer cell, Gtk.TreeModel model, Gtk.TreeIter iter)
  446. {
  447. Value type;
  448. model.get_value(iter, Column.TYPE, out type);
  449. cell.set_property("text", prettify_type((string)type));
  450. }
  451. private void list_view_size_text_func(Gtk.CellLayout cell_layout, Gtk.CellRenderer cell, Gtk.TreeModel model, Gtk.TreeIter iter)
  452. {
  453. Value val;
  454. model.get_value(iter, Column.SIZE, out val);
  455. uint64 size = (uint64)val;
  456. if (size != 0)
  457. cell.set_property("text", prettify_size(size));
  458. else
  459. cell.set_property("text", "n/a");
  460. }
  461. private void list_view_mtime_text_func(Gtk.CellLayout cell_layout, Gtk.CellRenderer cell, Gtk.TreeModel model, Gtk.TreeIter iter)
  462. {
  463. Value type;
  464. model.get_value(iter, Column.MTIME, out type);
  465. uint64 mtime = (uint64)type;
  466. if (mtime != 0)
  467. cell.set_property("text", prettify_time(mtime));
  468. else
  469. cell.set_property("text", "n/a");
  470. }
  471. private void list_view_name_text_func(Gtk.CellLayout cell_layout, Gtk.CellRenderer cell, Gtk.TreeModel model, Gtk.TreeIter iter)
  472. {
  473. Value name;
  474. model.get_value(iter, Column.NAME, out name);
  475. if (name == "..")
  476. cell.set_property("text", "n/a");
  477. else
  478. cell.set_property("text", (string)name);
  479. }
  480. public void reveal(string type, string name)
  481. {
  482. _list_store.foreach((model, path, iter) => {
  483. GLib.Value val;
  484. string store_type;
  485. string store_name;
  486. model.get_value(iter, Column.TYPE, out val);
  487. store_type = (string)val;
  488. model.get_value(iter, Column.NAME, out val);
  489. store_name = (string)val;
  490. if (store_name == name && store_type == type) {
  491. _icon_view.select_path(path);
  492. _icon_view.scroll_to_path(path, false, 0.0f, 0.0f);
  493. _list_view.get_selection().select_path(path);
  494. _list_view.scroll_to_cell(path, null, false, 0.0f, 0.0f);
  495. return true;
  496. }
  497. return false;
  498. });
  499. }
  500. public bool selected_path(out Gtk.TreePath? path)
  501. {
  502. if (_stack.get_visible_child() == _icon_view_window) {
  503. GLib.List<Gtk.TreePath> selected_paths = _icon_view.get_selected_items();
  504. if (selected_paths.length() == 0u) {
  505. path = null;
  506. return false;
  507. }
  508. path = selected_paths.nth(0).data;
  509. return true;
  510. } else if (_stack.get_visible_child() == _list_view_window) {
  511. Gtk.TreeModel selected_model;
  512. Gtk.TreeIter iter;
  513. if (!_list_view.get_selection().get_selected(out selected_model, out iter)) {
  514. path = null;
  515. return false;
  516. }
  517. path = selected_model.get_path(iter);
  518. return true;
  519. } else {
  520. path = null;
  521. return false;
  522. }
  523. }
  524. private bool on_icon_view_query_tooltip(int x, int y, bool keyboard_tooltip, Gtk.Tooltip tooltip)
  525. {
  526. int bx;
  527. int by;
  528. _icon_view.convert_widget_to_bin_window_coords((int)x, (int)y, out bx, out by);
  529. Gtk.TreePath? path = _icon_view.get_path_at_pos(bx, by);
  530. if (path == null)
  531. return false;
  532. Gtk.TreeIter iter;
  533. _list_store.get_iter(out iter, path);
  534. Value val;
  535. _list_store.get_value(iter, Column.TYPE, out val);
  536. string type = (string)val;
  537. _list_store.get_value(iter, Column.NAME, out val);
  538. string name = (string)val;
  539. _list_store.get_value(iter, Column.SIZE, out val);
  540. uint64 size = (uint64)val;
  541. _list_store.get_value(iter, Column.MTIME, out val);
  542. uint64 mtime = (uint64)val;
  543. string text = "<b>%s</b>\nType: %s\nSize: %s\nModified: %s".printf(GLib.Markup.escape_text(name)
  544. , GLib.Markup.escape_text(prettify_type(type))
  545. , size == 0 ? "n/a" : prettify_size(size)
  546. , mtime == 0 ? "n/a" : prettify_time(mtime)
  547. );
  548. tooltip.set_markup(text);
  549. return true;
  550. }
  551. private static string prettify_type(string type)
  552. {
  553. if (type == "<folder>")
  554. return "Folder";
  555. else
  556. return type;
  557. }
  558. private static string prettify_size(uint64 size)
  559. {
  560. uint64 si_size;
  561. string si_unit;
  562. if (size >= 1024*1024*1024) {
  563. si_size = size / (1024*1024*1024);
  564. si_unit = "GiB";
  565. } else if (size >= 1024*1024) {
  566. si_size = size / (1024*1024);
  567. si_unit = "MiB";
  568. } else if (size >= 1024) {
  569. si_size = size / 1024;
  570. si_unit = "KiB";
  571. } else {
  572. si_size = size;
  573. si_unit = size > 1 ? "bytes" : "byte";
  574. }
  575. return "%d %s".printf((int)si_size, si_unit);
  576. }
  577. private static string prettify_time(uint64 time)
  578. {
  579. int64 mtime_secs = (int64)(time / (1000*1000*1000));
  580. GLib.DateTime date_time = new GLib.DateTime.from_unix_local(mtime_secs);
  581. return date_time.format("%d %b %Y; %H:%M:%S");
  582. }
  583. private Gtk.TreePath? path_at_pos(int x, int y)
  584. {
  585. Gtk.TreePath? path = null;
  586. if (_stack.get_visible_child() == _icon_view_window) {
  587. path = _icon_view.get_path_at_pos(x, y);
  588. } else if (_stack.get_visible_child() == _list_view_window) {
  589. int bx;
  590. int by;
  591. _list_view.convert_widget_to_bin_window_coords(x, y, out bx, out by);
  592. if (!_list_view.get_path_at_pos(bx, by, out path, null, null, null))
  593. path = null;
  594. } else {
  595. assert(false);
  596. return null;
  597. }
  598. return path;
  599. }
  600. private void resource_at_path(out string type, out string name, Gtk.TreePath? path)
  601. {
  602. if (path != null) {
  603. Gtk.TreeIter iter;
  604. _list_store.get_iter(out iter, path);
  605. Value val;
  606. _list_store.get_value(iter, Column.TYPE, out val);
  607. type = (string)val;
  608. _list_store.get_value(iter, Column.NAME, out val);
  609. name = (string)val;
  610. } else {
  611. type = _selected_type;
  612. name = _selected_name;
  613. }
  614. }
  615. }
  616. public class ProjectBrowser : Gtk.Box
  617. {
  618. public enum SortMode
  619. {
  620. NAME_AZ,
  621. NAME_ZA,
  622. TYPE_AZ,
  623. TYPE_ZA,
  624. SIZE_MIN_MAX,
  625. SIZE_MAX_MIN,
  626. LAST_MTIME,
  627. FIRST_MTIME,
  628. COUNT;
  629. public string to_label()
  630. {
  631. switch (this) {
  632. case NAME_AZ:
  633. return "Name A-Z";
  634. case NAME_ZA:
  635. return "Name Z-A";
  636. case TYPE_AZ:
  637. return "Type A-Z";
  638. case TYPE_ZA:
  639. return "Type Z-A";
  640. case SIZE_MIN_MAX:
  641. return "Size min-Max";
  642. case SIZE_MAX_MIN:
  643. return "Size Max-min";
  644. case LAST_MTIME:
  645. return "Last Modified";
  646. case FIRST_MTIME:
  647. return "First Modified";
  648. default:
  649. return "Unknown";
  650. }
  651. }
  652. }
  653. // Data
  654. public ProjectStore _project_store;
  655. public ThumbnailCache _thumbnail_cache;
  656. // Widgets
  657. public Gtk.TreeModelFilter _tree_filter;
  658. public Gtk.TreeModelSort _tree_sort;
  659. public Gtk.TreeView _tree_view;
  660. public Gtk.TreeSelection _tree_selection;
  661. public Gdk.Pixbuf _empty_pixbuf;
  662. public ProjectFolderView _folder_view;
  663. public bool _show_folder_view;
  664. public Gtk.Image _toggle_folder_view_image;
  665. public Gtk.Button _toggle_folder_view;
  666. public Gtk.Box _tree_view_content;
  667. public Gtk.Image _toggle_icon_view_image;
  668. public Gtk.Button _toggle_icon_view;
  669. public Gtk.ListStore _folder_list_store;
  670. public Gtk.TreeModelSort _folder_list_sort;
  671. public SortMode _sort_mode;
  672. public Gtk.Box _sort_items_box;
  673. public Gtk.Popover _sort_items_popover;
  674. public Gtk.MenuButton _sort_items;
  675. public Gtk.Box _empty_favorites_box;
  676. public Gtk.Stack _folder_stack;
  677. public Gtk.Box _folder_view_content;
  678. public Gtk.ScrolledWindow _scrolled_window;
  679. public Gtk.Paned _paned;
  680. public Gtk.GestureMultiPress _tree_view_gesture_click;
  681. public bool _hide_core_resources;
  682. public ProjectBrowser(ProjectStore project_store, ThumbnailCache thumbnail_cache)
  683. {
  684. Object(orientation: Gtk.Orientation.VERTICAL);
  685. // Data
  686. _project_store = project_store;
  687. _thumbnail_cache = thumbnail_cache;
  688. _thumbnail_cache.changed.connect(() => {
  689. _tree_view.queue_draw();
  690. _folder_view.queue_draw();
  691. });
  692. // Widgets
  693. _tree_filter = new Gtk.TreeModelFilter(_project_store._tree_store, null);
  694. _tree_filter.set_visible_func((model, iter) => {
  695. if (_project_store.project_root_path() != null)
  696. _tree_view.expand_row(_project_store.project_root_path(), false);
  697. Value type;
  698. Value name;
  699. model.get_value(iter, ProjectStore.Column.TYPE, out type);
  700. model.get_value(iter, ProjectStore.Column.NAME, out name);
  701. bool should_show = (string)type != null
  702. && (string)name != null
  703. && !row_should_be_hidden((string)type, (string)name)
  704. ;
  705. if (_show_folder_view) {
  706. // Hide all descendants of the favorites root.
  707. Gtk.TreePath? path = model.get_path(iter);
  708. if (path != null && _project_store.favorites_root_path() != null && path.is_descendant(_project_store.favorites_root_path()))
  709. return false;
  710. return should_show && (type == "<folder>" || type == "<favorites>");
  711. } else {
  712. return should_show;
  713. }
  714. });
  715. _tree_sort = new Gtk.TreeModelSort.with_model(_tree_filter);
  716. _tree_sort.set_default_sort_func((model, iter_a, iter_b) => {
  717. Value type_a;
  718. Value type_b;
  719. model.get_value(iter_a, ProjectStore.Column.TYPE, out type_a);
  720. model.get_value(iter_b, ProjectStore.Column.TYPE, out type_b);
  721. // Favorites is always on top.
  722. if ((string)type_a == "<favorites>")
  723. return -1;
  724. if ((string)type_b == "<favorites>")
  725. return 1;
  726. // Then folders.
  727. if ((string)type_a == "<folder>") {
  728. if ((string)type_b != "<folder>")
  729. return -1;
  730. } else if ((string)type_b == "<folder>") {
  731. if ((string)type_a != "<folder>")
  732. return 1;
  733. }
  734. // And finally, regular files.
  735. Value id_a;
  736. Value id_b;
  737. model.get_value(iter_a, ProjectStore.Column.NAME, out id_a);
  738. model.get_value(iter_b, ProjectStore.Column.NAME, out id_b);
  739. return strcmp(GLib.Path.get_basename((string)id_a), GLib.Path.get_basename((string)id_b));
  740. });
  741. Gtk.CellRendererPixbuf cell_pixbuf = new Gtk.CellRendererPixbuf();
  742. cell_pixbuf.stock_size = Gtk.IconSize.SMALL_TOOLBAR;
  743. Gtk.CellRendererText cell_text = new Gtk.CellRendererText();
  744. Gtk.TreeViewColumn column = new Gtk.TreeViewColumn();
  745. column.pack_start(cell_pixbuf, false);
  746. column.pack_start(cell_text, true);
  747. column.set_cell_data_func(cell_pixbuf, pixbuf_func);
  748. column.set_cell_data_func(cell_text, text_func);
  749. _tree_view = new Gtk.TreeView();
  750. _tree_view.append_column(column);
  751. #if 0
  752. // For debugging.
  753. _tree_view.insert_column_with_attributes(-1
  754. , "Segment"
  755. , new Gtk.CellRendererText()
  756. , "text"
  757. , ProjectStore.Column.SEGMENT
  758. , null
  759. );
  760. _tree_view.insert_column_with_attributes(-1
  761. , "Name"
  762. , new Gtk.CellRendererText()
  763. , "text"
  764. , ProjectStore.Column.NAME
  765. , null
  766. );
  767. _tree_view.insert_column_with_attributes(-1
  768. , "Type"
  769. , new Gtk.CellRendererText()
  770. , "text"
  771. , ProjectStore.Column.TYPE
  772. , null
  773. );
  774. #endif /* if 0 */
  775. _tree_view.model = _tree_sort;
  776. _tree_view.headers_visible = false;
  777. _tree_view_gesture_click = new Gtk.GestureMultiPress(_tree_view);
  778. _tree_view_gesture_click.set_button(0);
  779. _tree_view_gesture_click.pressed.connect(on_button_pressed);
  780. _tree_view.enable_model_drag_source(Gdk.ModifierType.BUTTON1_MASK, dnd_targets, Gdk.DragAction.COPY);
  781. _tree_view.drag_data_get.connect(on_drag_data_get);
  782. _tree_view.drag_begin.connect_after(on_drag_begin);
  783. _tree_view.drag_end.connect(on_drag_end);
  784. _tree_selection = _tree_view.get_selection();
  785. _tree_selection.set_mode(Gtk.SelectionMode.BROWSE);
  786. _tree_selection.changed.connect(() => { update_folder_view(); });
  787. _empty_pixbuf = new Gdk.Pixbuf.from_data({ 0x00, 0x00, 0x00, 0x00 }, Gdk.Colorspace.RGB, true, 8, 1, 1, 4);
  788. _project_store._tree_store.row_inserted.connect((path, iter) => { update_folder_view(); });
  789. _project_store._tree_store.row_changed.connect((path, iter) => { update_folder_view(); });
  790. _project_store._tree_store.row_deleted.connect((path) => { update_folder_view(); });
  791. // Create icon view.
  792. _folder_view = new ProjectFolderView(_project_store, thumbnail_cache);
  793. // Create switch button.
  794. _show_folder_view = true;
  795. _toggle_folder_view_image = new Gtk.Image.from_icon_name("level-tree-symbolic", Gtk.IconSize.SMALL_TOOLBAR);
  796. _toggle_folder_view = new Gtk.Button();
  797. _toggle_folder_view.add(_toggle_folder_view_image);
  798. _toggle_folder_view.get_style_context().add_class("flat");
  799. _toggle_folder_view.get_style_context().add_class("image-button");
  800. _toggle_folder_view.can_focus = false;
  801. _toggle_folder_view.clicked.connect(() => {
  802. _show_folder_view = !_show_folder_view;
  803. if (_show_folder_view) {
  804. // Save the currently selected resource and a path to its parent. Those will be
  805. // used later, after the tree has been refiltered, to show the correct folder
  806. // and reveal the selected resource in the icon view.
  807. string? selected_type = null;
  808. string? selected_name = null;
  809. Gtk.TreePath? parent_path = null;
  810. Gtk.TreeModel selected_model;
  811. Gtk.TreeIter selected_iter;
  812. if (_tree_selection.get_selected(out selected_model, out selected_iter)) {
  813. Value val;
  814. selected_model.get_value(selected_iter, ProjectStore.Column.TYPE, out val);
  815. selected_type = (string)val;
  816. selected_model.get_value(selected_iter, ProjectStore.Column.NAME, out val);
  817. selected_name = (string)val;
  818. if (selected_type != "<folder>") {
  819. Gtk.TreeIter parent_iter;
  820. if (selected_model.iter_parent(out parent_iter, selected_iter))
  821. parent_path = _tree_view.model.get_path(parent_iter);
  822. }
  823. }
  824. _tree_filter.refilter();
  825. if (parent_path != null) {
  826. _tree_selection.select_path(parent_path);
  827. _folder_view.reveal(selected_type, selected_name);
  828. }
  829. _folder_view_content.show_all();
  830. _toggle_folder_view_image.set_from_icon_name("level-tree-symbolic", Gtk.IconSize.SMALL_TOOLBAR);
  831. } else {
  832. // Save the currently selected resource. This will be used later, after the tree
  833. // has been refiltered, to reveal the selected resource in the tree view.
  834. string? selected_type = null;
  835. string? selected_name = null;
  836. Gtk.TreePath selected_path;
  837. if (_folder_view.selected_path(out selected_path)) {
  838. Gtk.TreeIter iter;
  839. _folder_view._list_store.get_iter(out iter, selected_path);
  840. GLib.Value val;
  841. _folder_view._list_store.get_value(iter, ProjectFolderView.Column.TYPE, out val);
  842. selected_type = (string)val;
  843. _folder_view._list_store.get_value(iter, ProjectFolderView.Column.NAME, out val);
  844. selected_name = (string)val;
  845. }
  846. _tree_filter.refilter();
  847. if (selected_type != null && selected_type != "<folder>") {
  848. reveal(selected_type, selected_name);
  849. }
  850. _folder_view_content.hide();
  851. _toggle_folder_view_image.set_from_icon_name("browser-icon-view", Gtk.IconSize.SMALL_TOOLBAR);
  852. _tree_view.queue_draw(); // It doesn't draw by itself sometimes...
  853. }
  854. });
  855. // Create paned split-view.
  856. _scrolled_window = new Gtk.ScrolledWindow(null, null);
  857. _scrolled_window.add(_tree_view);
  858. var _tree_view_control = new Gtk.Box(Gtk.Orientation.HORIZONTAL, 0);
  859. _tree_view_control.pack_end(_toggle_folder_view, false, false);
  860. _tree_view_content = new Gtk.Box(Gtk.Orientation.VERTICAL, 0);
  861. _tree_view_content.pack_start(_tree_view_control, false);
  862. _tree_view_content.pack_start(_scrolled_window, true, true);
  863. // Setup sort menu button popover.
  864. _sort_items_box = new Gtk.Box(Gtk.Orientation.VERTICAL, 0);
  865. Gtk.RadioButton? button = null;
  866. for (int i = 0; i < SortMode.COUNT; ++i)
  867. button = add_sort_item(button, (SortMode)i);
  868. _sort_items_box.show_all();
  869. _sort_items_popover = new Gtk.Popover(null);
  870. _sort_items_popover.add(_sort_items_box);
  871. _sort_items = new Gtk.MenuButton();
  872. _sort_items.add(new Gtk.Image.from_icon_name("list-sort", Gtk.IconSize.SMALL_TOOLBAR));
  873. _sort_items.get_style_context().add_class("flat");
  874. _sort_items.get_style_context().add_class("image-button");
  875. _sort_items.can_focus = false;
  876. _sort_items.set_popover(_sort_items_popover);
  877. bool _show_icon_view = true;
  878. _toggle_icon_view_image = new Gtk.Image.from_icon_name("browser-list-view", Gtk.IconSize.SMALL_TOOLBAR);
  879. _toggle_icon_view = new Gtk.Button();
  880. _toggle_icon_view.add(_toggle_icon_view_image);
  881. _toggle_icon_view.get_style_context().add_class("flat");
  882. _toggle_icon_view.get_style_context().add_class("image-button");
  883. _toggle_icon_view.can_focus = false;
  884. _toggle_icon_view.clicked.connect(() => {
  885. Gtk.TreePath path;
  886. bool any_selected = _folder_view.selected_path(out path);
  887. if (_show_icon_view) {
  888. if (any_selected) {
  889. Gtk.TreeIter iter;
  890. _folder_view._list_store.get_iter(out iter, path);
  891. _folder_view._list_view.get_selection().select_iter(iter);
  892. }
  893. _folder_view._stack.set_visible_child_full("list-view", Gtk.StackTransitionType.NONE);
  894. _toggle_icon_view_image.set_from_icon_name("browser-icon-view", Gtk.IconSize.SMALL_TOOLBAR);
  895. } else {
  896. if (any_selected)
  897. _folder_view._icon_view.select_path(path);
  898. _folder_view._stack.set_visible_child_full("icon-view", Gtk.StackTransitionType.NONE);
  899. _toggle_icon_view_image.set_from_icon_name("browser-list-view", Gtk.IconSize.SMALL_TOOLBAR);
  900. }
  901. _show_icon_view = !_show_icon_view;
  902. });
  903. var _folder_view_control = new Gtk.Box(Gtk.Orientation.HORIZONTAL, 0);
  904. _folder_view_control.pack_end(_toggle_icon_view, false, false);
  905. _folder_view_control.pack_end(_sort_items, false, false);
  906. _empty_favorites_box = new Gtk.Box(Gtk.Orientation.VERTICAL, 0);
  907. _empty_favorites_box.valign = Gtk.Align.CENTER;
  908. _empty_favorites_box.pack_start(new Gtk.Image.from_icon_name("browser-favorites", Gtk.IconSize.DIALOG), false, false);
  909. _empty_favorites_box.pack_start(new Gtk.Label("Favorites is empty"), false, false);
  910. _folder_stack = new Gtk.Stack();
  911. _folder_stack.add_named(_folder_view, "folder-view");
  912. _folder_stack.add_named(_empty_favorites_box, "empty-favorites");
  913. _folder_stack.set_visible_child_full("folder-view", Gtk.StackTransitionType.NONE);
  914. _folder_view_content = new Gtk.Box(Gtk.Orientation.VERTICAL, 0);
  915. _folder_view_content.pack_start(_folder_view_control, false);
  916. _folder_view_content.pack_start(_folder_stack, true, true);
  917. _paned = new Gtk.Paned(Gtk.Orientation.VERTICAL);
  918. _paned.pack1(_tree_view_content, true, false);
  919. _paned.pack2(_folder_view_content, true, false);
  920. _paned.set_position(400);
  921. _hide_core_resources = true;
  922. _folder_list_store = new Gtk.ListStore(ProjectStore.Column.COUNT
  923. , typeof(string) // ProjectStore.Column.NAME
  924. , typeof(string) // ProjectStore.Column.TYPE
  925. , typeof(uint64) // ProjectStore.Column.SIZE
  926. , typeof(uint64) // ProjectStore.Column.MTIME
  927. );
  928. _folder_list_sort = new Gtk.TreeModelSort.with_model(_folder_list_store);
  929. _folder_list_sort.set_default_sort_func((model, iter_a, iter_b) => {
  930. Value type_a;
  931. Value type_b;
  932. model.get_value(iter_a, ProjectStore.Column.TYPE, out type_a);
  933. model.get_value(iter_b, ProjectStore.Column.TYPE, out type_b);
  934. Value name_a;
  935. Value name_b;
  936. model.get_value(iter_a, ProjectStore.Column.NAME, out name_a);
  937. model.get_value(iter_b, ProjectStore.Column.NAME, out name_b);
  938. // Folders are always on top.
  939. if ((string)type_a == "<folder>" && (string)type_b != "<folder>") {
  940. return -1;
  941. } else if ((string)type_a != "<folder>" && (string)type_b == "<folder>") {
  942. return 1;
  943. } else if ((string)type_a == "<folder>" && (string)type_b == "<folder>") {
  944. // Special folders always first.
  945. if ((string)name_a == "..")
  946. return -1;
  947. else if ((string)name_b == "..")
  948. return 1;
  949. }
  950. switch (_sort_mode) {
  951. case SortMode.NAME_AZ:
  952. case SortMode.NAME_ZA: {
  953. int cmp = strcmp((string)name_a, (string)name_b);
  954. return _sort_mode == SortMode.NAME_AZ ? cmp : -cmp;
  955. }
  956. case SortMode.TYPE_AZ:
  957. case SortMode.TYPE_ZA: {
  958. int cmp = strcmp((string)type_a, (string)type_b);
  959. return _sort_mode == SortMode.TYPE_AZ ? cmp : -cmp;
  960. }
  961. case SortMode.SIZE_MIN_MAX:
  962. case SortMode.SIZE_MAX_MIN: {
  963. Value size_a;
  964. Value size_b;
  965. model.get_value(iter_a, ProjectStore.Column.SIZE, out size_a);
  966. model.get_value(iter_b, ProjectStore.Column.SIZE, out size_b);
  967. int cmp = (uint64)size_a <= (uint64)size_b ? -1 : 1;
  968. return _sort_mode == SortMode.SIZE_MIN_MAX ? cmp : -cmp;
  969. }
  970. case SortMode.LAST_MTIME:
  971. case SortMode.FIRST_MTIME: {
  972. Value mtime_a;
  973. Value mtime_b;
  974. model.get_value(iter_a, ProjectStore.Column.MTIME, out mtime_a);
  975. model.get_value(iter_b, ProjectStore.Column.MTIME, out mtime_b);
  976. int cmp = (uint64)mtime_a >= (uint64)mtime_b ? -1 : 1;
  977. return _sort_mode == SortMode.LAST_MTIME ? cmp : -cmp;
  978. }
  979. default:
  980. return 0;
  981. }
  982. });
  983. // Actions.
  984. GLib.ActionEntry[] action_entries =
  985. {
  986. { "open-directory", on_open_directory, "s", null },
  987. { "favorite-resource", on_favorite_resource, "(ss)", null },
  988. { "unfavorite-resource", on_unfavorite_resource, "(ss)", null }
  989. };
  990. GLib.Application.get_default().add_action_entries(action_entries, this);
  991. this.pack_start(_paned);
  992. }
  993. private void on_drag_data_get(Gdk.DragContext context, Gtk.SelectionData data, uint info, uint time_)
  994. {
  995. // https://valadoc.org/gtk+-3.0/Gtk.Widget.drag_data_get.html
  996. Gtk.TreeModel selected_model;
  997. Gtk.TreeIter selected_iter;
  998. if (!_tree_selection.get_selected(out selected_model, out selected_iter))
  999. return;
  1000. Value val;
  1001. string type;
  1002. string name;
  1003. selected_model.get_value(selected_iter, ProjectStore.Column.TYPE, out val);
  1004. type = (string)val;
  1005. selected_model.get_value(selected_iter, ProjectStore.Column.NAME, out val);
  1006. name = (string)val;
  1007. string resource_path = ResourceId.path(type, name);
  1008. data.set(Gdk.Atom.intern_static_string("RESOURCE_PATH"), 8, resource_path.data);
  1009. }
  1010. private void on_drag_begin(Gdk.DragContext context)
  1011. {
  1012. // https://valadoc.org/gtk+-3.0/Gtk.Widget.drag_begin.html
  1013. Gtk.drag_set_icon_pixbuf(context, _empty_pixbuf, 0, 0);
  1014. }
  1015. private void on_drag_end(Gdk.DragContext context)
  1016. {
  1017. // https://valadoc.org/gtk+-3.0/Gtk.Widget.drag_end.html
  1018. GLib.Application.get_default().activate_action("cancel-place", null);
  1019. }
  1020. // Returns true if the row should be hidden.
  1021. private bool row_should_be_hidden(string type, string name)
  1022. {
  1023. return type == "<folder>" && name == "core" && _hide_core_resources
  1024. || type == "importer_settings"
  1025. || name == Project.LEVEL_EDITOR_TEST_NAME
  1026. || _project_store._project.is_type_importable(type)
  1027. ;
  1028. }
  1029. public void reveal(string type, string name)
  1030. {
  1031. if (name.has_prefix("core/")) {
  1032. _hide_core_resources = false;
  1033. _tree_filter.refilter();
  1034. }
  1035. string parent_type = type;
  1036. string parent_name = name;
  1037. Gtk.TreePath filter_path = null;
  1038. do {
  1039. Gtk.TreePath store_path;
  1040. if (!_project_store.path_for_resource_type_name(out store_path, parent_type, parent_name)) {
  1041. break;
  1042. }
  1043. filter_path = _tree_filter.convert_child_path_to_path(store_path);
  1044. if (filter_path == null) {
  1045. // Either the path is not valid or points to a non-visible row in the model.
  1046. parent_type = "<folder>";
  1047. parent_name = ResourceId.parent_folder(parent_name);
  1048. continue;
  1049. }
  1050. Gtk.TreePath sort_path = _tree_sort.convert_child_path_to_path(filter_path);
  1051. if (sort_path == null) {
  1052. // The path is not valid.
  1053. break;
  1054. }
  1055. _tree_view.expand_to_path(sort_path);
  1056. _tree_view.get_selection().select_path(sort_path);
  1057. _tree_view.scroll_to_cell(sort_path, null, false, 0.0f, 0.0f);
  1058. _folder_view.reveal(type, name);
  1059. } while (filter_path == null);
  1060. }
  1061. private void on_open_directory(GLib.SimpleAction action, GLib.Variant? param)
  1062. {
  1063. string dir_name = param.get_string();
  1064. if (dir_name.has_prefix("core/") || dir_name == "core") {
  1065. _hide_core_resources = false;
  1066. _tree_filter.refilter();
  1067. }
  1068. Gtk.TreePath store_path;
  1069. if (_project_store.path_for_resource_type_name(out store_path, "<folder>", dir_name)) {
  1070. Gtk.TreePath filter_path = _tree_filter.convert_child_path_to_path(store_path);
  1071. if (filter_path == null) // Either the path is not valid or points to a non-visible row in the model.
  1072. return;
  1073. Gtk.TreePath sort_path = _tree_sort.convert_child_path_to_path(filter_path);
  1074. if (sort_path == null) // The path is not valid.
  1075. return;
  1076. _tree_view.expand_to_path(sort_path);
  1077. _tree_view.get_selection().select_path(sort_path);
  1078. }
  1079. }
  1080. private void on_favorite_resource(GLib.SimpleAction action, GLib.Variant? param)
  1081. {
  1082. string type = (string)param.get_child_value(0);
  1083. string name = (string)param.get_child_value(1);
  1084. _project_store.add_to_favorites(type, name);
  1085. }
  1086. private void on_unfavorite_resource(GLib.SimpleAction action, GLib.Variant? param)
  1087. {
  1088. string type = (string)param.get_child_value(0);
  1089. string name = (string)param.get_child_value(1);
  1090. _project_store.remove_from_favorites(type, name);
  1091. }
  1092. private void on_button_pressed(int n_press, double x, double y)
  1093. {
  1094. int bx;
  1095. int by;
  1096. Gtk.TreePath path;
  1097. _tree_view.convert_widget_to_bin_window_coords((int)x, (int)y, out bx, out by);
  1098. if (!_tree_view.get_path_at_pos(bx, by, out path, null, null, null))
  1099. return;
  1100. uint button = _tree_view_gesture_click.get_current_button();
  1101. if (button == Gdk.BUTTON_SECONDARY) {
  1102. Gtk.TreeIter iter;
  1103. _tree_view.model.get_iter(out iter, path);
  1104. Value type;
  1105. Value name;
  1106. _tree_view.model.get_value(iter, ProjectStore.Column.TYPE, out type);
  1107. _tree_view.model.get_value(iter, ProjectStore.Column.NAME, out name);
  1108. Gtk.TreePath? filter_path = _tree_sort.convert_path_to_child_path(path);
  1109. Gtk.TreePath? store_path = _tree_filter.convert_path_to_child_path(filter_path);
  1110. GLib.Menu? menu_model;
  1111. if (store_path.is_descendant(_project_store.project_root_path()) || store_path.compare(_project_store.project_root_path()) == 0)
  1112. menu_model = project_entry_menu_create((string)type, (string)name);
  1113. else if (store_path.is_descendant(_project_store.favorites_root_path()))
  1114. menu_model = favorites_entry_menu_create((string)type, (string)name);
  1115. else
  1116. menu_model = null;
  1117. if (menu_model != null) {
  1118. Gtk.Popover menu = new Gtk.Popover.from_model(_tree_view, menu_model);
  1119. menu.set_pointing_to({ (int)x, (int)y, 1, 1 });
  1120. menu.set_position(Gtk.PositionType.BOTTOM);
  1121. menu.popup();
  1122. }
  1123. } else if (button == Gdk.BUTTON_PRIMARY && n_press == 2) {
  1124. Gtk.TreeIter iter;
  1125. _tree_view.model.get_iter(out iter, path);
  1126. Value type;
  1127. _tree_view.model.get_value(iter, ProjectStore.Column.TYPE, out type);
  1128. if ((string)type == "<folder>")
  1129. return;
  1130. Value name;
  1131. _tree_view.model.get_value(iter, ProjectStore.Column.NAME, out name);
  1132. GLib.Application.get_default().activate_action("open-resource", ResourceId.path((string)type, (string)name));
  1133. }
  1134. return;
  1135. }
  1136. private void update_folder_view()
  1137. {
  1138. _folder_list_store.clear();
  1139. _folder_view._list_store.clear();
  1140. // Get the selected node's type and name.
  1141. Gtk.TreeModel selected_model;
  1142. Gtk.TreeIter selected_iter;
  1143. if (!_tree_selection.get_selected(out selected_model, out selected_iter))
  1144. return;
  1145. string selected_type;
  1146. string selected_name;
  1147. Value val;
  1148. selected_model.get_value(selected_iter, ProjectStore.Column.TYPE, out val);
  1149. selected_type = (string)val;
  1150. selected_model.get_value(selected_iter, ProjectStore.Column.NAME, out val);
  1151. selected_name = (string)val;
  1152. if (selected_type == "<folder>") {
  1153. _folder_view._showing_project_folder = true;
  1154. // Add parent folder.
  1155. if (selected_name != "") {
  1156. Gtk.TreeIter dummy;
  1157. _folder_list_store.insert_with_values(out dummy
  1158. , -1
  1159. , ProjectStore.Column.TYPE
  1160. , "<folder>"
  1161. , ProjectStore.Column.NAME
  1162. , ".."
  1163. , ProjectStore.Column.SIZE
  1164. , 0u
  1165. , ProjectStore.Column.MTIME
  1166. , 0u
  1167. , -1
  1168. );
  1169. }
  1170. // Fill the intermediate icon view list with paths matching the selected node's name.
  1171. _project_store._list_store.foreach((model, path, iter) => {
  1172. string type;
  1173. string name;
  1174. model.get_value(iter, ProjectStore.Column.TYPE, out val);
  1175. type = (string)val;
  1176. model.get_value(iter, ProjectStore.Column.NAME, out val);
  1177. name = (string)val;
  1178. if (row_should_be_hidden(type, name))
  1179. return false;
  1180. // Skip paths without common ancestor.
  1181. if (ResourceId.parent_folder(name) != selected_name)
  1182. return false;
  1183. // Skip paths that are too deep in the hierarchy:
  1184. // selected_name: foo
  1185. // hierarchy:
  1186. // foo/bar OK
  1187. // foo/baz OK
  1188. // foo/bar/baz NOPE
  1189. string name_suffix;
  1190. if (selected_name == "") // Project folder.
  1191. name_suffix = name.substring((selected_name).length);
  1192. else if (selected_name != name) // Folder itself.
  1193. name_suffix = name.substring((selected_name).length + 1);
  1194. else
  1195. return false;
  1196. if (name_suffix.index_of_char('/') != -1)
  1197. return false;
  1198. uint64 size;
  1199. uint64 mtime;
  1200. model.get_value(iter, ProjectStore.Column.SIZE, out val);
  1201. size = (uint64)val;
  1202. model.get_value(iter, ProjectStore.Column.MTIME, out val);
  1203. mtime = (uint64)val;
  1204. // Add the path to the list.
  1205. Gtk.TreeIter dummy;
  1206. _folder_list_store.insert_with_values(out dummy
  1207. , -1
  1208. , ProjectStore.Column.TYPE
  1209. , type
  1210. , ProjectStore.Column.NAME
  1211. , name
  1212. , ProjectStore.Column.SIZE
  1213. , size
  1214. , ProjectStore.Column.MTIME
  1215. , mtime
  1216. , -1
  1217. );
  1218. return false;
  1219. });
  1220. _folder_view._selected_type = selected_type;
  1221. _folder_view._selected_name = selected_name;
  1222. _folder_stack.set_visible_child_full("folder-view", Gtk.StackTransitionType.NONE);
  1223. } else if (selected_type == "<favorites>") {
  1224. _folder_view._showing_project_folder = false;
  1225. int num_items = 0;
  1226. // Fill the icon view list with paths whose ancestor is the favorites root.
  1227. _project_store._tree_store.foreach((model, path, iter) => {
  1228. string type;
  1229. string name;
  1230. model.get_value(iter, ProjectStore.Column.TYPE, out val);
  1231. type = (string)val;
  1232. model.get_value(iter, ProjectStore.Column.NAME, out val);
  1233. name = (string)val;
  1234. if (!path.is_descendant(_project_store.favorites_root_path()))
  1235. return false;
  1236. uint64 size;
  1237. uint64 mtime;
  1238. model.get_value(iter, ProjectStore.Column.SIZE, out val);
  1239. size = (uint64)val;
  1240. model.get_value(iter, ProjectStore.Column.MTIME, out val);
  1241. mtime = (uint64)val;
  1242. // Add the path to the list.
  1243. Gtk.TreeIter dummy;
  1244. _folder_list_store.insert_with_values(out dummy
  1245. , -1
  1246. , ProjectStore.Column.TYPE
  1247. , type
  1248. , ProjectStore.Column.NAME
  1249. , name
  1250. , ProjectStore.Column.SIZE
  1251. , size
  1252. , ProjectStore.Column.MTIME
  1253. , mtime
  1254. , -1
  1255. );
  1256. ++num_items;
  1257. return false;
  1258. });
  1259. if (num_items == 0)
  1260. _folder_stack.set_visible_child_full("empty-favorites", Gtk.StackTransitionType.NONE);
  1261. else
  1262. _folder_stack.set_visible_child_full("folder-view", Gtk.StackTransitionType.NONE);
  1263. }
  1264. // Now, fill the actual icon view list with correctly sorted paths.
  1265. _folder_list_sort.foreach((model, path, iter) => {
  1266. string type;
  1267. string name;
  1268. uint64 size;
  1269. uint64 mtime;
  1270. model.get_value(iter, ProjectStore.Column.TYPE, out val);
  1271. type = (string)val;
  1272. model.get_value(iter, ProjectStore.Column.NAME, out val);
  1273. name = (string)val;
  1274. model.get_value(iter, ProjectStore.Column.SIZE, out val);
  1275. size = (uint64)val;
  1276. model.get_value(iter, ProjectStore.Column.MTIME, out val);
  1277. mtime = (uint64)val;
  1278. // Add the path to the list.
  1279. Gtk.TreeIter dummy;
  1280. _folder_view._list_store.insert_with_values(out dummy
  1281. , -1
  1282. , ProjectFolderView.Column.TYPE
  1283. , type
  1284. , ProjectFolderView.Column.NAME
  1285. , name
  1286. , ProjectFolderView.Column.SIZE
  1287. , size
  1288. , ProjectFolderView.Column.MTIME
  1289. , mtime
  1290. , -1
  1291. );
  1292. return false;
  1293. });
  1294. }
  1295. public void select_project_root()
  1296. {
  1297. Gtk.TreePath? filter_path = _tree_filter.convert_child_path_to_path(_project_store.project_root_path());
  1298. if (filter_path == null)
  1299. return;
  1300. Gtk.TreePath? sort_path = _tree_sort.convert_child_path_to_path(filter_path);
  1301. if (sort_path == null)
  1302. return;
  1303. _tree_selection.select_path(sort_path);
  1304. }
  1305. private void pixbuf_func(Gtk.CellLayout cell_layout, Gtk.CellRenderer cell, Gtk.TreeModel model, Gtk.TreeIter iter)
  1306. {
  1307. Value val;
  1308. string type;
  1309. string name;
  1310. model.get_value(iter, ProjectStore.Column.TYPE, out val);
  1311. type = (string)val;
  1312. model.get_value(iter, ProjectStore.Column.NAME, out val);
  1313. name = (string)val;
  1314. set_thumbnail(cell, type, name, 16, _thumbnail_cache);
  1315. }
  1316. private void text_func(Gtk.CellLayout cell_layout, Gtk.CellRenderer cell, Gtk.TreeModel model, Gtk.TreeIter iter)
  1317. {
  1318. Value name;
  1319. Value type;
  1320. model.get_value(iter, ProjectStore.Column.NAME, out name);
  1321. model.get_value(iter, ProjectStore.Column.TYPE, out type);
  1322. string basename = GLib.Path.get_basename((string)name);
  1323. if ((string)type == "<folder>") {
  1324. if ((string)name == "")
  1325. cell.set_property("text", _project_store._project.name());
  1326. else
  1327. cell.set_property("text", basename);
  1328. } else if ((string)type == "<favorites>") {
  1329. cell.set_property("text", "Favorites");
  1330. } else {
  1331. cell.set_property("text", ResourceId.path((string)type, basename));
  1332. }
  1333. }
  1334. private Gtk.RadioButton add_sort_item(Gtk.RadioButton? group, SortMode mode)
  1335. {
  1336. var button = new Gtk.RadioButton.with_label_from_widget(group, mode.to_label());
  1337. button.toggled.connect(() => {
  1338. _sort_mode = mode;
  1339. update_folder_view();
  1340. _sort_items_popover.popdown();
  1341. });
  1342. _sort_items_box.pack_start(button, false, false);
  1343. return button;
  1344. }
  1345. }
  1346. } /* namespace Crown */