From 4dd47bfe222a1dcd73db077320200dc445c6d744 Mon Sep 17 00:00:00 2001 From: Leonmmcoset Date: Tue, 24 Mar 2026 19:03:05 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8DFiles=20APP=E7=9A=84=E4=B8=80?= =?UTF-8?q?=E4=B8=AAbug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- BuildTime.txt | 2 +- GitCommit.txt | 2 +- Gui/Apps/Files.cs | 88 ++++++++++++++++++++++++++++++++++++++++++++++- utils/PathUtil.cs | 5 +++ 4 files changed, 94 insertions(+), 3 deletions(-) diff --git a/BuildTime.txt b/BuildTime.txt index 6ce4325..bdb9053 100644 --- a/BuildTime.txt +++ b/BuildTime.txt @@ -1 +1 @@ -2026-03-24 18:39:25 \ No newline at end of file +2026-03-24 18:59:31 \ No newline at end of file diff --git a/GitCommit.txt b/GitCommit.txt index 6ca38a4..207462b 100644 --- a/GitCommit.txt +++ b/GitCommit.txt @@ -1 +1 @@ -fc61afb \ No newline at end of file +d044e97 \ No newline at end of file diff --git a/Gui/Apps/Files.cs b/Gui/Apps/Files.cs index 9abf648..85c453d 100644 --- a/Gui/Apps/Files.cs +++ b/Gui/Apps/Files.cs @@ -35,6 +35,8 @@ namespace CMLeonOS.Gui.Apps Table shortcutsTable; ImageBlock up; + Button newFileButton; + Button newFolderButton; TextBox pathBox; @@ -82,7 +84,9 @@ namespace CMLeonOS.Gui.Apps private const int pathBoxMargin = 4; private const int pathBoxHeight = 24; private const int shortcutsWidth = 128; - private const int headerHeight = 32; + private const int headerHeight = 56; + private const int toolbarButtonWidth = 80; + private const int toolbarButtonGap = 4; private const string dirEmptyMessage = "This folder is empty."; @@ -154,6 +158,13 @@ namespace CMLeonOS.Gui.Apps } } + private void RefreshCurrentDirectory() + { + PopulateEntryTable(); + RenderHeader(); + wm.Update(window); + } + private void PopulateShortcutTable() { shortcutsTable.Cells.Clear(); @@ -272,6 +283,68 @@ namespace CMLeonOS.Gui.Apps } } + private void PromptCreateFile() + { + PromptBox promptBox = new PromptBox(this, "New File", "Create an empty file in the current folder.", "NewFile.txt", (string name) => + { + try + { + string sanitizedName = Path.GetFileName(PathUtil.Sanitize((name ?? string.Empty).Trim())); + if (string.IsNullOrWhiteSpace(sanitizedName)) + { + return; + } + + string path = Path.Combine(currentDir, sanitizedName); + if (File.Exists(path) || Directory.Exists(path)) + { + new MessageBox(this, "Files", "A file or folder with that name already exists.").Show(); + return; + } + + File.WriteAllText(path, string.Empty); + RefreshCurrentDirectory(); + } + catch (Exception ex) + { + Logger.Logger.Instance.Error("Files", $"Error creating file: {ex.Message}"); + new MessageBox(this, "Files", "Unable to create the file.").Show(); + } + }); + promptBox.Show(); + } + + private void PromptCreateFolder() + { + PromptBox promptBox = new PromptBox(this, "New Folder", "Create a folder in the current directory.", "NewFolder", (string name) => + { + try + { + string sanitizedName = Path.GetFileName(PathUtil.Sanitize((name ?? string.Empty).Trim())); + if (string.IsNullOrWhiteSpace(sanitizedName)) + { + return; + } + + string path = Path.Combine(currentDir, sanitizedName); + if (File.Exists(path) || Directory.Exists(path)) + { + new MessageBox(this, "Files", "A file or folder with that name already exists.").Show(); + return; + } + + Directory.CreateDirectory(path); + RefreshCurrentDirectory(); + } + catch (Exception ex) + { + Logger.Logger.Instance.Error("Files", $"Error creating folder: {ex.Message}"); + new MessageBox(this, "Files", "Unable to create the folder.").Show(); + } + }); + promptBox.Show(); + } + private void RenderHeader(bool updateWindow = true) { header.Clear(Color.DarkBlue); @@ -320,6 +393,9 @@ namespace CMLeonOS.Gui.Apps pathBox.Submitted = PathBoxSubmitted; wm.AddWindow(pathBox); + int buttonY = pathBoxHeight + 28; + int buttonX = shortcutsWidth + 8; + entryTable = new Table(window, shortcutsWidth, pathBoxHeight + headerHeight, window.Width - shortcutsWidth, window.Height - pathBoxHeight - headerHeight); entryTable.OnDoubleClick = EntryTableDoubleClicked; PopulateEntryTable(); @@ -329,6 +405,16 @@ namespace CMLeonOS.Gui.Apps wm.AddWindow(header); RenderHeader(updateWindow: false); + newFileButton = new Button(window, buttonX, buttonY, toolbarButtonWidth, 20); + newFileButton.Text = "New File"; + newFileButton.OnClick = (_, _) => PromptCreateFile(); + wm.AddWindow(newFileButton); + + newFolderButton = new Button(window, buttonX + toolbarButtonWidth + toolbarButtonGap, buttonY, toolbarButtonWidth, 20); + newFolderButton.Text = "New Folder"; + newFolderButton.OnClick = (_, _) => PromptCreateFolder(); + wm.AddWindow(newFolderButton); + shortcutsTable = new Table(window, 0, pathBoxHeight, shortcutsWidth, window.Height - pathBoxHeight); shortcutsTable.AllowDeselection = false; shortcutsTable.Background = Color.DarkGray; diff --git a/utils/PathUtil.cs b/utils/PathUtil.cs index 66cc619..a3bc965 100644 --- a/utils/PathUtil.cs +++ b/utils/PathUtil.cs @@ -50,6 +50,11 @@ namespace CMLeonOS.Utils rest = rest.TrimEnd('/'); rest = rest.TrimEnd('\\'); + if (hasDrive && string.IsNullOrWhiteSpace(rest)) + { + return drive + @"\"; + } + return drive + rest; }