Allow disabling configuration options for certain conditions

This commit is contained in:
Thorsten Sommer 2024-07-27 14:08:51 +02:00
parent af7532a83b
commit 6017bc5dbc
Signed by: tsommer
GPG Key ID: 371BBA77A02C0108
6 changed files with 95 additions and 6 deletions

View File

@ -1,7 +1,7 @@
@inherits ConfigurationBase @inherits ConfigurationBase
<MudField Label="@this.OptionDescription" Variant="Variant.Outlined" HelperText="@this.OptionHelp" Class="@MARGIN_CLASS"> <MudField Disabled="@this.Disabled()" Label="@this.OptionDescription" Variant="Variant.Outlined" HelperText="@this.OptionHelp" Class="@MARGIN_CLASS">
<MudSwitch T="bool" Value="@this.State()" ValueChanged="@(updatedState => this.OptionChanged(updatedState))" Color="Color.Primary"> <MudSwitch T="bool" Disabled="@this.Disabled()" Value="@this.State()" ValueChanged="@(updatedState => this.OptionChanged(updatedState))" Color="Color.Primary">
@(this.State() ? this.LabelOn : this.LabelOff) @(this.State() ? this.LabelOn : this.LabelOff)
</MudSwitch> </MudSwitch>
</MudField> </MudField>

View File

@ -1,3 +1,5 @@
using AIStudio.Tools;
using Microsoft.AspNetCore.Components; using Microsoft.AspNetCore.Components;
namespace AIStudio.Components.Blocks; namespace AIStudio.Components.Blocks;
@ -5,7 +7,7 @@ namespace AIStudio.Components.Blocks;
/// <summary> /// <summary>
/// Configuration component for any boolean option. /// Configuration component for any boolean option.
/// </summary> /// </summary>
public partial class ConfigurationOption : ConfigurationBase public partial class ConfigurationOption : ConfigurationBase, IMessageBusReceiver
{ {
/// <summary> /// <summary>
/// Text to display when the option is true. /// Text to display when the option is true.
@ -30,6 +32,25 @@ public partial class ConfigurationOption : ConfigurationBase
/// </summary> /// </summary>
[Parameter] [Parameter]
public Action<bool> StateUpdate { get; set; } = _ => { }; public Action<bool> StateUpdate { get; set; } = _ => { };
/// <summary>
/// Is the option disabled?
/// </summary>
[Parameter]
public Func<bool> Disabled { get; set; } = () => false;
#region Overrides of ComponentBase
protected override async Task OnInitializedAsync()
{
// Register this component with the message bus:
this.MessageBus.RegisterComponent(this);
this.MessageBus.ApplyFilters(this, [], [ Event.CONFIGURATION_CHANGED ]);
await base.OnInitializedAsync();
}
#endregion
private async Task OptionChanged(bool updatedState) private async Task OptionChanged(bool updatedState)
{ {
@ -37,4 +58,25 @@ public partial class ConfigurationOption : ConfigurationBase
await this.SettingsManager.StoreSettings(); await this.SettingsManager.StoreSettings();
await this.InformAboutChange(); await this.InformAboutChange();
} }
#region Implementation of IMessageBusReceiver
public Task ProcessMessage<TMsg>(ComponentBase? sendingComponent, Event triggeredEvent, TMsg? data)
{
switch (triggeredEvent)
{
case Event.CONFIGURATION_CHANGED:
this.StateHasChanged();
break;
}
return Task.CompletedTask;
}
public Task<TResult?> ProcessMessageWithResult<TPayload, TResult>(ComponentBase? sendingComponent, Event triggeredEvent, TPayload? data)
{
return Task.FromResult<TResult?>(default);
}
#endregion
} }

View File

@ -1 +1 @@
<ConfigurationSelect OptionDescription="Preselected provider" OptionHelp="Select a provider that is preselected." Data="@this.Data" SelectedValue="@this.SelectedValue" SelectionUpdate="@this.SelectionUpdate"/> <ConfigurationSelect OptionDescription="Preselected provider" Disabled="@this.Disabled" OptionHelp="Select a provider that is preselected." Data="@this.Data" SelectedValue="@this.SelectedValue" SelectionUpdate="@this.SelectionUpdate"/>

View File

@ -16,6 +16,12 @@ public partial class ConfigurationProviderSelection : ComponentBase, IMessageBus
[Parameter] [Parameter]
public IEnumerable<ConfigurationSelectData<string>> Data { get; set; } = new List<ConfigurationSelectData<string>>(); public IEnumerable<ConfigurationSelectData<string>> Data { get; set; } = new List<ConfigurationSelectData<string>>();
/// <summary>
/// Is the selection component disabled?
/// </summary>
[Parameter]
public Func<bool> Disabled { get; set; } = () => false;
[Inject] [Inject]
private SettingsManager SettingsManager { get; init; } = null!; private SettingsManager SettingsManager { get; init; } = null!;

View File

@ -1,7 +1,7 @@
@inherits ConfigurationBase @inherits ConfigurationBase
@typeparam T @typeparam T
<MudSelect T="T" Value="@this.SelectedValue()" Strict="@true" Margin="Margin.Dense" Label="@this.OptionDescription" Class="@GetClass" Variant="Variant.Outlined" HelperText="@this.OptionHelp" ValueChanged="selectedValue => this.OptionChanged(selectedValue)"> <MudSelect T="T" Value="@this.SelectedValue()" Strict="@true" Disabled="@this.Disabled()" Margin="Margin.Dense" Label="@this.OptionDescription" Class="@GetClass" Variant="Variant.Outlined" HelperText="@this.OptionHelp" ValueChanged="selectedValue => this.OptionChanged(selectedValue)">
@foreach (var data in this.Data) @foreach (var data in this.Data)
{ {
<MudSelectItem Value="@data.Value"> <MudSelectItem Value="@data.Value">

View File

@ -1,4 +1,5 @@
using AIStudio.Settings; using AIStudio.Settings;
using AIStudio.Tools;
using Microsoft.AspNetCore.Components; using Microsoft.AspNetCore.Components;
@ -8,7 +9,7 @@ namespace AIStudio.Components.Blocks;
/// Configuration component for selecting a value from a list. /// Configuration component for selecting a value from a list.
/// </summary> /// </summary>
/// <typeparam name="T">The type of the value to select.</typeparam> /// <typeparam name="T">The type of the value to select.</typeparam>
public partial class ConfigurationSelect<T> : ConfigurationBase public partial class ConfigurationSelect<T> : ConfigurationBase, IMessageBusReceiver
{ {
/// <summary> /// <summary>
/// The data to select from. /// The data to select from.
@ -28,6 +29,25 @@ public partial class ConfigurationSelect<T> : ConfigurationBase
[Parameter] [Parameter]
public Action<T> SelectionUpdate { get; set; } = _ => { }; public Action<T> SelectionUpdate { get; set; } = _ => { };
/// <summary>
/// Is the selection component disabled?
/// </summary>
[Parameter]
public Func<bool> Disabled { get; set; } = () => false;
#region Overrides of ComponentBase
protected override async Task OnInitializedAsync()
{
// Register this component with the message bus:
this.MessageBus.RegisterComponent(this);
this.MessageBus.ApplyFilters(this, [], [ Event.CONFIGURATION_CHANGED ]);
await base.OnInitializedAsync();
}
#endregion
private async Task OptionChanged(T updatedValue) private async Task OptionChanged(T updatedValue)
{ {
this.SelectionUpdate(updatedValue); this.SelectionUpdate(updatedValue);
@ -36,4 +56,25 @@ public partial class ConfigurationSelect<T> : ConfigurationBase
} }
private static string GetClass => $"{MARGIN_CLASS} rounded-lg"; private static string GetClass => $"{MARGIN_CLASS} rounded-lg";
#region Implementation of IMessageBusReceiver
public Task ProcessMessage<TMsg>(ComponentBase? sendingComponent, Event triggeredEvent, TMsg? data)
{
switch (triggeredEvent)
{
case Event.CONFIGURATION_CHANGED:
this.StateHasChanged();
break;
}
return Task.CompletedTask;
}
public Task<TResult?> ProcessMessageWithResult<TPayload, TResult>(ComponentBase? sendingComponent, Event triggeredEvent, TPayload? data)
{
return Task.FromResult<TResult?>(default);
}
#endregion
} }