using System; using System.Collections.Generic; using System.Linq; namespace CMLeonOS.UI { public delegate void ButtonClickHandler(); public class Button { public Rect Bounds { get; set; } public string Text { get; set; } public ConsoleColor ForegroundColor { get; set; } = ConsoleColor.White; public ConsoleColor BackgroundColor { get; set; } = ConsoleColor.DarkBlue; public ConsoleColor BorderColor { get; set; } = ConsoleColor.White; public bool IsEnabled { get; set; } = true; public bool IsFocused { get; set; } = false; public ButtonClickHandler OnClick { get; set; } public Button(Rect bounds, string text) { Bounds = bounds; Text = text; } public Button(Rect bounds, string text, ConsoleColor foregroundColor, ConsoleColor backgroundColor, ConsoleColor borderColor) { Bounds = bounds; Text = text; ForegroundColor = foregroundColor; BackgroundColor = backgroundColor; BorderColor = borderColor; } public void Render() { if (!IsEnabled) { TUIHelper.SetColors(ConsoleColor.DarkGray, ConsoleColor.Black); TUIHelper.DrawBox(Bounds, Text, ConsoleColor.DarkGray); return; } if (IsFocused) { TUIHelper.SetColors(ForegroundColor, BackgroundColor); TUIHelper.DrawBox(Bounds, Text, BorderColor); } else { TUIHelper.SetColors(ConsoleColor.Gray, ConsoleColor.Black); TUIHelper.DrawBox(Bounds, Text, ConsoleColor.DarkGray); } } public bool HandleKey(ConsoleKeyInfo key) { if (!IsEnabled) return false; if (key.Key == ConsoleKey.Enter || key.Key == ConsoleKey.Spacebar) { OnClick?.Invoke(); return true; } return false; } } public class InputBox { public Rect Bounds { get; set; } public string Label { get; set; } public string Value { get; set; } = string.Empty; public string Placeholder { get; set; } = string.Empty; public int MaxLength { get; set; } = 50; public bool IsPassword { get; set; } = false; public bool IsFocused { get; set; } = false; public ConsoleColor ForegroundColor { get; set; } = ConsoleColor.White; public ConsoleColor BackgroundColor { get; set; } = ConsoleColor.Black; public ConsoleColor BorderColor { get; set; } = ConsoleColor.White; public InputBox(Rect bounds, string label, string placeholder, int maxLength, bool isPassword, ConsoleColor foregroundColor, ConsoleColor backgroundColor, ConsoleColor borderColor) { Bounds = bounds; Label = label; Placeholder = placeholder; MaxLength = maxLength; IsPassword = isPassword; ForegroundColor = foregroundColor; BackgroundColor = backgroundColor; BorderColor = borderColor; } public void Render() { TUIHelper.SetColors(ForegroundColor, BackgroundColor); TUIHelper.DrawBox(Bounds, Label, BorderColor); int inputY = Bounds.Top + 1; string displayValue = IsPassword ? new string('*', Value.Length) : Value; string displayText = string.IsNullOrEmpty(Value) ? Placeholder : displayValue; Console.SetCursorPosition(Bounds.Left + 1, inputY); Console.Write(TUIHelper.PadText(displayText, Bounds.Width - 2)); if (IsFocused) { TUIHelper.SaveCursor(); Console.SetCursorPosition(Bounds.Left + 1 + displayText.Length, inputY); TUIHelper.ShowCursor(); } } public bool HandleKey(ConsoleKeyInfo key) { if (!IsFocused) return false; if (key.Key == ConsoleKey.Backspace) { if (Value.Length > 0) { Value = Value.Substring(0, Value.Length - 1); } return true; } if (key.Key == ConsoleKey.Enter) { return true; } if (char.IsLetterOrDigit(key.KeyChar)) { if (Value.Length < MaxLength) { Value += key.KeyChar; } return true; } return false; } } public class Dialog { public Rect Bounds { get; set; } public string Title { get; set; } public string Message { get; set; } public List