project_browser.vala 48 KB

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