diff --git a/src/modules/cmdpal/Exts/Microsoft.CmdPal.Ext.WindowsTerminal/Commands/LaunchProfileCommand.cs b/src/modules/cmdpal/Exts/Microsoft.CmdPal.Ext.WindowsTerminal/Commands/LaunchProfileCommand.cs new file mode 100644 index 0000000000..f43e660f11 --- /dev/null +++ b/src/modules/cmdpal/Exts/Microsoft.CmdPal.Ext.WindowsTerminal/Commands/LaunchProfileCommand.cs @@ -0,0 +1,70 @@ +// 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.Collections.Generic; +using System.Diagnostics; +using System.Linq; +using System.Resources; +using System.Text; +using System.Threading.Tasks; +using Microsoft.CmdPal.Ext.WindowsTerminal.Helpers; +using Microsoft.CmdPal.Extensions; +using Microsoft.CmdPal.Extensions.Helpers; +using Windows.UI; + +namespace Microsoft.CmdPal.Ext.WindowsTerminal.Commands; + +internal sealed partial class LaunchProfileCommand : InvokableCommand +{ + private readonly string _id; + private readonly string _profile; + private readonly bool _openNewTab; + private readonly bool _openQuake; + + internal LaunchProfileCommand(string id, string profile, bool openNewTab, bool openQuake) + { + this._id = id; + this._profile = profile; + this._openNewTab = openNewTab; + this._openQuake = openQuake; + + this.Name = "Launch Profile"; + } + + private void Launch(string id, string profile) + { + var appManager = new ApplicationActivationManager(); + const ActivateOptions noFlags = ActivateOptions.None; + var queryArguments = TerminalHelper.GetArguments(profile, _openNewTab, _openQuake); + try + { + appManager.ActivateApplication(id, queryArguments, noFlags, out var unusedPid); + } +#pragma warning disable IDE0059, CS0168 + catch (Exception ex) + { + // TODO: We need to figure out some logging + // var name = "Plugin: " + Resources.plugin_name; + // var message = Resources.run_terminal_failed; + // Log.Exception("Failed to open Windows Terminal", ex, GetType()); + // _context.API.ShowMsg(name, message, string.Empty); + } + } +#pragma warning restore IDE0059, CS0168 + + public override CommandResult Invoke() + { + try + { + Launch(_id, _profile); + } + catch + { + // TODO: We need to figure out some logging + } + + return CommandResult.KeepOpen(); + } +} diff --git a/src/modules/cmdpal/Exts/Microsoft.CmdPal.Ext.WindowsTerminal/Helpers/TerminalHelper.cs b/src/modules/cmdpal/Exts/Microsoft.CmdPal.Ext.WindowsTerminal/Helpers/TerminalHelper.cs index 8032cf881c..a4b4e839a7 100644 --- a/src/modules/cmdpal/Exts/Microsoft.CmdPal.Ext.WindowsTerminal/Helpers/TerminalHelper.cs +++ b/src/modules/cmdpal/Exts/Microsoft.CmdPal.Ext.WindowsTerminal/Helpers/TerminalHelper.cs @@ -94,6 +94,9 @@ public static class TerminalHelper profileElement.TryGetProperty("guid", out JsonElement guidElement); var guid = guidElement.ValueKind == JsonValueKind.String ? Guid.Parse(guidElement.GetString()) : null as Guid?; - return new TerminalProfile(terminal, name, guid, hidden); + profileElement.TryGetProperty("icon", out JsonElement iconElement); + var icon = iconElement.ValueKind == JsonValueKind.String ? iconElement.GetString() : null; + + return new TerminalProfile(terminal, name, guid, hidden, icon); } } diff --git a/src/modules/cmdpal/Exts/Microsoft.CmdPal.Ext.WindowsTerminal/Pages/ProfilesListPage.cs b/src/modules/cmdpal/Exts/Microsoft.CmdPal.Ext.WindowsTerminal/Pages/ProfilesListPage.cs index 94b9d5680b..9d438862f3 100644 --- a/src/modules/cmdpal/Exts/Microsoft.CmdPal.Ext.WindowsTerminal/Pages/ProfilesListPage.cs +++ b/src/modules/cmdpal/Exts/Microsoft.CmdPal.Ext.WindowsTerminal/Pages/ProfilesListPage.cs @@ -13,10 +13,12 @@ using System.Runtime.InteropServices.WindowsRuntime; using System.Text.RegularExpressions; using System.Threading.Tasks; using System.Xml.Linq; +using Microsoft.CmdPal.Ext.WindowsTerminal.Commands; using Microsoft.CmdPal.Ext.WindowsTerminal.Helpers; using Microsoft.CmdPal.Extensions; using Microsoft.CmdPal.Extensions.Helpers; using Microsoft.UI.Windowing; +using Microsoft.UI.Xaml.Media.Imaging; using static System.Formats.Asn1.AsnWriter; namespace Microsoft.CmdPal.Ext.WindowsTerminal; @@ -24,11 +26,12 @@ namespace Microsoft.CmdPal.Ext.WindowsTerminal; internal sealed partial class ProfilesListPage : ListPage { private readonly TerminalQuery _terminalQuery = new(); + private readonly Dictionary _logoCache = new(); public ProfilesListPage() { Icon = new(string.Empty); - Name = "Find and launch your Windows Terminal profiles"; + Name = "Windows Terminal Profiles"; } #pragma warning disable SA1108 @@ -45,10 +48,11 @@ internal sealed partial class ProfilesListPage : ListPage continue; } - result.Add(new ListItem(new NoOpCommand()) + result.Add(new ListItem(new LaunchProfileCommand(profile.Terminal.AppUserModelId, profile.Name, true, false)) { Title = profile.Name, Subtitle = profile.Terminal.DisplayName, + Icon = new(profile.Icon), // Icon = () => GetLogo(profile.Terminal), // Action = _ => @@ -74,4 +78,17 @@ internal sealed partial class ProfilesListPage : ListPage } ]; } + + private BitmapImage GetLogo(TerminalPackage terminal) + { + var aumid = terminal.AppUserModelId; + + if (!_logoCache.TryGetValue(aumid, out BitmapImage value)) + { + value = terminal.GetLogo(); + _logoCache.Add(aumid, value); + } + + return value; + } } diff --git a/src/modules/cmdpal/Exts/Microsoft.CmdPal.Ext.WindowsTerminal/TerminalProfile.cs b/src/modules/cmdpal/Exts/Microsoft.CmdPal.Ext.WindowsTerminal/TerminalProfile.cs index 4dfb2b3ef0..2eae2fdeba 100644 --- a/src/modules/cmdpal/Exts/Microsoft.CmdPal.Ext.WindowsTerminal/TerminalProfile.cs +++ b/src/modules/cmdpal/Exts/Microsoft.CmdPal.Ext.WindowsTerminal/TerminalProfile.cs @@ -16,11 +16,14 @@ public class TerminalProfile public bool Hidden { get; } - public TerminalProfile(TerminalPackage terminal, string name, Guid? identifier, bool hidden) + public string Icon { get; } + + public TerminalProfile(TerminalPackage terminal, string name, Guid? identifier, bool hidden, string icon) { Terminal = terminal; Name = name; Identifier = identifier; Hidden = hidden; + Icon = icon; } }