Skip to content

Commit

Permalink
Make the menus run synchronously
Browse files Browse the repository at this point in the history
  • Loading branch information
YuriyDurov committed Jan 12, 2024
1 parent b9ce7ea commit f40d195
Show file tree
Hide file tree
Showing 8 changed files with 38 additions and 65 deletions.
4 changes: 2 additions & 2 deletions sample/BitzArt.Console.TestApp/Menus/0.MainMenu.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
internal class MainMenu : ConsoleSelectionMenu
{
[MenuSelectionItem("Veggies")]
public async Task SubmenuVeggiesAsync()
public void NavigateToVeggiesMenu()
{
await RunAsync<VeggiesMenu>();
Run<VeggiesMenu>();
}
}
4 changes: 2 additions & 2 deletions sample/BitzArt.Console.TestApp/Menus/1.VeggiesMenu.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@ public void Potato()
_focusOnPotato = true;
}

protected override void OnSelectionBeforeInvoke(ConsoleSelectionMenuItem selection)
protected override void OnSelection(ConsoleSelectionMenuItem selection)
{
_focusOnPotato = false;
}

protected override void OnSelection(ConsoleSelectionMenuItem selection)
protected override void OnSelectionInvoked(ConsoleSelectionMenuItem selection)
{
AnsiConsole.WriteLine($"You selected '{selection.Name}'");

Expand Down
12 changes: 6 additions & 6 deletions src/BitzArt.Console/App/ConsoleApp.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,22 @@ public ConsoleApp(IConsoleAppBuilder builder)
Services = builder.Services.BuildServiceProvider();
}

public void Run(CancellationToken cancellationToken = default)
public void Run()
{
var navigationManager = Services.GetRequiredService<IConsoleAppNavigationManager>();
navigationManager.NavigateToMainMenuAsync().Wait(cancellationToken);
navigationManager.NavigateToMainMenu();
}

public void Run(Type mainMenuType, CancellationToken cancellationToken = default)
public void Run(Type mainMenuType)
{
var navigationManager = Services.GetRequiredService<IConsoleAppNavigationManager>();
navigationManager.NavigateAsync(mainMenuType).Wait(cancellationToken);
navigationManager.Navigate(mainMenuType);
}

public void Run<TConsoleMenu>(CancellationToken cancellationToken = default) where TConsoleMenu : class
public void Run<TConsoleMenu>() where TConsoleMenu : class
{
var navigationManager = Services.GetRequiredService<IConsoleAppNavigationManager>();
navigationManager.NavigateAsync<TConsoleMenu>().Wait(cancellationToken);
navigationManager.Navigate<TConsoleMenu>();
}

public static IConsoleAppBuilder CreateBuilder()
Expand Down
8 changes: 4 additions & 4 deletions src/BitzArt.Console/Extensions/RunConsoleMenuExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ namespace BitzArt.Console;

public static class RunConsoleMenuExtension
{
internal static async Task RunConsoleMenuAsync<TConsoleMenu>(this IServiceProvider serviceProvider)
internal static void RunConsoleMenuAsync<TConsoleMenu>(this IServiceProvider serviceProvider)
where TConsoleMenu : class
{
var menu = serviceProvider.GetRequiredService<TConsoleMenu>();
Expand All @@ -14,18 +14,18 @@ internal static async Task RunConsoleMenuAsync<TConsoleMenu>(this IServiceProvid

consoleMenu.Populate(serviceProvider);

await consoleMenu.RunAsync();
consoleMenu.Run();
}

internal static async Task RunConsoleMenuAsync(this IServiceProvider serviceProvider, Type menuType)
internal static void RunConsoleMenuAsync(this IServiceProvider serviceProvider, Type menuType)
{
var menu = serviceProvider.GetRequiredService(menuType);

if (menu is not ConsoleMenu consoleMenu) return;

consoleMenu.Populate(serviceProvider);

await consoleMenu.RunAsync();
consoleMenu.Run();
}

private static void Populate(this ConsoleMenu menu, IServiceProvider serviceProvider)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

public interface IConsoleAppNavigationManager
{
Task NavigateAsync(Type menuType);
Task NavigateAsync<T>() where T : class;
Task NavigateToMainMenuAsync();
public void Navigate(Type menuType);
public void Navigate<T>() where T : class;
public void NavigateToMainMenu();
}
29 changes: 7 additions & 22 deletions src/BitzArt.Console/Menus/ConsoleMenu.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,18 @@ public abstract class ConsoleMenu

public bool? IsMainMenu { get; internal set; }

public async Task RunAsync()
public void Run()
{
await RenderAsync();
Render();
}

internal virtual async Task RenderAsync()
internal virtual void Render()
{
AnsiConsole.Clear();
DisplayTitle();

OnRender();
await OnRenderAsync();

await DisplayAsync();

await OnDisplayAsync();
Display();
OnDisplay();
}

Expand All @@ -37,32 +33,21 @@ internal virtual void DisplayTitle()
AnsiConsole.WriteLine();
}

protected virtual Task OnRenderAsync()
{
return Task.CompletedTask;
}

protected virtual void OnRender()
{
}

internal virtual Task DisplayAsync()
{
return Task.CompletedTask;
}

protected virtual Task OnDisplayAsync()
internal virtual void Display()
{
return Task.CompletedTask;
}

protected virtual void OnDisplay()
{
}

public async Task RunAsync<TConsoleMenu>() where TConsoleMenu : class
public void Run<TConsoleMenu>() where TConsoleMenu : class
{
var navigationManager = App!.Services.GetRequiredService<IConsoleAppNavigationManager>();
await navigationManager.NavigateAsync<TConsoleMenu>();
navigationManager.Navigate<TConsoleMenu>();
}
}
28 changes: 8 additions & 20 deletions src/BitzArt.Console/Menus/ConsoleSelectionMenu.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,24 @@ public void AddSubmenu<TMenu>(string? selectionName = null)
where TMenu : class
{
selectionName ??= typeof(TMenu).Name;
AddSelection(selectionName, async () => await RunAsync<TMenu>());
AddSelection(selectionName, Run<TMenu>);
}

public void AddSelection(string name, Action? action = null, bool pauseOnComplete = false)
{
SelectionItems.Add(new ConsoleSelectionMenuItem(name, action, pauseOnComplete));
}

internal override async Task RenderAsync()
internal override void Render()
{
while(true)
{
if (_exiting) break;
await base.RenderAsync();
base.Render();
}
}

internal override async Task DisplayAsync()
internal override void Display()
{
var selectionPrompt = new SelectionPrompt<ConsoleSelectionMenuItem>();

Expand All @@ -46,23 +46,16 @@ internal override async Task DisplayAsync()
return;
}

await OnSelectionBeforeInvokeAsync(selected);
OnSelectionBeforeInvoke(selected);
OnSelection(selected);

InvokeSelection(selected);

await OnSelectionAsync(selected);
OnSelection(selected);
OnSelectionInvoked(selected);

AfterSelectionInvoked(selected);
}

protected virtual Task OnSelectionBeforeInvokeAsync(ConsoleSelectionMenuItem selection)
{
return Task.CompletedTask;
}

protected virtual void OnSelectionBeforeInvoke(ConsoleSelectionMenuItem selection)
protected virtual void OnSelection(ConsoleSelectionMenuItem selection)
{
}

Expand All @@ -71,12 +64,7 @@ internal virtual void InvokeSelection(ConsoleSelectionMenuItem selection)
selection.Action?.Invoke();
}

protected virtual Task OnSelectionAsync(ConsoleSelectionMenuItem selection)
{
return Task.CompletedTask;
}

protected virtual void OnSelection(ConsoleSelectionMenuItem selection)
protected virtual void OnSelectionInvoked(ConsoleSelectionMenuItem selection)
{
}

Expand Down
12 changes: 6 additions & 6 deletions src/BitzArt.Console/Services/ConsoleAppNavigationManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,20 @@ internal class ConsoleAppNavigationManager(IServiceProvider serviceProvider) : I
{
private readonly IServiceProvider _serviceProvider = serviceProvider;

public async Task NavigateToMainMenuAsync()
public void NavigateToMainMenu()
{
var map = _serviceProvider.GetRequiredService<MenuMap>();
var mainMenu = map.GetMainMenuItem();
await _serviceProvider.RunConsoleMenuAsync(mainMenu.MenuType);
_serviceProvider.RunConsoleMenuAsync(mainMenu.MenuType);
}

public async Task NavigateAsync<T>() where T : class
public void Navigate<T>() where T : class
{
await _serviceProvider.RunConsoleMenuAsync<T>();
_serviceProvider.RunConsoleMenuAsync<T>();
}

public async Task NavigateAsync(Type menuType)
public void Navigate(Type menuType)
{
await _serviceProvider.RunConsoleMenuAsync(menuType);
_serviceProvider.RunConsoleMenuAsync(menuType);
}
}

0 comments on commit f40d195

Please sign in to comment.