Add printLine support for commands and implement basic UI controls rendering

This commit is contained in:
Stone_Red
2026-06-08 19:51:22 +02:00
parent fc71af028f
commit 8b900c9d58
14 changed files with 178 additions and 29 deletions
+2 -2
View File
@@ -41,7 +41,7 @@ public class Kernel : Sys.Kernel
for (int i = 0; i < 5; i++)
{
ProcessManager.SpawnProcess<TestProcess>();
//ProcessManager.SpawnProcess<TestProcess>();
}
Console.WriteLine(CosmosFeatures.MouseEnabled);
@@ -66,7 +66,7 @@ public class Kernel : Sys.Kernel
return;
}
if (CommandManager.TryExecute(input))
if (CommandManager.TryExecute(input, line => Console.WriteLine(line)))
{
return;
}
+28
View File
@@ -36,6 +36,34 @@ public class DesktopProcess : Process
// Start the terminal process within the desktop environment
ProcessManager.SpawnProcess<TerminalProcess>();
// Create a test window for new UI controls
Window testWindow = WindowManager.CreateWindow(this, "UI Controls Test", new System.Drawing.Point(500, 50), new System.Drawing.Size(200, 180));
testWindow.CreateUIElement<RemSox.UI.GUI.UIEelements.Controls.Button>(b =>
{
b.Position = new System.Drawing.Point(20, 30);
b.Size = new System.Drawing.Size(100, 30);
b.Text = "Click Me";
b.BackgroundColor = System.Drawing.Color.LightBlue;
});
testWindow.CreateUIElement<RemSox.UI.GUI.UIEelements.Controls.CheckBox>(c =>
{
c.Position = new System.Drawing.Point(20, 80);
c.Text = "Check Me";
c.IsChecked = true;
});
testWindow.CreateUIElement<RemSox.UI.GUI.UIEelements.Shapes.Line>(l =>
{
l.Position = new System.Drawing.Point(20, 130);
l.EndPosition = new System.Drawing.Point(180, 130);
l.Color = System.Drawing.Color.Red;
});
testWindow.AutoFlush = true;
testWindow.Flush();
while (!StopRequested)
{
WindowManager.Update();
+1 -5
View File
@@ -77,15 +77,11 @@ public class TerminalProcess : Process
}
else
{
bool found = CommandManager.TryExecute(cmd);
bool found = CommandManager.TryExecute(cmd, line => PrintLine(line));
if (!found)
{
PrintLine($"\"{cmd}\" is not a command");
}
else
{
PrintLine("Command executed. (Output sent to background console)");
}
}
}
UpdateDisplay();
+2 -2
View File
@@ -24,7 +24,7 @@ public static class CommandManager
return commands.Values.OrderBy(command => command.Name);
}
public static bool TryExecute(string input)
public static bool TryExecute(string input, Action<string> printLine)
{
string trimmedInput = input.Trim();
@@ -51,7 +51,7 @@ public static class CommandManager
arguments = trimmedInput.Substring(commandName.Length).TrimStart();
}
entry.Value.Execute(arguments);
entry.Value.Execute(arguments, printLine);
return true;
}
+1 -1
View File
@@ -8,7 +8,7 @@ public sealed class ClearCommand : ICommand
public string Description => "Clear the screen";
public void Execute(string? arguments)
public void Execute(string? arguments, Action<string> printLine)
{
Console.Clear();
}
+2 -2
View File
@@ -8,9 +8,9 @@ public sealed class HaltCommand : ICommand
public string Description => "Halt the system";
public void Execute(string? arguments)
public void Execute(string? arguments, Action<string> printLine)
{
Console.WriteLine("Halting system...");
printLine("Halting system...");
Environment.Exit(0);
}
}
+3 -3
View File
@@ -8,13 +8,13 @@ public sealed class HelpCommand : ICommand
public string Description => "Show this help message";
public void Execute(string? arguments)
public void Execute(string? arguments, Action<string> printLine)
{
Console.WriteLine("Available commands:");
printLine("Available commands:");
foreach (ICommand command in CommandManager.GetCommands())
{
Console.WriteLine($" {command.Name,-12} - {command.Description}");
printLine($" {command.Name,-12} - {command.Description}");
}
}
}
+9 -9
View File
@@ -9,10 +9,10 @@ public sealed class SpawnTestProcessCommand : ICommand
public string Description => "Spawn the test process";
public void Execute(string? arguments)
public void Execute(string? arguments, Action<string> printLine)
{
int processId = ProcessManager.SpawnProcess<TestProcess>();
Console.WriteLine($"Spawned TestProcess with ID {processId}");
printLine($"Spawned TestProcess with ID {processId}");
}
}
@@ -22,15 +22,15 @@ public sealed class ListProcessesCommand : ICommand
public string Description => "List running processes";
public void Execute(string? arguments)
public void Execute(string? arguments, Action<string> printLine)
{
IEnumerable<Process> processes = ProcessManager.GetAllProcesses();
Console.WriteLine("Running processes:");
printLine("Running processes:");
foreach (Process process in processes)
{
Console.WriteLine($" ID: {process.Id}, Name: {process.Name}");
printLine($" ID: {process.Id}, Name: {process.Name}");
}
}
}
@@ -41,23 +41,23 @@ public sealed class StopProcessCommand : ICommand
public string Description => "Stop a process by ID";
public void Execute(string? arguments)
public void Execute(string? arguments, Action<string> printLine)
{
string? idText = arguments;
if (string.IsNullOrWhiteSpace(idText))
{
Console.WriteLine("Usage: stop <process-id>");
printLine("Usage: stop <process-id>");
return;
}
if (int.TryParse(idText, out int processId))
{
ProcessManager.StopProcess(processId);
Console.WriteLine($"Stopped process with ID {processId}");
printLine($"Stopped process with ID {processId}");
return;
}
Console.WriteLine("Invalid process ID");
printLine("Invalid process ID");
}
}
+3 -1
View File
@@ -1,10 +1,12 @@
namespace RemSox.UI.GUI.CLI;
using System;
public interface ICommand
{
string Name { get; }
string Description { get; }
void Execute(string? arguments);
void Execute(string? arguments, Action<string> printLine);
}
+2 -2
View File
@@ -10,9 +10,9 @@ public class StartGuiCommand : ICommand
public string Name => "start-gui";
public string Description => "Starts the Graphical User Interface (Desktop Process)";
public void Execute(string? args)
public void Execute(string? arguments, Action<string> printLine)
{
Console.WriteLine("Starting Desktop Process...");
printLine("Starting Desktop Process...");
ProcessManager.SpawnProcess<DesktopProcess>();
}
}
+71 -2
View File
@@ -90,6 +90,21 @@ public sealed class CanvasRenderSource : IRenderSource
{
RenderText(windowCanvas, command);
}
if (command.ElementType == "Line")
{
RenderLine(windowCanvas, command);
}
if (command.ElementType == "Button")
{
RenderButton(windowCanvas, command);
}
if (command.ElementType == "CheckBox")
{
RenderCheckBox(windowCanvas, command);
}
}
if (changed)
@@ -111,7 +126,7 @@ public sealed class CanvasRenderSource : IRenderSource
isZOrderDirty = false;
}
//screenCanvas.Clear(Color.Black);
screenCanvas.Clear(Color.Black);
foreach (var windowId in orderedWindowsCache)
{
@@ -195,7 +210,61 @@ public sealed class CanvasRenderSource : IRenderSource
if (!string.IsNullOrEmpty(content))
{
canvas.DrawString(content, Cosmos.Kernel.System.Graphics.Fonts.PCScreenFont.DefaultFont, color, command.Position.X, command.Position.Y);
canvas.DrawString(content, PCScreenFont.DefaultFont, color, command.Position.X, command.Position.Y);
}
}
private static void RenderLine(Canvas canvas, RenderCommand command)
{
Color color = command.Properties.TryGetValue("Color", out object? rawColor) && rawColor is Color lineColor
? lineColor
: Color.White;
Point endPosition = command.Properties.TryGetValue("EndPosition", out object? rawEnd) && rawEnd is Point endPoint
? endPoint
: command.Position;
canvas.DrawLine(color, command.Position.X, command.Position.Y, endPosition.X, endPosition.Y);
}
private static void RenderButton(Canvas canvas, RenderCommand command)
{
Color bgColor = command.Properties.TryGetValue("BackgroundColor", out object? rawBgColor) && rawBgColor is Color c1 ? c1 : Color.LightGray;
Color textColor = command.Properties.TryGetValue("TextColor", out object? rawTextColor) && rawTextColor is Color c2 ? c2 : Color.Black;
Size size = command.Properties.TryGetValue("Size", out object? rawSize) && rawSize is Size s ? s : new Size(60, 20);
string text = command.Properties.TryGetValue("Text", out object? rawText) && rawText is string t ? t : string.Empty;
canvas.DrawFilledRectangle(bgColor, command.Position.X, command.Position.Y, size.Width, size.Height);
canvas.DrawRectangle(Color.DarkGray, command.Position.X, command.Position.Y, size.Width, size.Height);
if (!string.IsNullOrEmpty(text))
{
int textX = command.Position.X + (size.Width / 2) - (text.Length * 8 / 2);
int textY = command.Position.Y + (size.Height / 2) - 8;
canvas.DrawString(text, PCScreenFont.DefaultFont, textColor, textX, textY);
}
}
private static void RenderCheckBox(Canvas canvas, RenderCommand command)
{
Color bgColor = command.Properties.TryGetValue("BackgroundColor", out object? rawBgColor) && rawBgColor is Color c1 ? c1 : Color.LightGray;
Color textColor = command.Properties.TryGetValue("TextColor", out object? rawTextColor) && rawTextColor is Color c2 ? c2 : Color.White;
bool isChecked = command.Properties.TryGetValue("IsChecked", out object? rawChecked) && rawChecked is bool chk ? chk : false;
string text = command.Properties.TryGetValue("Text", out object? rawText) && rawText is string t ? t : string.Empty;
int boxSize = 12;
canvas.DrawFilledRectangle(bgColor, command.Position.X, command.Position.Y, boxSize, boxSize);
canvas.DrawRectangle(Color.DarkGray, command.Position.X, command.Position.Y, boxSize, boxSize);
if (isChecked)
{
canvas.DrawFilledRectangle(Color.Black, command.Position.X + 3, command.Position.Y + 3, boxSize - 6, boxSize - 6);
}
if (!string.IsNullOrEmpty(text))
{
canvas.DrawString(text, PCScreenFont.DefaultFont, textColor, command.Position.X + boxSize + 5, command.Position.Y - 2);
}
}
}
+18
View File
@@ -0,0 +1,18 @@
using System.Drawing;
namespace RemSox.UI.GUI.UIEelements.Controls;
public class Button() : Control("Button")
{
public string Text
{
get;
set => SetProperty(nameof(Text), ref field, value);
} = string.Empty;
public Color TextColor
{
get;
set => SetProperty(nameof(TextColor), ref field, value);
} = Color.Black;
}
+24
View File
@@ -0,0 +1,24 @@
using System.Drawing;
namespace RemSox.UI.GUI.UIEelements.Controls;
public class CheckBox() : Control("CheckBox")
{
public bool IsChecked
{
get;
set => SetProperty(nameof(IsChecked), ref field, value);
} = false;
public string Text
{
get;
set => SetProperty(nameof(Text), ref field, value);
} = string.Empty;
public Color TextColor
{
get;
set => SetProperty(nameof(TextColor), ref field, value);
} = Color.White;
}
+12
View File
@@ -0,0 +1,12 @@
using System.Drawing;
namespace RemSox.UI.GUI.UIEelements.Shapes;
public class Line() : Shape("Line")
{
public Point EndPosition
{
get;
set => SetProperty(nameof(EndPosition), ref field, value);
}
}