Skip to content

Commit 735cc14

Browse files
authored
Merge pull request #3203 from Jack251970/context_menu_back_to_query
Improve context menu item action response & Fix origin query null exception
2 parents 6c22aea + 80f54ba commit 735cc14

File tree

6 files changed

+35
-36
lines changed

6 files changed

+35
-36
lines changed

Flow.Launcher.Plugin/Query.cs

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,11 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.Linq;
4-
using System.Text.Json.Serialization;
1+
using System.Text.Json.Serialization;
52

63
namespace Flow.Launcher.Plugin
74
{
85
public class Query
96
{
107
public Query() { }
118

12-
[Obsolete("Use the default Query constructor.")]
13-
public Query(string rawQuery, string search, string[] terms, string[] searchTerms, string actionKeyword = "")
14-
{
15-
Search = search;
16-
RawQuery = rawQuery;
17-
SearchTerms = searchTerms;
18-
ActionKeyword = actionKeyword;
19-
}
20-
219
/// <summary>
2210
/// Raw query, this includes action keyword if it has
2311
/// We didn't recommend use this property directly. You should always use Search property.

Flow.Launcher/Storage/TopMostRecord.cs

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ public class TopMostRecord
1212

1313
internal bool IsTopMost(Result result)
1414
{
15+
// origin query is null when user select the context menu item directly of one item from query list
16+
// in this case, we do not need to check if the result is top most
1517
if (records.IsEmpty || result.OriginQuery == null ||
1618
!records.TryGetValue(result.OriginQuery.RawQuery, out var value))
1719
{
@@ -24,11 +26,25 @@ internal bool IsTopMost(Result result)
2426

2527
internal void Remove(Result result)
2628
{
29+
// origin query is null when user select the context menu item directly of one item from query list
30+
// in this case, we do not need to remove the record
31+
if (result.OriginQuery == null)
32+
{
33+
return;
34+
}
35+
2736
records.Remove(result.OriginQuery.RawQuery, out _);
2837
}
2938

3039
internal void AddOrUpdate(Result result)
3140
{
41+
// origin query is null when user select the context menu item directly of one item from query list
42+
// in this case, we do not need to add or update the record
43+
if (result.OriginQuery == null)
44+
{
45+
return;
46+
}
47+
3248
var record = new Record
3349
{
3450
PluginID = result.PluginID,
@@ -38,11 +54,6 @@ internal void AddOrUpdate(Result result)
3854
};
3955
records.AddOrUpdate(result.OriginQuery.RawQuery, record, (key, oldValue) => record);
4056
}
41-
42-
public void Load(Dictionary<string, Record> dictionary)
43-
{
44-
records = new ConcurrentDictionary<string, Record>(dictionary);
45-
}
4657
}
4758

4859
public class Record

Flow.Launcher/Storage/UserSelectedRecord.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ private static int GenerateResultHashCode(Result result)
5757

5858
private static int GenerateQueryAndResultHashCode(Query query, Result result)
5959
{
60+
// query is null when user select the context menu item directly of one item from query list
61+
// so we only need to consider the result
6062
if (query == null)
6163
{
6264
return GenerateResultHashCode(result);

Flow.Launcher/ViewModel/MainViewModel.cs

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,6 @@ public partial class MainViewModel : BaseModel, ISavable
3434

3535
private bool _isQueryRunning;
3636
private Query _lastQuery;
37-
private Result lastContextMenuResult = new Result();
38-
private List<Result> lastContextMenuResults = new List<Result>();
3937
private string _queryTextBeforeLeaveResults;
4038

4139
private readonly FlowLauncherJsonStorage<History> _historyItemsStorage;
@@ -398,11 +396,15 @@ private async Task OpenResultAsync(string index)
398396
})
399397
.ConfigureAwait(false);
400398

401-
402399
if (SelectedIsFromQueryResults())
403400
{
404401
_userSelectedRecord.Add(result);
405-
_history.Add(result.OriginQuery.RawQuery);
402+
// origin query is null when user select the context menu item directly of one item from query list
403+
// so we don't want to add it to history
404+
if (result.OriginQuery != null)
405+
{
406+
_history.Add(result.OriginQuery.RawQuery);
407+
}
406408
lastHistoryIndex = 1;
407409
}
408410

@@ -986,19 +988,10 @@ private void QueryContextMenu()
986988
if (selected != null) // SelectedItem returns null if selection is empty.
987989
{
988990
List<Result> results;
989-
if (selected == lastContextMenuResult)
990-
{
991-
results = lastContextMenuResults;
992-
}
993-
else
994-
{
995-
results = PluginManager.GetContextMenusForPlugin(selected);
996-
lastContextMenuResults = results;
997-
lastContextMenuResult = selected;
998-
results.Add(ContextMenuTopMost(selected));
999-
results.Add(ContextMenuPluginInfo(selected.PluginID));
1000-
}
1001991

992+
results = PluginManager.GetContextMenusForPlugin(selected);
993+
results.Add(ContextMenuTopMost(selected));
994+
results.Add(ContextMenuPluginInfo(selected.PluginID));
1002995

1003996
if (!string.IsNullOrEmpty(query))
1004997
{
@@ -1273,6 +1266,8 @@ private Result ContextMenuTopMost(Result result)
12731266
{
12741267
_topMostRecord.Remove(result);
12751268
App.API.ShowMsg(InternationalizationManager.Instance.GetTranslation("success"));
1269+
App.API.BackToQueryResults();
1270+
App.API.ReQuery();
12761271
return false;
12771272
}
12781273
};
@@ -1289,6 +1284,8 @@ private Result ContextMenuTopMost(Result result)
12891284
{
12901285
_topMostRecord.AddOrUpdate(result);
12911286
App.API.ShowMsg(InternationalizationManager.Instance.GetTranslation("success"));
1287+
App.API.BackToQueryResults();
1288+
App.API.ReQuery();
12921289
return false;
12931290
}
12941291
};
@@ -1377,8 +1374,6 @@ public async void Hide()
13771374
lastHistoryIndex = 1;
13781375
// Trick for no delay
13791376
MainWindowOpacity = 0;
1380-
lastContextMenuResult = new Result();
1381-
lastContextMenuResults = new List<Result>();
13821377

13831378
if (ExternalPreviewVisible)
13841379
CloseExternalPreview();

Plugins/Flow.Launcher.Plugin.Explorer/ContextMenu.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,7 @@ public List<Result> LoadContextMenus(Result selectedResult)
242242
var name = "Plugin: Folder";
243243
var message = $"File not found: {e.Message}";
244244
Context.API.ShowMsgError(name, message);
245+
return false;
245246
}
246247

247248
return true;

Plugins/Flow.Launcher.Plugin.Program/Main.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,8 @@ public List<Result> LoadContextMenus(Result selectedResult)
264264
Context.API.GetTranslation("flowlauncher_plugin_program_disable_dlgtitle_success"),
265265
Context.API.GetTranslation(
266266
"flowlauncher_plugin_program_disable_dlgtitle_success_message"));
267+
Context.API.BackToQueryResults();
268+
Context.API.ReQuery();
267269
return false;
268270
},
269271
IcoPath = "Images/disable.png",

0 commit comments

Comments
 (0)