mirror of
https://github.com/MindWorkAI/AI-Studio.git
synced 2026-10-15 20:53:47 +00:00
Improved the shortcut handling (#684)
Build and Release / Read metadata (push) Waiting to run
Build and Release / Build app (${{ matrix.dotnet_runtime }}) (-aarch64-apple-darwin, osx-arm64, macos-latest, aarch64-apple-darwin, dmg updater) (push) Blocked by required conditions
Build and Release / Build app (${{ matrix.dotnet_runtime }}) (-aarch64-pc-windows-msvc.exe, win-arm64, windows-latest, aarch64-pc-windows-msvc, nsis updater) (push) Blocked by required conditions
Build and Release / Build app (${{ matrix.dotnet_runtime }}) (-aarch64-unknown-linux-gnu, linux-arm64, ubuntu-22.04-arm, aarch64-unknown-linux-gnu, appimage deb updater) (push) Blocked by required conditions
Build and Release / Build app (${{ matrix.dotnet_runtime }}) (-x86_64-apple-darwin, osx-x64, macos-latest, x86_64-apple-darwin, dmg updater) (push) Blocked by required conditions
Build and Release / Build app (${{ matrix.dotnet_runtime }}) (-x86_64-pc-windows-msvc.exe, win-x64, windows-latest, x86_64-pc-windows-msvc, nsis updater) (push) Blocked by required conditions
Build and Release / Build app (${{ matrix.dotnet_runtime }}) (-x86_64-unknown-linux-gnu, linux-x64, ubuntu-22.04, x86_64-unknown-linux-gnu, appimage deb updater) (push) Blocked by required conditions
Build and Release / Prepare & create release (push) Blocked by required conditions
Build and Release / Publish release (push) Blocked by required conditions
Build and Release / Read metadata (push) Waiting to run
Build and Release / Build app (${{ matrix.dotnet_runtime }}) (-aarch64-apple-darwin, osx-arm64, macos-latest, aarch64-apple-darwin, dmg updater) (push) Blocked by required conditions
Build and Release / Build app (${{ matrix.dotnet_runtime }}) (-aarch64-pc-windows-msvc.exe, win-arm64, windows-latest, aarch64-pc-windows-msvc, nsis updater) (push) Blocked by required conditions
Build and Release / Build app (${{ matrix.dotnet_runtime }}) (-aarch64-unknown-linux-gnu, linux-arm64, ubuntu-22.04-arm, aarch64-unknown-linux-gnu, appimage deb updater) (push) Blocked by required conditions
Build and Release / Build app (${{ matrix.dotnet_runtime }}) (-x86_64-apple-darwin, osx-x64, macos-latest, x86_64-apple-darwin, dmg updater) (push) Blocked by required conditions
Build and Release / Build app (${{ matrix.dotnet_runtime }}) (-x86_64-pc-windows-msvc.exe, win-x64, windows-latest, x86_64-pc-windows-msvc, nsis updater) (push) Blocked by required conditions
Build and Release / Build app (${{ matrix.dotnet_runtime }}) (-x86_64-unknown-linux-gnu, linux-x64, ubuntu-22.04, x86_64-unknown-linux-gnu, appimage deb updater) (push) Blocked by required conditions
Build and Release / Prepare & create release (push) Blocked by required conditions
Build and Release / Publish release (push) Blocked by required conditions
This commit is contained in:
1 parent
7e5e078cdb
commit
0c702c579b
7 files changed
+156
-80
No files matched your search
@@ -9,6 +9,7 @@ public enum Event
|
||||
CONFIGURATION_CHANGED,
|
||||
COLOR_THEME_CHANGED,
|
||||
STARTUP_PLUGIN_SYSTEM,
|
||||
STARTUP_COMPLETED,
|
||||
STARTUP_ENTERPRISE_ENVIRONMENT,
|
||||
PLUGINS_RELOADED,
|
||||
SHOW_ERROR,
|
||||
|
||||
@@ -8,8 +8,16 @@ namespace AIStudio.Tools.Services;
|
||||
|
||||
public sealed class GlobalShortcutService : BackgroundService, IMessageBusReceiver
|
||||
{
|
||||
private static bool IS_INITIALIZED;
|
||||
private static bool IS_STARTUP_COMPLETED;
|
||||
|
||||
private enum ShortcutSyncSource
|
||||
{
|
||||
CONFIGURATION_CHANGED,
|
||||
STARTUP_COMPLETED,
|
||||
PLUGINS_RELOADED,
|
||||
}
|
||||
|
||||
private readonly SemaphoreSlim registrationSemaphore = new(1, 1);
|
||||
private readonly ILogger<GlobalShortcutService> logger;
|
||||
private readonly SettingsManager settingsManager;
|
||||
private readonly MessageBus messageBus;
|
||||
@@ -27,22 +35,19 @@ public sealed class GlobalShortcutService : BackgroundService, IMessageBusReceiv
|
||||
this.rustService = rustService;
|
||||
|
||||
this.messageBus.RegisterComponent(this);
|
||||
this.ApplyFilters([], [Event.CONFIGURATION_CHANGED]);
|
||||
this.ApplyFilters([], [Event.CONFIGURATION_CHANGED, Event.PLUGINS_RELOADED, Event.STARTUP_COMPLETED]);
|
||||
}
|
||||
|
||||
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
|
||||
{
|
||||
// Wait until the app is fully initialized:
|
||||
while (!stoppingToken.IsCancellationRequested && !IS_INITIALIZED)
|
||||
await Task.Delay(TimeSpan.FromSeconds(1), stoppingToken);
|
||||
|
||||
// Register shortcuts on startup:
|
||||
await this.RegisterAllShortcuts();
|
||||
this.logger.LogInformation("The global shortcut service was initialized.");
|
||||
await Task.Delay(Timeout.InfiniteTimeSpan, stoppingToken);
|
||||
}
|
||||
|
||||
public override async Task StopAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
this.messageBus.Unregister(this);
|
||||
this.registrationSemaphore.Dispose();
|
||||
await base.StopAsync(cancellationToken);
|
||||
}
|
||||
|
||||
@@ -53,7 +58,22 @@ public sealed class GlobalShortcutService : BackgroundService, IMessageBusReceiv
|
||||
switch (triggeredEvent)
|
||||
{
|
||||
case Event.CONFIGURATION_CHANGED:
|
||||
await this.RegisterAllShortcuts();
|
||||
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);
|
||||
break;
|
||||
}
|
||||
}
|
||||
@@ -62,33 +82,64 @@ public sealed class GlobalShortcutService : BackgroundService, IMessageBusReceiv
|
||||
|
||||
#endregion
|
||||
|
||||
private async Task RegisterAllShortcuts()
|
||||
private async Task RegisterAllShortcuts(ShortcutSyncSource source)
|
||||
{
|
||||
this.logger.LogInformation("Registering global shortcuts.");
|
||||
foreach (var shortcutId in Enum.GetValues<Shortcut>())
|
||||
await this.registrationSemaphore.WaitAsync();
|
||||
try
|
||||
{
|
||||
if(shortcutId is Shortcut.NONE)
|
||||
continue;
|
||||
|
||||
var shortcut = this.GetShortcutValue(shortcutId);
|
||||
var isEnabled = this.IsShortcutAllowed(shortcutId);
|
||||
|
||||
if (isEnabled && !string.IsNullOrWhiteSpace(shortcut))
|
||||
this.logger.LogInformation("Registering global shortcuts (source='{Source}').", source);
|
||||
foreach (var shortcutId in Enum.GetValues<Shortcut>())
|
||||
{
|
||||
var success = await this.rustService.UpdateGlobalShortcut(shortcutId, shortcut);
|
||||
if (success)
|
||||
this.logger.LogInformation("Global shortcut '{ShortcutId}' ({Shortcut}) registered.", shortcutId, shortcut);
|
||||
if(shortcutId is Shortcut.NONE)
|
||||
continue;
|
||||
|
||||
var shortcutState = await this.GetShortcutState(shortcutId, source);
|
||||
var shortcut = shortcutState.Shortcut;
|
||||
var isEnabled = shortcutState.IsEnabled;
|
||||
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);
|
||||
}
|
||||
|
||||
if (isEnabled && !string.IsNullOrWhiteSpace(shortcut))
|
||||
{
|
||||
var success = await this.rustService.UpdateGlobalShortcut(shortcutId, shortcut);
|
||||
if (success)
|
||||
this.logger.LogInformation("Global shortcut '{ShortcutId}' ({Shortcut}) registered.", shortcutId, shortcut);
|
||||
else
|
||||
this.logger.LogWarning("Failed to register global shortcut '{ShortcutId}' ({Shortcut}).", shortcutId, shortcut);
|
||||
}
|
||||
else
|
||||
this.logger.LogWarning("Failed to register global shortcut '{ShortcutId}' ({Shortcut}).", shortcutId, shortcut);
|
||||
}
|
||||
else
|
||||
{
|
||||
// Disable the shortcut when empty or feature is disabled:
|
||||
await this.rustService.UpdateGlobalShortcut(shortcutId, string.Empty);
|
||||
}
|
||||
}
|
||||
{
|
||||
this.logger.LogInformation(
|
||||
"Disabling global shortcut '{ShortcutId}' (source='{Source}', enabled={IsEnabled}, configured='{Shortcut}').",
|
||||
shortcutId,
|
||||
source,
|
||||
isEnabled,
|
||||
shortcut);
|
||||
|
||||
this.logger.LogInformation("Global shortcuts registration completed.");
|
||||
// Disable the shortcut when empty or feature is disabled:
|
||||
await this.rustService.UpdateGlobalShortcut(shortcutId, string.Empty);
|
||||
}
|
||||
}
|
||||
|
||||
this.logger.LogInformation("Global shortcuts registration completed (source='{Source}').", source);
|
||||
}
|
||||
finally
|
||||
{
|
||||
this.registrationSemaphore.Release();
|
||||
}
|
||||
}
|
||||
|
||||
private string GetShortcutValue(Shortcut name) => name switch
|
||||
@@ -107,5 +158,30 @@ public sealed class GlobalShortcutService : BackgroundService, IMessageBusReceiv
|
||||
_ => true,
|
||||
};
|
||||
|
||||
public static void Initialize() => IS_INITIALIZED = true;
|
||||
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 =
|
||||
settingsSnapshot.App.EnabledPreviewFeatures.Contains(PreviewFeatures.PRE_SPEECH_TO_TEXT_2026) &&
|
||||
!string.IsNullOrWhiteSpace(settingsSnapshot.App.UseTranscriptionProvider);
|
||||
|
||||
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);
|
||||
}
|
||||
Reference in new issue
Block a user