Files增强打开文件

This commit is contained in:
2026-03-24 20:17:10 +08:00
parent 4dd47bfe22
commit 10e3a9da96
5 changed files with 167 additions and 8 deletions

View File

@@ -1 +1 @@
2026-03-24 18:59:31 2026-03-24 20:13:51

View File

@@ -1 +1 @@
d044e97 4dd47bf

View File

@@ -25,6 +25,11 @@ namespace CMLeonOS.Gui.Apps.CodeStudio
{ {
internal CodeStudio() : base("CodeStudio", ProcessType.Application) { } internal CodeStudio() : base("CodeStudio", ProcessType.Application) { }
internal CodeStudio(string path) : base("CodeStudio", ProcessType.Application)
{
initialPath = path;
}
Window splash; Window splash;
WindowManager wm = ProcessManager.GetProcess<WindowManager>(); WindowManager wm = ProcessManager.GetProcess<WindowManager>();
@@ -34,6 +39,7 @@ namespace CMLeonOS.Gui.Apps.CodeStudio
private static Bitmap splashBitmap = new Bitmap(_splashBytes); private static Bitmap splashBitmap = new Bitmap(_splashBytes);
private Ide ide; private Ide ide;
private string initialPath = null;
private bool ideCreated = false; private bool ideCreated = false;
@@ -55,6 +61,10 @@ namespace CMLeonOS.Gui.Apps.CodeStudio
{ {
ide = new Ide(this, wm); ide = new Ide(this, wm);
ide.Start(); ide.Start();
if (!string.IsNullOrWhiteSpace(initialPath))
{
ide.Open(initialPath);
}
wm.RemoveWindow(splash); wm.RemoveWindow(splash);
ideCreated = true; ideCreated = true;
} }

View File

@@ -236,13 +236,19 @@ namespace CMLeonOS.Gui.Apps
string extension = Path.GetExtension(path).ToLower(); string extension = Path.GetExtension(path).ToLower();
if (extension == ".txt" || extension == ".md") if (extension == ".txt" || extension == ".md")
{ {
var notepad = new Notepad(path); ProcessManager.AddProcess(this, new Notepad(path)).Start();
var metadata = AppManager.GetAppMetadata("Notepad"); }
metadata.Start(); else if (extension == ".lua")
{
ProcessManager.AddProcess(this, new CodeStudio.CodeStudio(path)).Start();
}
else if (extension == ".bmp")
{
ProcessManager.AddProcess(this, new ImageViewer(path)).Start();
} }
else else
{ {
Logger.Logger.Instance.Warning("Files", $"Cannot open file: {path}"); ShowOpenWithPrompt(path);
} }
} }
catch (Exception ex) catch (Exception ex)
@@ -253,6 +259,75 @@ namespace CMLeonOS.Gui.Apps
} }
} }
private void OpenWithApp(string path, string appName)
{
switch (appName)
{
case "Notepad":
ProcessManager.AddProcess(this, new Notepad(path)).Start();
break;
case "CodeStudio":
ProcessManager.AddProcess(this, new CodeStudio.CodeStudio(path)).Start();
break;
case "Image Viewer":
ProcessManager.AddProcess(this, new ImageViewer(path)).Start();
break;
default:
Logger.Logger.Instance.Warning("Files", $"Unsupported open-with app: {appName}");
break;
}
}
private void ShowOpenWithPrompt(string path)
{
AppWindow openWithWindow = new AppWindow(this, 320, 240, 300, 176);
openWithWindow.Title = "Open With";
openWithWindow.Icon = AppManager.DefaultAppIcon;
wm.AddWindow(openWithWindow);
openWithWindow.Clear(UITheme.Surface);
openWithWindow.DrawRectangle(0, 0, openWithWindow.Width, openWithWindow.Height, UITheme.SurfaceBorder);
openWithWindow.DrawString("Open this file with:", UITheme.TextPrimary, 12, 12);
openWithWindow.DrawString(Path.GetFileName(path), UITheme.TextSecondary, 12, 32);
Button notepadButton = new Button(openWithWindow, 12, 68, 84, 24);
notepadButton.Text = "Notepad";
notepadButton.OnClick = (_, _) =>
{
wm.RemoveWindow(openWithWindow);
OpenWithApp(path, "Notepad");
};
wm.AddWindow(notepadButton);
Button codeStudioButton = new Button(openWithWindow, 108, 68, 84, 24);
codeStudioButton.Text = "CodeStudio";
codeStudioButton.OnClick = (_, _) =>
{
wm.RemoveWindow(openWithWindow);
OpenWithApp(path, "CodeStudio");
};
wm.AddWindow(codeStudioButton);
Button imageViewerButton = new Button(openWithWindow, 204, 68, 84, 24);
imageViewerButton.Text = "Image";
imageViewerButton.OnClick = (_, _) =>
{
wm.RemoveWindow(openWithWindow);
OpenWithApp(path, "Image Viewer");
};
wm.AddWindow(imageViewerButton);
Button cancelButton = new Button(openWithWindow, openWithWindow.Width - 80 - 12, openWithWindow.Height - 20 - 12, 80, 20);
cancelButton.Text = "Cancel";
cancelButton.OnClick = (_, _) =>
{
wm.RemoveWindow(openWithWindow);
};
wm.AddWindow(cancelButton);
wm.Update(openWithWindow);
}
private void ShortcutsTableCellSelected(int index) private void ShortcutsTableCellSelected(int index)
{ {
if (index != -1) if (index != -1)

View File

@@ -294,10 +294,84 @@ namespace CMLeonOS.Gui.Apps
currentCategoryWindow = users; currentCategoryWindow = users;
users.DrawString("Users", Color.DarkBlue, 12, 12); users.DrawString("Users", Color.DarkBlue, 12, 12);
users.DrawImageAlpha(Icons.Icon_Info, 12, window.Height - 16 - 12); users.DrawImageAlpha(Icons.Icon_Info, 12, window.Height - 16 - 12);
users.DrawString("Double-click on a user for info.", Color.Gray, 36, window.Height - 16 - 12); users.DrawString("Double-click a user for details.", Color.Gray, 36, window.Height - 16 - 12);
wm.AddWindow(users); wm.AddWindow(users);
Table usersTable = new Table(users, 12, 40, users.Width - 24, users.Height - 40 - 12 - 16 - 12); bool canManageUsers = UserSystem.CurrentLoggedInUser != null && UserSystem.CurrentLoggedInUser.Admin;
Table usersTable = null;
if (canManageUsers)
{
Button createUser = new Button(users, 12, 40, 108, 22);
createUser.Text = "Create User";
createUser.OnClick = (_, _) =>
{
PromptBox promptBox = new PromptBox(this, "Create User", "Enter username and password.\nFormat: username password", "newuser password", (string value) =>
{
string[] parts = (value ?? string.Empty).Trim().Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
if (parts.Length < 2)
{
ShowMessage("Users", "Use the format: username password");
return;
}
bool created = UserManager.AddUser(parts[0], parts[1], false);
if (!created)
{
ShowMessage("Users", "Failed to create user.");
return;
}
ShowMessage("Users", $"User '{parts[0]}' created.");
ShowUsersCategory();
});
promptBox.Show();
};
wm.AddWindow(createUser);
Button deleteUser = new Button(users, 130, 40, 108, 22);
deleteUser.Text = "Delete User";
deleteUser.OnClick = (_, _) =>
{
if (usersTable.SelectedCellIndex == -1)
{
ShowMessage("Users", "Select a user first.");
return;
}
User selectedUser = (User)usersTable.Cells[usersTable.SelectedCellIndex].Tag;
if (selectedUser == null)
{
ShowMessage("Users", "Select a valid user first.");
return;
}
if (UserSystem.CurrentLoggedInUser != null
&& selectedUser.Username.Equals(UserSystem.CurrentLoggedInUser.Username, StringComparison.OrdinalIgnoreCase))
{
ShowMessage("Users", "You cannot delete the current logged in user.");
return;
}
bool deleted = UserManager.RemoveUser(selectedUser.Username);
if (!deleted)
{
ShowMessage("Users", "Failed to delete user.");
return;
}
ShowMessage("Users", $"User '{selectedUser.Username}' deleted.");
ShowUsersCategory();
};
wm.AddWindow(deleteUser);
}
else
{
users.DrawString("Only administrators can create or delete users.", Color.Gray, 12, 44);
}
int tableY = canManageUsers ? 74 : 40;
usersTable = new Table(users, 12, tableY, users.Width - 24, users.Height - tableY - 12 - 16 - 12);
foreach (User user in UserSystem.GetUsers()) foreach (User user in UserSystem.GetUsers())
{ {
usersTable.Cells.Add(new TableCell(user.Admin ? Icons.Icon_Admin : Icons.Icon_User, user.Username, tag: user)); usersTable.Cells.Add(new TableCell(user.Admin ? Icons.Icon_Admin : Icons.Icon_User, user.Username, tag: user));