修复Files APP的一个bug

This commit is contained in:
2026-03-24 19:03:05 +08:00
parent d044e97cea
commit 4dd47bfe22
4 changed files with 94 additions and 3 deletions

View File

@@ -1 +1 @@
2026-03-24 18:39:25
2026-03-24 18:59:31

View File

@@ -1 +1 @@
fc61afb
d044e97

View File

@@ -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;

View File

@@ -50,6 +50,11 @@ namespace CMLeonOS.Utils
rest = rest.TrimEnd('/');
rest = rest.TrimEnd('\\');
if (hasDrive && string.IsNullOrWhiteSpace(rest))
{
return drive + @"\";
}
return drive + rest;
}