mirror of
https://github.com/MindWorkAI/AI-Studio.git
synced 2026-03-29 15:51:39 +00:00
Replaced the time-based solution using a startup completed event
This commit is contained in:
parent
29e651721e
commit
fa8cec4aa4
@ -97,7 +97,6 @@ public partial class MainLayout : LayoutComponentBase, IMessageBusReceiver, ILan
|
|||||||
|
|
||||||
// Set the snackbar for the update service:
|
// Set the snackbar for the update service:
|
||||||
UpdateService.SetBlazorDependencies(this.Snackbar);
|
UpdateService.SetBlazorDependencies(this.Snackbar);
|
||||||
GlobalShortcutService.Initialize();
|
|
||||||
TemporaryChatService.Initialize();
|
TemporaryChatService.Initialize();
|
||||||
|
|
||||||
// Should the navigation bar be open by default?
|
// Should the navigation bar be open by default?
|
||||||
@ -251,6 +250,7 @@ public partial class MainLayout : LayoutComponentBase, IMessageBusReceiver, ILan
|
|||||||
|
|
||||||
// Set up hot reloading for plugins:
|
// Set up hot reloading for plugins:
|
||||||
PluginFactory.SetUpHotReloading();
|
PluginFactory.SetUpHotReloading();
|
||||||
|
await this.MessageBus.SendMessage<bool>(this, Event.STARTUP_COMPLETED);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
break;
|
break;
|
||||||
|
|||||||
@ -9,6 +9,7 @@ public enum Event
|
|||||||
CONFIGURATION_CHANGED,
|
CONFIGURATION_CHANGED,
|
||||||
COLOR_THEME_CHANGED,
|
COLOR_THEME_CHANGED,
|
||||||
STARTUP_PLUGIN_SYSTEM,
|
STARTUP_PLUGIN_SYSTEM,
|
||||||
|
STARTUP_COMPLETED,
|
||||||
STARTUP_ENTERPRISE_ENVIRONMENT,
|
STARTUP_ENTERPRISE_ENVIRONMENT,
|
||||||
PLUGINS_RELOADED,
|
PLUGINS_RELOADED,
|
||||||
SHOW_ERROR,
|
SHOW_ERROR,
|
||||||
|
|||||||
@ -8,13 +8,12 @@ namespace AIStudio.Tools.Services;
|
|||||||
|
|
||||||
public sealed class GlobalShortcutService : BackgroundService, IMessageBusReceiver
|
public sealed class GlobalShortcutService : BackgroundService, IMessageBusReceiver
|
||||||
{
|
{
|
||||||
private static bool IS_INITIALIZED;
|
private static bool IS_STARTUP_COMPLETED;
|
||||||
private static readonly TimeSpan STARTUP_RECOVERY_WINDOW = TimeSpan.FromSeconds(15);
|
|
||||||
|
|
||||||
private enum ShortcutSyncSource
|
private enum ShortcutSyncSource
|
||||||
{
|
{
|
||||||
STARTUP,
|
|
||||||
CONFIGURATION_CHANGED,
|
CONFIGURATION_CHANGED,
|
||||||
|
STARTUP_COMPLETED,
|
||||||
PLUGINS_RELOADED,
|
PLUGINS_RELOADED,
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -23,7 +22,6 @@ public sealed class GlobalShortcutService : BackgroundService, IMessageBusReceiv
|
|||||||
private readonly SettingsManager settingsManager;
|
private readonly SettingsManager settingsManager;
|
||||||
private readonly MessageBus messageBus;
|
private readonly MessageBus messageBus;
|
||||||
private readonly RustService rustService;
|
private readonly RustService rustService;
|
||||||
private readonly DateTimeOffset serviceStartedAt = DateTimeOffset.UtcNow;
|
|
||||||
|
|
||||||
public GlobalShortcutService(
|
public GlobalShortcutService(
|
||||||
ILogger<GlobalShortcutService> logger,
|
ILogger<GlobalShortcutService> logger,
|
||||||
@ -37,17 +35,13 @@ public sealed class GlobalShortcutService : BackgroundService, IMessageBusReceiv
|
|||||||
this.rustService = rustService;
|
this.rustService = rustService;
|
||||||
|
|
||||||
this.messageBus.RegisterComponent(this);
|
this.messageBus.RegisterComponent(this);
|
||||||
this.ApplyFilters([], [Event.CONFIGURATION_CHANGED, Event.PLUGINS_RELOADED]);
|
this.ApplyFilters([], [Event.CONFIGURATION_CHANGED, Event.PLUGINS_RELOADED, Event.STARTUP_COMPLETED]);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
|
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
|
||||||
{
|
{
|
||||||
// Wait until the app is fully initialized:
|
this.logger.LogInformation("The global shortcut service was initialized.");
|
||||||
while (!stoppingToken.IsCancellationRequested && !IS_INITIALIZED)
|
await Task.Delay(Timeout.InfiniteTimeSpan, stoppingToken);
|
||||||
await Task.Delay(TimeSpan.FromSeconds(1), stoppingToken);
|
|
||||||
|
|
||||||
// Register shortcuts on startup:
|
|
||||||
await this.RegisterAllShortcuts(ShortcutSyncSource.STARTUP);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public override async Task StopAsync(CancellationToken cancellationToken)
|
public override async Task StopAsync(CancellationToken cancellationToken)
|
||||||
@ -64,10 +58,21 @@ public sealed class GlobalShortcutService : BackgroundService, IMessageBusReceiv
|
|||||||
switch (triggeredEvent)
|
switch (triggeredEvent)
|
||||||
{
|
{
|
||||||
case Event.CONFIGURATION_CHANGED:
|
case Event.CONFIGURATION_CHANGED:
|
||||||
|
if (!IS_STARTUP_COMPLETED)
|
||||||
|
return;
|
||||||
|
|
||||||
await this.RegisterAllShortcuts(ShortcutSyncSource.CONFIGURATION_CHANGED);
|
await this.RegisterAllShortcuts(ShortcutSyncSource.CONFIGURATION_CHANGED);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
case Event.STARTUP_COMPLETED:
|
||||||
|
IS_STARTUP_COMPLETED = true;
|
||||||
|
await this.RegisterAllShortcuts(ShortcutSyncSource.STARTUP_COMPLETED);
|
||||||
|
break;
|
||||||
|
|
||||||
case Event.PLUGINS_RELOADED:
|
case Event.PLUGINS_RELOADED:
|
||||||
|
if (!IS_STARTUP_COMPLETED)
|
||||||
|
return;
|
||||||
|
|
||||||
await this.RegisterAllShortcuts(ShortcutSyncSource.PLUGINS_RELOADED);
|
await this.RegisterAllShortcuts(ShortcutSyncSource.PLUGINS_RELOADED);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@ -88,7 +93,9 @@ public sealed class GlobalShortcutService : BackgroundService, IMessageBusReceiv
|
|||||||
if(shortcutId is Shortcut.NONE)
|
if(shortcutId is Shortcut.NONE)
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
var (shortcut, isEnabled, usesPersistedFallback) = await this.GetShortcutState(shortcutId);
|
var shortcutState = await this.GetShortcutState(shortcutId, source);
|
||||||
|
var shortcut = shortcutState.Shortcut;
|
||||||
|
var isEnabled = shortcutState.IsEnabled;
|
||||||
this.logger.LogInformation(
|
this.logger.LogInformation(
|
||||||
"Sync shortcut '{ShortcutId}' (source='{Source}', enabled={IsEnabled}, configured='{Shortcut}').",
|
"Sync shortcut '{ShortcutId}' (source='{Source}', enabled={IsEnabled}, configured='{Shortcut}').",
|
||||||
shortcutId,
|
shortcutId,
|
||||||
@ -96,10 +103,10 @@ public sealed class GlobalShortcutService : BackgroundService, IMessageBusReceiv
|
|||||||
isEnabled,
|
isEnabled,
|
||||||
shortcut);
|
shortcut);
|
||||||
|
|
||||||
if (usesPersistedFallback)
|
if (shortcutState.UsesPersistedFallback)
|
||||||
{
|
{
|
||||||
this.logger.LogWarning(
|
this.logger.LogWarning(
|
||||||
"Using persisted shortcut fallback for '{ShortcutId}' during startup recovery (source='{Source}', configured='{Shortcut}').",
|
"Using persisted shortcut fallback for '{ShortcutId}' during startup completion (source='{Source}', configured='{Shortcut}').",
|
||||||
shortcutId,
|
shortcutId,
|
||||||
source,
|
source,
|
||||||
shortcut);
|
shortcut);
|
||||||
@ -151,14 +158,14 @@ public sealed class GlobalShortcutService : BackgroundService, IMessageBusReceiv
|
|||||||
_ => true,
|
_ => true,
|
||||||
};
|
};
|
||||||
|
|
||||||
private async Task<ShortcutState> GetShortcutState(Shortcut shortcutId)
|
private async Task<ShortcutState> GetShortcutState(Shortcut shortcutId, ShortcutSyncSource source)
|
||||||
{
|
{
|
||||||
var shortcut = this.GetShortcutValue(shortcutId);
|
var shortcut = this.GetShortcutValue(shortcutId);
|
||||||
var isEnabled = this.IsShortcutAllowed(shortcutId);
|
var isEnabled = this.IsShortcutAllowed(shortcutId);
|
||||||
if (isEnabled && !string.IsNullOrWhiteSpace(shortcut))
|
if (isEnabled && !string.IsNullOrWhiteSpace(shortcut))
|
||||||
return new(shortcut, true, false);
|
return new(shortcut, true, false);
|
||||||
|
|
||||||
if (!this.IsWithinStartupRecoveryWindow() || shortcutId is not Shortcut.VOICE_RECORDING_TOGGLE)
|
if (source is not ShortcutSyncSource.STARTUP_COMPLETED || shortcutId is not Shortcut.VOICE_RECORDING_TOGGLE)
|
||||||
return new(shortcut, isEnabled, false);
|
return new(shortcut, isEnabled, false);
|
||||||
|
|
||||||
var settingsSnapshot = await this.settingsManager.TryReadSettingsSnapshot();
|
var settingsSnapshot = await this.settingsManager.TryReadSettingsSnapshot();
|
||||||
@ -166,16 +173,15 @@ public sealed class GlobalShortcutService : BackgroundService, IMessageBusReceiv
|
|||||||
return new(shortcut, isEnabled, false);
|
return new(shortcut, isEnabled, false);
|
||||||
|
|
||||||
var fallbackShortcut = settingsSnapshot.App.ShortcutVoiceRecording;
|
var fallbackShortcut = settingsSnapshot.App.ShortcutVoiceRecording;
|
||||||
var fallbackEnabled = settingsSnapshot.App.EnabledPreviewFeatures.Contains(PreviewFeatures.PRE_SPEECH_TO_TEXT_2026);
|
var fallbackEnabled =
|
||||||
|
settingsSnapshot.App.EnabledPreviewFeatures.Contains(PreviewFeatures.PRE_SPEECH_TO_TEXT_2026) &&
|
||||||
|
!string.IsNullOrWhiteSpace(settingsSnapshot.App.UseTranscriptionProvider);
|
||||||
|
|
||||||
if (!fallbackEnabled || string.IsNullOrWhiteSpace(fallbackShortcut))
|
if (!fallbackEnabled || string.IsNullOrWhiteSpace(fallbackShortcut))
|
||||||
return new(shortcut, isEnabled, false);
|
return new(shortcut, isEnabled, false);
|
||||||
|
|
||||||
return new(fallbackShortcut, true, true);
|
return new(fallbackShortcut, true, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
private bool IsWithinStartupRecoveryWindow() => DateTimeOffset.UtcNow - this.serviceStartedAt <= STARTUP_RECOVERY_WINDOW;
|
|
||||||
|
|
||||||
private readonly record struct ShortcutState(string Shortcut, bool IsEnabled, bool UsesPersistedFallback);
|
private readonly record struct ShortcutState(string Shortcut, bool IsEnabled, bool UsesPersistedFallback);
|
||||||
|
|
||||||
public static void Initialize() => IS_INITIALIZED = true;
|
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user