Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 18 additions & 19 deletions Flow.Launcher.Core/Plugin/JsonRPCPlugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ private List<Result> ParseResults(JsonRPCQueryResponseModel queryResponseModel)

foreach (var result in queryResponseModel.Result)
{
result.Action = c =>
result.AsyncAction = async c =>
{
UpdateSettings(result.SettingsChange);

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

if (string.IsNullOrEmpty(actionResponse))
if (actionResponse.Length == 0)
{
return !result.JsonRPCAction.DontHideAfterAction;
}

var jsonRpcRequestModel =
JsonSerializer.Deserialize<JsonRPCRequestModel>(actionResponse, options);
var jsonRpcRequestModel = await
JsonSerializer.DeserializeAsync<JsonRPCRequestModel>(actionResponse, options);

if (jsonRpcRequestModel?.Method?.StartsWith("Flow.Launcher.") ?? false)
{
Expand All @@ -166,19 +166,20 @@ private List<Result> ParseResults(JsonRPCQueryResponseModel queryResponseModel)
private void ExecuteFlowLauncherAPI(string method, object[] parameters)
{
var parametersTypeArray = parameters.Select(param => param.GetType()).ToArray();
MethodInfo methodInfo = PluginManager.API.GetType().GetMethod(method, parametersTypeArray);
if (methodInfo != null)
var methodInfo = typeof(IPublicAPI).GetMethod(method, parametersTypeArray);
if (methodInfo == null)
{
return;
}
try
{
methodInfo.Invoke(PluginManager.API, parameters);
}
catch (Exception)
{
try
{
methodInfo.Invoke(PluginManager.API, parameters);
}
catch (Exception)
{
#if (DEBUG)
throw;
throw;
#endif
}
}
}

Expand Down Expand Up @@ -365,17 +366,15 @@ public Control CreateSettingPanel()
var settingWindow = new UserControl();
var mainPanel = new StackPanel
{
Margin = settingPanelMargin,
Orientation = Orientation.Vertical
Margin = settingPanelMargin, Orientation = Orientation.Vertical
};
settingWindow.Content = mainPanel;

foreach (var (type, attribute) in _settingsTemplate.Body)
{
var panel = new StackPanel
{
Orientation = Orientation.Horizontal,
Margin = settingControlMargin
Orientation = Orientation.Horizontal, Margin = settingControlMargin
};
var name = new TextBlock()
{
Expand Down
14 changes: 14 additions & 0 deletions Flow.Launcher.Plugin/Result.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks;
using System.Windows.Media;

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

/// <summary>
/// Delegate. An Async action to take in the form of a function call when the result has been selected
/// <returns>
/// true to hide flowlauncher after select result
/// </returns>
/// </summary>
public Func<ActionContext, ValueTask<bool>> AsyncAction { get; set; }

/// <summary>
/// Priority of the current result
/// <value>default: 0</value>
Expand Down Expand Up @@ -169,5 +178,10 @@ public override string ToString()
/// Show message as ToolTip on result SubTitle hover over
/// </summary>
public string SubTitleToolTip { get; set; }

public ValueTask<bool> ExecuteAsync(ActionContext context)
{
return AsyncAction?.Invoke(context) ?? ValueTask.FromResult(Action?.Invoke(context) ?? false);
}
}
}
4 changes: 2 additions & 2 deletions Flow.Launcher.Test/Plugins/JsonRPCPluginTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public async Task GivenVariousJsonText_WhenVariousNamingCase_ThenExpectNotNullRe
foreach (var result in results)
{
Assert.IsNotNull(result);
Assert.IsNotNull(result.Action);
Assert.IsNotNull(result.AsyncAction);
Assert.IsNotNull(result.Title);
}

Expand Down Expand Up @@ -92,7 +92,7 @@ public async Task GivenModel_WhenSerializeWithDifferentNamingPolicy_ThenExpectSa
Assert.AreEqual(result1, referenceResult);

Assert.IsNotNull(result1);
Assert.IsNotNull(result1.Action);
Assert.IsNotNull(result1.AsyncAction);
}
}

Expand Down
2 changes: 1 addition & 1 deletion Flow.Launcher/ResultListBox.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@
Height="32"
Margin="0,0,0,0"
HorizontalAlignment="Center"
Source="{Binding Image}"
Source="{Binding Image, TargetNullValue={x:Null}}"
Stretch="Uniform"
Visibility="{Binding ShowIcon}" />
</Border>
Expand Down
41 changes: 21 additions & 20 deletions Flow.Launcher/ViewModel/MainViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -194,37 +194,38 @@ private void InitializeKeyCommands()
PluginManager.API.OpenUrl("https://github.com/Flow-Launcher/Flow.Launcher/wiki/Flow-Launcher/");
});
OpenSettingCommand = new RelayCommand(_ => { App.API.OpenSettingDialog(); });
OpenResultCommand = new RelayCommand(index =>
OpenResultCommand = new RelayCommand(async index =>
{
var results = SelectedResults;

if (index != null)
{
results.SelectedIndex = int.Parse(index.ToString());
results.SelectedIndex = int.Parse(index.ToString()!);
}

var result = results.SelectedItem?.Result;
if (result != null) // SelectedItem returns null if selection is empty.
if (result == null)
{
bool hideWindow = result.Action != null && result.Action(new ActionContext
{
SpecialKeyState = GlobalHotkey.CheckModifiers()
});
return;
}
var hideWindow = await result.ExecuteAsync(new ActionContext
{
SpecialKeyState = GlobalHotkey.CheckModifiers()
}).ConfigureAwait(false);

if (hideWindow)
{
Hide();
}
if (hideWindow)
{
Hide();
}

if (SelectedIsFromQueryResults())
{
_userSelectedRecord.Add(result);
_history.Add(result.OriginQuery.RawQuery);
}
else
{
SelectedResults = Results;
}
if (SelectedIsFromQueryResults())
{
_userSelectedRecord.Add(result);
_history.Add(result.OriginQuery.RawQuery);
}
else
{
SelectedResults = Results;
}
});

Expand Down