Skip to content

Commit

Permalink
Add async menu flow interception
Browse files Browse the repository at this point in the history
  • Loading branch information
YuriyDurov committed Jan 12, 2024
1 parent ef713d0 commit b9ce7ea
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 13 deletions.
35 changes: 25 additions & 10 deletions src/BitzArt.Console/Menus/ConsoleMenu.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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()
{
}

Expand Down
21 changes: 18 additions & 3 deletions src/BitzArt.Console/Menus/ConsoleSelectionMenu.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<ConsoleSelectionMenuItem>();

Expand All @@ -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)
{
}
Expand All @@ -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)
{
}
Expand Down

0 comments on commit b9ce7ea

Please sign in to comment.