Browse Source

[fileBrowser] FancyGallery context menu

Clément Espeute 2 tháng trước cách đây
mục cha
commit
4091d4ffa1
2 tập tin đã thay đổi với 69 bổ sung25 xóa
  1. 17 0
      hide/comp/FancyGallery.hx
  2. 52 25
      hide/view/FileBrowser.hx

+ 17 - 0
hide/comp/FancyGallery.hx

@@ -73,6 +73,8 @@ class FancyGallery<GalleryItem> extends hide.comp.Component {
 				queueRefresh();
 				queueRefresh();
 			}
 			}
 		}
 		}
+
+		itemContainer.oncontextmenu = contextMenuHandler.bind(null);
 		scroll = el.find("fancy-scroll").get(0);
 		scroll = el.find("fancy-scroll").get(0);
 
 
 		scroll.onscroll = (e) -> queueRefresh();
 		scroll.onscroll = (e) -> queueRefresh();
@@ -117,6 +119,15 @@ class FancyGallery<GalleryItem> extends hide.comp.Component {
 
 
 	}
 	}
 
 
+	/**
+		Called when the user right click an item (or the background) of the gallery.
+		`item` will be null if the background was clicked. Default is do nothing
+	**/
+	public dynamic function onContextMenu(item: GalleryItem, event : js.html.MouseEvent) {
+		event.stopPropagation();
+		event.preventDefault();
+	}
+
 	/**
 	/**
 		Drag and drop interface.
 		Drag and drop interface.
 		Set this struct with all of it's function callback to handle drag and drop inside your tree.
 		Set this struct with all of it's function callback to handle drag and drop inside your tree.
@@ -226,6 +237,10 @@ class FancyGallery<GalleryItem> extends hide.comp.Component {
 		currentRefreshFlags = GalleryRefreshFlags.ofInt(0);
 		currentRefreshFlags = GalleryRefreshFlags.ofInt(0);
 	}
 	}
 
 
+	function contextMenuHandler(item: GalleryItem, event: js.html.MouseEvent) {
+		onContextMenu(item, event);
+	}
+
 	function setupDragAndDrop(data : GalleryItemData<GalleryItem>) {
 	function setupDragAndDrop(data : GalleryItemData<GalleryItem>) {
 		if (dragAndDropInterface == null)
 		if (dragAndDropInterface == null)
 			return;
 			return;
@@ -263,6 +278,8 @@ class FancyGallery<GalleryItem> extends hide.comp.Component {
 				onDoubleClick(data.item);
 				onDoubleClick(data.item);
 			}
 			}
 
 
+			data.element.oncontextmenu = contextMenuHandler.bind(data.item);
+
 			setupDragAndDrop(data);
 			setupDragAndDrop(data);
 		}
 		}
 
 

+ 52 - 25
hide/view/FileBrowser.hx

@@ -44,15 +44,22 @@ class FileEntry {
 
 
 	public function refreshChildren() {
 	public function refreshChildren() {
 		var fullPath = getPath();
 		var fullPath = getPath();
-		var paths = js.node.Fs.readdirSync(fullPath);
-
-		var oldChildren : Map<String, FileEntry> = [for (file in (children ?? [])) file.name => file];
 
 
 		if (children == null)
 		if (children == null)
 			children = [];
 			children = [];
 		else
 		else
 			children.resize(0);
 			children.resize(0);
 
 
+		if (!js.node.Fs.existsSync(fullPath)) {
+			return;
+		}
+
+		var paths = js.node.Fs.readdirSync(fullPath);
+
+		var oldChildren : Map<String, FileEntry> = [for (file in (children ?? [])) file.name => file];
+
+
+
 		for (path in paths) {
 		for (path in paths) {
 			if (StringTools.startsWith(path, "."))
 			if (StringTools.startsWith(path, "."))
 				continue;
 				continue;
@@ -407,9 +414,10 @@ class FileBrowser extends hide.ui.View<FileBrowserState> {
 
 
 				var roots = getRoots(files);
 				var roots = getRoots(files);
 				var outerFiles: Array<{from: String, to: String}> = [];
 				var outerFiles: Array<{from: String, to: String}> = [];
+				var targetPath = target.getPath();
 				for (root in roots) {
 				for (root in roots) {
-					var movePath = targetPath + "/" + file.split("/").pop();
-					outerFiles.push({from: file, to: movePath});
+					var movePath = targetPath + "/" + root.split("/").pop();
+					outerFiles.push({from: root, to: movePath});
 				}
 				}
 
 
 				function exec(isUndo: Bool) {
 				function exec(isUndo: Bool) {
@@ -505,6 +513,9 @@ class FileBrowser extends hide.ui.View<FileBrowserState> {
 			}
 			}
 		}
 		}
 
 
+		fancyGallery.onContextMenu = contextMenu.bind(true);
+
+
 		if (Ide.inst.ideConfig.filebrowserDebugShowMenu) {
 		if (Ide.inst.ideConfig.filebrowserDebugShowMenu) {
 			layout.find(".btn-collapse-folders").after(new Element('<fancy-button class="btn-debug"><span class="ico ico-bug"></span></fancy-button>'));
 			layout.find(".btn-collapse-folders").after(new Element('<fancy-button class="btn-debug"><span class="ico ico-bug"></span></fancy-button>'));
 			var button = layout.find(".btn-debug").get(0);
 			var button = layout.find(".btn-debug").get(0);
@@ -615,17 +626,28 @@ class FileBrowser extends hide.ui.View<FileBrowserState> {
 	}
 	}
 
 
 	function deleteFiles(fullPaths : Array<String>) {
 	function deleteFiles(fullPaths : Array<String>) {
+		//trace(fullPaths);
 		var roots = getRoots(fullPaths);
 		var roots = getRoots(fullPaths);
-		if( sys.FileSystem.isDirectory(fullPath) ) {
-			for( f in sys.FileSystem.readDirectory(fullPath) )
-				onDeleteFile(path + "/" + f);
-			sys.FileSystem.deleteDirectory(fullPath);
-		} else
-			sys.FileSystem.deleteFile(fullPath);
+		for (fullPath in roots) {
+			if( sys.FileSystem.isDirectory(fullPath) ) {
+				var filesInDir = [];
+				for (f in sys.FileSystem.readDirectory(fullPath)) {
+					trace("files in dir :", f);
+					filesInDir.push(fullPath + "/" + f);
+				}
+				if (filesInDir.length > 0)
+					deleteFiles(filesInDir);
+				sys.FileSystem.deleteDirectory(fullPath);
+			} else
+				sys.FileSystem.deleteFile(fullPath);
+		}
 	}
 	}
 
 
-	function getItemAndSelection(isGallery: Bool) : Array<FileEntry> {
-		var items = [currentFolder];
+	function getItemAndSelection(baseItem: FileEntry, isGallery: Bool) : Array<FileEntry> {
+		var items = [];
+		if (baseItem != null) {
+			items.push(baseItem);
+		}
 		if (!isGallery) {
 		if (!isGallery) {
 			return items.concat(fancyTree.getSelectedItems());
 			return items.concat(fancyTree.getSelectedItems());
 		}
 		}
@@ -672,34 +694,39 @@ class FileBrowser extends hide.ui.View<FileBrowserState> {
 		if (item == null)
 		if (item == null)
 			item = currentFolder;
 			item = currentFolder;
 
 
-		currentFolder = item;
+		/*currentFolder = item;
 		fancyTree.selectItem(currentFolder);
 		fancyTree.selectItem(currentFolder);
-		queueGalleryRefresh();
+		queueGalleryRefresh();*/
 
 
 		var newMenu = [];
 		var newMenu = [];
 		for (e in @:privateAccess hide.view.FileTree.EXTENSIONS) {
 		for (e in @:privateAccess hide.view.FileTree.EXTENSIONS) {
 			if (e.options.createNew != null) {
 			if (e.options.createNew != null) {
 				newMenu.push({
 				newMenu.push({
 				label: e.options.createNew,
 				label: e.options.createNew,
-				click : createNew.bind(currentFolder.getPath(), e),
+				click : createNew.bind(item.getPath(), e),
 				icon : e.options.icon,
 				icon : e.options.icon,
 				});
 				});
 			}
 			}
 		}
 		}
 
 
 		var options : Array<hide.comp.ContextMenu.MenuItem> = [];
 		var options : Array<hide.comp.ContextMenu.MenuItem> = [];
-		options.push({
-			label: "New ...",
-			menu: newMenu,
-		});
 
 
-		options.push({
-			isSeparator: true;
-		});
+		if (item.kind == Dir) {
+			options.push({
+				label: "New ...",
+				menu: newMenu,
+			});
+
+			options.push({
+				isSeparator: true,
+			});
+		}
 
 
 		options.push({
 		options.push({
-			lable: "Delete", click: deleteFile.bind()
-		})
+			label: "Delete", click: () -> {
+				deleteFiles([for (file in getItemAndSelection(item, isGallery)) file.getPath()]);
+			}
+		});
 
 
 		hide.comp.ContextMenu.createFromEvent(event, options);
 		hide.comp.ContextMenu.createFromEvent(event, options);
 	}
 	}