From b9ce7ea532e9b053be7d6683f6ccbf4db139e3a0 Mon Sep 17 00:00:00 2001 From: Yuriy Durov Date: Fri, 12 Jan 2024 17:43:01 +0400 Subject: [PATCH] Add async menu flow interception --- src/BitzArt.Console/Menus/ConsoleMenu.cs | 35 +++++++++++++------ .../Menus/ConsoleSelectionMenu.cs | 21 +++++++++-- 2 files changed, 43 insertions(+), 13 deletions(-) diff --git a/src/BitzArt.Console/Menus/ConsoleMenu.cs b/src/BitzArt.Console/Menus/ConsoleMenu.cs index 557b408..b08de11 100644 --- a/src/BitzArt.Console/Menus/ConsoleMenu.cs +++ b/src/BitzArt.Console/Menus/ConsoleMenu.cs @@ -12,19 +12,23 @@ public abstract class ConsoleMenu public bool? IsMainMenu { get; internal set; } - public Task RunAsync() + public async Task RunAsync() { - Render(); - return Task.CompletedTask; + await RenderAsync(); } - internal virtual void Render() + internal virtual async Task RenderAsync() { AnsiConsole.Clear(); DisplayTitle(); - OnBeforeDisplay(); - Display(); - OnDisplayed(); + + OnRender(); + await OnRenderAsync(); + + await DisplayAsync(); + + await OnDisplayAsync(); + OnDisplay(); } internal virtual void DisplayTitle() @@ -33,15 +37,26 @@ internal virtual void DisplayTitle() AnsiConsole.WriteLine(); } - protected virtual void OnBeforeDisplay() + protected virtual Task OnRenderAsync() { + return Task.CompletedTask; } - internal virtual void Display() + protected virtual void OnRender() { } - protected virtual void OnDisplayed() + internal virtual Task DisplayAsync() + { + return Task.CompletedTask; + } + + protected virtual Task OnDisplayAsync() + { + return Task.CompletedTask; + } + + protected virtual void OnDisplay() { } diff --git a/src/BitzArt.Console/Menus/ConsoleSelectionMenu.cs b/src/BitzArt.Console/Menus/ConsoleSelectionMenu.cs index 85ad702..83da3f2 100644 --- a/src/BitzArt.Console/Menus/ConsoleSelectionMenu.cs +++ b/src/BitzArt.Console/Menus/ConsoleSelectionMenu.cs @@ -19,16 +19,16 @@ public void AddSelection(string name, Action? action = null, bool pauseOnComplet SelectionItems.Add(new ConsoleSelectionMenuItem(name, action, pauseOnComplete)); } - internal override void Render() + internal override async Task RenderAsync() { while(true) { if (_exiting) break; - base.Render(); + await base.RenderAsync(); } } - internal override void Display() + internal override async Task DisplayAsync() { var selectionPrompt = new SelectionPrompt(); @@ -46,12 +46,22 @@ internal override void Display() return; } + await OnSelectionBeforeInvokeAsync(selected); OnSelectionBeforeInvoke(selected); + InvokeSelection(selected); + + await OnSelectionAsync(selected); OnSelection(selected); + AfterSelectionInvoked(selected); } + protected virtual Task OnSelectionBeforeInvokeAsync(ConsoleSelectionMenuItem selection) + { + return Task.CompletedTask; + } + protected virtual void OnSelectionBeforeInvoke(ConsoleSelectionMenuItem selection) { } @@ -61,6 +71,11 @@ internal virtual void InvokeSelection(ConsoleSelectionMenuItem selection) selection.Action?.Invoke(); } + protected virtual Task OnSelectionAsync(ConsoleSelectionMenuItem selection) + { + return Task.CompletedTask; + } + protected virtual void OnSelection(ConsoleSelectionMenuItem selection) { }