Compare commits

...

6 Commits

Author SHA1 Message Date
Leilei Zhang
4c3fc5f817 remove unused import 2025-06-26 10:56:29 +08:00
Leilei Zhang
5ef63a19ce use lazy 2025-06-26 10:44:03 +08:00
Leilei Zhang
d51e2b5357 check runtime before creat 2025-06-25 20:13:12 +08:00
Leilei Zhang
e6d6edce57 add logger 2025-06-25 19:52:22 +08:00
Leilei Zhang
b3cda2cd55 Merge branch 'main' of https://github.com/microsoft/PowerToys into leilzh/initaction 2025-06-25 18:47:54 +08:00
Leilei Zhang
b941fc64bf init actions 2025-06-25 17:32:02 +08:00
3 changed files with 83 additions and 13 deletions

View File

@@ -0,0 +1,47 @@
// Copyright (c) Microsoft Corporation
// The Microsoft Corporation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System;
using System.Threading;
using System.Threading.Tasks;
using ManagedCommon;
using Windows.AI.Actions;
namespace Microsoft.CmdPal.Ext.Indexer.Data;
public static class ActionRuntimeManager
{
private static readonly Lazy<Task<ActionRuntime>> _lazyRuntime = new(InitializeAsync);
public static Task<ActionRuntime> InstanceAsync => _lazyRuntime.Value;
private static async Task<ActionRuntime> InitializeAsync()
{
// If we tried 3 times and failed, should we think the action runtime is not working?
// then we should not use it anymore.
const int maxAttempts = 3;
for (var attempt = 1; attempt <= maxAttempts; attempt++)
{
try
{
var runtime = ActionRuntimeFactory.CreateActionRuntime();
await Task.Delay(500);
return runtime;
}
catch (Exception ex)
{
Logger.LogError($"Attempt {attempt} to initialize ActionRuntime failed: {ex.Message}");
if (attempt == maxAttempts)
{
Logger.LogError($"Failed to initialize ActionRuntime: {ex.Message}");
}
}
}
return null;
}
}

View File

@@ -2,9 +2,11 @@
// The Microsoft Corporation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using Microsoft.CmdPal.Ext.Indexer.Data;
using Microsoft.CmdPal.Ext.Indexer.Properties;
using Microsoft.CommandPalette.Extensions;
using Microsoft.CommandPalette.Extensions.Toolkit;
using Windows.Foundation.Metadata;
namespace Microsoft.CmdPal.Ext.Indexer;
@@ -17,6 +19,10 @@ public partial class IndexerCommandsProvider : CommandProvider
Id = "Files";
DisplayName = Resources.IndexerCommandsProvider_DisplayName;
Icon = Icons.FileExplorer;
if (ApiInformation.IsApiContractPresent("Windows.AI.Actions.ActionsContract", 4))
{
_ = ActionRuntimeManager.InstanceAsync;
}
}
public override ICommandItem[] TopLevelCommands()

View File

@@ -2,10 +2,12 @@
// The Microsoft Corporation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Threading;
using System.Threading.Tasks;
using ManagedCommon;
using Microsoft.CmdPal.Ext.Indexer.Commands;
using Microsoft.CmdPal.Ext.Indexer.Data;
using Microsoft.CmdPal.Ext.Indexer.Properties;
@@ -15,7 +17,7 @@ using Windows.System;
namespace Microsoft.CmdPal.Ext.Indexer.Pages;
internal sealed partial class ActionsListContextItem : CommandContextItem
internal sealed partial class ActionsListContextItem : CommandContextItem, IDisposable
{
private readonly string fullPath;
private readonly List<CommandContextItem> actions = [];
@@ -41,20 +43,24 @@ internal sealed partial class ActionsListContextItem : CommandContextItem
private void UpdateMoreCommands()
{
try
lock (UpdateMoreCommandsLock)
{
lock (UpdateMoreCommandsLock)
if (actionRuntime == null)
{
if (actionRuntime == null)
{
actionRuntime = ActionRuntimeFactory.CreateActionRuntime();
Task.Delay(500).Wait();
}
actionRuntime.ActionCatalog.Changed -= ActionCatalog_Changed;
actionRuntime.ActionCatalog.Changed += ActionCatalog_Changed;
actionRuntime = ActionRuntimeManager.InstanceAsync.GetAwaiter().GetResult();
}
if (actionRuntime == null)
{
return;
}
actionRuntime.ActionCatalog.Changed -= ActionCatalog_Changed;
actionRuntime.ActionCatalog.Changed += ActionCatalog_Changed;
}
try
{
var extension = System.IO.Path.GetExtension(fullPath).ToLower(CultureInfo.InvariantCulture);
ActionEntity entity = null;
if (extension != null)
@@ -85,9 +91,20 @@ internal sealed partial class ActionsListContextItem : CommandContextItem
MoreCommands = [.. actions];
}
}
catch
catch (Exception ex)
{
actionRuntime = null;
Logger.LogError($"Error updating commands: {ex.Message}");
}
}
public void Dispose()
{
lock (UpdateMoreCommandsLock)
{
if (actionRuntime != null)
{
actionRuntime.ActionCatalog.Changed -= ActionCatalog_Changed;
}
}
}
}