浏览代码

[fileBrowser] FancyGallery context menu

Clément Espeute 2 月之前
父节点
当前提交
4091d4ffa1
共有 2 个文件被更改,包括 69 次插入25 次删除
  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();
 			}
 		}
+
+		itemContainer.oncontextmenu = contextMenuHandler.bind(null);
 		scroll = el.find("fancy-scroll").get(0);
 
 		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.
 		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);
 	}
 
+	function contextMenuHandler(item: GalleryItem, event: js.html.MouseEvent) {
+		onContextMenu(item, event);
+	}
+
 	function setupDragAndDrop(data : GalleryItemData<GalleryItem>) {
 		if (dragAndDropInterface == null)
 			return;
@@ -263,6 +278,8 @@ class FancyGallery<GalleryItem> extends hide.comp.Component {
 				onDoubleClick(data.item);
 			}
 
+			data.element.oncontextmenu = contextMenuHandler.bind(data.item);
+
 			setupDragAndDrop(data);
 		}
 

+ 52 - 25
hide/view/FileBrowser.hx

@@ -44,15 +44,22 @@ class FileEntry {
 
 	public function refreshChildren() {
 		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)
 			children = [];
 		else
 			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) {
 			if (StringTools.startsWith(path, "."))
 				continue;
@@ -407,9 +414,10 @@ class FileBrowser extends hide.ui.View<FileBrowserState> {
 
 				var roots = getRoots(files);
 				var outerFiles: Array<{from: String, to: String}> = [];
+				var targetPath = target.getPath();
 				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) {
@@ -505,6 +513,9 @@ class FileBrowser extends hide.ui.View<FileBrowserState> {
 			}
 		}
 
+		fancyGallery.onContextMenu = contextMenu.bind(true);
+
+
 		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>'));
 			var button = layout.find(".btn-debug").get(0);
@@ -615,17 +626,28 @@ class FileBrowser extends hide.ui.View<FileBrowserState> {
 	}
 
 	function deleteFiles(fullPaths : Array<String>) {
+		//trace(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) {
 			return items.concat(fancyTree.getSelectedItems());
 		}
@@ -672,34 +694,39 @@ class FileBrowser extends hide.ui.View<FileBrowserState> {
 		if (item == null)
 			item = currentFolder;
 
-		currentFolder = item;
+		/*currentFolder = item;
 		fancyTree.selectItem(currentFolder);
-		queueGalleryRefresh();
+		queueGalleryRefresh();*/
 
 		var newMenu = [];
 		for (e in @:privateAccess hide.view.FileTree.EXTENSIONS) {
 			if (e.options.createNew != null) {
 				newMenu.push({
 				label: e.options.createNew,
-				click : createNew.bind(currentFolder.getPath(), e),
+				click : createNew.bind(item.getPath(), e),
 				icon : e.options.icon,
 				});
 			}
 		}
 
 		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({
-			lable: "Delete", click: deleteFile.bind()
-		})
+			label: "Delete", click: () -> {
+				deleteFiles([for (file in getItemAndSelection(item, isGallery)) file.getPath()]);
+			}
+		});
 
 		hide.comp.ContextMenu.createFromEvent(event, options);
 	}