project_browser.vala 50 KB

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