1 module unecht.core.components.editor.ui.fileDialog; 2 3 version(UEIncludeEditor): 4 5 import unecht.core.component; 6 import unecht.core.components.internal.gui; 7 8 import derelict.imgui.imgui; 9 10 import std.file; 11 import std.path; 12 13 /// 14 final class UEFileDialog : UEComponent 15 { 16 mixin(UERegisterObject!()); 17 18 static bool isOpen; 19 static string path; 20 static bool wasOk; 21 private static string currentFile; 22 private static string pattern; 23 private static string extension; 24 private static DirEntry[] dirList; 25 private static string renameSource; 26 private static string renameTarget; 27 28 /// 29 public static void open(string _pattern, string _extension) 30 { 31 currentFile.length = 0; 32 isOpen = true; 33 wasOk = true; 34 pattern = _pattern; 35 extension = _extension; 36 37 update(getcwd()~"/"); 38 } 39 40 private static update(string _path) 41 { 42 import std.path:dirName,relativePath; 43 44 path = isDir(_path) ? _path : dirName(_path); 45 dirList.length = 0; 46 47 foreach (DirEntry e; dirEntries(path, SpanMode.shallow)) 48 { 49 if(e.isDir || (e.isFile && globMatch(e.name,pattern))) 50 dirList ~= e; 51 } 52 } 53 54 public void render() 55 { 56 if(!isOpen) 57 return; 58 59 igOpenPopup("file dialog"); 60 61 if(igBeginPopupModal("file dialog")) 62 { 63 scope(exit){igEndPopup();} 64 65 UEGui.Text("path: "~path); 66 UEGui.Text("pattern: "~pattern); 67 igSeparator(); 68 69 if(UEGui.Button("..")) 70 { 71 path = buildNormalizedPath(path~"/.."); 72 update(path); 73 return; 74 } 75 76 foreach(entry; dirList) 77 { 78 if(renameSource == entry.name) 79 { 80 if(renderRename()) 81 break; 82 } 83 else 84 { 85 UEGui.Text(relativePath(entry.name,path)); 86 if(igIsItemHovered()) 87 { 88 if(entry.isDir) 89 { 90 if(igIsMouseDoubleClicked(0)) 91 { 92 update(entry.name); 93 return; 94 } 95 } 96 else 97 { 98 if(igIsMouseClicked(0)) 99 { 100 currentFile = baseName(entry); 101 } 102 } 103 } 104 105 } 106 } 107 108 igSeparator(); 109 110 UEGui.InputText("filename", currentFile); 111 112 igSeparator(); 113 114 if(igButton("new folder")) 115 { 116 import std.file:mkdir; 117 mkdir(path~"/"~"new folder"); 118 update(path); 119 } 120 121 igSameLine(); 122 123 if(igButton("ok")) 124 { 125 path ~= "/"~currentFile; 126 path = setExtension(path,extension); 127 igCloseCurrentPopup(); 128 isOpen = false; 129 } 130 131 igSameLine(); 132 133 if(igButton("cancel")) 134 { 135 igCloseCurrentPopup(); 136 isOpen = false; 137 wasOk = false; 138 } 139 } 140 } 141 142 private bool renderRename() 143 { 144 if(UEGui.InputText("rename",renameTarget)) 145 { 146 import unecht.core.logger; 147 import std.file:rename; 148 log.logf("rename %s -> %s",renameSource,renameTarget); 149 150 rename(renameSource,renameTarget); 151 renameSource.length = 0; 152 update(path); 153 return true; 154 } 155 156 return false; 157 } 158 }