Browse Source

tools: extract menu_create() from ProjectBrowser

Daniele Bartolini 2 years ago
parent
commit
163f4c14cd
2 changed files with 244 additions and 222 deletions
  1. 22 3
      tools/level_editor/level_editor.vala
  2. 222 219
      tools/level_editor/project_browser.vala

+ 22 - 3
tools/level_editor/level_editor.vala

@@ -333,7 +333,8 @@ public class LevelEditorApplication : Gtk.Application
 		{ "deploy",        on_deploy,        null, null },
 		{ "close",         on_close,         null, null },
 		{ "quit",          on_quit,          null, null },
-		{ "open-resource", on_open_resource, "s",  null }
+		{ "open-resource", on_open_resource, "s",  null },
+		{ "copy-path",     on_copy_path,     "s",  null }
 	};
 
 	private const GLib.ActionEntry[] action_entries_edit =
@@ -2102,6 +2103,17 @@ public class LevelEditorApplication : Gtk.Application
 		}
 	}
 
+	private void on_copy_path(GLib.SimpleAction action, GLib.Variant? param)
+	{
+		string path  = param.get_string();
+
+		string abs_path = _project.absolute_path(path);
+
+		var clip = Gtk.Clipboard.get_default(Gdk.Display.get_default());
+		clip.set_text(abs_path, abs_path.length);
+		clip.store();
+	}
+
 	private void on_show_grid(GLib.SimpleAction action, GLib.Variant? param)
 	{
 		_show_grid = !action.get_state().get_boolean();
@@ -2503,9 +2515,16 @@ public class LevelEditorApplication : Gtk.Application
 
 	private void on_open_containing(GLib.SimpleAction action, GLib.Variant? param)
 	{
-		string parent_name = param.get_string();
+		string path = param.get_string();
+		GLib.File abs_path = GLib.File.new_for_path(_project.absolute_path(path));
+		GLib.File? abs_parent = abs_path.get_parent();
+
+		string abs_parent_path = abs_parent != null
+			? abs_parent.get_path()
+			: abs_path.get_path()
+			;
 
-		open_directory(_project.absolute_path(parent_name));
+		open_directory(abs_parent_path);
 	}
 
 	public void delete_tree(GLib.File file) throws Error

+ 222 - 219
tools/level_editor/project_browser.vala

@@ -8,6 +8,224 @@ using Gee;
 
 namespace Crown
 {
+private Gtk.Menu? menu_create(string type, string name)
+{
+	Gtk.Menu? menu;
+
+	if (type == "<folder>") {
+		menu = new Gtk.Menu();
+
+		Gtk.MenuItem mi;
+
+		mi = new Gtk.MenuItem.with_label("Import...");
+		mi.activate.connect(() => {
+				GLib.Application.get_default().activate_action("import", new GLib.Variant.string((string)name));
+			});
+		menu.add(mi);
+
+		mi = new Gtk.SeparatorMenuItem();
+		menu.add(mi);
+
+		mi = new Gtk.MenuItem.with_label("New Script...");
+		mi.activate.connect(() => {
+				Gtk.Dialog dg = new Gtk.Dialog.with_buttons("Script Name"
+					, ((Gtk.Application)GLib.Application.get_default()).active_window
+					, DialogFlags.MODAL
+					, "Cancel"
+					, ResponseType.CANCEL
+					, "Ok"
+					, ResponseType.OK
+					, null
+					);
+
+				EntryText sb = new EntryText();
+				sb.activate.connect(() => { dg.response(ResponseType.OK); });
+				dg.get_content_area().add(sb);
+				dg.skip_taskbar_hint = true;
+				dg.show_all();
+
+				if (dg.run() == (int)ResponseType.OK) {
+					if (sb.text.strip() == "") {
+						dg.destroy();
+						return;
+					}
+
+					var tuple = new GLib.Variant.tuple({(string)name, sb.text, true});
+					GLib.Application.get_default().activate_action("create-script", tuple);
+				}
+
+				dg.destroy();
+			});
+		menu.add(mi);
+
+		mi = new Gtk.MenuItem.with_label("New Script (Unit)...");
+		mi.activate.connect(() => {
+				Gtk.Dialog dg = new Gtk.Dialog.with_buttons("Script Name"
+					, ((Gtk.Application)GLib.Application.get_default()).active_window
+					, DialogFlags.MODAL
+					, "Cancel"
+					, ResponseType.CANCEL
+					, "Ok"
+					, ResponseType.OK
+					, null
+					);
+
+				EntryText sb = new EntryText();
+				sb.activate.connect(() => { dg.response(ResponseType.OK); });
+				dg.get_content_area().add(sb);
+				dg.skip_taskbar_hint = true;
+				dg.show_all();
+
+				if (dg.run() == (int)ResponseType.OK) {
+					if (sb.text.strip() == "") {
+						dg.destroy();
+						return;
+					}
+
+					var tuple = new GLib.Variant.tuple({(string)name, sb.text, false});
+					GLib.Application.get_default().activate_action("create-script", tuple);
+				}
+
+				dg.destroy();
+			});
+		menu.add(mi);
+
+		mi = new Gtk.SeparatorMenuItem();
+		menu.add(mi);
+
+		mi = new Gtk.MenuItem.with_label("New Unit...");
+		mi.activate.connect(() => {
+				Gtk.Dialog dg = new Gtk.Dialog.with_buttons("Unit Name"
+					, ((Gtk.Application)GLib.Application.get_default()).active_window
+					, DialogFlags.MODAL
+					, "Cancel"
+					, ResponseType.CANCEL
+					, "Ok"
+					, ResponseType.OK
+					, null
+					);
+
+				EntryText sb = new EntryText();
+				sb.activate.connect(() => { dg.response(ResponseType.OK); });
+				dg.get_content_area().add(sb);
+				dg.skip_taskbar_hint = true;
+				dg.show_all();
+
+				if (dg.run() == (int)ResponseType.OK) {
+					if (sb.text.strip() == "") {
+						dg.destroy();
+						return;
+					}
+				}
+
+				var tuple = new GLib.Variant.tuple({(string)name, sb.text});
+				GLib.Application.get_default().activate_action("create-unit", tuple);
+
+				dg.destroy();
+			});
+		menu.add(mi);
+
+		mi = new Gtk.SeparatorMenuItem();
+		menu.add(mi);
+
+		mi = new Gtk.MenuItem.with_label("New Folder...");
+		mi.activate.connect(() => {
+				Gtk.Dialog dg = new Gtk.Dialog.with_buttons("Folder Name"
+					, ((Gtk.Application)GLib.Application.get_default()).active_window
+					, DialogFlags.MODAL
+					, "Cancel"
+					, ResponseType.CANCEL
+					, "Ok"
+					, ResponseType.OK
+					, null
+					);
+
+				EntryText sb = new EntryText();
+				sb.activate.connect(() => { dg.response(ResponseType.OK); });
+				dg.get_content_area().add(sb);
+				dg.skip_taskbar_hint = true;
+				dg.show_all();
+
+				if (dg.run() == (int)ResponseType.OK) {
+					if (sb.text.strip() == "") {
+						dg.destroy();
+						return;
+					}
+
+					var tuple = new GLib.Variant.tuple({(string)name, sb.text});
+					GLib.Application.get_default().activate_action("create-directory", tuple);
+				}
+
+				dg.destroy();
+			});
+		menu.add(mi);
+
+		if ((string)name != ProjectStore.ROOT_FOLDER) {
+			mi = new Gtk.MenuItem.with_label("Delete Folder");
+			mi.activate.connect(() => {
+					Gtk.MessageDialog md = new Gtk.MessageDialog(((Gtk.Application)GLib.Application.get_default()).active_window
+						, Gtk.DialogFlags.MODAL
+						, Gtk.MessageType.WARNING
+						, Gtk.ButtonsType.NONE
+						, "Delete Folder " + (string)name + "?"
+						);
+
+					Gtk.Widget btn;
+					md.add_button("_Cancel", ResponseType.CANCEL);
+					btn = md.add_button("_Delete", ResponseType.YES);
+					btn.get_style_context().add_class(Gtk.STYLE_CLASS_DESTRUCTIVE_ACTION);
+
+					md.set_default_response(ResponseType.CANCEL);
+
+					int rt = md.run();
+					md.destroy();
+
+					if (rt != (int)ResponseType.YES)
+						return;
+
+					GLib.Application.get_default().activate_action("delete-directory", new GLib.Variant.string((string)name));
+				});
+			menu.add(mi);
+		}
+	} else { // If file
+		menu = new Gtk.Menu();
+
+		Gtk.MenuItem mi;
+
+		mi = new Gtk.MenuItem.with_label("Delete File");
+		mi.activate.connect(() => {
+				GLib.Application.get_default().activate_action("delete-file", new GLib.Variant.string(ResourceId.path((string)type, (string)name)));
+			});
+		menu.add(mi);
+
+		mi = new Gtk.MenuItem.with_label("Open Containing Folder...");
+		mi.activate.connect(() => {
+				GLib.Application.get_default().activate_action("open-containing", new GLib.Variant.string(name));
+			});
+		menu.add(mi);
+	}
+
+	// Add shared menu items.
+	Gtk.MenuItem mi;
+
+	mi = new Gtk.SeparatorMenuItem();
+	menu.add(mi);
+
+	mi = new Gtk.MenuItem.with_label("Copy Path");
+	mi.activate.connect(() => {
+			string path;
+			if (type == "<folder>")
+				path = name;
+			else
+				path = ResourceId.path(type, name);
+
+			GLib.Application.get_default().activate_action("copy-path", new GLib.Variant.string(path));
+		});
+	menu.add(mi);
+
+	return menu;
+}
+
 public class ProjectBrowser : Gtk.Box
 {
 	// Data
@@ -227,226 +445,11 @@ public class ProjectBrowser : Gtk.Box
 				_tree_view.model.get_value(iter, ProjectStore.Column.TYPE, out type);
 				_tree_view.model.get_value(iter, ProjectStore.Column.NAME, out name);
 
-				Gtk.Menu menu = new Gtk.Menu();
-
-				if (type == "<folder>") {
-					Gtk.MenuItem mi;
-
-					mi = new Gtk.MenuItem.with_label("Import...");
-					mi.activate.connect(() => {
-							GLib.Application.get_default().activate_action("import", new GLib.Variant.string((string)name));
-						});
-					menu.add(mi);
-
-					mi = new Gtk.SeparatorMenuItem();
-					menu.add(mi);
-
-					mi = new Gtk.MenuItem.with_label("New Script...");
-					mi.activate.connect(() => {
-							Gtk.Dialog dg = new Gtk.Dialog.with_buttons("Script Name"
-								, (Gtk.Window)this.get_toplevel()
-								, DialogFlags.MODAL
-								, "Cancel"
-								, ResponseType.CANCEL
-								, "Ok"
-								, ResponseType.OK
-								, null
-								);
-
-							EntryText sb = new EntryText();
-							sb.activate.connect(() => { dg.response(ResponseType.OK); });
-							dg.get_content_area().add(sb);
-							dg.skip_taskbar_hint = true;
-							dg.show_all();
-
-							if (dg.run() == (int)ResponseType.OK) {
-								if (sb.text.strip() == "") {
-									dg.destroy();
-									return;
-								}
-
-								var tuple = new GLib.Variant.tuple({(string)name, sb.text, true});
-								GLib.Application.get_default().activate_action("create-script", tuple);
-							}
-
-							dg.destroy();
-						});
-					menu.add(mi);
-
-					mi = new Gtk.MenuItem.with_label("New Script (Unit)...");
-					mi.activate.connect(() => {
-							Gtk.Dialog dg = new Gtk.Dialog.with_buttons("Script Name"
-								, (Gtk.Window)this.get_toplevel()
-								, DialogFlags.MODAL
-								, "Cancel"
-								, ResponseType.CANCEL
-								, "Ok"
-								, ResponseType.OK
-								, null
-								);
-
-							EntryText sb = new EntryText();
-							sb.activate.connect(() => { dg.response(ResponseType.OK); });
-							dg.get_content_area().add(sb);
-							dg.skip_taskbar_hint = true;
-							dg.show_all();
-
-							if (dg.run() == (int)ResponseType.OK) {
-								if (sb.text.strip() == "") {
-									dg.destroy();
-									return;
-								}
-
-								var tuple = new GLib.Variant.tuple({(string)name, sb.text, false});
-								GLib.Application.get_default().activate_action("create-script", tuple);
-							}
-
-							dg.destroy();
-						});
-					menu.add(mi);
-
-					mi = new Gtk.SeparatorMenuItem();
-					menu.add(mi);
-
-					mi = new Gtk.MenuItem.with_label("New Unit...");
-					mi.activate.connect(() => {
-							Gtk.Dialog dg = new Gtk.Dialog.with_buttons("Unit Name"
-								, (Gtk.Window)this.get_toplevel()
-								, DialogFlags.MODAL
-								, "Cancel"
-								, ResponseType.CANCEL
-								, "Ok"
-								, ResponseType.OK
-								, null
-								);
-
-							EntryText sb = new EntryText();
-							sb.activate.connect(() => { dg.response(ResponseType.OK); });
-							dg.get_content_area().add(sb);
-							dg.skip_taskbar_hint = true;
-							dg.show_all();
-
-							if (dg.run() == (int)ResponseType.OK) {
-								if (sb.text.strip() == "") {
-									dg.destroy();
-									return;
-								}
-							}
-
-							var tuple = new GLib.Variant.tuple({(string)name, sb.text});
-							GLib.Application.get_default().activate_action("create-unit", tuple);
-
-							dg.destroy();
-						});
-					menu.add(mi);
-
-					mi = new Gtk.SeparatorMenuItem();
-					menu.add(mi);
-
-					mi = new Gtk.MenuItem.with_label("New Folder...");
-					mi.activate.connect(() => {
-							Gtk.Dialog dg = new Gtk.Dialog.with_buttons("Folder Name"
-								, (Gtk.Window)this.get_toplevel()
-								, DialogFlags.MODAL
-								, "Cancel"
-								, ResponseType.CANCEL
-								, "Ok"
-								, ResponseType.OK
-								, null
-								);
-
-							EntryText sb = new EntryText();
-							sb.activate.connect(() => { dg.response(ResponseType.OK); });
-							dg.get_content_area().add(sb);
-							dg.skip_taskbar_hint = true;
-							dg.show_all();
-
-							if (dg.run() == (int)ResponseType.OK) {
-								if (sb.text.strip() == "") {
-									dg.destroy();
-									return;
-								}
-
-								var tuple = new GLib.Variant.tuple({(string)name, sb.text});
-								GLib.Application.get_default().activate_action("create-directory", tuple);
-							}
-
-							dg.destroy();
-						});
-					menu.add(mi);
-
-					if ((string)name != ProjectStore.ROOT_FOLDER) {
-						mi = new Gtk.MenuItem.with_label("Delete Folder");
-						mi.activate.connect(() => {
-								Gtk.MessageDialog md = new Gtk.MessageDialog((Gtk.Window)this.get_toplevel()
-									, Gtk.DialogFlags.MODAL
-									, Gtk.MessageType.WARNING
-									, Gtk.ButtonsType.NONE
-									, "Delete Folder " + (string)name + "?"
-									);
-
-								Gtk.Widget btn;
-								md.add_button("_Cancel", ResponseType.CANCEL);
-								btn = md.add_button("_Delete", ResponseType.YES);
-								btn.get_style_context().add_class(Gtk.STYLE_CLASS_DESTRUCTIVE_ACTION);
-
-								md.set_default_response(ResponseType.CANCEL);
-
-								int rt = md.run();
-								md.destroy();
-
-								if (rt != (int)ResponseType.YES)
-									return;
-
-								GLib.Application.get_default().activate_action("delete-directory", new GLib.Variant.string((string)name));
-							});
-						menu.add(mi);
-					}
-				} else { // If file
-					Gtk.MenuItem mi;
-
-					mi = new Gtk.MenuItem.with_label("Delete File");
-					mi.activate.connect(() => {
-							GLib.Application.get_default().activate_action("delete-file", new GLib.Variant.string(ResourceId.path((string)type, (string)name)));
-						});
-					menu.add(mi);
-
-					mi = new Gtk.MenuItem.with_label("Open Containing Folder...");
-					mi.activate.connect(() => {
-							Gtk.TreeIter parent;
-							if (_tree_view.model.iter_parent(out parent, iter)) {
-								Value parent_name;
-								_tree_view.model.get_value(parent, ProjectStore.Column.NAME, out parent_name);
-
-								GLib.Application.get_default().activate_action("open-containing", new GLib.Variant.string((string)parent_name));
-							}
-						});
-					menu.add(mi);
+				var? menu = menu_create((string)type, (string)name);
+				if (menu != null) {
+					menu.show_all();
+					menu.popup_at_pointer(ev);
 				}
-
-				// Add shared menu items.
-				Gtk.MenuItem mi;
-
-				mi = new Gtk.SeparatorMenuItem();
-				menu.add(mi);
-
-				mi = new Gtk.MenuItem.with_label("Copy Path");
-				mi.activate.connect(() => {
-						string abs_path;
-
-						if ((string)type == "<folder>")
-							abs_path = _project_store._project.absolute_path((string)name);
-						else
-							abs_path = _project_store._project.absolute_path(ResourceId.path((string)type, (string)name));
-
-						var clip = Gtk.Clipboard.get_default(Gdk.Display.get_default());
-						clip.set_text(abs_path, abs_path.length);
-						clip.store();
-					});
-				menu.add(mi);
-
-				menu.show_all();
-				menu.popup_at_pointer(ev);
 			}
 		} else if (ev.button == Gdk.BUTTON_PRIMARY) {
 			if (ev.type == Gdk.EventType.@2BUTTON_PRESS) {