Code cleanup

This commit is contained in:
Stone_Red
2026-06-08 21:30:40 +02:00
parent 124b5368b1
commit 145055b77e
25 changed files with 232 additions and 192 deletions
+42 -23
View File
@@ -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);
+31 -32
View File
@@ -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);
}
}