project_browser.vala 50 KB

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