using System; using System.Drawing; using CC_Functions.Misc; namespace CC_Functions.Commandline.TUI { /// /// Provides a screen that stays in the middle of the console. Use ContentPanel to attach controls /// public class CenteredScreen : Screen { private bool _resizing; private Size _actualSize; /// /// The panel used for storing and rendering the actual controls /// public Panel ContentPanel; private string _title = "CC-Functions.CommandLine app"; private readonly Label _titleLabel; /// /// Creates a screen that stays in the middle of the console. Use ContentPanel to attach controls /// /// The width of the content panel /// The height of the content panel /// The content panels background (Should be different from Console.BackgroundColor) /// Whether to use color when drawing public CenteredScreen(int width, int height, ConsoleColor contentBack, bool color = true) : base(width, height, color) { ContentPanel = new Panel {BackColor = contentBack}; ActualSize = new Size(width, height); _titleLabel = new Label(Title); Controls.Add(ContentPanel); Controls.Add(_titleLabel); WindowResize += (screen, args) => CalculatePosition(); ((Control) this).Resize += (caller, args) => CalculatePosition(); CalculatePosition(true); } /// /// The title to display at the top of the console /// public string Title { get => _title; set { if (_title != value && !string.IsNullOrWhiteSpace(value)) { _title = value; CalculatePosition(true); } } } /// /// The actual size of this control. The "Size" property is assigned automatically /// public Size ActualSize { get => _actualSize; set { if (_actualSize != value) { _actualSize = value; CalculatePosition(true); } } } /// /// Calculates the Size variable, Title and ContentPanel position/size /// /// Whether this is the initial calculation public void CalculatePosition(bool initial = false) { if (!_resizing) { _resizing = true; Size = new Size(Console.WindowWidth, Console.WindowHeight - 1); _titleLabel.Content = Title + Environment.NewLine + new string(SpecialChars.OneLineSimple.LeftRight, Console.WindowWidth); ContentPanel.Size = ActualSize; ContentPanel.Point = new Point((Console.WindowWidth - ActualSize.Width) / 2, (Console.WindowHeight - ActualSize.Height) / 2); if (!initial) { Console.Clear(); DiffDraw.Draw(Color, true); } _resizing = false; } } } }