Files
CMLeonOS/BootMenu.cs
2026-03-01 17:03:49 +08:00

162 lines
4.6 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System;
using Sys = Cosmos.System;
using Cosmos.HAL;
using Cosmos.Core;
using System.Threading;
namespace CMLeonOS
{
public enum BootMenuAction
{
NormalBoot,
GuiBoot,
Reboot,
Shutdown
}
internal static class BootMenu
{
private static void PrintOption(string text, bool selected)
{
Console.SetCursorPosition(1, Console.GetCursorPosition().Top);
Console.BackgroundColor = selected ? ConsoleColor.White : ConsoleColor.Black;
Console.ForegroundColor = selected ? ConsoleColor.Black : ConsoleColor.White;
Console.WriteLine(text);
}
private static void Render(int selIdx, int remainingTime)
{
Console.BackgroundColor = ConsoleColor.Black;
Console.ForegroundColor = ConsoleColor.Cyan;
Console.SetCursorPosition(0, 0);
uint mem = Cosmos.Core.CPU.GetAmountOfRAM();
Console.WriteLine($"{Version.DisplayVersion} [{mem} MB memory]");
// 这里老显示 unknown谁知道为啥
// Console.WriteLine($"Git Commit: {Version.GitCommit}");
Console.WriteLine($"Build Time: {GetBuildTime()}");
Console.WriteLine();
Console.WriteLine($"Auto-select in {remainingTime} seconds...");
Console.WriteLine();
Console.WriteLine("Select an option:");
Console.WriteLine();
PrintOption("Normal Boot", selIdx == 0);
PrintOption("GUI Boot", selIdx == 1);
PrintOption("Reboot", selIdx == 2);
PrintOption("Shutdown", selIdx == 3);
}
private static BootMenuAction Confirm(int selIdx)
{
Console.BackgroundColor = ConsoleColor.Black;
Console.ForegroundColor = ConsoleColor.White;
Console.Clear();
Console.SetCursorPosition(0, 0);
Console.CursorVisible = true;
switch (selIdx)
{
case 0:
return BootMenuAction.NormalBoot;
case 1:
return BootMenuAction.GuiBoot;
case 2:
Sys.Power.Reboot();
return BootMenuAction.Reboot;
case 3:
Sys.Power.Shutdown();
return BootMenuAction.Shutdown;
default:
return BootMenuAction.NormalBoot;
}
}
public static BootMenuAction Show()
{
Console.BackgroundColor = ConsoleColor.Black;
Console.ForegroundColor = ConsoleColor.White;
Console.Clear();
Console.CursorVisible = false;
int selIdx = 0;
int remainingTime = 10;
int counter = 0;
while (true)
{
Render(selIdx, remainingTime);
if (Sys.KeyboardManager.TryReadKey(out var key))
{
if (key.Key == Sys.ConsoleKeyEx.Enter)
{
return Confirm(selIdx);
}
else if (key.Key == Sys.ConsoleKeyEx.DownArrow)
{
selIdx++;
remainingTime = 10;
counter = 0;
}
else if (key.Key == Sys.ConsoleKeyEx.UpArrow)
{
selIdx--;
remainingTime = 10;
counter = 0;
}
}
else
{
Thread.Sleep(100);
counter++;
if (counter >= 10)
{
remainingTime--;
counter = 0;
if (remainingTime <= 0)
{
return Confirm(selIdx);
}
}
}
if (selIdx < 0)
{
selIdx = 3;
}
if (selIdx > 3)
{
selIdx = 0;
}
}
}
private static string GetBuildTime()
{
try
{
if (Kernel.buildTimeFile != null && Kernel.buildTimeFile.Length > 0)
{
return System.Text.Encoding.UTF8.GetString(Kernel.buildTimeFile);
}
}
catch
{
}
return "Unknown";
}
}
}