project_browser.vala 49 KB

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