Skip to content

Commit

Permalink
More interception points on Menu Render
Browse files Browse the repository at this point in the history
  • Loading branch information
YuriyDurov committed Jan 12, 2024
1 parent d03b4ef commit 47023d3
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 16 deletions.
15 changes: 15 additions & 0 deletions src/BitzArt.Console/Menus/ConsoleMenu.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,24 @@ public Task RunAsync()
protected virtual void Render()
{
AnsiConsole.Clear();
OnBeforeDisplay();
Display();
OnDisplayed();
}

protected virtual void OnBeforeDisplay()
{
}

protected virtual void Display()
{
AnsiConsoleMenu.WriteTitle(Title!);
}

protected virtual void OnDisplayed()
{
}

public async Task RunAsync<TConsoleMenu>() where TConsoleMenu : class
{
var navigationManager = App!.Services.GetRequiredService<IConsoleAppNavigationManager>();
Expand Down
38 changes: 22 additions & 16 deletions src/BitzArt.Console/Menus/ConsoleSelectionMenu.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ namespace BitzArt.Console;
public abstract class ConsoleSelectionMenu : ConsoleMenu
{
protected virtual List<ConsoleSelectionMenuItem> SelectionItems { get; set; } = [];
private bool _exiting = false;

public void AddSubmenu<TMenu>(string? selectionName = null)
where TMenu : class
Expand All @@ -22,28 +23,33 @@ protected override void Render()
{
while(true)
{
AnsiConsole.Clear();

var selectionPrompt = new SelectionPrompt<ConsoleSelectionMenuItem>().Title($"[green]{Title}[/]");
if (_exiting) break;
base.Render();
}
}

selectionPrompt.AddChoices(SelectionItems);
protected override void Display()
{
var selectionPrompt = new SelectionPrompt<ConsoleSelectionMenuItem>().Title($"[green]{Title}[/]");

var backSelectionOption = IsMainMenu!.Value ? ConsoleSelectionMenuItem.ExitItem : ConsoleSelectionMenuItem.BackItem;
selectionPrompt.AddChoice(backSelectionOption);
selectionPrompt.AddChoices(SelectionItems);

var selected = selectionPrompt.Show(AnsiConsole.Console);
var backSelectionOption = IsMainMenu!.Value ? ConsoleSelectionMenuItem.ExitItem : ConsoleSelectionMenuItem.BackItem;
selectionPrompt.AddChoice(backSelectionOption);

if (selected.IsExit)
{
OnExit();
return;
}
var selected = selectionPrompt.Show(AnsiConsole.Console);

OnBeforeSelectionInvoke(selected);
InvokeSelection(selected);
OnSelection(selected);
OnAfterSelectionInvoked(selected);
if (selected.IsExit)
{
_exiting = true;
OnExit();
return;
}

OnBeforeSelectionInvoke(selected);
InvokeSelection(selected);
OnSelection(selected);
OnAfterSelectionInvoked(selected);
}

public virtual void OnBeforeSelectionInvoke(ConsoleSelectionMenuItem selection)
Expand Down

0 comments on commit 47023d3

Please sign in to comment.