Skip to content

Commit 97b7ef3

Browse files
authored
feat: add fallback commands (#66)
1 parent 681a4de commit 97b7ef3

3 files changed

Lines changed: 69 additions & 3 deletions

File tree

CmdPalWebSearchShortcut/WebSearchShortcut/Commands/SearchWebCommand.cs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,14 @@ namespace WebSearchShortcut.Commands;
55

66
internal sealed partial class SearchWebCommand : InvokableCommand
77
{
8-
private readonly string _query;
8+
public new string Id
9+
{
10+
get => base.Id;
11+
set => base.Id = value;
12+
}
13+
14+
public string Query { get; internal set; } = string.Empty;
15+
916
private readonly WebSearchShortcutDataEntry _shortcut;
1017
private readonly BrowserExecutionInfo _browserInfo;
1118
// private readonly SettingsManager _settingsManager;
@@ -14,15 +21,16 @@ public SearchWebCommand(WebSearchShortcutDataEntry shortcut, string query)
1421
{
1522
Name = $"[UNBOUND] {nameof(SearchWebCommand)}.{nameof(Name)} required - shortcut='{shortcut.Name}', query='{query}'";
1623

17-
_query = query;
24+
Query = query;
25+
1826
_shortcut = shortcut;
1927
_browserInfo = new BrowserExecutionInfo(shortcut);
2028
// _settingsManager = settingsManager;
2129
}
2230

2331
public override CommandResult Invoke()
2432
{
25-
if (!ShellHelpers.OpenCommandInShell(_browserInfo.Path, _browserInfo.ArgumentsPattern, WebSearchShortcutDataEntry.GetSearchUrl(_shortcut, _query)))
33+
if (!ShellHelpers.OpenCommandInShell(_browserInfo.Path, _browserInfo.ArgumentsPattern, WebSearchShortcutDataEntry.GetSearchUrl(_shortcut, Query)))
2634
{
2735
// TODO GH# 138 --> actually display feedback from the extension somewhere.
2836
return CommandResult.KeepOpen();
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
using Microsoft.CommandPalette.Extensions.Toolkit;
2+
using WebSearchShortcut.Commands;
3+
using WebSearchShortcut.Helpers;
4+
using WebSearchShortcut.Properties;
5+
using WebSearchShortcut.Services;
6+
7+
namespace WebSearchShortcut;
8+
9+
internal sealed partial class FallbackSearchWebItem : FallbackCommandItem
10+
{
11+
private readonly SearchWebCommand _searchWebCommand;
12+
private readonly WebSearchShortcutDataEntry _shortcut;
13+
14+
public FallbackSearchWebItem(WebSearchShortcutDataEntry shortcut)
15+
: base(new SearchWebCommand(shortcut, string.Empty) { Id = $"{shortcut.Id}.fallback" }, shortcut.Name)
16+
{
17+
_shortcut = shortcut;
18+
19+
_searchWebCommand = (SearchWebCommand) Command!;
20+
_searchWebCommand.Name = string.Empty;
21+
22+
Title = string.Empty;
23+
Subtitle = string.Empty;
24+
Icon = IconService.GetIconInfo(shortcut);
25+
MoreCommands = [
26+
new CommandContextItem(
27+
new OpenHomePageCommand(_shortcut)
28+
{
29+
Name = StringFormatter.Format(Resources.OpenHomepageItem_NameTemplate, new() { ["shortcut"] = _shortcut.Name })
30+
}
31+
)
32+
{
33+
Title = StringFormatter.Format(Resources.OpenHomepageItem_TitleTemplate, new() { ["shortcut"] = _shortcut.Name }),
34+
Icon = Icons.Home
35+
}
36+
];
37+
}
38+
39+
public override void UpdateQuery(string query)
40+
{
41+
bool isEmpty = string.IsNullOrWhiteSpace(query);
42+
43+
// Order matters: update command state before Title. (Verified 2025-08-21)
44+
// Title may be derived from/overwritten by Command.Name.
45+
// Set Command.Name before Title to avoid stale or inconsistent UI.
46+
_searchWebCommand.Name = isEmpty ? string.Empty : StringFormatter.Format(Resources.SearchQueryItem_NameTemplate, new() { ["shortcut"] = _shortcut.Name, ["query"] = query });
47+
_searchWebCommand.Query = query;
48+
49+
Title = isEmpty ? string.Empty : StringFormatter.Format(Resources.SearchQueryItem_TitleTemplate, new() { ["shortcut"] = _shortcut.Name, ["query"] = query });
50+
Subtitle = isEmpty ? string.Empty : StringFormatter.Format(Resources.SearchQueryItem_SubtitleTemplate, new() { ["shortcut"] = _shortcut.Name, ["query"] = query });
51+
}
52+
}

CmdPalWebSearchShortcut/WebSearchShortcut/WebSearchShortcutCommandsProvider.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ public partial class WebSearchShortcutCommandsProvider : CommandProvider
2020
{
2121
private readonly ICommandItem _addShortcutItem;
2222
private ICommandItem[] _topLevelCommands = [];
23+
private IFallbackCommandItem[] _fallbackCommands = [];
2324
private Storage? _storage;
2425

2526
public WebSearchShortcutCommandsProvider()
@@ -49,6 +50,8 @@ public override ICommandItem[] TopLevelCommands()
4950
return _topLevelCommands;
5051
}
5152

53+
public override IFallbackCommandItem[] FallbackCommands() => _fallbackCommands;
54+
5255
internal static string GetShortcutsJsonPath()
5356
{
5457
string directory = Utilities.BaseSettingsPath("WebSearchShortcut");
@@ -102,6 +105,7 @@ private void SaveAndRefresh()
102105
private void ReloadCommands()
103106
{
104107
List<ICommandItem> items = [_addShortcutItem];
108+
List<IFallbackCommandItem> fallbackItem = [];
105109

106110
if (_storage is null)
107111
{
@@ -111,9 +115,11 @@ private void ReloadCommands()
111115
if (_storage is not null)
112116
{
113117
items.AddRange(_storage.Data.Select(CreateCommandItem));
118+
fallbackItem.AddRange(_storage.Data.Select(shortcut => new FallbackSearchWebItem(shortcut)));
114119
}
115120

116121
_topLevelCommands = [.. items];
122+
_fallbackCommands = [.. fallbackItem];
117123
}
118124

119125
private void LoadShortcutFromFile()

0 commit comments

Comments
 (0)