panel_projects_list.vala 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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 class ProjectRow : Gtk.ListBoxRow
  8. {
  9. public Gtk.Box _vbox;
  10. public Gtk.Box _hbox;
  11. public Gtk.Label _name;
  12. public Gtk.Label _source_dir;
  13. public Gtk.Button _remove_button;
  14. public Gtk.Button _open_button;
  15. public ProjectsList _projects_list;
  16. public ProjectRow(string source_dir, string time, string name, ProjectsList pl)
  17. {
  18. this.set_data("source_dir", source_dir);
  19. this.set_data("mtime", time);
  20. _projects_list = pl;
  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.set_tooltip_text("Remove the project from the list.");
  41. _remove_button.get_style_context().add_class("flat");
  42. _remove_button.get_style_context().add_class("destructive-action");
  43. _remove_button.set_halign(Gtk.Align.CENTER);
  44. _remove_button.set_valign(Gtk.Align.CENTER);
  45. _remove_button.set_margin_end(12);
  46. _hbox.pack_end(_remove_button, false, false, 0);
  47. _open_button = new Gtk.Button.with_label("Open");
  48. _open_button.set_tooltip_text("Open the project.");
  49. _open_button.get_style_context().add_class("flat");
  50. _open_button.set_halign(Gtk.Align.CENTER);
  51. _open_button.set_valign(Gtk.Align.CENTER);
  52. // _open_button.set_margin_end(12);
  53. _hbox.pack_end(_open_button, false, false, 0);
  54. _remove_button.clicked.connect(on_remove_button_clicked);
  55. _open_button.clicked.connect(on_open_button_clicked);
  56. this.add(_hbox);
  57. }
  58. public void on_remove_button_clicked()
  59. {
  60. GLib.Application.get_default().activate_action("remove-project"
  61. , new GLib.Variant.string(this.get_data("source_dir"))
  62. );
  63. }
  64. public void on_open_button_clicked()
  65. {
  66. GLib.Application.get_default().activate_action("open-project"
  67. , new GLib.Variant.tuple({this.get_data<string>("source_dir"), LEVEL_NONE})
  68. );
  69. }
  70. }
  71. public class ProjectsList : Gtk.Box
  72. {
  73. // Data
  74. public User _user;
  75. // Widgets
  76. public Gtk.Label _projects_list_label;
  77. public Gtk.Label _local_label;
  78. public Gtk.Box _project_list_empty;
  79. public Gtk.ListBox _list_projects;
  80. public Gtk.Button _button_import_project;
  81. public Gtk.Button _button_new_project;
  82. public Gtk.Box _buttons_box;
  83. public Gtk.Box _projects_box;
  84. public Clamp _clamp;
  85. public ProjectsList(User user)
  86. {
  87. Object(orientation: Gtk.Orientation.VERTICAL);
  88. // Data
  89. _user = user;
  90. _projects_list_label = new Gtk.Label(null);
  91. _projects_list_label.xalign = 0;
  92. _projects_list_label.set_markup("<span font_weight=\"bold\" size=\"x-large\">Projects</span>");
  93. _local_label = new Gtk.Label("Local");
  94. _local_label.set_markup("<span font_weight=\"bold\">Local</span>");
  95. _project_list_empty = new Gtk.Box(Gtk.Orientation.VERTICAL, 6);
  96. _project_list_empty.margin_top = 12;
  97. _project_list_empty.margin_bottom = 12;
  98. var label = new Gtk.Label(null);
  99. label.set_markup("<span font_size=\"large\"><b>No projects found</b></span>");
  100. _project_list_empty.pack_start(label, false, false);
  101. label = new Gtk.Label(null);
  102. label.set_markup("Use the buttons above to create a new project or import an already existing one.");
  103. _project_list_empty.pack_start(label, false, false);
  104. _project_list_empty.show_all();
  105. _list_projects = new Gtk.ListBox();
  106. _list_projects.set_placeholder(_project_list_empty);
  107. _list_projects.set_sort_func((row1, row2) => {
  108. int64 mtime1 = int64.parse(row1.get_data("mtime"));
  109. int64 mtime2 = int64.parse(row2.get_data("mtime"));
  110. return mtime1 > mtime2 ? -1 : 1; // LRU
  111. });
  112. _button_import_project = new Gtk.Button.with_label("Import...");
  113. _button_import_project.set_tooltip_text("Import an existing project.");
  114. _button_import_project.action_name = "app.add-project";
  115. _button_new_project = new Gtk.Button.with_label("Create New");
  116. _button_new_project.set_tooltip_text("Create a new project.");
  117. _button_new_project.get_style_context().add_class("suggested-action");
  118. _button_new_project.action_name = "app.new-project";
  119. _buttons_box = new Gtk.Box(Gtk.Orientation.HORIZONTAL, 0);
  120. _buttons_box.spacing = 6;
  121. _buttons_box.pack_start(_local_label, false, true);
  122. _buttons_box.pack_end(_button_new_project, false, true);
  123. _buttons_box.pack_end(_button_import_project, false, true);
  124. _projects_box = new Gtk.Box(Gtk.Orientation.VERTICAL, 0);
  125. _projects_box.margin_start = 12;
  126. _projects_box.margin_end = 12;
  127. _projects_box.margin_top = 32;
  128. _projects_box.margin_bottom = 32;
  129. _projects_box.spacing = 12;
  130. _projects_box.pack_start(_projects_list_label, false, true);
  131. _projects_box.pack_start(_buttons_box, false, true);
  132. _projects_box.pack_start(_list_projects, false, true);
  133. _clamp = new Clamp();
  134. _clamp.set_child(_projects_box);
  135. this.add(_clamp);
  136. _user.recent_project_added.connect(on_recent_project_added);
  137. _user.recent_project_touched.connect(on_recent_project_touched);
  138. _user.recent_project_removed.connect(on_recent_project_removed);
  139. }
  140. public void on_recent_project_added(string source_dir, string name, string time)
  141. {
  142. // Add project row.
  143. var row = new ProjectRow(source_dir, time, name, this);
  144. _list_projects.add(row);
  145. _list_projects.show_all(); // Otherwise the list is not always updated...
  146. if (!GLib.FileUtils.test(source_dir, FileTest.EXISTS))
  147. row._open_button.sensitive = false;
  148. invalidate_sort();
  149. }
  150. public void on_recent_project_touched(string source_dir, string mtime)
  151. {
  152. _list_projects.foreach((row) => {
  153. if (row.get_data<string>("source_dir") == source_dir) {
  154. row.set_data("mtime", mtime);
  155. return;
  156. }
  157. });
  158. invalidate_sort();
  159. }
  160. public void on_recent_project_removed(string source_dir)
  161. {
  162. _list_projects.foreach((row) => {
  163. if (row.get_data<string>("source_dir") == source_dir) {
  164. _list_projects.remove(row);
  165. return;
  166. }
  167. });
  168. }
  169. public void invalidate_sort()
  170. {
  171. _list_projects.invalidate_sort();
  172. // Give focus to most recent project's open button.
  173. ProjectRow? first_row = (ProjectRow?)_list_projects.get_row_at_index(0);
  174. if (first_row != null)
  175. first_row._open_button.has_focus = true;
  176. }
  177. }
  178. } /* namespace Crown */