using AIStudio.Dialogs; using Microsoft.AspNetCore.Components; using DialogOptions = AIStudio.Dialogs.DialogOptions; namespace AIStudio.Components; /// /// A configuration component for capturing and displaying keyboard shortcuts. /// public partial class ConfigurationShortcut : ConfigurationBaseCore { [Inject] private IDialogService DialogService { get; init; } = null!; /// /// The current shortcut value. /// [Parameter] public Func Shortcut { get; set; } = () => string.Empty; /// /// An action which is called when the shortcut was changed. /// [Parameter] public Func ShortcutUpdate { get; set; } = _ => Task.CompletedTask; /// /// The name/identifier of the shortcut (used for conflict detection and registration). /// [Parameter] public string ShortcutName { get; set; } = string.Empty; /// /// The icon to display. /// [Parameter] public string Icon { get; set; } = Icons.Material.Filled.Keyboard; /// /// The color of the icon. /// [Parameter] public Color IconColor { get; set; } = Color.Default; #region Overrides of ConfigurationBase protected override bool Stretch => true; protected override Variant Variant => Variant.Outlined; protected override string Label => this.OptionDescription; #endregion private string GetDisplayShortcut() { var shortcut = this.Shortcut(); if (string.IsNullOrWhiteSpace(shortcut)) return string.Empty; // Convert internal format to display format return shortcut .Replace("CmdOrControl", OperatingSystem.IsMacOS() ? "Cmd" : "Ctrl") .Replace("CommandOrControl", OperatingSystem.IsMacOS() ? "Cmd" : "Ctrl"); } private async Task OpenDialog() { var currentShortcut = this.Shortcut(); var dialogParameters = new DialogParameters { { x => x.InitialShortcut, currentShortcut }, { x => x.ShortcutName, this.ShortcutName }, }; var dialogReference = await this.DialogService.ShowAsync( this.T("Configure Keyboard Shortcut"), dialogParameters, DialogOptions.FULLSCREEN); var dialogResult = await dialogReference.Result; if (dialogResult is null || dialogResult.Canceled) return; if (dialogResult.Data is string newShortcut) { await this.ShortcutUpdate(newShortcut); await this.SettingsManager.StoreSettings(); await this.InformAboutChange(); } } }