Skip to content

Commit 9bd9b94

Browse files
authored
Merge pull request #1123 from Flow-Launcher/AsyncResultCallback
Implement Async Result Execution
2 parents 4c4ba92 + de1b0a2 commit 9bd9b94

File tree

5 files changed

+56
-42
lines changed

5 files changed

+56
-42
lines changed

Flow.Launcher.Core/Plugin/JsonRPCPlugin.cs

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ private List<Result> ParseResults(JsonRPCQueryResponseModel queryResponseModel)
115115

116116
foreach (var result in queryResponseModel.Result)
117117
{
118-
result.Action = c =>
118+
result.AsyncAction = async c =>
119119
{
120120
UpdateSettings(result.SettingsChange);
121121

@@ -133,15 +133,15 @@ private List<Result> ParseResults(JsonRPCQueryResponseModel queryResponseModel)
133133
}
134134
else
135135
{
136-
var actionResponse = Request(result.JsonRPCAction);
136+
var actionResponse = await RequestAsync(result.JsonRPCAction);
137137

138-
if (string.IsNullOrEmpty(actionResponse))
138+
if (actionResponse.Length == 0)
139139
{
140140
return !result.JsonRPCAction.DontHideAfterAction;
141141
}
142142

143-
var jsonRpcRequestModel =
144-
JsonSerializer.Deserialize<JsonRPCRequestModel>(actionResponse, options);
143+
var jsonRpcRequestModel = await
144+
JsonSerializer.DeserializeAsync<JsonRPCRequestModel>(actionResponse, options);
145145

146146
if (jsonRpcRequestModel?.Method?.StartsWith("Flow.Launcher.") ?? false)
147147
{
@@ -166,19 +166,20 @@ private List<Result> ParseResults(JsonRPCQueryResponseModel queryResponseModel)
166166
private void ExecuteFlowLauncherAPI(string method, object[] parameters)
167167
{
168168
var parametersTypeArray = parameters.Select(param => param.GetType()).ToArray();
169-
MethodInfo methodInfo = PluginManager.API.GetType().GetMethod(method, parametersTypeArray);
170-
if (methodInfo != null)
169+
var methodInfo = typeof(IPublicAPI).GetMethod(method, parametersTypeArray);
170+
if (methodInfo == null)
171+
{
172+
return;
173+
}
174+
try
175+
{
176+
methodInfo.Invoke(PluginManager.API, parameters);
177+
}
178+
catch (Exception)
171179
{
172-
try
173-
{
174-
methodInfo.Invoke(PluginManager.API, parameters);
175-
}
176-
catch (Exception)
177-
{
178180
#if (DEBUG)
179-
throw;
181+
throw;
180182
#endif
181-
}
182183
}
183184
}
184185

@@ -366,17 +367,15 @@ public Control CreateSettingPanel()
366367
var settingWindow = new UserControl();
367368
var mainPanel = new StackPanel
368369
{
369-
Margin = settingPanelMargin,
370-
Orientation = Orientation.Vertical
370+
Margin = settingPanelMargin, Orientation = Orientation.Vertical
371371
};
372372
settingWindow.Content = mainPanel;
373373

374374
foreach (var (type, attribute) in _settingsTemplate.Body)
375375
{
376376
var panel = new StackPanel
377377
{
378-
Orientation = Orientation.Horizontal,
379-
Margin = settingControlMargin
378+
Orientation = Orientation.Horizontal, Margin = settingControlMargin
380379
};
381380
var name = new TextBlock()
382381
{

Flow.Launcher.Plugin/Result.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.Collections.Generic;
33
using System.IO;
4+
using System.Threading.Tasks;
45
using System.Windows.Media;
56

67
namespace Flow.Launcher.Plugin
@@ -85,6 +86,14 @@ public string IcoPath
8586
/// </summary>
8687
public Func<ActionContext, bool> Action { get; set; }
8788

89+
/// <summary>
90+
/// Delegate. An Async action to take in the form of a function call when the result has been selected
91+
/// <returns>
92+
/// true to hide flowlauncher after select result
93+
/// </returns>
94+
/// </summary>
95+
public Func<ActionContext, ValueTask<bool>> AsyncAction { get; set; }
96+
8897
/// <summary>
8998
/// Priority of the current result
9099
/// <value>default: 0</value>
@@ -169,5 +178,10 @@ public override string ToString()
169178
/// Show message as ToolTip on result SubTitle hover over
170179
/// </summary>
171180
public string SubTitleToolTip { get; set; }
181+
182+
public ValueTask<bool> ExecuteAsync(ActionContext context)
183+
{
184+
return AsyncAction?.Invoke(context) ?? ValueTask.FromResult(Action?.Invoke(context) ?? false);
185+
}
172186
}
173187
}

Flow.Launcher.Test/Plugins/JsonRPCPluginTest.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public async Task GivenVariousJsonText_WhenVariousNamingCase_ThenExpectNotNullRe
4848
foreach (var result in results)
4949
{
5050
Assert.IsNotNull(result);
51-
Assert.IsNotNull(result.Action);
51+
Assert.IsNotNull(result.AsyncAction);
5252
Assert.IsNotNull(result.Title);
5353
}
5454

@@ -92,7 +92,7 @@ public async Task GivenModel_WhenSerializeWithDifferentNamingPolicy_ThenExpectSa
9292
Assert.AreEqual(result1, referenceResult);
9393

9494
Assert.IsNotNull(result1);
95-
Assert.IsNotNull(result1.Action);
95+
Assert.IsNotNull(result1.AsyncAction);
9696
}
9797
}
9898

Flow.Launcher/ResultListBox.xaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@
8686
Height="32"
8787
Margin="0,0,0,0"
8888
HorizontalAlignment="Center"
89-
Source="{Binding Image}"
89+
Source="{Binding Image, TargetNullValue={x:Null}}"
9090
Stretch="Uniform"
9191
Visibility="{Binding ShowIcon}" />
9292
</Border>

Flow.Launcher/ViewModel/MainViewModel.cs

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -194,37 +194,38 @@ private void InitializeKeyCommands()
194194
PluginManager.API.OpenUrl("https://github.com/Flow-Launcher/Flow.Launcher/wiki/Flow-Launcher/");
195195
});
196196
OpenSettingCommand = new RelayCommand(_ => { App.API.OpenSettingDialog(); });
197-
OpenResultCommand = new RelayCommand(index =>
197+
OpenResultCommand = new RelayCommand(async index =>
198198
{
199199
var results = SelectedResults;
200200

201201
if (index != null)
202202
{
203-
results.SelectedIndex = int.Parse(index.ToString());
203+
results.SelectedIndex = int.Parse(index.ToString()!);
204204
}
205205

206206
var result = results.SelectedItem?.Result;
207-
if (result != null) // SelectedItem returns null if selection is empty.
207+
if (result == null)
208208
{
209-
bool hideWindow = result.Action != null && result.Action(new ActionContext
210-
{
211-
SpecialKeyState = GlobalHotkey.CheckModifiers()
212-
});
209+
return;
210+
}
211+
var hideWindow = await result.ExecuteAsync(new ActionContext
212+
{
213+
SpecialKeyState = GlobalHotkey.CheckModifiers()
214+
}).ConfigureAwait(false);
213215

214-
if (hideWindow)
215-
{
216-
Hide();
217-
}
216+
if (hideWindow)
217+
{
218+
Hide();
219+
}
218220

219-
if (SelectedIsFromQueryResults())
220-
{
221-
_userSelectedRecord.Add(result);
222-
_history.Add(result.OriginQuery.RawQuery);
223-
}
224-
else
225-
{
226-
SelectedResults = Results;
227-
}
221+
if (SelectedIsFromQueryResults())
222+
{
223+
_userSelectedRecord.Add(result);
224+
_history.Add(result.OriginQuery.RawQuery);
225+
}
226+
else
227+
{
228+
SelectedResults = Results;
228229
}
229230
});
230231

0 commit comments

Comments
 (0)