From 6e87e44a159b2a2f8d7de2fed7b896ae2a1210ed Mon Sep 17 00:00:00 2001 From: Leonmmcoset Date: Sat, 7 Feb 2026 14:50:39 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E7=99=BB=E5=BD=95=E9=A1=B5=E9=9D=A2UI?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- System/UserSystem.cs | 185 ++++------- UI/Components.cs | 511 +++++++++++++++++++++++++++++++ shell/Commands/TestTUICommand.cs | 315 +++++++++++++++++++ 3 files changed, 893 insertions(+), 118 deletions(-) diff --git a/System/UserSystem.cs b/System/UserSystem.cs index 302ea3f..8b337d0 100644 --- a/System/UserSystem.cs +++ b/System/UserSystem.cs @@ -523,129 +523,78 @@ namespace CMLeonOS public bool Login() { - Console.WriteLine("===================================="); - Console.WriteLine(" System Login"); - Console.WriteLine("===================================="); - // Console.ReadKey(true); - - // 检测ALT+Space按键 - bool useFixMode = false; - ConsoleKeyInfo keyInfo; - try + CMLeonOS.UI.TUIHelper.SetColors(global::System.ConsoleColor.White, global::System.ConsoleColor.Black); + global::System.Console.Clear(); + + var titleBar = new CMLeonOS.UI.Window(new CMLeonOS.UI.Rect(0, 0, 80, 3), "System Login", () => { }, false); + titleBar.Render(); + + var loginBox = new CMLeonOS.UI.Window(new CMLeonOS.UI.Rect(15, 8, 50, 10), "Login", () => { }, true); + loginBox.Render(); + + CMLeonOS.UI.TUIHelper.SetColors(global::System.ConsoleColor.White, global::System.ConsoleColor.Black); + global::System.Console.SetCursorPosition(17, 10); + global::System.Console.Write("Username: "); + string username = global::System.Console.ReadLine(); + + if (string.IsNullOrWhiteSpace(username)) { - if (fixmode == true) { - keyInfo = Console.ReadKey(true); - if (keyInfo.Key == ConsoleKey.Spacebar && (keyInfo.Modifiers & ConsoleModifiers.Alt) != 0) - { - // 检测到ALT+Space,进入修复模式 - useFixMode = true; - Console.WriteLine(); - Console.WriteLine("Fix Mode Activated"); - Console.Write("Enter fix code: "); - - string fixCode = ""; - while (true) - { - var codeKey = Console.ReadKey(true); - if (codeKey.Key == ConsoleKey.Enter) - { - Console.WriteLine(); - break; - } - else if (codeKey.Key == ConsoleKey.Backspace) - { - if (fixCode.Length > 0) - { - fixCode = fixCode.Substring(0, fixCode.Length - 1); - } - } - else - { - fixCode += codeKey.KeyChar; - Console.Write(codeKey.KeyChar); - } - } - - if (fixCode == "FixMyComputer") - { - Console.WriteLine("Fix mode enabled!"); - } - else - { - Console.WriteLine("Invalid fix code. Exiting fix mode."); - useFixMode = false; - } - } - } - else - { - // 正常登录流程 - Console.Write("Username: "); - string username = Console.ReadLine(); - - if (string.IsNullOrWhiteSpace(username)) - { - ShowError("Username cannot be empty."); - return false; - } - - Console.Write("Password: "); - string password = ReadPassword(); - - // 查找用户 - User foundUser = null; - foreach (User user in users) - { - if (user.Username.ToLower() == username.ToLower()) - { - foundUser = user; - break; - } - } - - if (foundUser == null) - { - ShowError("User not found."); - return false; - } - - // 使用SHA256加密输入的密码后比较 - string hashedInputPassword = HashPasswordSha256(password); - // Console.WriteLine($"Hashed Input Password: {hashedInputPassword}"); - // Console.WriteLine($"Stored Password: {foundUser.Password}"); - - if (foundUser.Password != hashedInputPassword) - { - ShowError("Invalid password."); - return false; - } - - ShowSuccess("Login successful!"); - Console.Beep(); - - // 设置当前登录用户 - currentLoggedInUser = foundUser; - - // 创建用户文件夹 - CreateUserFolder(foundUser.Username); - - return true; - } - } - catch - { - // 如果读取按键失败,使用普通登录 - ShowError("Error reading key input. Using normal login."); + CMLeonOS.UI.TUIHelper.SetColors(global::System.ConsoleColor.Red, global::System.ConsoleColor.Black); + global::System.Console.SetCursorPosition(17, 24); + global::System.Console.Write("Username cannot be empty. "); + global::System.Threading.Thread.Sleep(1000); return false; } - - // 如果使用了修复模式,返回true - if (useFixMode) + + CMLeonOS.UI.TUIHelper.SetColors(global::System.ConsoleColor.White, global::System.ConsoleColor.Black); + global::System.Console.SetCursorPosition(17, 11); + global::System.Console.Write("Password: "); + string password = ReadPassword(); + + User foundUser = null; + foreach (User user in users) { - return true; + if (user.Username.ToLower() == username.ToLower()) + { + foundUser = user; + break; + } } - - return false; + + if (foundUser == null) + { + CMLeonOS.UI.TUIHelper.SetColors(global::System.ConsoleColor.Red, global::System.ConsoleColor.Black); + global::System.Console.SetCursorPosition(17, 24); + global::System.Console.Write("User not found. "); + global::System.Threading.Thread.Sleep(1000); + return false; + } + + string hashedInputPassword = HashPasswordSha256(password); + + if (foundUser.Password != hashedInputPassword) + { + CMLeonOS.UI.TUIHelper.SetColors(global::System.ConsoleColor.Red, global::System.ConsoleColor.Black); + global::System.Console.SetCursorPosition(17, 24); + global::System.Console.Write("Invalid password. "); + global::System.Threading.Thread.Sleep(1000); + return false; + } + + CMLeonOS.UI.TUIHelper.SetColors(global::System.ConsoleColor.Green, global::System.ConsoleColor.Black); + global::System.Console.SetCursorPosition(17, 24); + global::System.Console.Write("Login successful! "); + // global::System.Console.WriteLine("Please wait... "); + global::System.Threading.Thread.Sleep(1500); + global::System.Console.ResetColor(); + global::System.Console.Clear(); + + global::System.Console.Beep(); + + currentLoggedInUser = foundUser; + CreateUserFolder(foundUser.Username); + + return true; } public bool AddUser(string args, bool isAdmin) diff --git a/UI/Components.cs b/UI/Components.cs index 4e5622d..7927819 100644 --- a/UI/Components.cs +++ b/UI/Components.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Linq; namespace CMLeonOS.UI { @@ -589,4 +590,514 @@ namespace CMLeonOS.UI RenderContent?.Invoke(); } } + + public class ListBox + { + public Rect Bounds { get; set; } + public List Items { get; set; } = new List(); + public int SelectedIndex { get; set; } = 0; + public int ScrollOffset { get; set; } = 0; + public ConsoleColor SelectedColor { get; set; } = ConsoleColor.Yellow; + public ConsoleColor NormalColor { get; set; } = ConsoleColor.White; + public ConsoleColor BorderColor { get; set; } = ConsoleColor.White; + public ConsoleColor BackgroundColor { get; set; } = ConsoleColor.DarkBlue; + public bool MultiSelect { get; set; } = false; + public List SelectedIndices { get; set; } = new List(); + + public ListBox() + { + } + + public ListBox(Rect bounds) + { + Bounds = bounds; + } + + public void Render() + { + TUIHelper.SetColors(BorderColor, BackgroundColor); + TUIHelper.DrawBox(Bounds, string.Empty, BorderColor); + + int startY = Bounds.Top + 1; + int maxHeight = Bounds.Height - 2; + + for (int i = ScrollOffset; i < Items.Count && i < ScrollOffset + maxHeight; i++) + { + int y = startY + (i - ScrollOffset); + ConsoleColor color = (i == SelectedIndex) ? SelectedColor : NormalColor; + string prefix = (i == SelectedIndex) ? "> " : " "; + + if (MultiSelect && SelectedIndices.Contains(i)) + { + prefix = "[x] "; + } + else if (MultiSelect) + { + prefix = "[ ] "; + } + + TUIHelper.SetColors(color, BackgroundColor); + Console.SetCursorPosition(Bounds.Left + 1, y); + Console.Write(prefix + TUIHelper.TruncateText(Items[i], Bounds.Width - 3)); + } + + if (Items.Count > maxHeight) + { + int scrollY = Bounds.Bottom - 1; + TUIHelper.SetColors(ConsoleColor.Gray, BackgroundColor); + Console.SetCursorPosition(Bounds.Right - 10, scrollY); + Console.Write($"({ScrollOffset + 1}/{(int)Math.Ceiling((double)Items.Count / maxHeight)})"); + } + } + + public bool HandleKey(ConsoleKeyInfo key) + { + if (key.Key == ConsoleKey.UpArrow || key.Key == ConsoleKey.PageUp) + { + SelectedIndex = (SelectedIndex - 1 + Items.Count) % Items.Count; + if (SelectedIndex < ScrollOffset) ScrollOffset = SelectedIndex; + return true; + } + + if (key.Key == ConsoleKey.DownArrow || key.Key == ConsoleKey.PageDown) + { + SelectedIndex = (SelectedIndex + 1) % Items.Count; + int maxScroll = Items.Count - (Bounds.Height - 2); + if (SelectedIndex >= ScrollOffset + (Bounds.Height - 2)) + { + ScrollOffset = SelectedIndex - (Bounds.Height - 3); + } + return true; + } + + if (key.Key == ConsoleKey.Enter) + { + if (MultiSelect) + { + if (SelectedIndices.Contains(SelectedIndex)) + { + SelectedIndices.Remove(SelectedIndex); + } + else + { + SelectedIndices.Add(SelectedIndex); + } + } + return true; + } + + if (key.Key == ConsoleKey.Spacebar && MultiSelect) + { + if (SelectedIndices.Contains(SelectedIndex)) + { + SelectedIndices.Remove(SelectedIndex); + } + else + { + SelectedIndices.Add(SelectedIndex); + } + return true; + } + + return false; + } + } + + public class CheckBox + { + public Point Position { get; set; } + public string Text { get; set; } + public bool IsChecked { get; set; } = false; + public bool IsFocused { get; set; } = false; + public ConsoleColor TextColor { get; set; } = ConsoleColor.White; + public ConsoleColor BoxColor { get; set; } = ConsoleColor.White; + public ConsoleColor BackgroundColor { get; set; } = ConsoleColor.Black; + public ConsoleColor FocusedTextColor { get; set; } = ConsoleColor.Yellow; + public ConsoleColor FocusedBoxColor { get; set; } = ConsoleColor.Yellow; + public ConsoleColor FocusedBackgroundColor { get; set; } = ConsoleColor.DarkBlue; + + public CheckBox() + { + } + + public CheckBox(Point position, string text) + { + Position = position; + Text = text; + } + + public void Render() + { + if (IsFocused) + { + TUIHelper.SetColors(FocusedTextColor, FocusedBackgroundColor); + Console.SetCursorPosition(Position.X, Position.Y); + Console.Write(IsChecked ? "[X] " : "[ ] "); + Console.Write(Text); + } + else + { + TUIHelper.SetColors(TextColor, BackgroundColor); + Console.SetCursorPosition(Position.X, Position.Y); + Console.Write(IsChecked ? "[x] " : "[ ] "); + Console.Write(Text); + } + } + + public bool HandleKey(ConsoleKeyInfo key) + { + if (key.Key == ConsoleKey.Enter || key.Key == ConsoleKey.Spacebar) + { + IsChecked = !IsChecked; + return true; + } + return false; + } + } + + public class RadioButton + { + public Point Position { get; set; } + public string Text { get; set; } + public bool IsChecked { get; set; } = false; + public bool IsFocused { get; set; } = false; + public ConsoleColor TextColor { get; set; } = ConsoleColor.White; + public ConsoleColor BoxColor { get; set; } = ConsoleColor.White; + public ConsoleColor BackgroundColor { get; set; } = ConsoleColor.Black; + public string GroupName { get; set; } = string.Empty; + public ConsoleColor FocusedTextColor { get; set; } = ConsoleColor.Yellow; + public ConsoleColor FocusedBoxColor { get; set; } = ConsoleColor.Yellow; + public ConsoleColor FocusedBackgroundColor { get; set; } = ConsoleColor.DarkBlue; + + public RadioButton() + { + } + + public RadioButton(Point position, string text) + { + Position = position; + Text = text; + } + + public void Render() + { + if (IsFocused) + { + TUIHelper.SetColors(FocusedTextColor, FocusedBackgroundColor); + Console.SetCursorPosition(Position.X, Position.Y); + Console.Write(IsChecked ? "(*) " : "( ) "); + Console.Write(Text); + } + else + { + TUIHelper.SetColors(TextColor, BackgroundColor); + Console.SetCursorPosition(Position.X, Position.Y); + Console.Write(IsChecked ? "(*) " : "( ) "); + Console.Write(Text); + } + } + + public bool HandleKey(ConsoleKeyInfo key) + { + if (key.Key == ConsoleKey.Enter || key.Key == ConsoleKey.Spacebar) + { + IsChecked = !IsChecked; + return true; + } + return false; + } + } + + public class TreeViewNode + { + public string Text { get; set; } + public List Children { get; set; } = new List(); + public bool IsExpanded { get; set; } = false; + public int Level { get; set; } = 0; + public TreeViewNode Parent { get; set; } + + public bool HasChildren => Children.Count > 0; + + public TreeViewNode() + { + } + + public TreeViewNode(string text) + { + Text = text; + } + } + + public class TreeView + { + public Rect Bounds { get; set; } + public TreeViewNode Root { get; set; } + public List SelectedNodes { get; set; } = new List(); + public ConsoleColor SelectedColor { get; set; } = ConsoleColor.Yellow; + public ConsoleColor NormalColor { get; set; } = ConsoleColor.White; + public ConsoleColor BorderColor { get; set; } = ConsoleColor.White; + public ConsoleColor BackgroundColor { get; set; } = ConsoleColor.DarkBlue; + public int ScrollOffset { get; set; } = 0; + + public TreeView() + { + } + + public TreeView(Rect bounds) + { + Bounds = bounds; + } + + public void Render() + { + TUIHelper.SetColors(BorderColor, BackgroundColor); + TUIHelper.DrawBox(Bounds, string.Empty, BorderColor); + + int startY = Bounds.Top + 1; + int maxHeight = Bounds.Height - 2; + + List visibleNodes = GetVisibleNodes(); + for (int i = ScrollOffset; i < visibleNodes.Count && i < ScrollOffset + maxHeight; i++) + { + int y = startY + (i - ScrollOffset); + var node = visibleNodes[i]; + ConsoleColor color = SelectedNodes.Contains(node) ? SelectedColor : NormalColor; + string prefix = GetNodePrefix(node); + + TUIHelper.SetColors(color, BackgroundColor); + Console.SetCursorPosition(Bounds.Left + 1, y); + Console.Write(prefix + TUIHelper.TruncateText(node.Text, Bounds.Width - 3 - node.Level * 2)); + } + + if (visibleNodes.Count > maxHeight) + { + int scrollY = Bounds.Bottom - 1; + TUIHelper.SetColors(ConsoleColor.Gray, BackgroundColor); + Console.SetCursorPosition(Bounds.Right - 10, scrollY); + Console.Write($"({ScrollOffset + 1}/{(int)Math.Ceiling((double)visibleNodes.Count / maxHeight)})"); + } + } + + private List GetVisibleNodes() + { + List nodes = new List(); + if (Root != null) + { + AddVisibleNodes(Root, nodes); + } + return nodes; + } + + private void AddVisibleNodes(TreeViewNode node, List nodes) + { + nodes.Add(node); + if (node.IsExpanded) + { + foreach (var child in node.Children) + { + AddVisibleNodes(child, nodes); + } + } + } + + private string GetNodePrefix(TreeViewNode node) + { + string prefix = ""; + for (int i = 0; i < node.Level; i++) + { + prefix += " "; + } + + if (node.HasChildren) + { + prefix += node.IsExpanded ? "- " : "+ "; + } + else + { + prefix += "+ "; + } + + return prefix; + } + + public bool HandleKey(ConsoleKeyInfo key) + { + var visibleNodes = GetVisibleNodes(); + if (visibleNodes.Count == 0) return false; + + if (key.Key == ConsoleKey.UpArrow || key.Key == ConsoleKey.PageUp) + { + int currentIndex = visibleNodes.FindIndex(n => SelectedNodes.Contains(n)); + if (currentIndex > 0) + { + SelectedNodes.Clear(); + SelectedNodes.Add(visibleNodes[currentIndex - 1]); + if (currentIndex - 1 < ScrollOffset) ScrollOffset = currentIndex - 1; + } + return true; + } + + if (key.Key == ConsoleKey.DownArrow || key.Key == ConsoleKey.PageDown) + { + int currentIndex = visibleNodes.FindIndex(n => SelectedNodes.Contains(n)); + if (currentIndex < visibleNodes.Count - 1) + { + SelectedNodes.Clear(); + SelectedNodes.Add(visibleNodes[currentIndex + 1]); + int maxScroll = visibleNodes.Count - (Bounds.Height - 2); + if (currentIndex + 1 >= ScrollOffset + (Bounds.Height - 2)) + { + ScrollOffset = (currentIndex + 1) - (Bounds.Height - 3); + } + } + return true; + } + + if (key.Key == ConsoleKey.Enter || key.Key == ConsoleKey.Spacebar) + { + var currentNode = visibleNodes.FirstOrDefault(n => SelectedNodes.Contains(n)); + if (currentNode != null && currentNode.HasChildren) + { + currentNode.IsExpanded = !currentNode.IsExpanded; + } + else if (currentNode != null) + { + if (SelectedNodes.Contains(currentNode)) + { + SelectedNodes.Remove(currentNode); + } + else + { + SelectedNodes.Add(currentNode); + } + } + return true; + } + + return false; + } + } + + public class ScrollBar + { + public Point Position { get; set; } + public int Height { get; set; } + public int MaxValue { get; set; } + public int Value { get; set; } = 0; + public ConsoleColor BarColor { get; set; } = ConsoleColor.White; + public ConsoleColor BackgroundColor { get; set; } = ConsoleColor.DarkGray; + public ConsoleColor BorderColor { get; set; } = ConsoleColor.White; + public bool IsVertical { get; set; } = true; + + public ScrollBar() + { + } + + public ScrollBar(Point position, int height) + { + Position = position; + Height = height; + } + + public void Render() + { + TUIHelper.SetColors(BorderColor, BackgroundColor); + + if (IsVertical) + { + Console.SetCursorPosition(Position.X, Position.Y); + Console.Write('┌'); + for (int i = 0; i < Height - 2; i++) + { + Console.SetCursorPosition(Position.X, Position.Y + 1 + i); + Console.Write('│'); + } + Console.SetCursorPosition(Position.X, Position.Y + Height - 1); + Console.Write('└'); + + int barHeight = Height - 2; + int barPosition = (int)((double)Value / MaxValue * barHeight); + for (int i = 0; i < barHeight; i++) + { + Console.SetCursorPosition(Position.X, Position.Y + 1 + i); + if (i >= barPosition && i < barPosition + (int)((double)barHeight / MaxValue * Value)) + { + Console.Write('█'); + } + else + { + Console.Write('░'); + } + } + } + else + { + Console.SetCursorPosition(Position.X, Position.Y); + Console.Write('┌'); + for (int i = 0; i < Height - 2; i++) + { + Console.SetCursorPosition(Position.X + 1 + i, Position.Y); + Console.Write('─'); + } + Console.SetCursorPosition(Position.X + Height - 1, Position.Y); + Console.Write('┐'); + + int barWidth = Height - 2; + int barPosition = (int)((double)Value / MaxValue * barWidth); + for (int i = 0; i < barWidth; i++) + { + Console.SetCursorPosition(Position.X + 1 + i, Position.Y); + if (i >= barPosition && i < barPosition + (int)((double)barWidth / MaxValue * Value)) + { + Console.Write('█'); + } + else + { + Console.Write('░'); + } + } + } + } + + public bool HandleKey(ConsoleKeyInfo key) + { + if (IsVertical) + { + if (key.Key == ConsoleKey.UpArrow) + { + Value = Math.Max(0, Value - 1); + return true; + } + if (key.Key == ConsoleKey.DownArrow) + { + Value = Math.Min(MaxValue, Value + 1); + return true; + } + if (key.Key == ConsoleKey.PageUp) + { + Value = Math.Max(0, Value - 10); + return true; + } + if (key.Key == ConsoleKey.PageDown) + { + Value = Math.Min(MaxValue, Value + 10); + return true; + } + } + else + { + if (key.Key == ConsoleKey.LeftArrow) + { + Value = Math.Max(0, Value - 1); + return true; + } + if (key.Key == ConsoleKey.RightArrow) + { + Value = Math.Min(MaxValue, Value + 1); + return true; + } + } + return false; + } + } } diff --git a/shell/Commands/TestTUICommand.cs b/shell/Commands/TestTUICommand.cs index b70848d..1225bf1 100644 --- a/shell/Commands/TestTUICommand.cs +++ b/shell/Commands/TestTUICommand.cs @@ -27,6 +27,11 @@ namespace CMLeonOS.Commands menu.Items.Add(new MenuItem("Progress Bar Demo", () => ShowProgressBarDemo())); menu.Items.Add(new MenuItem("Tab Control Demo", () => ShowTabControlDemo())); menu.Items.Add(new MenuItem("Menu Demo", () => ShowMenuDemo())); + menu.Items.Add(new MenuItem("ListBox Demo", () => ShowListBoxDemo())); + menu.Items.Add(new MenuItem("CheckBox Demo", () => ShowCheckBoxDemo())); + menu.Items.Add(new MenuItem("RadioButton Demo", () => ShowRadioButtonDemo())); + menu.Items.Add(new MenuItem("TreeView Demo", () => ShowTreeViewDemo())); + menu.Items.Add(new MenuItem("ScrollBar Demo", () => ShowScrollBarDemo())); menu.Items.Add(new MenuItem("Exit", () => global::System.Environment.Exit(0))); menu.Render(); @@ -365,5 +370,315 @@ namespace CMLeonOS.Commands } } } + + private static void ShowListBoxDemo() + { + global::System.Console.Clear(); + + var statusBar = new StatusBar(new Rect(0, 24, 80, 1)); + statusBar.Items.Add(new StatusBarItem("Use UP/DOWN to navigate, SPACE to select, ESC to return")); + statusBar.Render(); + + var listBox = new ListBox(new Rect(10, 5, 60, 15)); + listBox.Items.Add("Item 1: Basic functionality"); + listBox.Items.Add("Item 2: Advanced features"); + listBox.Items.Add("Item 3: Settings"); + listBox.Items.Add("Item 4: Help"); + listBox.Items.Add("Item 5: About"); + listBox.Items.Add("Item 6: Documentation"); + listBox.Items.Add("Item 7: Configuration"); + listBox.Items.Add("Item 8: System info"); + listBox.Items.Add("Item 9: Network settings"); + listBox.Items.Add("Item 10: User management"); + listBox.MultiSelect = true; + + listBox.Render(); + + bool demoRunning = true; + while (demoRunning) + { + var key = global::System.Console.ReadKey(true); + + if (key.Key == global::System.ConsoleKey.Escape) + { + demoRunning = false; + } + else if (listBox.HandleKey(key)) + { + listBox.Render(); + } + } + } + + private static void ShowCheckBoxDemo() + { + global::System.Console.Clear(); + + var statusBar = new StatusBar(new Rect(0, 24, 80, 1)); + statusBar.Items.Add(new StatusBarItem("Use UP/DOWN to navigate, SPACE to toggle, ESC to return")); + statusBar.Render(); + + var checkBox1 = new CheckBox(new Point(10, 8), "Enable feature 1"); + checkBox1.IsChecked = true; + checkBox1.IsFocused = true; + var checkBox2 = new CheckBox(new Point(10, 10), "Enable feature 2"); + checkBox2.IsChecked = false; + var checkBox3 = new CheckBox(new Point(10, 12), "Enable feature 3"); + checkBox3.IsChecked = false; + var checkBox4 = new CheckBox(new Point(10, 14), "Enable feature 4"); + checkBox4.IsChecked = false; + + checkBox1.Render(); + checkBox2.Render(); + checkBox3.Render(); + checkBox4.Render(); + + int focusedCheckBox = 0; + + bool demoRunning = true; + while (demoRunning) + { + var key = global::System.Console.ReadKey(true); + + if (key.Key == global::System.ConsoleKey.Escape) + { + demoRunning = false; + } + else if (key.Key == global::System.ConsoleKey.UpArrow) + { + checkBox1.IsFocused = false; + checkBox2.IsFocused = false; + checkBox3.IsFocused = false; + checkBox4.IsFocused = false; + focusedCheckBox = (focusedCheckBox - 1 + 4) % 4; + + if (focusedCheckBox == 0) checkBox1.IsFocused = true; + if (focusedCheckBox == 1) checkBox2.IsFocused = true; + if (focusedCheckBox == 2) checkBox3.IsFocused = true; + if (focusedCheckBox == 3) checkBox4.IsFocused = true; + } + else if (key.Key == global::System.ConsoleKey.DownArrow) + { + checkBox1.IsFocused = false; + checkBox2.IsFocused = false; + checkBox3.IsFocused = false; + checkBox4.IsFocused = false; + focusedCheckBox = (focusedCheckBox + 1) % 4; + + if (focusedCheckBox == 0) checkBox1.IsFocused = true; + if (focusedCheckBox == 1) checkBox2.IsFocused = true; + if (focusedCheckBox == 2) checkBox3.IsFocused = true; + if (focusedCheckBox == 3) checkBox4.IsFocused = true; + } + else if (key.Key == global::System.ConsoleKey.Enter || key.Key == global::System.ConsoleKey.Spacebar) + { + if (focusedCheckBox == 0) checkBox1.IsChecked = !checkBox1.IsChecked; + if (focusedCheckBox == 1) checkBox2.IsChecked = !checkBox2.IsChecked; + if (focusedCheckBox == 2) checkBox3.IsChecked = !checkBox3.IsChecked; + if (focusedCheckBox == 3) checkBox4.IsChecked = !checkBox4.IsChecked; + } + + checkBox1.Render(); + checkBox2.Render(); + checkBox3.Render(); + checkBox4.Render(); + } + } + + private static void ShowRadioButtonDemo() + { + global::System.Console.Clear(); + + var statusBar = new StatusBar(new Rect(0, 24, 80, 1)); + statusBar.Items.Add(new StatusBarItem("Use UP/DOWN to navigate, SPACE to select, ESC to return")); + statusBar.Render(); + + var radioButton1 = new RadioButton(new Point(10, 8), "Option 1"); + radioButton1.IsChecked = true; + radioButton1.IsFocused = true; + radioButton1.GroupName = "group1"; + var radioButton2 = new RadioButton(new Point(10, 10), "Option 2"); + radioButton2.IsChecked = false; + radioButton2.GroupName = "group1"; + var radioButton3 = new RadioButton(new Point(10, 12), "Option 3"); + radioButton3.IsChecked = false; + radioButton3.GroupName = "group1"; + var radioButton4 = new RadioButton(new Point(10, 14), "Option 4"); + radioButton4.IsChecked = false; + radioButton4.GroupName = "group1"; + + radioButton1.Render(); + radioButton2.Render(); + radioButton3.Render(); + radioButton4.Render(); + + int focusedRadioButton = 0; + + bool demoRunning = true; + while (demoRunning) + { + var key = global::System.Console.ReadKey(true); + + if (key.Key == global::System.ConsoleKey.Escape) + { + demoRunning = false; + } + else if (key.Key == global::System.ConsoleKey.UpArrow) + { + radioButton1.IsFocused = false; + radioButton2.IsFocused = false; + radioButton3.IsFocused = false; + radioButton4.IsFocused = false; + focusedRadioButton = (focusedRadioButton - 1 + 4) % 4; + + if (focusedRadioButton == 0) radioButton1.IsFocused = true; + if (focusedRadioButton == 1) radioButton2.IsFocused = true; + if (focusedRadioButton == 2) radioButton3.IsFocused = true; + if (focusedRadioButton == 3) radioButton4.IsFocused = true; + } + else if (key.Key == global::System.ConsoleKey.DownArrow) + { + radioButton1.IsFocused = false; + radioButton2.IsFocused = false; + radioButton3.IsFocused = false; + radioButton4.IsFocused = false; + focusedRadioButton = (focusedRadioButton + 1) % 4; + + if (focusedRadioButton == 0) radioButton1.IsFocused = true; + if (focusedRadioButton == 1) radioButton2.IsFocused = true; + if (focusedRadioButton == 2) radioButton3.IsFocused = true; + if (focusedRadioButton == 3) radioButton4.IsFocused = true; + } + else if (key.Key == global::System.ConsoleKey.Enter || key.Key == global::System.ConsoleKey.Spacebar) + { + if (focusedRadioButton == 0) radioButton1.IsChecked = true; + if (focusedRadioButton == 1) radioButton2.IsChecked = true; + if (focusedRadioButton == 2) radioButton3.IsChecked = true; + if (focusedRadioButton == 3) radioButton4.IsChecked = true; + } + + radioButton1.Render(); + radioButton2.Render(); + radioButton3.Render(); + radioButton4.Render(); + } + } + + private static void ShowTreeViewDemo() + { + global::System.Console.Clear(); + + var statusBar = new StatusBar(new Rect(0, 24, 80, 1)); + statusBar.Items.Add(new StatusBarItem("Use UP/DOWN to navigate, ENTER/SPACE to expand/collapse, ESC to return")); + statusBar.Render(); + + var treeView = new TreeView(new Rect(5, 5, 70, 15)); + treeView.Root = new TreeViewNode { Text = "Root" }; + + var child1 = new TreeViewNode { Text = "Folder 1", Level = 1, Parent = treeView.Root }; + var child2 = new TreeViewNode { Text = "Folder 2", Level = 1, Parent = treeView.Root }; + var child3 = new TreeViewNode { Text = "File 1", Level = 2, Parent = child1 }; + var child4 = new TreeViewNode { Text = "File 2", Level = 2, Parent = child1 }; + var child5 = new TreeViewNode { Text = "File 3", Level = 2, Parent = child2 }; + + child1.Children.Add(child3); + child1.Children.Add(child4); + child2.Children.Add(child5); + + treeView.Root.Children.Add(child1); + treeView.Root.Children.Add(child2); + + treeView.Render(); + + bool demoRunning = true; + while (demoRunning) + { + var key = global::System.Console.ReadKey(true); + + if (key.Key == global::System.ConsoleKey.Escape) + { + demoRunning = false; + } + else if (treeView.HandleKey(key)) + { + treeView.Render(); + } + } + } + + private static void ShowScrollBarDemo() + { + global::System.Console.Clear(); + + var statusBar = new StatusBar(new Rect(0, 24, 80, 1)); + statusBar.Items.Add(new StatusBarItem("Use UP/DOWN or LEFT/RIGHT to adjust, ESC to return")); + statusBar.Render(); + + var scrollBar1 = new ScrollBar(new Point(15, 10), 10); + scrollBar1.MaxValue = 100; + scrollBar1.Value = 50; + scrollBar1.IsVertical = true; + + var scrollBar2 = new ScrollBar(new Point(40, 10), 30); + scrollBar2.MaxValue = 100; + scrollBar2.Value = 30; + scrollBar2.IsVertical = false; + + var scrollBar3 = new ScrollBar(new Point(15, 15), 10); + scrollBar3.MaxValue = 50; + scrollBar3.Value = 25; + scrollBar3.IsVertical = true; + + var scrollBar4 = new ScrollBar(new Point(40, 15), 30); + scrollBar4.MaxValue = 50; + scrollBar4.Value = 15; + scrollBar4.IsVertical = false; + + var label1 = new Label(new Point(15, 8), "Vertical ScrollBar:"); + var label2 = new Label(new Point(40, 8), "Horizontal ScrollBar:"); + var label3 = new Label(new Point(15, 13), "Small Vertical:"); + var label4 = new Label(new Point(40, 13), "Small Horizontal:"); + + label1.Render(); + label2.Render(); + label3.Render(); + label4.Render(); + + scrollBar1.Render(); + scrollBar2.Render(); + scrollBar3.Render(); + scrollBar4.Render(); + + int focusedScrollBar = 0; + + bool demoRunning = true; + while (demoRunning) + { + var key = global::System.Console.ReadKey(true); + + if (key.Key == global::System.ConsoleKey.Escape) + { + demoRunning = false; + } + else if (key.Key == global::System.ConsoleKey.UpArrow || key.Key == global::System.ConsoleKey.DownArrow) + { + if (focusedScrollBar == 0 && scrollBar1.HandleKey(key)) scrollBar1.Render(); + if (focusedScrollBar == 1 && scrollBar2.HandleKey(key)) scrollBar2.Render(); + if (focusedScrollBar == 2 && scrollBar3.HandleKey(key)) scrollBar3.Render(); + if (focusedScrollBar == 3 && scrollBar4.HandleKey(key)) scrollBar4.Render(); + } + else if (key.Key == global::System.ConsoleKey.LeftArrow || key.Key == global::System.ConsoleKey.RightArrow) + { + if (focusedScrollBar == 0 && scrollBar1.HandleKey(key)) scrollBar1.Render(); + if (focusedScrollBar == 1 && scrollBar2.HandleKey(key)) scrollBar2.Render(); + if (focusedScrollBar == 2 && scrollBar3.HandleKey(key)) scrollBar3.Render(); + if (focusedScrollBar == 3 && scrollBar4.HandleKey(key)) scrollBar4.Render(); + } + else if (key.Key == global::System.ConsoleKey.Tab) + { + focusedScrollBar = (focusedScrollBar + 1) % 4; + } + } + } } }