project_browser.vala 64 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036
  1. /*
  2. * Copyright (c) 2012-2026 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. public 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. public 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. public 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. public void set_thumbnail(Gtk.CellRenderer cell, string type, string name, int icon_size, ThumbnailCache thumbnail_cache)
  115. {
  116. // https://specifications.freedesktop.org/icon-naming-spec/icon-naming-spec-latest.html
  117. if (type == "<folder>")
  118. cell.set_property("icon-name", "browser-folder-symbolic");
  119. else if ((string)type == "<favorites>")
  120. cell.set_property("icon-name", "browser-favorites");
  121. else if ((string)type == OBJECT_TYPE_STATE_MACHINE)
  122. cell.set_property("icon-name", "object-state-machine");
  123. else if ((string)type == "config")
  124. cell.set_property("icon-name", "object-config");
  125. else if ((string)type == OBJECT_TYPE_FONT)
  126. cell.set_property("icon-name", "object-font");
  127. else if ((string)type == OBJECT_TYPE_LEVEL)
  128. cell.set_property("icon-name", "object-level");
  129. else if ((string)type == OBJECT_TYPE_MATERIAL)
  130. cell.set_property("pixbuf", thumbnail_cache.get(type, name, icon_size));
  131. else if ((string)type == OBJECT_TYPE_MESH)
  132. cell.set_property("icon-name", "object-mesh");
  133. else if ((string)type == "package")
  134. cell.set_property("icon-name", "object-package");
  135. else if ((string)type == "physics_config")
  136. cell.set_property("icon-name", "object-config");
  137. else if ((string)type == "render_config")
  138. cell.set_property("icon-name", "object-config");
  139. else if ((string)type == "lua")
  140. cell.set_property("icon-name", "object-script");
  141. else if ((string)type == OBJECT_TYPE_UNIT)
  142. cell.set_property("pixbuf", thumbnail_cache.get(type, name, icon_size));
  143. else if ((string)type == "shader")
  144. cell.set_property("icon-name", "object-shader");
  145. else if ((string)type == OBJECT_TYPE_SOUND)
  146. cell.set_property("pixbuf", thumbnail_cache.get(type, name, icon_size));
  147. else if ((string)type == OBJECT_TYPE_SPRITE_ANIMATION)
  148. cell.set_property("icon-name", "object-animation");
  149. else if ((string)type == OBJECT_TYPE_SPRITE)
  150. cell.set_property("icon-name", "object-sprite");
  151. else if ((string)type == OBJECT_TYPE_TEXTURE)
  152. cell.set_property("pixbuf", thumbnail_cache.get(type, name, icon_size));
  153. else if ((string)type == OBJECT_TYPE_MESH_ANIMATION)
  154. cell.set_property("icon-name", "object-animation");
  155. else if ((string)type == OBJECT_TYPE_MESH_SKELETON)
  156. cell.set_property("icon-name", "object-skeleton");
  157. else {
  158. Project project = thumbnail_cache._project;
  159. string td = type.down();
  160. if (project.is_type_image(td))
  161. cell.set_property("icon-name", "object-texture");
  162. else if (project.is_type_mesh(td))
  163. cell.set_property("icon-name", "object-mesh");
  164. else if (project.is_type_sound(td))
  165. cell.set_property("icon-name", "object-sound");
  166. else if (project.is_type_font(td))
  167. cell.set_property("icon-name", "object-font");
  168. else
  169. cell.set_property("icon-name", "text-x-generic-symbolic");
  170. }
  171. }
  172. public enum BrowseMode
  173. {
  174. REGULAR, ///< Just browsing resources.
  175. SEARCH, ///< Filter resources based on search needle.
  176. COUNT
  177. }
  178. public class ProjectFolderView : Gtk.Box
  179. {
  180. public string _selected_type;
  181. public string _selected_name;
  182. public ProjectBrowser _project_browser;
  183. public ProjectStore _project_store;
  184. public ThumbnailCache _thumbnail_cache;
  185. public Gtk.ListStore _list_store;
  186. public Gtk.IconView _icon_view;
  187. public Gtk.TreeView _list_view;
  188. public Gtk.CellRendererPixbuf _cell_renderer_pixbuf;
  189. public Gtk.CellRendererText _cell_renderer_text;
  190. public Gdk.Pixbuf _empty_pixbuf;
  191. public bool _showing_project_folder;
  192. public Gtk.ScrolledWindow _icon_view_window;
  193. public Gtk.ScrolledWindow _list_view_window;
  194. public Gtk.GestureMultiPress _icon_view_gesture_click;
  195. public Gtk.GestureMultiPress _list_view_gesture_click;
  196. public Gtk.Stack _stack;
  197. public BrowseMode _browse_mode;
  198. public ProjectFolderView(ProjectBrowser project_browser, ProjectStore project_store, ThumbnailCache thumbnail_cache)
  199. {
  200. Object(orientation: Gtk.Orientation.VERTICAL);
  201. _project_browser = project_browser;
  202. _project_store = project_store;
  203. _thumbnail_cache = thumbnail_cache;
  204. _list_store = new Gtk.ListStore(4
  205. , typeof(string) // ProjectStore.Column.TYPE
  206. , typeof(string) // ProjectStore.Column.NAME
  207. , typeof(uint64) // ProjectStore.Column.SIZE
  208. , typeof(uint64) // ProjectStore.Column.MTIME
  209. );
  210. _icon_view = new Gtk.IconView();
  211. _icon_view.set_model(_list_store);
  212. _icon_view.set_item_width(80);
  213. _icon_view.enable_model_drag_source(Gdk.ModifierType.BUTTON1_MASK, dnd_targets, Gdk.DragAction.COPY);
  214. _icon_view.drag_data_get.connect(on_drag_data_get);
  215. _icon_view.drag_begin.connect_after(on_drag_begin);
  216. _icon_view.drag_end.connect(on_drag_end);
  217. _icon_view.drag_data_received.connect(on_drag_data_received);
  218. _icon_view.has_tooltip = true;
  219. _icon_view.query_tooltip.connect(on_icon_view_query_tooltip);
  220. /*
  221. _icon_view_gesture_click = new Gtk.GestureMultiPress(_icon_view);
  222. _icon_view_gesture_click.set_button(0);
  223. _icon_view_gesture_click.pressed.connect((n_press, x, y) => {
  224. on_button_pressed(_icon_view_gesture_click.get_current_button(), n_press, x, y);
  225. });
  226. */
  227. _icon_view.button_press_event.connect((ev) => {
  228. int n_press = 1;
  229. if (ev.type == Gdk.EventType.@2BUTTON_PRESS)
  230. n_press = 2;
  231. return on_button_pressed(ev.button, n_press, ev.x, ev.y);
  232. });
  233. const Gtk.TargetEntry targets[] =
  234. {
  235. { "text/uri-list", 0, 0 },
  236. };
  237. _icon_view.enable_model_drag_dest(targets
  238. , Gdk.DragAction.COPY
  239. | Gdk.DragAction.MOVE
  240. );
  241. // https://gitlab.gnome.org/GNOME/gtk/-/blob/3.24.43/gtk/gtkiconview.c#L5147
  242. _cell_renderer_text = new Gtk.CellRendererText();
  243. _cell_renderer_text.set("wrap-mode", Pango.WrapMode.WORD_CHAR);
  244. _cell_renderer_text.set("alignment", Pango.Alignment.CENTER);
  245. _cell_renderer_text.set("xalign", 0.5);
  246. _cell_renderer_text.set("yalign", 0.0);
  247. int wrap_width = _icon_view.item_width;
  248. wrap_width -= 2 * _icon_view.item_padding * 2;
  249. _cell_renderer_text.set("wrap-width", wrap_width);
  250. _cell_renderer_text.set("width", wrap_width);
  251. _icon_view.pack_end(_cell_renderer_text, false);
  252. _icon_view.set_cell_data_func(_cell_renderer_text, icon_view_text_func);
  253. _cell_renderer_pixbuf = new Gtk.CellRendererPixbuf();
  254. _cell_renderer_pixbuf.stock_size = Gtk.IconSize.DIALOG;
  255. _icon_view.pack_start(_cell_renderer_pixbuf, false);
  256. _icon_view.set_cell_data_func(_cell_renderer_pixbuf, icon_view_pixbuf_func);
  257. _list_view = new Gtk.TreeView();
  258. _list_view.set_model(_list_store);
  259. _list_view.enable_model_drag_source(Gdk.ModifierType.BUTTON1_MASK, dnd_targets, Gdk.DragAction.COPY);
  260. _list_view.drag_data_get.connect(on_drag_data_get);
  261. _list_view.drag_begin.connect_after(on_drag_begin);
  262. _list_view.drag_end.connect(on_drag_end);
  263. _list_view_gesture_click = new Gtk.GestureMultiPress(_list_view);
  264. _list_view_gesture_click.set_button(0);
  265. _list_view_gesture_click.pressed.connect((n_press, x, y) => {
  266. on_button_pressed(_list_view_gesture_click.get_current_button(), n_press, x, y);
  267. });
  268. var cell_pixbuf = new Gtk.CellRendererPixbuf();
  269. cell_pixbuf.stock_size = Gtk.IconSize.DND;
  270. var cell_text = new Gtk.CellRendererText();
  271. Gtk.TreeViewColumn column = null;
  272. column = new Gtk.TreeViewColumn();
  273. // column.title = "Thumbnail";
  274. column.pack_start(cell_pixbuf, false);
  275. column.set_cell_data_func(cell_pixbuf, list_view_pixbuf_func);
  276. _list_view.append_column(column);
  277. column = new Gtk.TreeViewColumn();
  278. column.title = "Basename";
  279. column.pack_start(cell_text, true);
  280. column.set_cell_data_func(cell_text, list_view_basename_text_func);
  281. _list_view.append_column(column);
  282. column = new Gtk.TreeViewColumn();
  283. column.title = "Type";
  284. column.pack_start(cell_text, true);
  285. column.set_cell_data_func(cell_text, list_view_type_text_func);
  286. _list_view.append_column(column);
  287. column = new Gtk.TreeViewColumn();
  288. column.title = "Size";
  289. column.pack_start(cell_text, true);
  290. column.set_cell_data_func(cell_text, list_view_size_text_func);
  291. _list_view.append_column(column);
  292. column = new Gtk.TreeViewColumn();
  293. column.title = "Modified";
  294. column.pack_start(cell_text, true);
  295. column.set_cell_data_func(cell_text, list_view_mtime_text_func);
  296. _list_view.append_column(column);
  297. column = new Gtk.TreeViewColumn();
  298. column.title = "Name";
  299. column.pack_start(cell_text, true);
  300. column.set_cell_data_func(cell_text, list_view_name_text_func);
  301. _list_view.append_column(column);
  302. _empty_pixbuf = new Gdk.Pixbuf.from_data({ 0x00, 0x00, 0x00, 0x00 }, Gdk.Colorspace.RGB, true, 8, 1, 1, 4);
  303. _showing_project_folder = true;
  304. _icon_view_window = new Gtk.ScrolledWindow(null, null);
  305. _icon_view_window.add(_icon_view);
  306. _list_view_window = new Gtk.ScrolledWindow(null, null);
  307. _list_view_window.add(_list_view);
  308. _stack = new Gtk.Stack();
  309. _stack.add_named(_icon_view_window, "icon-view");
  310. _stack.add_named(_list_view_window, "list-view");
  311. _stack.set_visible_child_full("icon-view", Gtk.StackTransitionType.NONE);
  312. this.pack_start(_stack);
  313. _browse_mode = BrowseMode.REGULAR;
  314. }
  315. public void on_drag_data_get(Gdk.DragContext context, Gtk.SelectionData data, uint info, uint time_)
  316. {
  317. // https://valadoc.org/gtk+-3.0/Gtk.Widget.drag_data_get.html
  318. Gtk.TreePath path;
  319. if (!selected_path(out path))
  320. return;
  321. string type;
  322. string name;
  323. resource_at_path(out type, out name, path);
  324. string resource_path = ResourceId.path(type, name);
  325. data.set(Gdk.Atom.intern_static_string("RESOURCE_PATH"), 8, resource_path.data);
  326. }
  327. public void on_drag_begin(Gdk.DragContext context)
  328. {
  329. // https://valadoc.org/gtk+-3.0/Gtk.Widget.drag_begin.html
  330. Gtk.drag_set_icon_pixbuf(context, _empty_pixbuf, 0, 0);
  331. }
  332. public void on_drag_end(Gdk.DragContext context)
  333. {
  334. // https://valadoc.org/gtk+-3.0/Gtk.Widget.drag_end.html
  335. GLib.Application.get_default().activate_action("cancel-place", null);
  336. }
  337. public void on_drag_data_received(Gdk.DragContext context, int x, int y, Gtk.SelectionData selection_data, uint info, uint time_)
  338. {
  339. if (_browse_mode == BrowseMode.SEARCH || !_showing_project_folder) {
  340. Gtk.drag_finish(context, true, false, time_);
  341. return;
  342. }
  343. Gtk.TreePath? path = path_at_pos(x, y);
  344. if (path != null) {
  345. _icon_view.select_path(path);
  346. _icon_view.scroll_to_path(path, false, 0.0f, 0.0f);
  347. }
  348. string type;
  349. string name;
  350. resource_at_path(out type, out name, path);
  351. if (type == "<folder>") {
  352. string[] uris = selection_data.get_uris();
  353. string[] filenames = new string[uris.length];
  354. try {
  355. for (int i = 0; i < uris.length; ++i)
  356. filenames[i] = GLib.Filename.from_uri(uris[i]);
  357. GLib.Application.get_default().activate_action("import", new GLib.Variant.tuple({name, filenames}));
  358. } catch (GLib.ConvertError e) {
  359. loge(e.message);
  360. }
  361. }
  362. Gtk.drag_finish(context, true, false, time_);
  363. }
  364. public bool on_button_pressed(uint button, int n_press, double x, double y)
  365. {
  366. Gtk.TreePath? path = path_at_pos((int)x, (int)y);
  367. if (button == Gdk.BUTTON_SECONDARY) {
  368. string type;
  369. string name;
  370. if (path != null) {
  371. _icon_view.select_path(path);
  372. _icon_view.scroll_to_path(path, false, 0.0f, 0.0f);
  373. } else if (_browse_mode == BrowseMode.SEARCH) {
  374. return Gdk.EVENT_PROPAGATE;
  375. }
  376. resource_at_path(out type, out name, path);
  377. GLib.Menu? menu_model;
  378. if (_showing_project_folder)
  379. menu_model = project_entry_menu_create(type, name);
  380. else
  381. menu_model = favorites_entry_menu_create(type, name);
  382. if (menu_model != null) {
  383. Gtk.Popover menu = new Gtk.Popover.from_model(this, menu_model);
  384. if (_stack.get_visible_child() == _icon_view_window) {
  385. // Adjust for scroll offset since IconView fails to do it itself.
  386. var new_x = x - _icon_view_window.get_hadjustment().get_value();
  387. var new_y = y - _icon_view_window.get_vadjustment().get_value();
  388. menu.set_pointing_to({ (int)new_x, (int)new_y, 1, 1 });
  389. } else {
  390. menu.set_pointing_to({ (int)x, (int)y, 1, 1 });
  391. }
  392. menu.set_position(Gtk.PositionType.BOTTOM);
  393. menu.popup();
  394. }
  395. return Gdk.EVENT_STOP; // Stop the event. Otherwise, popover menu won't show on _icon_view.
  396. } else if (button == Gdk.BUTTON_PRIMARY && n_press == 2) {
  397. if (path != null) {
  398. string type;
  399. string name;
  400. resource_at_path(out type, out name, path);
  401. if (type == "<folder>") {
  402. string dir_name;
  403. if (name == "..")
  404. dir_name = ResourceId.parent_folder((string)_selected_name);
  405. else
  406. dir_name = name;
  407. GLib.Application.get_default().activate_action("open-directory", new GLib.Variant.string(dir_name));
  408. } else {
  409. GLib.Application.get_default().activate_action("open-resource", ResourceId.path(type, name));
  410. }
  411. }
  412. }
  413. return Gdk.EVENT_PROPAGATE;
  414. }
  415. public void icon_view_pixbuf_func(Gtk.CellLayout cell_layout, Gtk.CellRenderer cell, Gtk.TreeModel model, Gtk.TreeIter iter)
  416. {
  417. Value val;
  418. string type;
  419. string name;
  420. model.get_value(iter, ProjectStore.Column.TYPE, out val);
  421. type = (string)val;
  422. model.get_value(iter, ProjectStore.Column.NAME, out val);
  423. name = (string)val;
  424. set_thumbnail(cell, type, name, 64, _thumbnail_cache);
  425. }
  426. public void icon_view_text_func(Gtk.CellLayout cell_layout, Gtk.CellRenderer cell, Gtk.TreeModel model, Gtk.TreeIter iter)
  427. {
  428. Value type;
  429. Value name;
  430. model.get_value(iter, ProjectStore.Column.TYPE, out type);
  431. model.get_value(iter, ProjectStore.Column.NAME, out name);
  432. string basename = GLib.Path.get_basename((string)name);
  433. if ((string)type == "<folder>") {
  434. if (name == "..")
  435. cell.set_property("text", name);
  436. else
  437. cell.set_property("text", basename);
  438. } else {
  439. if (_project_browser._show_files_extension.get_active())
  440. basename += "." + (string)type;
  441. cell.set_property("text", basename);
  442. }
  443. }
  444. public void list_view_pixbuf_func(Gtk.CellLayout cell_layout, Gtk.CellRenderer cell, Gtk.TreeModel model, Gtk.TreeIter iter)
  445. {
  446. Value val;
  447. string type;
  448. string name;
  449. model.get_value(iter, ProjectStore.Column.TYPE, out val);
  450. type = (string)val;
  451. model.get_value(iter, ProjectStore.Column.NAME, out val);
  452. name = (string)val;
  453. set_thumbnail(cell, type, name, 32, _thumbnail_cache);
  454. }
  455. public void list_view_basename_text_func(Gtk.CellLayout cell_layout, Gtk.CellRenderer cell, Gtk.TreeModel model, Gtk.TreeIter iter)
  456. {
  457. Value name;
  458. model.get_value(iter, ProjectStore.Column.NAME, out name);
  459. if (name == "..")
  460. cell.set_property("text", name);
  461. else
  462. cell.set_property("text", GLib.Path.get_basename((string)name));
  463. }
  464. public void list_view_type_text_func(Gtk.CellLayout cell_layout, Gtk.CellRenderer cell, Gtk.TreeModel model, Gtk.TreeIter iter)
  465. {
  466. Value type;
  467. model.get_value(iter, ProjectStore.Column.TYPE, out type);
  468. cell.set_property("text", prettify_type((string)type));
  469. }
  470. public void list_view_size_text_func(Gtk.CellLayout cell_layout, Gtk.CellRenderer cell, Gtk.TreeModel model, Gtk.TreeIter iter)
  471. {
  472. Value val;
  473. model.get_value(iter, ProjectStore.Column.SIZE, out val);
  474. uint64 size = (uint64)val;
  475. if (size != 0)
  476. cell.set_property("text", prettify_size(size));
  477. else
  478. cell.set_property("text", "n/a");
  479. }
  480. public void list_view_mtime_text_func(Gtk.CellLayout cell_layout, Gtk.CellRenderer cell, Gtk.TreeModel model, Gtk.TreeIter iter)
  481. {
  482. Value type;
  483. model.get_value(iter, ProjectStore.Column.MTIME, out type);
  484. uint64 mtime = (uint64)type;
  485. if (mtime != 0)
  486. cell.set_property("text", prettify_time(mtime));
  487. else
  488. cell.set_property("text", "n/a");
  489. }
  490. public void list_view_name_text_func(Gtk.CellLayout cell_layout, Gtk.CellRenderer cell, Gtk.TreeModel model, Gtk.TreeIter iter)
  491. {
  492. Value name;
  493. model.get_value(iter, ProjectStore.Column.NAME, out name);
  494. if (name == "..")
  495. cell.set_property("text", "n/a");
  496. else
  497. cell.set_property("text", (string)name);
  498. }
  499. public void select_resource(string type, string name)
  500. {
  501. Gtk.TreePath? path_to_select = null;
  502. _list_store.foreach((model, path, iter) => {
  503. GLib.Value val;
  504. string store_type;
  505. string store_name;
  506. model.get_value(iter, ProjectStore.Column.TYPE, out val);
  507. store_type = (string)val;
  508. model.get_value(iter, ProjectStore.Column.NAME, out val);
  509. store_name = (string)val;
  510. if (store_name == name && store_type == type) {
  511. path_to_select = path;
  512. return true;
  513. }
  514. return false;
  515. });
  516. if (path_to_select != null) {
  517. _icon_view.select_path(path_to_select);
  518. _icon_view.scroll_to_path(path_to_select, false, 0.0f, 0.0f);
  519. _list_view.get_selection().select_path(path_to_select);
  520. _list_view.scroll_to_cell(path_to_select, null, false, 0.0f, 0.0f);
  521. }
  522. }
  523. public bool selected_path(out Gtk.TreePath? path)
  524. {
  525. if (_stack.get_visible_child() == _icon_view_window) {
  526. GLib.List<Gtk.TreePath> selected_paths = _icon_view.get_selected_items();
  527. if (selected_paths.length() == 0u) {
  528. path = null;
  529. return false;
  530. }
  531. path = selected_paths.nth(0).data;
  532. return true;
  533. } else if (_stack.get_visible_child() == _list_view_window) {
  534. Gtk.TreeModel selected_model;
  535. Gtk.TreeIter iter;
  536. if (!_list_view.get_selection().get_selected(out selected_model, out iter)) {
  537. path = null;
  538. return false;
  539. }
  540. path = selected_model.get_path(iter);
  541. return true;
  542. } else {
  543. path = null;
  544. return false;
  545. }
  546. }
  547. public bool on_icon_view_query_tooltip(int x, int y, bool keyboard_tooltip, Gtk.Tooltip tooltip)
  548. {
  549. int bx;
  550. int by;
  551. _icon_view.convert_widget_to_bin_window_coords((int)x, (int)y, out bx, out by);
  552. Gtk.TreePath? path = _icon_view.get_path_at_pos(bx, by);
  553. if (path == null)
  554. return false;
  555. string type;
  556. string name;
  557. resource_at_path(out type, out name, path);
  558. uint64 size;
  559. uint64 mtime;
  560. resource_info_at_path(out size, out mtime, path);
  561. string text = "<b>%s</b>\nType: %s\nSize: %s\nModified: %s".printf(GLib.Markup.escape_text(name)
  562. , GLib.Markup.escape_text(prettify_type(type))
  563. , size == 0 ? "n/a" : prettify_size(size)
  564. , mtime == 0 ? "n/a" : prettify_time(mtime)
  565. );
  566. tooltip.set_markup(text);
  567. return true;
  568. }
  569. public static string prettify_type(string type)
  570. {
  571. if (type == "<folder>")
  572. return "Folder";
  573. else
  574. return type;
  575. }
  576. public static string prettify_size(uint64 size)
  577. {
  578. uint64 si_size;
  579. string si_unit;
  580. if (size >= 1024*1024*1024) {
  581. si_size = size / (1024*1024*1024);
  582. si_unit = "GiB";
  583. } else if (size >= 1024*1024) {
  584. si_size = size / (1024*1024);
  585. si_unit = "MiB";
  586. } else if (size >= 1024) {
  587. si_size = size / 1024;
  588. si_unit = "KiB";
  589. } else {
  590. si_size = size;
  591. si_unit = size > 1 ? "bytes" : "byte";
  592. }
  593. return "%d %s".printf((int)si_size, si_unit);
  594. }
  595. public static string prettify_time(uint64 time)
  596. {
  597. int64 mtime_secs = (int64)(time / (1000*1000*1000));
  598. GLib.DateTime date_time = new GLib.DateTime.from_unix_local(mtime_secs);
  599. return date_time.format("%d %b %Y; %H:%M:%S");
  600. }
  601. public Gtk.TreePath? path_at_pos(int x, int y)
  602. {
  603. Gtk.TreePath? path = null;
  604. if (_stack.get_visible_child() == _icon_view_window) {
  605. path = _icon_view.get_path_at_pos(x, y);
  606. } else if (_stack.get_visible_child() == _list_view_window) {
  607. int bx;
  608. int by;
  609. _list_view.convert_widget_to_bin_window_coords(x, y, out bx, out by);
  610. if (!_list_view.get_path_at_pos(bx, by, out path, null, null, null))
  611. path = null;
  612. } else {
  613. assert(false);
  614. return null;
  615. }
  616. return path;
  617. }
  618. public void resource_at_path(out string type, out string name, Gtk.TreePath? path)
  619. {
  620. if (path != null) {
  621. Gtk.TreeModel model;
  622. Gtk.TreePath model_path = path_and_model(out model, path);
  623. Gtk.TreeIter iter;
  624. model.get_iter(out iter, model_path);
  625. Value val;
  626. model.get_value(iter, ProjectStore.Column.TYPE, out val);
  627. type = (string)val;
  628. model.get_value(iter, ProjectStore.Column.NAME, out val);
  629. name = (string)val;
  630. } else {
  631. type = _selected_type;
  632. name = _selected_name;
  633. }
  634. }
  635. public void resource_info_at_path(out uint64 size, out uint64 mtime, Gtk.TreePath? path)
  636. {
  637. if (path == null) {
  638. size = 0;
  639. mtime = 0;
  640. return;
  641. }
  642. Gtk.TreeModel model;
  643. Gtk.TreePath model_path = path_and_model(out model, path);
  644. Gtk.TreeIter iter;
  645. model.get_iter(out iter, model_path);
  646. Value val;
  647. model.get_value(iter, ProjectStore.Column.SIZE, out val);
  648. size = (uint64)val;
  649. model.get_value(iter, ProjectStore.Column.MTIME, out val);
  650. mtime = (uint64)val;
  651. }
  652. public void set_browse_mode(BrowseMode mode)
  653. {
  654. if (_browse_mode == mode)
  655. return;
  656. _browse_mode = mode;
  657. }
  658. public Gtk.TreePath path_and_model(out Gtk.TreeModel model, Gtk.TreePath? path)
  659. {
  660. model = _list_store;
  661. return path;
  662. }
  663. }
  664. public class ProjectBrowser : Gtk.Box
  665. {
  666. public enum SortMode
  667. {
  668. NAME_AZ,
  669. NAME_ZA,
  670. TYPE_AZ,
  671. TYPE_ZA,
  672. SIZE_MIN_MAX,
  673. SIZE_MAX_MIN,
  674. LAST_MTIME,
  675. FIRST_MTIME,
  676. COUNT;
  677. public string to_label()
  678. {
  679. switch (this) {
  680. case NAME_AZ:
  681. return "Name A-Z";
  682. case NAME_ZA:
  683. return "Name Z-A";
  684. case TYPE_AZ:
  685. return "Type A-Z";
  686. case TYPE_ZA:
  687. return "Type Z-A";
  688. case SIZE_MIN_MAX:
  689. return "Size min-Max";
  690. case SIZE_MAX_MIN:
  691. return "Size Max-min";
  692. case LAST_MTIME:
  693. return "Last Modified";
  694. case FIRST_MTIME:
  695. return "First Modified";
  696. default:
  697. return "Unknown";
  698. }
  699. }
  700. }
  701. // Data
  702. public ProjectStore _project_store;
  703. public ThumbnailCache _thumbnail_cache;
  704. // Widgets
  705. public string _needle;
  706. public Gtk.EntryBuffer _filter_buffer;
  707. public EntrySearch _filter_entry_tree;
  708. public EntrySearch _filter_entry_folder;
  709. public Gtk.TreeModelFilter _tree_search;
  710. public Gtk.TreeModelFilter _tree_filter;
  711. public Gtk.TreeModelSort _tree_sort;
  712. public Gtk.TreeView _tree_view;
  713. public Gtk.TreeSelection _tree_selection;
  714. public Gdk.Pixbuf _empty_pixbuf;
  715. public ProjectFolderView _folder_view;
  716. public bool _show_folder_view;
  717. public Gtk.Image _toggle_folder_view_image;
  718. public Gtk.Button _toggle_folder_view;
  719. public Gtk.Box _tree_view_content;
  720. public Gtk.Image _toggle_icon_view_image;
  721. public Gtk.Button _toggle_icon_view;
  722. public Gtk.ListStore _folder_list_store;
  723. public Gtk.TreeModelSort _folder_list_sort;
  724. public SortMode _sort_mode;
  725. public Gtk.CheckButton _show_all_files;
  726. public Gtk.CheckButton _show_files_extension;
  727. public Gtk.CheckButton _show_mapped_dirs;
  728. public Gtk.Box _sort_items_box;
  729. public Gtk.Popover _sort_items_popover;
  730. public Gtk.MenuButton _sort_items;
  731. public Gtk.Box _empty_favorites_box;
  732. public Gtk.Stack _folder_stack;
  733. public Gtk.Box _folder_view_content;
  734. public Gtk.ScrolledWindow _scrolled_window;
  735. public Gtk.Paned _paned;
  736. public Gtk.GestureMultiPress _tree_view_gesture_click;
  737. public bool _hide_core_resources;
  738. public BrowseMode _browse_mode;
  739. public ProjectBrowser(ProjectStore project_store, ThumbnailCache thumbnail_cache)
  740. {
  741. Object(orientation: Gtk.Orientation.VERTICAL);
  742. // Data
  743. _project_store = project_store;
  744. _thumbnail_cache = thumbnail_cache;
  745. _thumbnail_cache.changed.connect(() => {
  746. _tree_view.queue_draw();
  747. _folder_view.queue_draw();
  748. });
  749. _needle = "";
  750. _filter_buffer = new Gtk.EntryBuffer();
  751. _filter_entry_tree = new EntrySearch();
  752. _filter_entry_tree._entry.set_buffer(_filter_buffer);
  753. _filter_entry_tree.set_placeholder_text("Search...");
  754. _filter_entry_tree._entry.stop_search.connect(on_stop_search);
  755. _filter_entry_folder = new EntrySearch();
  756. _filter_entry_folder._entry.set_buffer(_filter_buffer);
  757. _filter_entry_folder.set_placeholder_text("Search...");
  758. _filter_entry_folder.search_changed.connect(on_filter_entry_text_changed);
  759. _filter_entry_folder._entry.stop_search.connect(on_stop_search);
  760. _tree_filter = new Gtk.TreeModelFilter(_project_store._tree_store, null);
  761. _tree_filter.set_visible_func((model, iter) => {
  762. if (_project_store.project_root_path() != null)
  763. _tree_view.expand_row(_project_store.project_root_path(), false);
  764. Value type;
  765. Value name;
  766. model.get_value(iter, ProjectStore.Column.TYPE, out type);
  767. model.get_value(iter, ProjectStore.Column.NAME, out name);
  768. bool should_show = (string)type != null
  769. && (string)name != null
  770. && !row_should_be_hidden((string)type, (string)name)
  771. ;
  772. if (_show_folder_view) {
  773. // Hide all descendants of the favorites root.
  774. Gtk.TreePath? path = model.get_path(iter);
  775. if (path != null && _project_store.favorites_root_path() != null && path.is_descendant(_project_store.favorites_root_path()))
  776. return false;
  777. return should_show && (type == "<folder>" || type == "<favorites>");
  778. } else {
  779. return should_show;
  780. }
  781. });
  782. _tree_search = new Gtk.TreeModelFilter(_tree_filter, null);
  783. _tree_search.set_visible_column(ProjectStore.Column.VISIBLE);
  784. _tree_sort = new Gtk.TreeModelSort.with_model(_tree_search);
  785. _tree_sort.set_default_sort_func((model, iter_a, iter_b) => {
  786. Value type_a;
  787. Value type_b;
  788. model.get_value(iter_a, ProjectStore.Column.TYPE, out type_a);
  789. model.get_value(iter_b, ProjectStore.Column.TYPE, out type_b);
  790. // Favorites is always on top.
  791. if ((string)type_a == "<favorites>")
  792. return -1;
  793. if ((string)type_b == "<favorites>")
  794. return 1;
  795. // Then folders.
  796. if ((string)type_a == "<folder>") {
  797. if ((string)type_b != "<folder>")
  798. return -1;
  799. } else if ((string)type_b == "<folder>") {
  800. if ((string)type_a != "<folder>")
  801. return 1;
  802. }
  803. // And finally, regular files.
  804. Value id_a;
  805. Value id_b;
  806. model.get_value(iter_a, ProjectStore.Column.NAME, out id_a);
  807. model.get_value(iter_b, ProjectStore.Column.NAME, out id_b);
  808. return strcmp(GLib.Path.get_basename((string)id_a), GLib.Path.get_basename((string)id_b));
  809. });
  810. Gtk.CellRendererPixbuf cell_pixbuf = new Gtk.CellRendererPixbuf();
  811. cell_pixbuf.stock_size = Gtk.IconSize.SMALL_TOOLBAR;
  812. Gtk.CellRendererText cell_text = new Gtk.CellRendererText();
  813. Gtk.TreeViewColumn column = new Gtk.TreeViewColumn();
  814. column.pack_start(cell_pixbuf, false);
  815. column.pack_start(cell_text, true);
  816. column.set_cell_data_func(cell_pixbuf, pixbuf_func);
  817. column.set_cell_data_func(cell_text, text_func);
  818. _tree_view = new Gtk.TreeView();
  819. _tree_view.append_column(column);
  820. #if 0
  821. // For debugging.
  822. _tree_view.insert_column_with_attributes(-1
  823. , "Segment"
  824. , new Gtk.CellRendererText()
  825. , "text"
  826. , ProjectStore.Column.SEGMENT
  827. , null
  828. );
  829. _tree_view.insert_column_with_attributes(-1
  830. , "Name"
  831. , new Gtk.CellRendererText()
  832. , "text"
  833. , ProjectStore.Column.NAME
  834. , null
  835. );
  836. _tree_view.insert_column_with_attributes(-1
  837. , "Type"
  838. , new Gtk.CellRendererText()
  839. , "text"
  840. , ProjectStore.Column.TYPE
  841. , null
  842. );
  843. #endif /* if 0 */
  844. _tree_view.model = _tree_sort;
  845. _tree_view.headers_visible = false;
  846. _tree_view_gesture_click = new Gtk.GestureMultiPress(_tree_view);
  847. _tree_view_gesture_click.set_button(0);
  848. _tree_view_gesture_click.pressed.connect(on_button_pressed);
  849. _tree_view.enable_model_drag_source(Gdk.ModifierType.BUTTON1_MASK, dnd_targets, Gdk.DragAction.COPY);
  850. _tree_view.drag_data_get.connect(on_drag_data_get);
  851. _tree_view.drag_begin.connect_after(on_drag_begin);
  852. _tree_view.drag_end.connect(on_drag_end);
  853. _tree_selection = _tree_view.get_selection();
  854. _tree_selection.set_mode(Gtk.SelectionMode.BROWSE);
  855. _tree_selection.changed.connect(() => { update_folder_view(); });
  856. _empty_pixbuf = new Gdk.Pixbuf.from_data({ 0x00, 0x00, 0x00, 0x00 }, Gdk.Colorspace.RGB, true, 8, 1, 1, 4);
  857. _project_store._tree_store.row_inserted.connect(on_project_store_row_inserted);
  858. _project_store._tree_store.row_changed.connect(on_project_store_row_changed);
  859. _project_store._tree_store.row_deleted.connect(on_project_store_row_deleted);
  860. // Create icon view.
  861. _folder_view = new ProjectFolderView(this, _project_store, thumbnail_cache);
  862. // Create switch button.
  863. _show_folder_view = true;
  864. _toggle_folder_view_image = new Gtk.Image.from_icon_name("level-tree-symbolic", Gtk.IconSize.SMALL_TOOLBAR);
  865. _toggle_folder_view = new Gtk.Button();
  866. _toggle_folder_view.set_tooltip_text("Tree view.");
  867. _toggle_folder_view.add(_toggle_folder_view_image);
  868. _toggle_folder_view.get_style_context().add_class("flat");
  869. _toggle_folder_view.get_style_context().add_class("image-button");
  870. _toggle_folder_view.can_focus = false;
  871. _toggle_folder_view.clicked.connect(() => {
  872. _show_folder_view = !_show_folder_view;
  873. _toggle_folder_view.set_tooltip_text(_show_folder_view ? "Tree view." : "Folder view.");
  874. if (_show_folder_view) { // Switch from regular tree view to folder view.
  875. // Save the currently selected resource and a path to its parent. Those will be
  876. // used later, after the tree has been refiltered, to show the correct folder
  877. // and reveal the selected resource in the icon view.
  878. string? selected_type = null;
  879. string? selected_name = null;
  880. Gtk.TreePath? parent_path = null;
  881. Gtk.TreeModel selected_model;
  882. Gtk.TreeIter selected_iter;
  883. if (_tree_selection.get_selected(out selected_model, out selected_iter)) {
  884. Value val;
  885. selected_model.get_value(selected_iter, ProjectStore.Column.TYPE, out val);
  886. selected_type = (string)val;
  887. selected_model.get_value(selected_iter, ProjectStore.Column.NAME, out val);
  888. selected_name = (string)val;
  889. if (selected_type != "<folder>") {
  890. Gtk.TreeIter parent_iter;
  891. if (selected_model.iter_parent(out parent_iter, selected_iter))
  892. parent_path = _tree_view.model.get_path(parent_iter);
  893. }
  894. }
  895. _tree_filter.refilter();
  896. if (parent_path != null) {
  897. _tree_selection.select_path(parent_path);
  898. _folder_view.select_resource(selected_type, selected_name);
  899. }
  900. _folder_view_content.show_all();
  901. _toggle_folder_view_image.set_from_icon_name("level-tree-symbolic", Gtk.IconSize.SMALL_TOOLBAR);
  902. _filter_entry_tree.search_changed.disconnect(on_filter_entry_text_changed);
  903. _filter_entry_folder.search_changed.connect(on_filter_entry_text_changed);
  904. _filter_entry_tree.hide();
  905. } else { // Switch from folder view to regular tree view.
  906. // Save the currently selected resource. This will be used later, after the tree
  907. // has been refiltered, to reveal the selected resource in the tree view.
  908. string? selected_type = null;
  909. string? selected_name = null;
  910. Gtk.TreePath selected_path;
  911. if (_folder_view.selected_path(out selected_path))
  912. _folder_view.resource_at_path(out selected_type, out selected_name, selected_path);
  913. _tree_filter.refilter();
  914. // Expand filtered tree to match regular filtering behavior.
  915. if (_browse_mode == BrowseMode.SEARCH)
  916. _tree_view.expand_all();
  917. if (selected_type != null && selected_type != "<folder>")
  918. select_resource(selected_type, selected_name);
  919. _folder_view_content.hide();
  920. _toggle_folder_view_image.set_from_icon_name("browser-icon-view", Gtk.IconSize.SMALL_TOOLBAR);
  921. _tree_view.queue_draw(); // It doesn't draw by itself sometimes...
  922. _filter_entry_folder.search_changed.disconnect(on_filter_entry_text_changed);
  923. _filter_entry_tree.search_changed.connect(on_filter_entry_text_changed);
  924. _filter_entry_tree.show();
  925. }
  926. });
  927. // Create paned split-view.
  928. _scrolled_window = new Gtk.ScrolledWindow(null, null);
  929. _scrolled_window.add(_tree_view);
  930. var _tree_view_control = new Gtk.Box(Gtk.Orientation.HORIZONTAL, 0);
  931. _tree_view_control.pack_start(_filter_entry_tree, true, true);
  932. _tree_view_control.pack_end(_toggle_folder_view, false, false);
  933. _tree_view_content = new Gtk.Box(Gtk.Orientation.VERTICAL, 0);
  934. _tree_view_content.pack_start(_tree_view_control, false);
  935. _tree_view_content.pack_start(_scrolled_window, true, true);
  936. // Setup sort menu button popover.
  937. _sort_items_box = new Gtk.Box(Gtk.Orientation.VERTICAL, 0);
  938. Gtk.RadioButton? button = null;
  939. for (int i = 0; i < SortMode.COUNT; ++i)
  940. button = add_sort_item(button, (SortMode)i);
  941. _show_all_files = new Gtk.CheckButton.with_label("Show all files");
  942. _show_all_files.set_tooltip_text("Show all files, not just resource files.");
  943. _show_all_files.set_active(false);
  944. _show_all_files.toggled.connect(on_show_all_files_toggled);
  945. _show_files_extension = new Gtk.CheckButton.with_label("Show files extension");
  946. _show_files_extension.set_tooltip_text("Do not hide files extension.");
  947. _show_files_extension.set_active(false);
  948. _show_files_extension.toggled.connect(on_show_files_extension_toggled);
  949. _show_mapped_dirs = new Gtk.CheckButton.with_label("Show mapped dirs");
  950. _show_mapped_dirs.set_tooltip_text("Show external mapped directories.");
  951. _show_mapped_dirs.set_active(false);
  952. _show_mapped_dirs.toggled.connect(on_show_mapped_dirs_toggled);
  953. _sort_items_box.pack_start(_show_mapped_dirs, false, false);
  954. _sort_items_box.pack_start(_show_all_files, false, false);
  955. _sort_items_box.pack_start(_show_files_extension, false, false);
  956. _sort_items_box.show_all();
  957. _sort_items_popover = new Gtk.Popover(null);
  958. _sort_items_popover.add(_sort_items_box);
  959. _sort_items = new Gtk.MenuButton();
  960. _sort_items.set_tooltip_text("Sort and filter items.");
  961. _sort_items.add(new Gtk.Image.from_icon_name("list-sort", Gtk.IconSize.SMALL_TOOLBAR));
  962. _sort_items.get_style_context().add_class("flat");
  963. _sort_items.get_style_context().add_class("image-button");
  964. _sort_items.can_focus = false;
  965. _sort_items.set_popover(_sort_items_popover);
  966. bool _show_icon_view = true;
  967. _toggle_icon_view_image = new Gtk.Image.from_icon_name("browser-list-view", Gtk.IconSize.SMALL_TOOLBAR);
  968. _toggle_icon_view = new Gtk.Button();
  969. _toggle_icon_view.set_tooltip_text("List view.");
  970. _toggle_icon_view.add(_toggle_icon_view_image);
  971. _toggle_icon_view.get_style_context().add_class("flat");
  972. _toggle_icon_view.get_style_context().add_class("image-button");
  973. _toggle_icon_view.can_focus = false;
  974. _toggle_icon_view.clicked.connect(() => {
  975. Gtk.TreePath path;
  976. bool any_selected = _folder_view.selected_path(out path);
  977. if (_show_icon_view) {
  978. if (any_selected) {
  979. Gtk.TreeIter iter;
  980. _folder_view._list_store.get_iter(out iter, path);
  981. _folder_view._list_view.get_selection().select_iter(iter);
  982. }
  983. _folder_view._stack.set_visible_child_full("list-view", Gtk.StackTransitionType.NONE);
  984. _toggle_icon_view_image.set_from_icon_name("browser-icon-view", Gtk.IconSize.SMALL_TOOLBAR);
  985. } else {
  986. if (any_selected)
  987. _folder_view._icon_view.select_path(path);
  988. _folder_view._stack.set_visible_child_full("icon-view", Gtk.StackTransitionType.NONE);
  989. _toggle_icon_view_image.set_from_icon_name("browser-list-view", Gtk.IconSize.SMALL_TOOLBAR);
  990. }
  991. _show_icon_view = !_show_icon_view;
  992. _toggle_icon_view.set_tooltip_text(_show_icon_view ? "List view." : "Icon view.");
  993. });
  994. var _folder_view_control = new Gtk.Box(Gtk.Orientation.HORIZONTAL, 0);
  995. _folder_view_control.pack_start(_filter_entry_folder, true, true);
  996. _folder_view_control.pack_end(_toggle_icon_view, false, false);
  997. _folder_view_control.pack_end(_sort_items, false, false);
  998. _empty_favorites_box = new Gtk.Box(Gtk.Orientation.VERTICAL, 0);
  999. _empty_favorites_box.valign = Gtk.Align.CENTER;
  1000. _empty_favorites_box.pack_start(new Gtk.Image.from_icon_name("browser-favorites", Gtk.IconSize.DIALOG), false, false);
  1001. _empty_favorites_box.pack_start(new Gtk.Label("Favorites is empty"), false, false);
  1002. _folder_stack = new Gtk.Stack();
  1003. _folder_stack.add_named(_folder_view, "folder-view");
  1004. _folder_stack.add_named(_empty_favorites_box, "empty-favorites");
  1005. _folder_stack.set_visible_child_full("folder-view", Gtk.StackTransitionType.NONE);
  1006. _folder_view_content = new Gtk.Box(Gtk.Orientation.VERTICAL, 0);
  1007. _folder_view_content.pack_start(_folder_view_control, false);
  1008. _folder_view_content.pack_start(_folder_stack, true, true);
  1009. _paned = new Gtk.Paned(Gtk.Orientation.VERTICAL);
  1010. _paned.pack1(_tree_view_content, true, false);
  1011. _paned.pack2(_folder_view_content, true, false);
  1012. _paned.set_position(400);
  1013. _hide_core_resources = true;
  1014. _browse_mode = BrowseMode.REGULAR;
  1015. _folder_view.set_browse_mode(_browse_mode);
  1016. _folder_list_store = new Gtk.ListStore(4
  1017. , typeof(string) // ProjectStore.Column.TYPE
  1018. , typeof(string) // ProjectStore.Column.NAME
  1019. , typeof(uint64) // ProjectStore.Column.SIZE
  1020. , typeof(uint64) // ProjectStore.Column.MTIME
  1021. );
  1022. _folder_list_sort = new Gtk.TreeModelSort.with_model(_folder_list_store);
  1023. _folder_list_sort.set_default_sort_func((model, iter_a, iter_b) => {
  1024. Value type_a;
  1025. Value type_b;
  1026. model.get_value(iter_a, ProjectStore.Column.TYPE, out type_a);
  1027. model.get_value(iter_b, ProjectStore.Column.TYPE, out type_b);
  1028. Value name_a;
  1029. Value name_b;
  1030. model.get_value(iter_a, ProjectStore.Column.NAME, out name_a);
  1031. model.get_value(iter_b, ProjectStore.Column.NAME, out name_b);
  1032. // Folders are always on top.
  1033. if ((string)type_a == "<folder>" && (string)type_b != "<folder>") {
  1034. return -1;
  1035. } else if ((string)type_a != "<folder>" && (string)type_b == "<folder>") {
  1036. return 1;
  1037. } else if ((string)type_a == "<folder>" && (string)type_b == "<folder>") {
  1038. // Special folders always first.
  1039. if ((string)name_a == "..")
  1040. return -1;
  1041. else if ((string)name_b == "..")
  1042. return 1;
  1043. }
  1044. switch (_sort_mode) {
  1045. case SortMode.NAME_AZ:
  1046. case SortMode.NAME_ZA: {
  1047. int cmp = strcmp((string)name_a, (string)name_b);
  1048. return _sort_mode == SortMode.NAME_AZ ? cmp : -cmp;
  1049. }
  1050. case SortMode.TYPE_AZ:
  1051. case SortMode.TYPE_ZA: {
  1052. int cmp = strcmp((string)type_a, (string)type_b);
  1053. return _sort_mode == SortMode.TYPE_AZ ? cmp : -cmp;
  1054. }
  1055. case SortMode.SIZE_MIN_MAX:
  1056. case SortMode.SIZE_MAX_MIN: {
  1057. Value size_a;
  1058. Value size_b;
  1059. model.get_value(iter_a, ProjectStore.Column.SIZE, out size_a);
  1060. model.get_value(iter_b, ProjectStore.Column.SIZE, out size_b);
  1061. int cmp = (uint64)size_a <= (uint64)size_b ? -1 : 1;
  1062. return _sort_mode == SortMode.SIZE_MIN_MAX ? cmp : -cmp;
  1063. }
  1064. case SortMode.LAST_MTIME:
  1065. case SortMode.FIRST_MTIME: {
  1066. Value mtime_a;
  1067. Value mtime_b;
  1068. model.get_value(iter_a, ProjectStore.Column.MTIME, out mtime_a);
  1069. model.get_value(iter_b, ProjectStore.Column.MTIME, out mtime_b);
  1070. int cmp = (uint64)mtime_a >= (uint64)mtime_b ? -1 : 1;
  1071. return _sort_mode == SortMode.LAST_MTIME ? cmp : -cmp;
  1072. }
  1073. default:
  1074. return 0;
  1075. }
  1076. });
  1077. // Actions.
  1078. GLib.ActionEntry[] action_entries =
  1079. {
  1080. { "open-directory", on_open_directory, "s", null },
  1081. { "favorite-resource", on_favorite_resource, "(ss)", null },
  1082. { "unfavorite-resource", on_unfavorite_resource, "(ss)", null }
  1083. };
  1084. GLib.Application.get_default().add_action_entries(action_entries, this);
  1085. this.pack_start(_paned);
  1086. this.show.connect(on_show);
  1087. }
  1088. public void on_show()
  1089. {
  1090. _filter_entry_tree.set_visible(!_show_folder_view);
  1091. }
  1092. public void on_drag_data_get(Gdk.DragContext context, Gtk.SelectionData data, uint info, uint time_)
  1093. {
  1094. // https://valadoc.org/gtk+-3.0/Gtk.Widget.drag_data_get.html
  1095. Gtk.TreeModel selected_model;
  1096. Gtk.TreeIter selected_iter;
  1097. if (!_tree_selection.get_selected(out selected_model, out selected_iter))
  1098. return;
  1099. Value val;
  1100. string type;
  1101. string name;
  1102. selected_model.get_value(selected_iter, ProjectStore.Column.TYPE, out val);
  1103. type = (string)val;
  1104. selected_model.get_value(selected_iter, ProjectStore.Column.NAME, out val);
  1105. name = (string)val;
  1106. string resource_path = ResourceId.path(type, name);
  1107. data.set(Gdk.Atom.intern_static_string("RESOURCE_PATH"), 8, resource_path.data);
  1108. }
  1109. public void on_drag_begin(Gdk.DragContext context)
  1110. {
  1111. // https://valadoc.org/gtk+-3.0/Gtk.Widget.drag_begin.html
  1112. Gtk.drag_set_icon_pixbuf(context, _empty_pixbuf, 0, 0);
  1113. }
  1114. public void on_drag_end(Gdk.DragContext context)
  1115. {
  1116. // https://valadoc.org/gtk+-3.0/Gtk.Widget.drag_end.html
  1117. GLib.Application.get_default().activate_action("cancel-place", null);
  1118. }
  1119. // Returns true if the row should be hidden.
  1120. public bool row_should_be_hidden(string type, string name)
  1121. {
  1122. if (_hide_core_resources) {
  1123. if (type == "<folder>") {
  1124. if (name == "core")
  1125. return true;
  1126. } else {
  1127. if (name.has_prefix("core/"))
  1128. return true;
  1129. }
  1130. }
  1131. if (_show_all_files.get_active())
  1132. return false;
  1133. return type == "importer_settings"
  1134. || name == Project.LEVEL_EDITOR_TEST_NAME
  1135. || _project_store._project.is_type_importable(type)
  1136. ;
  1137. }
  1138. public void select_resource(string type, string name)
  1139. {
  1140. string parent_type = type;
  1141. string parent_name = name;
  1142. Gtk.TreePath filter_path = null;
  1143. do {
  1144. Gtk.TreePath store_path;
  1145. if (!_project_store.path_for_resource_type_name(out store_path, parent_type, parent_name)) {
  1146. break;
  1147. }
  1148. filter_path = _tree_filter.convert_child_path_to_path(store_path);
  1149. if (filter_path == null) {
  1150. // Either the path is not valid or points to a non-visible row in the model.
  1151. parent_type = "<folder>";
  1152. parent_name = ResourceId.parent_folder(parent_name);
  1153. continue;
  1154. }
  1155. Gtk.TreePath search_path = _tree_search.convert_child_path_to_path(filter_path);
  1156. if (search_path == null) {
  1157. // Either the path is not valid or points to a non-visible row in the model.
  1158. break;
  1159. }
  1160. Gtk.TreePath sort_path = _tree_sort.convert_child_path_to_path(search_path);
  1161. if (sort_path == null) {
  1162. // The path is not valid.
  1163. break;
  1164. }
  1165. _tree_view.expand_to_path(sort_path);
  1166. _tree_view.get_selection().select_path(sort_path);
  1167. _tree_view.scroll_to_cell(sort_path, null, false, 0.0f, 0.0f);
  1168. } while (filter_path == null);
  1169. }
  1170. public void reveal(string type, string name)
  1171. {
  1172. exit_search();
  1173. if (name.has_prefix("core/"))
  1174. _show_mapped_dirs.set_active(true);
  1175. select_resource(type, name);
  1176. _folder_view.select_resource(type, name);
  1177. }
  1178. public void on_open_directory(GLib.SimpleAction action, GLib.Variant? param)
  1179. {
  1180. string dir_name = param.get_string();
  1181. if (dir_name.has_prefix("core/") || dir_name == "core") {
  1182. _hide_core_resources = false;
  1183. _tree_filter.refilter();
  1184. }
  1185. Gtk.TreePath store_path;
  1186. if (_project_store.path_for_resource_type_name(out store_path, "<folder>", dir_name)) {
  1187. Gtk.TreePath filter_path = _tree_filter.convert_child_path_to_path(store_path);
  1188. if (filter_path == null) // Either the path is not valid or points to a non-visible row in the model.
  1189. return;
  1190. Gtk.TreePath sort_path = _tree_sort.convert_child_path_to_path(filter_path);
  1191. if (sort_path == null) // The path is not valid.
  1192. return;
  1193. _tree_view.expand_to_path(sort_path);
  1194. _tree_view.get_selection().select_path(sort_path);
  1195. }
  1196. }
  1197. public void on_favorite_resource(GLib.SimpleAction action, GLib.Variant? param)
  1198. {
  1199. string type = (string)param.get_child_value(0);
  1200. string name = (string)param.get_child_value(1);
  1201. _project_store.add_to_favorites(type, name);
  1202. }
  1203. public void on_unfavorite_resource(GLib.SimpleAction action, GLib.Variant? param)
  1204. {
  1205. string type = (string)param.get_child_value(0);
  1206. string name = (string)param.get_child_value(1);
  1207. _project_store.remove_from_favorites(type, name);
  1208. }
  1209. public void on_button_pressed(int n_press, double x, double y)
  1210. {
  1211. int bx;
  1212. int by;
  1213. Gtk.TreePath path;
  1214. _tree_view.convert_widget_to_bin_window_coords((int)x, (int)y, out bx, out by);
  1215. if (!_tree_view.get_path_at_pos(bx, by, out path, null, null, null))
  1216. return;
  1217. uint button = _tree_view_gesture_click.get_current_button();
  1218. if (button == Gdk.BUTTON_SECONDARY) {
  1219. Gtk.TreeIter iter;
  1220. _tree_view.model.get_iter(out iter, path);
  1221. Value type;
  1222. Value name;
  1223. _tree_view.model.get_value(iter, ProjectStore.Column.TYPE, out type);
  1224. _tree_view.model.get_value(iter, ProjectStore.Column.NAME, out name);
  1225. Gtk.TreePath? filter_path = _tree_sort.convert_path_to_child_path(path);
  1226. Gtk.TreePath? search_path = _tree_search.convert_path_to_child_path(filter_path);
  1227. Gtk.TreePath? store_path = _tree_filter.convert_path_to_child_path(search_path);
  1228. GLib.Menu? menu_model;
  1229. if (store_path.is_descendant(_project_store.project_root_path()) || store_path.compare(_project_store.project_root_path()) == 0)
  1230. menu_model = project_entry_menu_create((string)type, (string)name);
  1231. else if (store_path.is_descendant(_project_store.favorites_root_path()))
  1232. menu_model = favorites_entry_menu_create((string)type, (string)name);
  1233. else
  1234. menu_model = null;
  1235. if (menu_model != null) {
  1236. Gtk.Popover menu = new Gtk.Popover.from_model(_tree_view, menu_model);
  1237. menu.set_pointing_to({ (int)x, (int)y, 1, 1 });
  1238. menu.set_position(Gtk.PositionType.BOTTOM);
  1239. menu.popup();
  1240. }
  1241. } else if (button == Gdk.BUTTON_PRIMARY && n_press == 2) {
  1242. Gtk.TreeIter iter;
  1243. _tree_view.model.get_iter(out iter, path);
  1244. Value type;
  1245. _tree_view.model.get_value(iter, ProjectStore.Column.TYPE, out type);
  1246. if ((string)type == "<folder>")
  1247. return;
  1248. Value name;
  1249. _tree_view.model.get_value(iter, ProjectStore.Column.NAME, out name);
  1250. GLib.Application.get_default().activate_action("open-resource", ResourceId.path((string)type, (string)name));
  1251. }
  1252. return;
  1253. }
  1254. public void update_folder_view()
  1255. {
  1256. _folder_list_store.clear();
  1257. _folder_view._list_store.clear();
  1258. if (_browse_mode == BrowseMode.REGULAR) {
  1259. // Get the selected node's type and name.
  1260. Gtk.TreeModel selected_model;
  1261. Gtk.TreeIter selected_iter;
  1262. if (!_tree_selection.get_selected(out selected_model, out selected_iter))
  1263. return;
  1264. string selected_type;
  1265. string selected_name;
  1266. Value val;
  1267. selected_model.get_value(selected_iter, ProjectStore.Column.TYPE, out val);
  1268. selected_type = (string)val;
  1269. selected_model.get_value(selected_iter, ProjectStore.Column.NAME, out val);
  1270. selected_name = (string)val;
  1271. if (selected_type == "<folder>") {
  1272. _folder_view._showing_project_folder = true;
  1273. // Add parent folder.
  1274. if (selected_name != "") {
  1275. Gtk.TreeIter dummy;
  1276. _folder_list_store.insert_with_values(out dummy
  1277. , -1
  1278. , ProjectStore.Column.TYPE
  1279. , "<folder>"
  1280. , ProjectStore.Column.NAME
  1281. , ".."
  1282. , ProjectStore.Column.SIZE
  1283. , 0u
  1284. , ProjectStore.Column.MTIME
  1285. , 0u
  1286. , -1
  1287. );
  1288. }
  1289. // Fill the intermediate icon view list with paths matching the selected node's name.
  1290. _project_store._list_store.foreach((model, path, iter) => {
  1291. string type;
  1292. string name;
  1293. model.get_value(iter, ProjectStore.Column.TYPE, out val);
  1294. type = (string)val;
  1295. model.get_value(iter, ProjectStore.Column.NAME, out val);
  1296. name = (string)val;
  1297. if (row_should_be_hidden(type, name))
  1298. return false;
  1299. // Skip paths without common ancestor.
  1300. if (ResourceId.parent_folder(name) != selected_name)
  1301. return false;
  1302. // Skip paths that are too deep in the hierarchy:
  1303. // selected_name: foo
  1304. // hierarchy:
  1305. // foo/bar OK
  1306. // foo/baz OK
  1307. // foo/bar/baz NOPE
  1308. string name_suffix;
  1309. if (selected_name == "") // Project folder.
  1310. name_suffix = name.substring((selected_name).length);
  1311. else if (selected_name != name) // Folder itself.
  1312. name_suffix = name.substring((selected_name).length + 1);
  1313. else
  1314. return false;
  1315. if (name_suffix.index_of_char('/') != -1)
  1316. return false;
  1317. uint64 size;
  1318. uint64 mtime;
  1319. model.get_value(iter, ProjectStore.Column.SIZE, out val);
  1320. size = (uint64)val;
  1321. model.get_value(iter, ProjectStore.Column.MTIME, out val);
  1322. mtime = (uint64)val;
  1323. // Add the path to the list.
  1324. Gtk.TreeIter dummy;
  1325. _folder_list_store.insert_with_values(out dummy
  1326. , -1
  1327. , ProjectStore.Column.TYPE
  1328. , type
  1329. , ProjectStore.Column.NAME
  1330. , name
  1331. , ProjectStore.Column.SIZE
  1332. , size
  1333. , ProjectStore.Column.MTIME
  1334. , mtime
  1335. , -1
  1336. );
  1337. return false;
  1338. });
  1339. _folder_view._selected_type = selected_type;
  1340. _folder_view._selected_name = selected_name;
  1341. _folder_stack.set_visible_child_full("folder-view", Gtk.StackTransitionType.NONE);
  1342. } else if (selected_type == "<favorites>") {
  1343. _folder_view._showing_project_folder = false;
  1344. int num_items = 0;
  1345. // Fill the icon view list with paths whose ancestor is the favorites root.
  1346. _project_store._tree_store.foreach((model, path, iter) => {
  1347. string type;
  1348. string name;
  1349. model.get_value(iter, ProjectStore.Column.TYPE, out val);
  1350. type = (string)val;
  1351. model.get_value(iter, ProjectStore.Column.NAME, out val);
  1352. name = (string)val;
  1353. if (!path.is_descendant(_project_store.favorites_root_path()))
  1354. return false;
  1355. uint64 size;
  1356. uint64 mtime;
  1357. model.get_value(iter, ProjectStore.Column.SIZE, out val);
  1358. size = (uint64)val;
  1359. model.get_value(iter, ProjectStore.Column.MTIME, out val);
  1360. mtime = (uint64)val;
  1361. // Add the path to the list.
  1362. Gtk.TreeIter dummy;
  1363. _folder_list_store.insert_with_values(out dummy
  1364. , -1
  1365. , ProjectStore.Column.TYPE
  1366. , type
  1367. , ProjectStore.Column.NAME
  1368. , name
  1369. , ProjectStore.Column.SIZE
  1370. , size
  1371. , ProjectStore.Column.MTIME
  1372. , mtime
  1373. , -1
  1374. );
  1375. ++num_items;
  1376. return false;
  1377. });
  1378. if (num_items == 0)
  1379. _folder_stack.set_visible_child_full("empty-favorites", Gtk.StackTransitionType.NONE);
  1380. else
  1381. _folder_stack.set_visible_child_full("folder-view", Gtk.StackTransitionType.NONE);
  1382. }
  1383. } else if (_browse_mode == BrowseMode.SEARCH) {
  1384. // Fill the intermediate icon view list with paths that matches search criteria.
  1385. _project_store._list_store.foreach((model, path, iter) => {
  1386. Value val;
  1387. bool visible;
  1388. model.get_value(iter, ProjectStore.Column.VISIBLE, out val);
  1389. visible = (bool)val;
  1390. if (!visible)
  1391. return false;
  1392. string type;
  1393. string name;
  1394. model.get_value(iter, ProjectStore.Column.TYPE, out val);
  1395. type = (string)val;
  1396. model.get_value(iter, ProjectStore.Column.NAME, out val);
  1397. name = (string)val;
  1398. if (type == "<folder>")
  1399. return false;
  1400. if (row_should_be_hidden(type, name))
  1401. return false;
  1402. uint64 size;
  1403. uint64 mtime;
  1404. model.get_value(iter, ProjectStore.Column.SIZE, out val);
  1405. size = (uint64)val;
  1406. model.get_value(iter, ProjectStore.Column.MTIME, out val);
  1407. mtime = (uint64)val;
  1408. // Add the path to the list.
  1409. Gtk.TreeIter dummy;
  1410. _folder_list_store.insert_with_values(out dummy
  1411. , -1
  1412. , ProjectStore.Column.TYPE
  1413. , type
  1414. , ProjectStore.Column.NAME
  1415. , name
  1416. , ProjectStore.Column.SIZE
  1417. , size
  1418. , ProjectStore.Column.MTIME
  1419. , mtime
  1420. , -1
  1421. );
  1422. return false;
  1423. });
  1424. }
  1425. // Now, fill the actual icon view list with correctly sorted paths.
  1426. _folder_list_sort.foreach((model, path, iter) => {
  1427. Value val;
  1428. string type;
  1429. string name;
  1430. uint64 size;
  1431. uint64 mtime;
  1432. model.get_value(iter, ProjectStore.Column.TYPE, out val);
  1433. type = (string)val;
  1434. model.get_value(iter, ProjectStore.Column.NAME, out val);
  1435. name = (string)val;
  1436. model.get_value(iter, ProjectStore.Column.SIZE, out val);
  1437. size = (uint64)val;
  1438. model.get_value(iter, ProjectStore.Column.MTIME, out val);
  1439. mtime = (uint64)val;
  1440. // Add the path to the list.
  1441. Gtk.TreeIter dummy;
  1442. _folder_view._list_store.insert_with_values(out dummy
  1443. , -1
  1444. , ProjectStore.Column.TYPE
  1445. , type
  1446. , ProjectStore.Column.NAME
  1447. , name
  1448. , ProjectStore.Column.SIZE
  1449. , size
  1450. , ProjectStore.Column.MTIME
  1451. , mtime
  1452. , -1
  1453. );
  1454. return false;
  1455. });
  1456. }
  1457. public void select_project_root()
  1458. {
  1459. Gtk.TreePath? filter_path = _tree_filter.convert_child_path_to_path(_project_store.project_root_path());
  1460. if (filter_path == null)
  1461. return;
  1462. Gtk.TreePath? sort_path = _tree_sort.convert_child_path_to_path(filter_path);
  1463. if (sort_path == null)
  1464. return;
  1465. _tree_selection.select_path(sort_path);
  1466. }
  1467. public void pixbuf_func(Gtk.CellLayout cell_layout, Gtk.CellRenderer cell, Gtk.TreeModel model, Gtk.TreeIter iter)
  1468. {
  1469. Value val;
  1470. string type;
  1471. string name;
  1472. model.get_value(iter, ProjectStore.Column.TYPE, out val);
  1473. type = (string)val;
  1474. model.get_value(iter, ProjectStore.Column.NAME, out val);
  1475. name = (string)val;
  1476. set_thumbnail(cell, type, name, 16, _thumbnail_cache);
  1477. }
  1478. public void text_func(Gtk.CellLayout cell_layout, Gtk.CellRenderer cell, Gtk.TreeModel model, Gtk.TreeIter iter)
  1479. {
  1480. Value name;
  1481. Value type;
  1482. model.get_value(iter, ProjectStore.Column.NAME, out name);
  1483. model.get_value(iter, ProjectStore.Column.TYPE, out type);
  1484. string basename = GLib.Path.get_basename((string)name);
  1485. if ((string)type == "<folder>") {
  1486. if ((string)name == "")
  1487. cell.set_property("text", _project_store._project.name());
  1488. else
  1489. cell.set_property("text", basename);
  1490. } else if ((string)type == "<favorites>") {
  1491. cell.set_property("text", "Favorites");
  1492. } else {
  1493. cell.set_property("text", ResourceId.path((string)type, basename));
  1494. }
  1495. }
  1496. public Gtk.RadioButton add_sort_item(Gtk.RadioButton? group, SortMode mode)
  1497. {
  1498. var button = new Gtk.RadioButton.with_label_from_widget(group, mode.to_label());
  1499. button.toggled.connect(() => {
  1500. _sort_mode = mode;
  1501. update_folder_view();
  1502. _sort_items_popover.popdown();
  1503. });
  1504. _sort_items_box.pack_start(button, false, false);
  1505. return button;
  1506. }
  1507. public bool save_tree_state(Gtk.TreeModel model, Gtk.TreePath path, Gtk.TreeIter iter)
  1508. {
  1509. Gtk.TreePath filter_path = _tree_filter.convert_child_path_to_path(path);
  1510. if (filter_path == null) {
  1511. // Either the path is not valid or points to a non-visible row in the model.
  1512. return false;
  1513. }
  1514. Gtk.TreePath search_path = _tree_search.convert_child_path_to_path(filter_path);
  1515. if (search_path == null) {
  1516. // Either the path is not valid or points to a non-visible row in the model.
  1517. assert(false);
  1518. return false;
  1519. }
  1520. Gtk.TreePath sort_path = _tree_sort.convert_child_path_to_path(search_path);
  1521. if (sort_path == null) {
  1522. // The path is not valid.
  1523. assert(false);
  1524. return false;
  1525. }
  1526. bool expanded = _tree_view.is_row_expanded(sort_path);
  1527. bool selected = _tree_view.get_selection().path_is_selected(sort_path);
  1528. uint32 user_data = 0;
  1529. user_data |= (uint32)expanded << 0;
  1530. user_data |= (uint32)selected << 1;
  1531. _project_store._tree_store.set(iter, ProjectStore.Column.USER_DATA, user_data, -1);
  1532. #if false // For debugging.
  1533. if (expanded || selected) {
  1534. Value val;
  1535. _project_store._tree_store.get_value(iter, ProjectStore.Column.NAME, out val);
  1536. logi("is expanded %d selected %d name %s user_data %x".printf((int)expanded, (int)selected, (string)val, user_data));
  1537. }
  1538. #endif
  1539. return false; // Continue iterating.
  1540. }
  1541. public bool restore_tree_state(Gtk.TreeModel model, Gtk.TreePath path, Gtk.TreeIter iter)
  1542. {
  1543. uint32 user_data;
  1544. Value val;
  1545. _project_store._tree_store.get_value(iter, ProjectStore.Column.USER_DATA, out val);
  1546. user_data = (uint32)val;
  1547. bool expanded = (bool)((user_data & 0x1) >> 0);
  1548. bool selected = (bool)((user_data & 0x2) >> 1);
  1549. #if false // For debugging.
  1550. if (expanded || selected) {
  1551. _project_store._tree_store.get_value(iter, ProjectStore.Column.NAME, out val);
  1552. logi("was expanded %d selected %d name %s user_data %x".printf((int)expanded, (int)selected, (string)val, user_data));
  1553. }
  1554. #endif
  1555. Gtk.TreePath filter_path = _tree_filter.convert_child_path_to_path(path);
  1556. if (filter_path == null) {
  1557. // Either the path is not valid or points to a non-visible row in the model.
  1558. return false;
  1559. }
  1560. Gtk.TreePath search_path = _tree_search.convert_child_path_to_path(filter_path);
  1561. if (search_path == null) {
  1562. // Either the path is not valid or points to a non-visible row in the model.
  1563. return false;
  1564. }
  1565. Gtk.TreePath sort_path = _tree_sort.convert_child_path_to_path(search_path);
  1566. if (sort_path == null) {
  1567. // The path is not valid.
  1568. return false;
  1569. }
  1570. if (expanded)
  1571. _tree_view.expand_to_path(sort_path);
  1572. else
  1573. _tree_view.collapse_row(sort_path);
  1574. if (selected)
  1575. _tree_view.get_selection().select_path(sort_path);
  1576. return false; // Continue iterating.
  1577. }
  1578. public void filter(string needle)
  1579. {
  1580. _project_store.make_visible(false);
  1581. _project_store.filter(needle);
  1582. _tree_search.refilter();
  1583. _tree_view.expand_all();
  1584. }
  1585. public void exit_search()
  1586. {
  1587. uint8 empty[] = { '\0' };
  1588. _filter_buffer.set_text(empty);
  1589. }
  1590. public void on_search_started()
  1591. {
  1592. assert(_browse_mode == BrowseMode.REGULAR);
  1593. _browse_mode = BrowseMode.SEARCH;
  1594. _folder_view.set_browse_mode(_browse_mode);
  1595. _folder_stack.set_visible_child_full("folder-view", Gtk.StackTransitionType.NONE);
  1596. disconnect_project_store_signals();
  1597. // Save the current tree state (expanded branches + selection)
  1598. // to restore it later when the search is done.
  1599. _project_store._tree_store.foreach(save_tree_state);
  1600. filter(_needle);
  1601. update_folder_view();
  1602. connect_project_store_signals();
  1603. }
  1604. public void on_search_changed()
  1605. {
  1606. assert(_browse_mode == BrowseMode.SEARCH);
  1607. disconnect_project_store_signals();
  1608. filter(_needle);
  1609. update_folder_view();
  1610. connect_project_store_signals();
  1611. }
  1612. public void on_search_stopped()
  1613. {
  1614. assert(_browse_mode == BrowseMode.SEARCH);
  1615. _browse_mode = BrowseMode.REGULAR;
  1616. _folder_view.set_browse_mode(_browse_mode);
  1617. Gtk.TreeModel selected_model;
  1618. Gtk.TreeIter selected_iter;
  1619. Gtk.TreeRowReference? selected_reference = null;
  1620. // Only restore the old selection if it has not been
  1621. // modified while searching (i.e. nothing is selected
  1622. // because entering search clears it).
  1623. if (_tree_view.get_selection().get_selected(out selected_model, out selected_iter))
  1624. selected_reference = new Gtk.TreeRowReference(selected_model, selected_model.get_path(selected_iter));
  1625. disconnect_project_store_signals();
  1626. _project_store.make_visible(true);
  1627. _tree_search.refilter();
  1628. // Restore the previous tree state (old expanded branches + old selection).
  1629. _project_store._tree_store.foreach(restore_tree_state);
  1630. update_folder_view();
  1631. connect_project_store_signals();
  1632. // If the selection changed while searching, restore it as well.
  1633. if (selected_reference != null) {
  1634. Gtk.TreePath path = selected_reference.get_path();
  1635. _tree_view.expand_to_path(path);
  1636. _tree_view.get_selection().select_path(path);
  1637. _tree_view.scroll_to_cell(path, null, false, 0.0f, 0.0f);
  1638. }
  1639. }
  1640. public void on_stop_search()
  1641. {
  1642. exit_search();
  1643. }
  1644. public void on_filter_entry_text_changed()
  1645. {
  1646. string old_needle = _needle;
  1647. _needle = _filter_buffer.text.strip().down();
  1648. if (old_needle == "" && _needle != "") {
  1649. on_search_started();
  1650. } else if (old_needle != "" && _needle == "") {
  1651. on_search_stopped();
  1652. } else if (_needle != "") {
  1653. on_search_changed();
  1654. }
  1655. }
  1656. void disconnect_project_store_signals()
  1657. {
  1658. _project_store._tree_store.row_inserted.disconnect(on_project_store_row_inserted);
  1659. _project_store._tree_store.row_changed.disconnect(on_project_store_row_changed);
  1660. _project_store._tree_store.row_deleted.disconnect(on_project_store_row_deleted);
  1661. }
  1662. void connect_project_store_signals()
  1663. {
  1664. _project_store._tree_store.row_inserted.connect(on_project_store_row_inserted);
  1665. _project_store._tree_store.row_changed.connect(on_project_store_row_changed);
  1666. _project_store._tree_store.row_deleted.connect(on_project_store_row_deleted);
  1667. }
  1668. void filter_and_update_folder_view()
  1669. {
  1670. if (_browse_mode == BrowseMode.SEARCH) {
  1671. disconnect_project_store_signals();
  1672. filter(_needle);
  1673. connect_project_store_signals();
  1674. }
  1675. update_folder_view();
  1676. }
  1677. void on_project_store_row_inserted(Gtk.TreePath path, Gtk.TreeIter iter)
  1678. {
  1679. filter_and_update_folder_view();
  1680. }
  1681. void on_project_store_row_changed(Gtk.TreePath path, Gtk.TreeIter iter)
  1682. {
  1683. filter_and_update_folder_view();
  1684. }
  1685. void on_project_store_row_deleted(Gtk.TreePath path)
  1686. {
  1687. filter_and_update_folder_view();
  1688. }
  1689. void on_show_all_files_toggled()
  1690. {
  1691. _tree_filter.refilter();
  1692. update_folder_view();
  1693. }
  1694. void on_show_mapped_dirs_toggled()
  1695. {
  1696. _hide_core_resources = !_hide_core_resources;
  1697. _tree_filter.refilter();
  1698. update_folder_view();
  1699. }
  1700. void on_show_files_extension_toggled()
  1701. {
  1702. update_folder_view();
  1703. }
  1704. }
  1705. } /* namespace Crown */