panel_projects_list.vala 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. /*
  2. * Copyright (c) 2012-2024 Daniele Bartolini et al.
  3. * SPDX-License-Identifier: GPL-3.0-or-later
  4. */
  5. using Gtk;
  6. using Gee;
  7. namespace Crown
  8. {
  9. public class ProjectRow : Gtk.ListBoxRow
  10. {
  11. public Gtk.Box _vbox;
  12. public Gtk.Box _hbox;
  13. public Gtk.Label _name;
  14. public Gtk.Label _source_dir;
  15. public Gtk.Button _remove_button;
  16. public Gtk.Button _open_button;
  17. public ProjectRow(string source_dir, string time, string name)
  18. {
  19. this.set_data("source_dir", source_dir);
  20. this.set_data("mtime", time);
  21. _vbox = new Gtk.Box(Gtk.Orientation.VERTICAL, 0);
  22. _name = new Gtk.Label(null);
  23. _name.set_margin_start(12);
  24. _name.set_margin_end(12);
  25. _name.set_margin_top(8);
  26. _name.set_margin_bottom(8);
  27. _name.set_markup("<b>%s</b>".printf(name));
  28. _name.set_xalign(0.0f);
  29. _vbox.pack_start(_name);
  30. _source_dir = new Gtk.Label(null);
  31. _source_dir.set_margin_start(12);
  32. _source_dir.set_margin_end(12);
  33. _source_dir.set_margin_bottom(8);
  34. _source_dir.set_markup("<small>%s</small>".printf(source_dir));
  35. _source_dir.set_xalign(0.0f);
  36. _vbox.pack_start(_source_dir);
  37. _hbox = new Gtk.Box(Gtk.Orientation.HORIZONTAL, 6);
  38. _hbox.pack_start(_vbox);
  39. _remove_button = new Gtk.Button.from_icon_name("list-remove-symbolic");
  40. _remove_button.get_style_context().add_class("flat");
  41. _remove_button.get_style_context().add_class("destructive-action");
  42. _remove_button.set_halign(Gtk.Align.CENTER);
  43. _remove_button.set_valign(Gtk.Align.CENTER);
  44. _remove_button.set_margin_end(12);
  45. _hbox.pack_end(_remove_button, false, false, 0);
  46. _open_button = new Gtk.Button.with_label("Open");
  47. _open_button.get_style_context().add_class("flat");
  48. _open_button.set_halign(Gtk.Align.CENTER);
  49. _open_button.set_valign(Gtk.Align.CENTER);
  50. // _open_button.set_margin_end(12);
  51. _hbox.pack_end(_open_button, false, false, 0);
  52. this.add(_hbox);
  53. }
  54. }
  55. public class PanelProjectsList : Gtk.ScrolledWindow
  56. {
  57. // Data
  58. User _user;
  59. // Widgets
  60. public Gtk.Label _projects_list_label;
  61. public Gtk.Label _local_label;
  62. public Gtk.Label _project_list_empty;
  63. public Gtk.ListBox _list_projects;
  64. public Gtk.Button _button_new_project;
  65. public Gtk.Button _button_import_project;
  66. public Gtk.Box _buttons_box;
  67. public Gtk.Box _projects_box;
  68. public Clamp _clamp;
  69. public PanelProjectsList(User user)
  70. {
  71. this.shadow_type = Gtk.ShadowType.NONE;
  72. // Data
  73. _user = user;
  74. _projects_list_label = new Gtk.Label(null);
  75. _projects_list_label.xalign = 0;
  76. _projects_list_label.set_markup("<span font_weight=\"bold\" size=\"x-large\">Projects</span>");
  77. _local_label = new Gtk.Label("Local");
  78. _local_label.set_markup("<span font_weight=\"bold\">Local</span>");
  79. _project_list_empty = new Gtk.Label("No projects found.\nUse the buttons above to create a new project or import an already existing one.");
  80. _project_list_empty.visible = true;
  81. _project_list_empty.margin_start = 12;
  82. _project_list_empty.margin_end = 12;
  83. _project_list_empty.margin_top = 8;
  84. _project_list_empty.margin_bottom = 8;
  85. _list_projects = new Gtk.ListBox();
  86. _list_projects.set_placeholder(_project_list_empty);
  87. _list_projects.set_sort_func((row1, row2) => {
  88. int64 mtime1 = int64.parse(row1.get_data("mtime"));
  89. int64 mtime2 = int64.parse(row2.get_data("mtime"));
  90. return mtime1 > mtime2 ? -1 : 1; // LRU
  91. });
  92. _button_new_project = new Gtk.Button.with_label("New");
  93. _button_new_project.get_style_context().add_class("flat");
  94. _button_new_project.clicked.connect(() => {
  95. var app = (LevelEditorApplication)GLib.Application.get_default();
  96. app.show_panel("panel_new_project", Gtk.StackTransitionType.SLIDE_DOWN);
  97. });
  98. _button_import_project = new Gtk.Button.with_label("Import");
  99. _button_import_project.get_style_context().add_class("flat");
  100. _button_import_project.clicked.connect(() => {
  101. GLib.Application.get_default().activate_action("add-project", null);
  102. });
  103. _buttons_box = new Gtk.Box(Gtk.Orientation.HORIZONTAL, 0);
  104. _buttons_box.spacing = 6;
  105. _buttons_box.pack_start(_local_label, false, true);
  106. _buttons_box.pack_end(_button_import_project, false, true);
  107. _buttons_box.pack_end(_button_new_project, false, true);
  108. _projects_box = new Gtk.Box(Gtk.Orientation.VERTICAL, 0);
  109. _projects_box.margin_start = 12;
  110. _projects_box.margin_end = 12;
  111. _projects_box.margin_top = 32;
  112. _projects_box.margin_bottom = 32;
  113. _projects_box.spacing = 12;
  114. _projects_box.pack_start(_projects_list_label, false, true);
  115. _projects_box.pack_start(_buttons_box, false, true);
  116. _projects_box.pack_start(_list_projects, false, true);
  117. _clamp = new Clamp();
  118. _clamp.add(_projects_box);
  119. this.add(_clamp);
  120. _user.recent_project_added.connect(on_recent_project_added);
  121. _user.recent_project_touched.connect(on_recent_project_touched);
  122. // _user.recent_project_removed.connect(on_recent_project_remove);
  123. }
  124. public void on_recent_project_added(string source_dir, string name, string time)
  125. {
  126. // Add project row.
  127. var row = new ProjectRow(source_dir, time, name);
  128. row._remove_button.clicked.connect(() => {
  129. Gtk.MessageDialog md = new Gtk.MessageDialog((Gtk.Window)this.get_toplevel()
  130. , Gtk.DialogFlags.MODAL
  131. , Gtk.MessageType.WARNING
  132. , Gtk.ButtonsType.NONE
  133. , "Remove \"%s\" from the list?\n\nThis action removes the project from the list only, files on disk will not be deleted.".printf(source_dir)
  134. );
  135. md.add_button("_Cancel", ResponseType.CANCEL);
  136. md.add_button("_Remove", ResponseType.YES);
  137. md.set_default_response(ResponseType.CANCEL);
  138. if (md.run() == ResponseType.YES) {
  139. _user.remove_recent_project(row.get_data("source_dir"));
  140. _list_projects.remove(row);
  141. }
  142. md.destroy();
  143. });
  144. row._open_button.clicked.connect(() => {
  145. GLib.Application.get_default().activate_action("open-project", new GLib.Variant.string(source_dir));
  146. });
  147. _list_projects.add(row);
  148. _list_projects.show_all(); // Otherwise the list is not always updated...
  149. if (!GLib.FileUtils.test(source_dir, FileTest.EXISTS))
  150. row._open_button.sensitive = false;
  151. invalidate_sort();
  152. }
  153. public void on_recent_project_touched(string source_dir, string mtime)
  154. {
  155. _list_projects.foreach((row) => {
  156. if (row.get_data<string>("source_dir") == source_dir) {
  157. row.set_data("mtime", mtime);
  158. return;
  159. }
  160. });
  161. invalidate_sort();
  162. }
  163. public void invalidate_sort()
  164. {
  165. _list_projects.invalidate_sort();
  166. // Give focus to most recent project's open button.
  167. ProjectRow? first_row = (ProjectRow?)_list_projects.get_row_at_index(0);
  168. if (first_row != null)
  169. first_row._open_button.has_focus = true;
  170. }
  171. }
  172. } /* namespace Crown */