diff --git a/SessionZero/.noai b/SessionZero/.noai new file mode 100644 index 0000000..e69de29 diff --git a/SessionZero/App.axaml.cs b/SessionZero/App.axaml.cs index a6f2ccb..31a9ad0 100644 --- a/SessionZero/App.axaml.cs +++ b/SessionZero/App.axaml.cs @@ -1,23 +1,36 @@ +using System.Reflection; using Avalonia; using Avalonia.Controls.ApplicationLifetimes; using Avalonia.Markup.Xaml; +using Cogwheel; +using SessionZero.Cogwheel; namespace SessionZero; public partial class App : Application { + public override void Initialize() { AvaloniaXamlLoader.Load(this); + InitCogwheel(); } public override void OnFrameworkInitializationCompleted() { if (ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop) { - desktop.MainWindow = new MainWindow(); + AppManager.MainWindow = new MainWindow(); + desktop.MainWindow = AppManager.MainWindow; } base.OnFrameworkInitializationCompleted(); } + + private void InitCogwheel() + { + AppManager.CommandsManager.AddAssembly(Assembly.GetAssembly(typeof(DeafultCogwheelConsole))); + AppManager.CommandsManager.AddAssembly(Assembly.GetExecutingAssembly()); + COGWHEEL.Initialize(AppManager.CommandsManager, AppManager.SzConsole); + } } \ No newline at end of file diff --git a/SessionZero/AppManager.cs b/SessionZero/AppManager.cs new file mode 100644 index 0000000..6a08750 --- /dev/null +++ b/SessionZero/AppManager.cs @@ -0,0 +1,41 @@ +using Cogwheel; +using SessionZero.Cogwheel; + +namespace SessionZero; + +public static class AppManager +{ + public static MainWindow MainWindow { get; set; } + public static ICogwheelConsole SzConsole { get; } = new SessionZeroConsole(); + public static CommandsManager CommandsManager { get; } = new(); + public static ConsoleControl ConsoleControl { get; } = new(); + + public static void ShowConsole() + { + MainWindow.ToggleConsole(true); + } + + public static void HideConsole() + { + MainWindow.ToggleConsole(false); + } + + // -- Global Commands -- // + [Command(Name = "exit", Description = "Hides the console")] + public static void ExitConsole() + { + SzConsole.Exit(); + } + + [Command(Name = "page", Description = "Change the current page to the given value, if it exists")] + public static void ChangePage(string pageName) + { + MainWindow.ChangePage(pageName); + } + + [Command(Name = "err")] + public static void ErrTest(string message) + { + COGWHEEL.LogError(message); + } +} \ No newline at end of file diff --git a/SessionZero/Cogwheel/ConsoleControl.axaml b/SessionZero/Cogwheel/ConsoleControl.axaml new file mode 100644 index 0000000..33f9293 --- /dev/null +++ b/SessionZero/Cogwheel/ConsoleControl.axaml @@ -0,0 +1,11 @@ + + + + + + diff --git a/SessionZero/Cogwheel/ConsoleControl.axaml.cs b/SessionZero/Cogwheel/ConsoleControl.axaml.cs new file mode 100644 index 0000000..66e2991 --- /dev/null +++ b/SessionZero/Cogwheel/ConsoleControl.axaml.cs @@ -0,0 +1,61 @@ +using Avalonia; +using Avalonia.Controls; +using Avalonia.Controls.Documents; +using Avalonia.Input; +using Avalonia.Markup.Xaml; +using Avalonia.Media; +using Cogwheel; + +namespace SessionZero.Cogwheel; + +public partial class ConsoleControl : UserControl +{ + private TextBlock? _output; + private TextBox? _input; + + public ConsoleControl() + { + InitializeComponent(); + + _output = this.FindControl("Output"); + _input = this.FindControl("Input"); + + _input?.KeyDown += Input_OnKeyDown; + } + + private void Input_OnKeyDown(object? sender, KeyEventArgs e) + { + if (e.Key == Key.Enter) + { + var inputText = _input!.Text ?? string.Empty; + Log(inputText); + if (inputText.Length > 0) COGWHEEL.RunCommand(inputText); + _input.Text = string.Empty; + e.Handled = true; + } + } + + public void Log(string text, Color? color = null) + { + if (_output == null) + return; + + _output?.Inlines?.Add(new Run("> ") + { + Foreground = Brushes.LightGray + }); + + _output?.Inlines?.Add(new Run(text) + { + Foreground = color != null ? new SolidColorBrush(color.Value) + : Brushes.White + }); + + _output?.Inlines?.Add(new LineBreak()); + } + + public void ClearOutput() + { + _output?.Inlines?.Clear(); + } +} \ No newline at end of file diff --git a/SessionZero/Cogwheel/SessionZeroConsole.cs b/SessionZero/Cogwheel/SessionZeroConsole.cs new file mode 100644 index 0000000..1d72e3e --- /dev/null +++ b/SessionZero/Cogwheel/SessionZeroConsole.cs @@ -0,0 +1,50 @@ +using System; +using Avalonia.Media; +using Cogwheel; + +namespace SessionZero.Cogwheel; + +public class SessionZeroConsole : ICogwheelConsole +{ + public string OpeningMessage { get; set; } = "SessionZero Console"; + public bool IsRunning { get; set; } + public CommandsManager CommandsManager { get; set; } + + public void Initialize(CommandsManager commandsManager) + { + CommandsManager = commandsManager; + } + + public void Log(string message) + { + Console.WriteLine(message); + AppManager.ConsoleControl.Log(message); + } + + public void LogError(string message) + { + Console.WriteLine("Error: " + message); + AppManager.ConsoleControl.Log($"Error: {message}", Colors.Red); + } + + public void LogWarning(string message) + { + Console.WriteLine("Warning: " + message); + AppManager.ConsoleControl.Log($"Warning: {message}", Colors.Yellow); + } + + public void Write(string message) + { + Console.Write(message); + } + + public void ClearConsole() + { + AppManager.ConsoleControl.ClearOutput(); + } + + public void Exit() + { + AppManager.HideConsole(); + } +} \ No newline at end of file diff --git a/SessionZero/MainWindow.axaml b/SessionZero/MainWindow.axaml index 518e2ae..8de6474 100644 --- a/SessionZero/MainWindow.axaml +++ b/SessionZero/MainWindow.axaml @@ -13,19 +13,19 @@ - - - - - + + + diff --git a/SessionZero/MainWindow.axaml.cs b/SessionZero/MainWindow.axaml.cs index 5f76200..ed47373 100644 --- a/SessionZero/MainWindow.axaml.cs +++ b/SessionZero/MainWindow.axaml.cs @@ -1,11 +1,90 @@ +using System; +using System.Collections.Generic; using Avalonia.Controls; +using Avalonia.Input; +using Cogwheel; +using SessionZero.Cogwheel; +using SessionZero.Pages; namespace SessionZero; public partial class MainWindow : Window { + private Dictionary _pages = new(); + + private ContentControl? _mainContentControl; + private ContentControl? _overlayContentControl; + private Button? _homeButton; + private Button? _sessionsButton; + private Button? _libraryButton; + private Button? _settingsButton; + public MainWindow() { InitializeComponent(); + InitControls(); + InitPages(); + + KeyDownEvent.AddClassHandler(OnKeyDown, handledEventsToo: true); + + ChangePage("Home"); } -} \ No newline at end of file + + public void ChangePage(string pageName) + { + if (_pages.TryGetValue(pageName.ToLower(), out var page)) + { + _mainContentControl?.Content = page; + } + else + { + COGWHEEL.LogError($"Page {pageName} does not exist"); + } + } + + public void ToggleOverlay(bool visible) + { + _overlayContentControl?.IsVisible = visible; + _overlayContentControl?.IsHitTestVisible = visible; + } + + public void ToggleConsole(bool visible) + { + ToggleOverlay(visible); + _overlayContentControl?.Content = visible ? AppManager.ConsoleControl : null; + } + + private void OnKeyDown(TopLevel topLevel, KeyEventArgs keyEventArgs) + { + if (keyEventArgs.Key == Key.OemTilde) + ToggleConsole(!_overlayContentControl.IsVisible); + } + + private void InitControls() + { + _mainContentControl = this.FindControl("MainContentControl"); + _overlayContentControl = this.FindControl("Overlay"); + _homeButton = this.FindControl