mirror of
https://github.com/Stone-Red-Code/RemSox.git
synced 2026-09-04 09:06:23 +02:00
Code cleanup
This commit is contained in:
@@ -1,6 +1,4 @@
|
||||
using System;
|
||||
|
||||
namespace RemSox.UI.GUI.CLI;
|
||||
namespace RemSox.UI.CLI;
|
||||
|
||||
public static class CommandManager
|
||||
{
|
||||
@@ -48,7 +46,7 @@ public static class CommandManager
|
||||
|
||||
if (trimmedInput.Length > commandName.Length)
|
||||
{
|
||||
arguments = trimmedInput.Substring(commandName.Length).TrimStart();
|
||||
arguments = trimmedInput[commandName.Length..].TrimStart();
|
||||
}
|
||||
|
||||
entry.Value.Execute(arguments, printLine);
|
||||
|
||||
@@ -1,6 +1,4 @@
|
||||
using System;
|
||||
|
||||
namespace RemSox.UI.GUI.CLI.Commands;
|
||||
namespace RemSox.UI.CLI.Commands;
|
||||
|
||||
public sealed class ClearCommand : ICommand
|
||||
{
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
using System;
|
||||
using RemSox.UI.CLI;
|
||||
|
||||
namespace RemSox.UI.GUI.CLI.Commands;
|
||||
namespace RemSox.UI.CLI.Commands;
|
||||
|
||||
public sealed class HaltCommand : ICommand
|
||||
{
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
using System;
|
||||
using RemSox.UI.CLI;
|
||||
|
||||
namespace RemSox.UI.GUI.CLI.Commands;
|
||||
namespace RemSox.UI.CLI.Commands;
|
||||
|
||||
public sealed class HelpCommand : ICommand
|
||||
{
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
using System;
|
||||
using RemSox.Processing;
|
||||
using RemSox.UI.CLI;
|
||||
|
||||
namespace RemSox.UI.GUI.CLI.Commands;
|
||||
namespace RemSox.UI.CLI.Commands;
|
||||
|
||||
public sealed class SpawnTestProcessCommand : ICommand
|
||||
{
|
||||
|
||||
@@ -1,9 +1,8 @@
|
||||
using System;
|
||||
using RemSox.Processing;
|
||||
using RemSox.Processes;
|
||||
using RemSox.UI.GUI.CLI;
|
||||
using RemSox.Processing;
|
||||
using RemSox.UI.CLI;
|
||||
|
||||
namespace RemSox.UI.GUI.CLI.Commands;
|
||||
namespace RemSox.UI.CLI.Commands;
|
||||
|
||||
public class StartGuiCommand : ICommand
|
||||
{
|
||||
@@ -13,6 +12,6 @@ public class StartGuiCommand : ICommand
|
||||
public void Execute(string? arguments, Action<string> printLine)
|
||||
{
|
||||
printLine("Starting Desktop Process...");
|
||||
ProcessManager.SpawnProcess<DesktopProcess>();
|
||||
_ = ProcessManager.SpawnProcess<DesktopProcess>();
|
||||
}
|
||||
}
|
||||
|
||||
+1
-4
@@ -1,7 +1,4 @@
|
||||
namespace RemSox.UI.GUI.CLI;
|
||||
|
||||
using System;
|
||||
|
||||
namespace RemSox.UI.CLI;
|
||||
/// <summary>
|
||||
/// Defines a command executable via the CLI or GUI terminal.
|
||||
/// </summary>
|
||||
|
||||
@@ -1,23 +1,21 @@
|
||||
using System;
|
||||
using System.Drawing;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Cosmos.Kernel.System.Graphics;
|
||||
using Cosmos.Kernel.System.Graphics.Fonts;
|
||||
|
||||
using System.Drawing;
|
||||
|
||||
namespace RemSox.UI.GUI.Rendering;
|
||||
|
||||
public sealed class CanvasRenderSource : IRenderSource
|
||||
{
|
||||
private static readonly Dictionary<int, Canvas> windowCanvases = new();
|
||||
private static readonly Dictionary<int, Point> windowPositions = new();
|
||||
private static readonly Dictionary<int, int> windowZIndices = new();
|
||||
private static readonly Dictionary<int, Canvas> windowCanvases = [];
|
||||
private static readonly Dictionary<int, Point> windowPositions = [];
|
||||
private static readonly Dictionary<int, int> windowZIndices = [];
|
||||
|
||||
private static bool isDirty = true;
|
||||
private static Point lastPointerPosition = new Point(-1, -1);
|
||||
private static List<int> orderedWindowsCache = new();
|
||||
private static Point lastPointerPosition = new(-1, -1);
|
||||
private static List<int> orderedWindowsCache = [];
|
||||
private static bool isZOrderDirty = true;
|
||||
private static readonly object renderLock = new object();
|
||||
private static readonly Lock renderLock = new();
|
||||
|
||||
public void Render(IEnumerable<RenderCommand> commands)
|
||||
{
|
||||
@@ -29,14 +27,14 @@ public sealed class CanvasRenderSource : IRenderSource
|
||||
changed = true;
|
||||
if (command.ElementType == "WindowClose")
|
||||
{
|
||||
windowCanvases.Remove(command.WindowId);
|
||||
windowPositions.Remove(command.WindowId);
|
||||
windowZIndices.Remove(command.WindowId);
|
||||
_ = windowCanvases.Remove(command.WindowId);
|
||||
_ = windowPositions.Remove(command.WindowId);
|
||||
_ = windowZIndices.Remove(command.WindowId);
|
||||
isZOrderDirty = true;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (command.ElementType == "Window" || command.ElementType == "WindowMove")
|
||||
if (command.ElementType is "Window" or "WindowMove")
|
||||
{
|
||||
if (command.Properties.TryGetValue("ZIndex", out object? rawZIndex) && rawZIndex is int z)
|
||||
{
|
||||
@@ -134,7 +132,7 @@ public sealed class CanvasRenderSource : IRenderSource
|
||||
|
||||
screenCanvas.Clear(Color.Black);
|
||||
|
||||
foreach (var windowId in orderedWindowsCache)
|
||||
foreach (int windowId in orderedWindowsCache)
|
||||
{
|
||||
if (windowPositions.TryGetValue(windowId, out Point position) && windowCanvases.TryGetValue(windowId, out Canvas? windowCanvas))
|
||||
{
|
||||
@@ -256,7 +254,7 @@ public sealed class CanvasRenderSource : IRenderSource
|
||||
{
|
||||
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;
|
||||
bool isChecked = command.Properties.TryGetValue("IsChecked", out object? rawChecked) && rawChecked is bool chk && chk;
|
||||
string text = command.Properties.TryGetValue("Text", out object? rawText) && rawText is string t ? t : string.Empty;
|
||||
|
||||
int boxSize = 12;
|
||||
|
||||
@@ -2,5 +2,5 @@ namespace RemSox.UI.GUI.Rendering;
|
||||
|
||||
public interface IRenderSource
|
||||
{
|
||||
public void Render(IEnumerable<RenderCommand> commands);
|
||||
void Render(IEnumerable<RenderCommand> commands);
|
||||
}
|
||||
@@ -1,4 +1,3 @@
|
||||
using System;
|
||||
using System.Drawing;
|
||||
|
||||
namespace RemSox.UI.GUI.Rendering;
|
||||
|
||||
@@ -1,4 +1,3 @@
|
||||
using System;
|
||||
using System.Drawing;
|
||||
|
||||
namespace RemSox.UI.GUI.UIEelements;
|
||||
|
||||
@@ -1,5 +1,3 @@
|
||||
using System.Drawing;
|
||||
|
||||
namespace RemSox.UI.GUI.UIEelements.Shapes;
|
||||
|
||||
public class Circle() : Shape("Circle")
|
||||
|
||||
@@ -1,10 +1,9 @@
|
||||
namespace RemSox.UI.GUI.UIEelements;
|
||||
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel;
|
||||
using System.Drawing;
|
||||
using RemSox.Utils;
|
||||
|
||||
using System.Drawing;
|
||||
|
||||
namespace RemSox.UI.GUI.UIEelements;
|
||||
|
||||
public abstract class UIElement(string type) : ChangedPropertiesTracker
|
||||
{
|
||||
public int Id { get; init; }
|
||||
|
||||
+42
-23
@@ -1,9 +1,8 @@
|
||||
using System;
|
||||
using System.Drawing;
|
||||
using System.Reflection;
|
||||
using RemSox.UI.GUI.Rendering;
|
||||
using RemSox.UI.GUI.UIEelements;
|
||||
|
||||
using System.Drawing;
|
||||
|
||||
namespace RemSox.UI.GUI.Windows;
|
||||
|
||||
/// <summary>
|
||||
@@ -38,11 +37,7 @@ public sealed class Window(string title, int processId, int id, IRenderSource re
|
||||
/// <summary> Gets or sets whether this window is currently focused. </summary>
|
||||
public bool IsFocused
|
||||
{
|
||||
get => WindowManager.IsWindowFocused(this);
|
||||
set
|
||||
{
|
||||
WindowManager.FocusWindow(value ? this : null);
|
||||
}
|
||||
get => WindowManager.IsWindowFocused(this); set => WindowManager.FocusWindow(value ? this : null);
|
||||
}
|
||||
|
||||
/// <summary> Gets or sets whether the window is visible. </summary>
|
||||
@@ -63,7 +58,7 @@ public sealed class Window(string title, int processId, int id, IRenderSource re
|
||||
/// <summary> Gets whether the window is currently being dragged. </summary>
|
||||
public bool IsDragging => currentInteraction == InteractionMode.Drag;
|
||||
|
||||
private readonly object uiElementsLock = new object();
|
||||
private readonly Lock uiElementsLock = new();
|
||||
private readonly Dictionary<int, UIElement> uiElements = [];
|
||||
|
||||
private int nextUIElementId = 1;
|
||||
@@ -74,8 +69,8 @@ public sealed class Window(string title, int processId, int id, IRenderSource re
|
||||
private Point interactionStartPointer;
|
||||
private Point dragOffset;
|
||||
|
||||
private Point lastRenderedPosition = new Point(-1, -1);
|
||||
private Size lastRenderedSize = new Size(-1, -1);
|
||||
private Point lastRenderedPosition = new(-1, -1);
|
||||
private Size lastRenderedSize = new(-1, -1);
|
||||
private bool lastRenderedIsFocused = false;
|
||||
private string lastRenderedTitle = string.Empty;
|
||||
private int lastRenderedZIndex = -1;
|
||||
@@ -235,14 +230,38 @@ public sealed class Window(string title, int processId, int id, IRenderSource re
|
||||
|
||||
if (IsResizable)
|
||||
{
|
||||
if (onTop && onLeft) currentInteraction = InteractionMode.ResizeTopLeft;
|
||||
else if (onTop && onRight) currentInteraction = InteractionMode.ResizeTopRight;
|
||||
else if (onBottom && onLeft) currentInteraction = InteractionMode.ResizeBottomLeft;
|
||||
else if (onBottom && onRight) currentInteraction = InteractionMode.ResizeBottomRight;
|
||||
else if (onLeft && inBounds) currentInteraction = InteractionMode.ResizeLeft;
|
||||
else if (onRight && inBounds) currentInteraction = InteractionMode.ResizeRight;
|
||||
else if (onTop && inBounds) currentInteraction = InteractionMode.ResizeTop;
|
||||
else if (onBottom && inBounds) currentInteraction = InteractionMode.ResizeBottom;
|
||||
if (onTop && onLeft)
|
||||
{
|
||||
currentInteraction = InteractionMode.ResizeTopLeft;
|
||||
}
|
||||
else if (onTop && onRight)
|
||||
{
|
||||
currentInteraction = InteractionMode.ResizeTopRight;
|
||||
}
|
||||
else if (onBottom && onLeft)
|
||||
{
|
||||
currentInteraction = InteractionMode.ResizeBottomLeft;
|
||||
}
|
||||
else if (onBottom && onRight)
|
||||
{
|
||||
currentInteraction = InteractionMode.ResizeBottomRight;
|
||||
}
|
||||
else if (onLeft && inBounds)
|
||||
{
|
||||
currentInteraction = InteractionMode.ResizeLeft;
|
||||
}
|
||||
else if (onRight && inBounds)
|
||||
{
|
||||
currentInteraction = InteractionMode.ResizeRight;
|
||||
}
|
||||
else if (onTop && inBounds)
|
||||
{
|
||||
currentInteraction = InteractionMode.ResizeTop;
|
||||
}
|
||||
else if (onBottom && inBounds)
|
||||
{
|
||||
currentInteraction = InteractionMode.ResizeBottom;
|
||||
}
|
||||
}
|
||||
|
||||
if (currentInteraction == InteractionMode.None && IsDraggable && IsPointInTitleBar(pointerPosition))
|
||||
@@ -297,22 +316,22 @@ public sealed class Window(string title, int processId, int id, IRenderSource re
|
||||
const int minWidth = 100;
|
||||
const int minHeight = 50;
|
||||
|
||||
if (currentInteraction == InteractionMode.ResizeRight || currentInteraction == InteractionMode.ResizeBottomRight || currentInteraction == InteractionMode.ResizeTopRight)
|
||||
if (currentInteraction is InteractionMode.ResizeRight or InteractionMode.ResizeBottomRight or InteractionMode.ResizeTopRight)
|
||||
{
|
||||
newW = Math.Max(minWidth, interactionStartBounds.Width + dx);
|
||||
}
|
||||
if (currentInteraction == InteractionMode.ResizeBottom || currentInteraction == InteractionMode.ResizeBottomRight || currentInteraction == InteractionMode.ResizeBottomLeft)
|
||||
if (currentInteraction is InteractionMode.ResizeBottom or InteractionMode.ResizeBottomRight or InteractionMode.ResizeBottomLeft)
|
||||
{
|
||||
newH = Math.Max(minHeight, interactionStartBounds.Height + dy);
|
||||
}
|
||||
if (currentInteraction == InteractionMode.ResizeLeft || currentInteraction == InteractionMode.ResizeBottomLeft || currentInteraction == InteractionMode.ResizeTopLeft)
|
||||
if (currentInteraction is InteractionMode.ResizeLeft or InteractionMode.ResizeBottomLeft or InteractionMode.ResizeTopLeft)
|
||||
{
|
||||
int maxDx = interactionStartBounds.Width - minWidth;
|
||||
int clampedDx = Math.Min(dx, maxDx);
|
||||
newX = Math.Max(0, interactionStartBounds.X + clampedDx);
|
||||
newW = interactionStartBounds.Width - clampedDx;
|
||||
}
|
||||
if (currentInteraction == InteractionMode.ResizeTop || currentInteraction == InteractionMode.ResizeTopLeft || currentInteraction == InteractionMode.ResizeTopRight)
|
||||
if (currentInteraction is InteractionMode.ResizeTop or InteractionMode.ResizeTopLeft or InteractionMode.ResizeTopRight)
|
||||
{
|
||||
int maxDy = interactionStartBounds.Height - minHeight;
|
||||
int clampedDy = Math.Min(dy, maxDy);
|
||||
|
||||
@@ -1,14 +1,12 @@
|
||||
using System;
|
||||
using System.Collections.Concurrent;
|
||||
using System.Collections.Generic;
|
||||
using System.Drawing;
|
||||
using System.Linq;
|
||||
using Cosmos.Kernel.System.Graphics;
|
||||
using Cosmos.Kernel.System.Mouse;
|
||||
using Cosmos.Kernel.System.Keyboard;
|
||||
using Cosmos.Kernel.System.Mouse;
|
||||
|
||||
using RemSox.Processing;
|
||||
using RemSox.UI.GUI.Rendering;
|
||||
|
||||
using System.Drawing;
|
||||
|
||||
namespace RemSox.UI.GUI.Windows;
|
||||
|
||||
/// <summary>
|
||||
@@ -16,9 +14,9 @@ namespace RemSox.UI.GUI.Windows;
|
||||
/// </summary>
|
||||
public static class WindowManager
|
||||
{
|
||||
private static readonly object windowsLock = new object();
|
||||
private static readonly Lock windowsLock = new();
|
||||
// Process ID to list of windows
|
||||
private static readonly Dictionary<int, List<Window>> windows = new();
|
||||
private static readonly Dictionary<int, List<Window>> windows = [];
|
||||
|
||||
private static int nextWindowId = 1;
|
||||
private static int nextZIndex = 1;
|
||||
@@ -28,7 +26,6 @@ public static class WindowManager
|
||||
private static Window? activeInteractWindow = null;
|
||||
private static Point lastPointerPosition = Point.Empty;
|
||||
private static bool wasLeftButtonDown = false;
|
||||
private static int mousePollCounter = 0;
|
||||
|
||||
private static readonly MuliRenderSource renderSource = new([]);
|
||||
|
||||
@@ -37,10 +34,9 @@ public static class WindowManager
|
||||
/// </summary>
|
||||
public static void Update()
|
||||
{
|
||||
mousePollCounter++;
|
||||
MouseManager.Poll();
|
||||
|
||||
Point pointerPosition = new((int)MouseManager.X, (int)MouseManager.Y);
|
||||
Point pointerPosition = new(MouseManager.X, MouseManager.Y);
|
||||
bool leftButtonDown = MouseManager.LeftButton;
|
||||
|
||||
if (leftButtonDown && !wasLeftButtonDown)
|
||||
@@ -59,7 +55,7 @@ public static class WindowManager
|
||||
|
||||
wasLeftButtonDown = leftButtonDown;
|
||||
|
||||
while (KeyboardManager.TryReadKey(out KeyEvent keyEvent))
|
||||
while (KeyboardManager.TryReadKey(out KeyEvent? keyEvent) && keyEvent is not null)
|
||||
{
|
||||
focusedWindow?.HandleKeyEvent(keyEvent);
|
||||
}
|
||||
@@ -73,12 +69,18 @@ public static class WindowManager
|
||||
/// <summary>
|
||||
/// Adds a new rendering source to the compositor.
|
||||
/// </summary>
|
||||
public static void AddRenderSource(IRenderSource source) => renderSource.AddSource(source);
|
||||
public static void AddRenderSource(IRenderSource source)
|
||||
{
|
||||
renderSource.AddSource(source);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Removes an existing rendering source from the compositor.
|
||||
/// </summary>
|
||||
public static void RemoveRenderSource(IRenderSource source) => renderSource.RemoveSource(source);
|
||||
public static void RemoveRenderSource(IRenderSource source)
|
||||
{
|
||||
renderSource.RemoveSource(source);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates and registers a new window for the specified process.
|
||||
@@ -112,13 +114,13 @@ public static class WindowManager
|
||||
{
|
||||
lock (windowsLock)
|
||||
{
|
||||
if (windows.TryGetValue(window.ProcessId, out var processWindows))
|
||||
if (windows.TryGetValue(window.ProcessId, out List<Window>? processWindows))
|
||||
{
|
||||
processWindows.Remove(window);
|
||||
_ = processWindows.Remove(window);
|
||||
}
|
||||
}
|
||||
|
||||
renderSource.Render(new[] { new RenderCommand { WindowId = window.Id, ElementId = window.Id, ElementType = "WindowClose", Position = window.Position, Properties = new Dictionary<string, object?>() } });
|
||||
renderSource.Render([new RenderCommand { WindowId = window.Id, ElementId = window.Id, ElementType = "WindowClose", Position = window.Position, Properties = new Dictionary<string, object?>() }]);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -128,7 +130,7 @@ public static class WindowManager
|
||||
{
|
||||
lock (windowsLock)
|
||||
{
|
||||
if (windows.TryGetValue(process.Id, out var processWindows))
|
||||
if (windows.TryGetValue(process.Id, out List<Window>? processWindows))
|
||||
{
|
||||
return processWindows.ToList();
|
||||
}
|
||||
@@ -150,20 +152,20 @@ public static class WindowManager
|
||||
/// </summary>
|
||||
public static void CloseWindowsForProcess(int processId)
|
||||
{
|
||||
List<Window> windowsToClose = new();
|
||||
List<Window> windowsToClose = [];
|
||||
lock (windowsLock)
|
||||
{
|
||||
if (windows.TryGetValue(processId, out var processWindows))
|
||||
if (windows.TryGetValue(processId, out List<Window>? processWindows))
|
||||
{
|
||||
windowsToClose.AddRange(processWindows);
|
||||
windows.Remove(processId);
|
||||
_ = windows.Remove(processId);
|
||||
}
|
||||
}
|
||||
|
||||
if (windowsToClose.Count > 0)
|
||||
{
|
||||
List<RenderCommand> closeCommands = new();
|
||||
foreach (var window in windowsToClose)
|
||||
List<RenderCommand> closeCommands = [];
|
||||
foreach (Window window in windowsToClose)
|
||||
{
|
||||
closeCommands.Add(new RenderCommand { WindowId = window.Id, ElementId = window.Id, ElementType = "WindowClose", Position = window.Position, Properties = new Dictionary<string, object?>() });
|
||||
}
|
||||
@@ -181,10 +183,7 @@ public static class WindowManager
|
||||
return;
|
||||
}
|
||||
|
||||
if (window != null)
|
||||
{
|
||||
window.ZIndex = nextZIndex++;
|
||||
}
|
||||
_ = window?.ZIndex = nextZIndex++;
|
||||
|
||||
Window? previousFocusedWindow = focusedWindow;
|
||||
focusedWindow = window;
|
||||
@@ -212,7 +211,7 @@ public static class WindowManager
|
||||
allWindows = windows.Values.SelectMany(w => w).OrderByDescending(w => w.ZIndex).ToList();
|
||||
}
|
||||
|
||||
foreach (var window in allWindows)
|
||||
foreach (Window window in allWindows)
|
||||
{
|
||||
if (window.TryBeginInteract(pointerPosition))
|
||||
{
|
||||
@@ -233,7 +232,7 @@ public static class WindowManager
|
||||
allWindows = windows.Values.SelectMany(w => w).ToList();
|
||||
}
|
||||
|
||||
foreach (var window in allWindows)
|
||||
foreach (Window window in allWindows)
|
||||
{
|
||||
window.Invalidate();
|
||||
}
|
||||
@@ -244,9 +243,9 @@ public static class WindowManager
|
||||
return nextWindowId++;
|
||||
}
|
||||
|
||||
sealed private class MuliRenderSource(List<IRenderSource> sources) : IRenderSource
|
||||
private sealed class MuliRenderSource(List<IRenderSource> sources) : IRenderSource
|
||||
{
|
||||
private readonly object sourcesLock = new object();
|
||||
private readonly Lock sourcesLock = new();
|
||||
|
||||
public void AddSource(IRenderSource source)
|
||||
{
|
||||
@@ -260,7 +259,7 @@ public static class WindowManager
|
||||
{
|
||||
lock (sourcesLock)
|
||||
{
|
||||
sources.Remove(source);
|
||||
_ = sources.Remove(source);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user