AI-Studio/app/MindWork AI Studio/Tools/Services/GlobalShortcutService.cs

265 lines
12 KiB
C#
Raw Normal View History

2026-01-24 19:05:34 +00:00
using AIStudio.Settings;
using AIStudio.Settings.DataModel;
2026-07-18 15:42:38 +00:00
using AIStudio.Tools.PluginSystem;
2026-01-24 19:05:34 +00:00
using AIStudio.Tools.Rust;
using Microsoft.AspNetCore.Components;
namespace AIStudio.Tools.Services;
public sealed class GlobalShortcutService : BackgroundService, IMessageBusReceiver
{
2026-03-10 19:50:45 +00:00
private static bool IS_STARTUP_COMPLETED;
2026-01-24 19:05:34 +00:00
2026-03-10 19:50:45 +00:00
private enum ShortcutSyncSource
{
CONFIGURATION_CHANGED,
STARTUP_COMPLETED,
PLUGINS_RELOADED,
VOICE_RECORDING_AVAILABILITY_CHANGED,
2026-03-10 19:50:45 +00:00
}
private readonly SemaphoreSlim registrationSemaphore = new(1, 1);
2026-07-18 15:42:38 +00:00
private readonly Dictionary<Shortcut, ShortcutState> lastSentStates = [];
private readonly Dictionary<Shortcut, string> lastNonEmptyShortcuts = [];
2026-01-24 19:05:34 +00:00
private readonly ILogger<GlobalShortcutService> logger;
private readonly SettingsManager settingsManager;
private readonly MessageBus messageBus;
private readonly RustService rustService;
private readonly VoiceRecordingAvailabilityService voiceRecordingAvailabilityService;
2026-01-24 19:05:34 +00:00
public GlobalShortcutService(
ILogger<GlobalShortcutService> logger,
SettingsManager settingsManager,
MessageBus messageBus,
RustService rustService,
VoiceRecordingAvailabilityService voiceRecordingAvailabilityService)
2026-01-24 19:05:34 +00:00
{
this.logger = logger;
this.settingsManager = settingsManager;
this.messageBus = messageBus;
this.rustService = rustService;
this.voiceRecordingAvailabilityService = voiceRecordingAvailabilityService;
2026-01-24 19:05:34 +00:00
this.messageBus.RegisterComponent(this);
2026-07-18 15:42:38 +00:00
this.ApplyFilters([], [Event.CONFIGURATION_CHANGED, Event.PLUGINS_RELOADED, Event.STARTUP_COMPLETED, Event.TAURI_EVENT_RECEIVED, Event.VOICE_RECORDING_AVAILABILITY_CHANGED]);
2026-01-24 19:05:34 +00:00
}
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
2026-03-10 19:50:45 +00:00
this.logger.LogInformation("The global shortcut service was initialized.");
await Task.Delay(Timeout.InfiniteTimeSpan, stoppingToken);
2026-01-24 19:05:34 +00:00
}
public override async Task StopAsync(CancellationToken cancellationToken)
{
this.messageBus.Unregister(this);
2026-03-10 19:50:45 +00:00
this.registrationSemaphore.Dispose();
2026-01-24 19:05:34 +00:00
await base.StopAsync(cancellationToken);
}
#region IMessageBusReceiver
public async Task ProcessMessage<T>(ComponentBase? sendingComponent, Event triggeredEvent, T? data)
{
switch (triggeredEvent)
{
case Event.CONFIGURATION_CHANGED:
2026-03-10 19:50:45 +00:00
if (!IS_STARTUP_COMPLETED)
return;
await this.RegisterAllShortcuts(ShortcutSyncSource.CONFIGURATION_CHANGED);
break;
case Event.STARTUP_COMPLETED:
IS_STARTUP_COMPLETED = true;
await this.RegisterAllShortcuts(ShortcutSyncSource.STARTUP_COMPLETED);
break;
case Event.PLUGINS_RELOADED:
if (!IS_STARTUP_COMPLETED)
return;
await this.RegisterAllShortcuts(ShortcutSyncSource.PLUGINS_RELOADED);
2026-01-24 19:05:34 +00:00
break;
case Event.VOICE_RECORDING_AVAILABILITY_CHANGED:
if (!IS_STARTUP_COMPLETED)
return;
await this.RegisterAllShortcuts(ShortcutSyncSource.VOICE_RECORDING_AVAILABILITY_CHANGED);
break;
2026-07-18 15:42:38 +00:00
case Event.TAURI_EVENT_RECEIVED:
if (data is TauriEvent tauriEvent
&& tauriEvent.TryGetShortcutChange(out var shortcutId, out var effectiveDisplayName))
{
await this.UpdateEffectiveDisplayName(shortcutId, effectiveDisplayName);
}
break;
2026-01-24 19:05:34 +00:00
}
}
public Task<TResult?> ProcessMessageWithResult<TPayload, TResult>(ComponentBase? sendingComponent, Event triggeredEvent, TPayload? data) => Task.FromResult<TResult?>(default);
#endregion
2026-03-10 19:50:45 +00:00
private async Task RegisterAllShortcuts(ShortcutSyncSource source)
2026-01-24 19:05:34 +00:00
{
2026-03-10 19:50:45 +00:00
await this.registrationSemaphore.WaitAsync();
try
2026-01-24 19:05:34 +00:00
{
2026-03-10 19:50:45 +00:00
this.logger.LogInformation("Registering global shortcuts (source='{Source}').", source);
foreach (var shortcutId in Enum.GetValues<Shortcut>())
2026-01-24 19:05:34 +00:00
{
2026-03-10 19:50:45 +00:00
if(shortcutId is Shortcut.NONE)
continue;
var shortcutState = await this.GetShortcutState(shortcutId, source);
var shortcut = shortcutState.Shortcut;
var isEnabled = shortcutState.IsEnabled;
2026-07-18 15:42:38 +00:00
var requestedState = new ShortcutState(isEnabled ? shortcut : string.Empty, isEnabled, shortcutState.UsesPersistedFallback);
2026-03-10 19:50:45 +00:00
this.logger.LogInformation(
"Sync shortcut '{ShortcutId}' (source='{Source}', enabled={IsEnabled}, configured='{Shortcut}').",
shortcutId,
source,
isEnabled,
shortcut);
if (shortcutState.UsesPersistedFallback)
{
this.logger.LogWarning(
"Using persisted shortcut fallback for '{ShortcutId}' during startup completion (source='{Source}', configured='{Shortcut}').",
shortcutId,
source,
shortcut);
}
2026-07-18 15:42:38 +00:00
if (this.lastSentStates.TryGetValue(shortcutId, out var lastSentState)
&& lastSentState.Shortcut == requestedState.Shortcut
&& lastSentState.IsEnabled == requestedState.IsEnabled)
2026-03-10 19:50:45 +00:00
{
2026-07-18 15:42:38 +00:00
this.logger.LogDebug("Skipping unchanged global shortcut '{ShortcutId}'.", shortcutId);
continue;
2026-03-10 19:50:45 +00:00
}
2026-07-18 15:42:38 +00:00
var description = await this.GetShortcutDescription(shortcutId);
var reconfigure = !string.IsNullOrWhiteSpace(requestedState.Shortcut)
&& this.lastNonEmptyShortcuts.TryGetValue(shortcutId, out var lastNonEmptyShortcut)
&& !string.Equals(lastNonEmptyShortcut, requestedState.Shortcut, StringComparison.Ordinal);
var result = await this.rustService.UpdateGlobalShortcut(shortcutId, requestedState.Shortcut, description, reconfigure);
this.lastSentStates[shortcutId] = requestedState;
if (!string.IsNullOrWhiteSpace(requestedState.Shortcut))
this.lastNonEmptyShortcuts[shortcutId] = requestedState.Shortcut;
if (result.Success)
2026-03-10 19:50:45 +00:00
{
this.logger.LogInformation(
2026-07-18 15:42:38 +00:00
"Global shortcut '{ShortcutId}' ({Shortcut}) synchronized through {Backend}.",
2026-03-10 19:50:45 +00:00
shortcutId,
2026-07-18 15:42:38 +00:00
requestedState.Shortcut,
result.Backend);
2026-03-10 19:50:45 +00:00
2026-07-18 15:42:38 +00:00
if (result.Backend is ShortcutBackend.PORTAL)
await this.UpdateEffectiveDisplayName(shortcutId, result.EffectiveDisplayName);
}
else
{
var userMessage = result.Cancelled
? TB("The global shortcut change was cancelled. The previous shortcut remains active.")
: TB("The global shortcut could not be registered. The previous shortcut remains active.");
this.logger.LogWarning(
"Failed to synchronize global shortcut '{ShortcutId}' ({Shortcut}, backend={Backend}, cancelled={Cancelled}): {Error}",
shortcutId,
requestedState.Shortcut,
result.Backend,
result.Cancelled,
result.ErrorMessage);
if (result.Cancelled)
await this.messageBus.SendWarning(new(Icons.Material.Filled.Keyboard, userMessage));
else
await this.messageBus.SendError(new(Icons.Material.Filled.Keyboard, userMessage));
2026-03-10 19:50:45 +00:00
}
2026-01-24 19:05:34 +00:00
}
2026-03-10 19:50:45 +00:00
this.logger.LogInformation("Global shortcuts registration completed (source='{Source}').", source);
}
finally
{
this.registrationSemaphore.Release();
}
2026-01-24 19:05:34 +00:00
}
private string GetShortcutValue(Shortcut name) => name switch
{
Shortcut.VOICE_RECORDING_TOGGLE => this.settingsManager.ConfigurationData.App.ShortcutVoiceRecording,
_ => string.Empty,
};
private bool IsShortcutAllowed(Shortcut name) => name switch
{
// Voice recording is a preview feature:
Shortcut.VOICE_RECORDING_TOGGLE => PreviewFeatures.PRE_SPEECH_TO_TEXT_2026.IsEnabled(this.settingsManager)
&& this.voiceRecordingAvailabilityService.IsAvailable,
2026-01-24 19:05:34 +00:00
// Other shortcuts are always allowed:
_ => true,
};
2026-07-18 15:42:38 +00:00
private async Task<string> GetShortcutDescription(Shortcut shortcutId)
{
var language = await this.settingsManager.GetActiveLanguagePlugin();
return shortcutId switch
{
Shortcut.VOICE_RECORDING_TOGGLE => I18N.I.GetText(language, "Toggle voice recording", typeof(GlobalShortcutService).Namespace, nameof(GlobalShortcutService)),
_ => I18N.I.GetText(language, "Global shortcut", typeof(GlobalShortcutService).Namespace, nameof(GlobalShortcutService)),
};
}
private async Task UpdateEffectiveDisplayName(Shortcut shortcutId, string effectiveDisplayName)
{
if (shortcutId is not Shortcut.VOICE_RECORDING_TOGGLE || string.IsNullOrWhiteSpace(effectiveDisplayName))
return;
var configuredShortcut = this.settingsManager.ConfigurationData.App.ShortcutVoiceRecording;
if (this.settingsManager.ConfigurationData.App.ShortcutVoiceRecordingDisplayName == effectiveDisplayName
&& this.settingsManager.ConfigurationData.App.ShortcutVoiceRecordingDisplaySource == configuredShortcut)
return;
this.settingsManager.ConfigurationData.App.ShortcutVoiceRecordingDisplayName = effectiveDisplayName;
this.settingsManager.ConfigurationData.App.ShortcutVoiceRecordingDisplaySource = configuredShortcut;
await this.settingsManager.StoreSettings();
await this.messageBus.SendMessage<bool>(null, Event.GLOBAL_SHORTCUT_CHANGED);
}
private static string TB(string fallbackEN) => I18N.I.T(fallbackEN, typeof(GlobalShortcutService).Namespace, nameof(GlobalShortcutService));
2026-03-10 19:50:45 +00:00
private async Task<ShortcutState> GetShortcutState(Shortcut shortcutId, ShortcutSyncSource source)
{
var shortcut = this.GetShortcutValue(shortcutId);
var isEnabled = this.IsShortcutAllowed(shortcutId);
if (isEnabled && !string.IsNullOrWhiteSpace(shortcut))
return new(shortcut, true, false);
if (source is not ShortcutSyncSource.STARTUP_COMPLETED || shortcutId is not Shortcut.VOICE_RECORDING_TOGGLE)
return new(shortcut, isEnabled, false);
var settingsSnapshot = await this.settingsManager.TryReadSettingsSnapshot();
if (settingsSnapshot is null)
return new(shortcut, isEnabled, false);
var fallbackShortcut = settingsSnapshot.App.ShortcutVoiceRecording;
var fallbackEnabled = !string.IsNullOrWhiteSpace(settingsSnapshot.App.UseTranscriptionProvider);
2026-03-10 19:50:45 +00:00
if (!fallbackEnabled || string.IsNullOrWhiteSpace(fallbackShortcut))
return new(shortcut, isEnabled, false);
return new(fallbackShortcut, true, true);
}
private readonly record struct ShortcutState(string Shortcut, bool IsEnabled, bool UsesPersistedFallback);
2026-07-18 15:42:38 +00:00
}