Browse Source

[filebrowser] Restored "show file in browser"

Clément Espeute 2 months ago
parent
commit
11b526e66c
4 changed files with 75 additions and 12 deletions
  1. 7 8
      hide/Ide.hx
  2. 10 0
      hide/comp/FancyGallery.hx
  3. 8 0
      hide/tools/FileManager.hx
  4. 50 4
      hide/view/FileBrowser.hx

+ 7 - 8
hide/Ide.hx

@@ -1684,14 +1684,13 @@ class Ide extends hide.tools.IdeData {
 	}
 
 	public function showFileInResources(path: String) {
-		js.Browser.alert("This feature is momentarely disabled");
-		// var filetree = getViews(hide.view.FileTree)[0];
-		// if( filetree != null) {
-		// 	if (@:privateAccess filetree.tree == null)
-		// 		filetree.onDisplay();
-		// 	filetree.activate();
-		// 	filetree.revealNode(path);
-		// }
+		var filebrowsers = getViews(hide.view.FileBrowser);
+		for (filebrowser in filebrowsers) {
+			if (@:privateAccess filebrowser.fancyTree == null)
+				filebrowser.onDisplay();
+			filebrowser.activate();
+			filebrowser.reveal(path);
+		}
 	}
 
 	public static function showFileInExplorer(path : String) {

+ 10 - 0
hide/comp/FancyGallery.hx

@@ -217,6 +217,16 @@ class FancyGallery<GalleryItem> extends hide.comp.Component {
 		}
 	}
 
+		public function selectItem(item: GalleryItem) {
+		clearSelection();
+		var data = itemMap.get(cast item);
+		if (data == null) {
+			return;
+		}
+		setSelection(data, true);
+		currentItem = data;
+	}
+
 	function dataClickHandler(data: GalleryItemData<GalleryItem>, event: js.html.MouseEvent) : Void {
 		if (!event.ctrlKey) {
 			clearSelection();

+ 8 - 0
hide/tools/FileManager.hx

@@ -39,6 +39,7 @@ class FileEntry {
 		this.kind = kind;
 
 		watch();
+		FileManager.inst.fileIndex.set(this.getRelPath(), this);
 	}
 
 	public final function toString() : String{
@@ -57,6 +58,7 @@ class FileEntry {
 			hide.Ide.inst.fileWatcher.unregister(this.getPath(), registeredWatcher.fun);
 			registeredWatcher = null;
 		}
+		FileManager.inst.fileIndex.remove(this.getRelPath());
 	}
 
 	function refreshChildren() {
@@ -136,6 +138,7 @@ typedef MiniatureReadyCallback = (miniaturePath: String) -> Void;
 class FileManager {
 
 	public var fileRoot: FileEntry;
+	var fileIndex : Map<String, FileEntry> = [];
 
 	public static final thumbnailGeneratorPort = 9669;
 	public static final thumbnailGeneratorUrl = "localhost";
@@ -211,6 +214,11 @@ class FileManager {
 		}
 	}
 
+	public function getFileEntry(path: String) {
+		var relPath = hide.Ide.inst.makeRelative(path);
+		return fileIndex.get(relPath);
+	}
+
 	// Deduplicate paths if they are contained in a directory
 	// also present in paths, to simplify bulk operations
 	public function getRoots(files: Array<FileEntry>) : Array<FileEntry> {

+ 50 - 4
hide/view/FileBrowser.hx

@@ -129,6 +129,9 @@ class FileBrowser extends hide.ui.View<FileBrowserState> {
 	var filters : Map<String, {exts: Array<String>, icon: String}> = [];
 	var filterState : Map<String, Bool> = [];
 	var ignorePatterns: Array<EReg> = [];
+	var delaySelectGallery: String = null;
+	var delaySelectFileTree: String = null;
+	var delayedSelectItem : FileEntry = null;
 
 	function set_filterEnabled(v : Bool) {
 		var anySet = false;
@@ -244,9 +247,30 @@ class FileBrowser extends hide.ui.View<FileBrowserState> {
 		}
 
 		fancyGallery.queueRefresh(Items);
+
+		if (delayedSelectItem != null) {
+			fancyGallery.selectItem(delayedSelectItem);
+			delayedSelectItem = null;
+		}
 	}
 
 	function onFileChange(file: FileEntry) {
+		if (delaySelectFileTree != null) {
+			var item = FileManager.inst.getFileEntry(delaySelectFileTree);
+			if (item != null) {
+				fancyTree.selectItem(item, false);
+				delaySelectFileTree = null;
+			}
+		}
+
+		if (delaySelectGallery != null) {
+			var item = FileManager.inst.getFileEntry(delaySelectGallery);
+			if (item != null) {
+				fancyGallery.selectItem(item);
+				delaySelectGallery = null;
+			}
+		}
+
 		fancyTree.invalidateChildren(file);
 		queueGalleryRefresh();
 	}
@@ -260,7 +284,7 @@ class FileBrowser extends hide.ui.View<FileBrowserState> {
 
 	function filterFiles(entry: FileEntry) {
 		for (excl in ignorePatterns) {
-			if (excl.match(entry.name))
+			if (excl.match(entry.getRelPath()))
 				return false;
 		}
 		return return true;
@@ -665,7 +689,7 @@ class FileBrowser extends hide.ui.View<FileBrowserState> {
 		FileManager.inst.onFileChangeHandlers.remove(onFileChange);
 	}
 
-	function createNew( directoryFullPath : String, ext : hide.view.FileTree.ExtensionDesc ) {
+	function createNew( directoryFullPath : String, ext : hide.view.FileTree.ExtensionDesc, isGallery: Bool) {
 
 		var file = ide.ask(ext.options.createNew + " name:");
 		if( file == null ) return;
@@ -676,7 +700,7 @@ class FileBrowser extends hide.ui.View<FileBrowserState> {
 
 		if( sys.FileSystem.exists(newFilePath) ) {
 			ide.error("File '" + file+"' already exists");
-			createNew(directoryFullPath, ext);
+			createNew(directoryFullPath, ext, isGallery);
 			return;
 		}
 
@@ -692,6 +716,12 @@ class FileBrowser extends hide.ui.View<FileBrowserState> {
 		sys.io.File.saveBytes(newFilePath, view.getDefaultContent());
 
 		ide.openFile(newFilePath);
+
+		if (isGallery) {
+			delaySelectGallery = newFilePath;
+		} else {
+			delaySelectFileTree = newFilePath;
+		}
 	}
 
 	function getItemAndSelection(baseItem: FileEntry, isGallery: Bool) : Array<FileEntry> {
@@ -738,6 +768,20 @@ class FileBrowser extends hide.ui.View<FileBrowserState> {
 		return roots;
 	}
 
+	public function reveal(path: String) {
+		var item = FileManager.inst.getFileEntry(path);
+		if (item == null)
+			return;
+
+		if (layout == SingleTree) {
+			fancyTree.selectItem(item);
+		} else {
+			openDir(item.parent, true);
+			delayedSelectItem = item;
+			queueGalleryRefresh();
+		}
+	}
+
 	function contextMenu(isGallery: Bool, item: FileEntry, event: js.html.MouseEvent) {
 		event.stopPropagation();
 		event.preventDefault();
@@ -776,7 +820,9 @@ class FileBrowser extends hide.ui.View<FileBrowserState> {
 			if (e.options.createNew != null) {
 				newMenu.push({
 				label: e.options.createNew,
-				click : createNew.bind(item.getPath(), e),
+				click : function() : Void {
+					createNew(item.getPath(), e, isGallery);
+				},
 				icon : e.options.icon,
 				});
 			}