Skip to content

Commit ecbde92

Browse files
committed
feat: Add setting for fend command
1 parent 83a3e6b commit ecbde92

9 files changed

+252
-25
lines changed

Flow.Launcher.Plugin.FendCalculator.csproj

+5
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@
1313
<GenerateDocumentationFile>true</GenerateDocumentationFile>
1414
</PropertyGroup>
1515

16+
<PropertyGroup>
17+
<UseWPF>true</UseWPF>
18+
<UseWindowsForms>true</UseWindowsForms>
19+
</PropertyGroup>
20+
1621
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
1722
<DebugSymbols>false</DebugSymbols>
1823
<DebugType>None</DebugType>

Main.cs

+52-24
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,29 @@
11
using System.Runtime.InteropServices;
22
using System.Diagnostics;
33
using System.Windows;
4+
using System.Windows.Controls;
45
using System.Text;
56
using System.Collections.Generic;
7+
using Flow.Launcher.Plugin.FendCalculator.ViewModels;
8+
using Flow.Launcher.Plugin.FendCalculator.Views;
69

710
namespace Flow.Launcher.Plugin.FendCalculator
811
{
9-
public class FendCalculator : IPlugin
12+
public class FendCalculator : IPlugin, ISettingProvider
1013
{
1114
private PluginInitContext _context;
15+
private Settings _settings;
16+
private static SettingsViewModel _viewModel;
1217

1318
public void Init(PluginInitContext context)
1419
{
1520
_context = context;
21+
_settings = context.API.LoadSettingJsonStorage<Settings>();
22+
_viewModel = new SettingsViewModel(_settings);
23+
if (string.IsNullOrEmpty(_settings.FendCommand))
24+
{
25+
_settings.FendCommand = "fend";
26+
}
1627
}
1728

1829
public List<Result> Query(Query query)
@@ -31,39 +42,56 @@ public List<Result> Query(Query query)
3142
UseShellExecute = false,
3243
RedirectStandardOutput = true,
3344
StandardOutputEncoding = Encoding.UTF8,
34-
FileName = "fend",
45+
FileName = $"{_settings.FendCommand}",
3546
Arguments = $"\"{query.Search}\""
3647
};
37-
Process process = Process.Start(startInfo);
38-
39-
string output = process.StandardOutput.ReadToEnd().TrimEnd();
40-
if (process.ExitCode == 0 && !string.IsNullOrEmpty(output))
48+
try
4149
{
42-
var result = new Result
50+
Process process = Process.Start(startInfo);
51+
string output = process.StandardOutput.ReadToEnd().TrimEnd();
52+
if (process.ExitCode == 0 && !string.IsNullOrEmpty(output))
4353
{
44-
Title = output,
45-
SubTitle = "Copy result to clipboard",
46-
IcoPath = "Images/calculator.png",
47-
Score = 300,
48-
CopyText = output,
49-
Action = c =>
54+
var result = new Result
5055
{
51-
try
56+
Title = output,
57+
SubTitle = "Copy result to clipboard",
58+
IcoPath = "Images/calculator.png",
59+
Score = 300,
60+
CopyText = output,
61+
Action = c =>
5262
{
53-
Clipboard.SetDataObject(output);
54-
return true;
63+
try
64+
{
65+
Clipboard.SetDataObject(output);
66+
return true;
67+
}
68+
catch (ExternalException)
69+
{
70+
MessageBox.Show("Copy failed, please try later");
71+
return false;
72+
}
5573
}
56-
catch (ExternalException)
57-
{
58-
MessageBox.Show("Copy failed, please try later");
59-
return false;
60-
}
61-
}
62-
};
63-
results.Add(result);
74+
};
75+
results.Add(result);
76+
}
77+
}
78+
catch (ExternalException)
79+
{
80+
results.Add(new Result
81+
{
82+
Title = "Error",
83+
SubTitle = "fend command error. Check installation path or fend config file",
84+
IcoPath = "Images/calculator.png",
85+
Score = 300
86+
});
87+
return results;
6488
}
6589

6690
return results;
6791
}
92+
public Control CreateSettingPanel()
93+
{
94+
return new FendCalculatorSettings(_viewModel);
95+
}
6896
}
6997
}

Readme.md

+5
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,10 @@ Flow.Launcher.Plugin.FendCalculator
33

44
Integration of [Fend](https://printfn.github.io/fend/) with [Flow launcher](https://github.com/Flow-Launcher/Flow.Launcher).
55

6+
## Settings
7+
| Setting | Type | Default | Description |
8+
| ------------ | ------ | ------- | ---------------------------------------------- |
9+
| Fend Command | string | fend | Command to run fend or path to fend executable |
10+
611
## WIP
712
This plugin is still being worked on, some features are yet to be implemented.

Settings.cs

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
namespace Flow.Launcher.Plugin.FendCalculator
2+
{
3+
public class Settings
4+
{
5+
public string FendCommand { get; set; } = "fend";
6+
}
7+
}

ViewModels/RelayCommand.cs

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
using System;
2+
using System.Windows.Input;
3+
4+
namespace Flow.Launcher.Plugin.FendCalculator.ViewModels
5+
{
6+
internal class RelayCommand : ICommand
7+
{
8+
private Action<object> _action;
9+
10+
public RelayCommand(Action<object> action)
11+
{
12+
_action = action;
13+
}
14+
15+
public virtual bool CanExecute(object parameter)
16+
{
17+
return true;
18+
}
19+
20+
public event EventHandler CanExecuteChanged;
21+
22+
public virtual void Execute(object parameter)
23+
{
24+
_action?.Invoke(parameter);
25+
}
26+
}
27+
}

ViewModels/SettingsViewModel.cs

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
using System.Collections.Generic;
2+
using System.Linq;
3+
4+
using System;
5+
using System.Collections.Generic;
6+
using System.Collections.ObjectModel;
7+
using System.Diagnostics;
8+
using System.Diagnostics.CodeAnalysis;
9+
using System.IO;
10+
using System.Linq;
11+
using System.Windows;
12+
using System.Windows.Forms;
13+
using System.Windows.Input;
14+
15+
namespace Flow.Launcher.Plugin.FendCalculator.ViewModels
16+
{
17+
public class SettingsViewModel : BaseModel
18+
{
19+
public SettingsViewModel(Settings settings)
20+
{
21+
Settings = settings;
22+
}
23+
24+
public Settings Settings { get; init; }
25+
26+
public string FendCommand
27+
{
28+
get => Settings.FendCommand;
29+
set
30+
{
31+
Settings.FendCommand = value;
32+
OnPropertyChanged();
33+
}
34+
}
35+
36+
private string? PromptUserSelectPath(string? initialDirectory = null)
37+
{
38+
string? path = null;
39+
40+
var openFileDialog = new OpenFileDialog();
41+
if (initialDirectory is not null)
42+
openFileDialog.InitialDirectory = initialDirectory;
43+
44+
if (openFileDialog.ShowDialog() != DialogResult.OK)
45+
return path;
46+
47+
path = openFileDialog.FileName;
48+
49+
return path;
50+
}
51+
52+
private ICommand? _openFendPathCommand;
53+
54+
public ICommand OpenFendPath => _openFendPathCommand ??= new RelayCommand(_ =>
55+
{
56+
var path = PromptUserSelectPath(Settings.FendCommand != null ? Path.GetDirectoryName(Settings.FendCommand) : null);
57+
if (path is null)
58+
return;
59+
60+
FendCommand = path;
61+
});
62+
}
63+
}

Views/FendCalculatorSettings.xaml

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<UserControl
2+
x:Class="Flow.Launcher.Plugin.FendCalculator.Views.FendCalculatorSettings"
3+
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
4+
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
5+
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
6+
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
7+
xmlns:ui="http://schemas.modernwpf.com/2019"
8+
xmlns:viewModels="clr-namespace:Flow.Launcher.Plugin.FendCalculator.ViewModels"
9+
xmlns:views="clr-namespace:Flow.Launcher.Plugin.FendCalculator.Views"
10+
d:DataContext="{d:DesignInstance viewModels:SettingsViewModel}"
11+
d:DesignHeight="450"
12+
d:DesignWidth="800"
13+
mc:Ignorable="d">
14+
15+
<!-- <UserControl.Resources>
16+
<core:LocalizationConverter x:Key="LocalizationConverter" />
17+
</UserControl.Resources> -->
18+
19+
<Grid Margin="70,14,0,14">
20+
<Grid.RowDefinitions>
21+
<RowDefinition Height="auto" />
22+
</Grid.RowDefinitions>
23+
<Grid.ColumnDefinitions>
24+
<ColumnDefinition Width="Auto" />
25+
<ColumnDefinition Width="3*" />
26+
</Grid.ColumnDefinitions>
27+
28+
<TextBlock
29+
Grid.Row="0"
30+
Grid.Column="0"
31+
Margin="0,0,10,0"
32+
VerticalAlignment="Center"
33+
FontSize="14"
34+
Text="Fend Command" />
35+
<StackPanel
36+
Grid.Row="0"
37+
Grid.Column="1"
38+
Margin="0,6,6,6"
39+
Orientation="Horizontal">
40+
<TextBox
41+
Width="250"
42+
HorizontalAlignment="Left"
43+
VerticalAlignment="Center"
44+
Text="{Binding FendCommand}"
45+
TextWrapping="NoWrap" />
46+
<Button
47+
MinWidth="50"
48+
Margin="5,0,0,0"
49+
HorizontalAlignment="Left"
50+
VerticalAlignment="Center"
51+
Command="{Binding OpenFendPath}"
52+
Content="..." />
53+
</StackPanel>
54+
</Grid>
55+
</UserControl>

Views/FendCalculatorSettings.xaml.cs

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
using System.Windows;
7+
using System.Windows.Controls;
8+
using System.Windows.Data;
9+
using System.Windows.Documents;
10+
using System.Windows.Input;
11+
using System.Windows.Media;
12+
using System.Windows.Media.Imaging;
13+
using System.Windows.Navigation;
14+
using System.Windows.Shapes;
15+
using Flow.Launcher.Plugin.FendCalculator.ViewModels;
16+
17+
namespace Flow.Launcher.Plugin.FendCalculator.Views
18+
{
19+
/// <summary>
20+
/// Interaction logic for FendCalculatorSettings.xaml
21+
/// </summary>
22+
public partial class FendCalculatorSettings : UserControl
23+
{
24+
private readonly SettingsViewModel _viewModel;
25+
private readonly Settings _settings;
26+
27+
public FendCalculatorSettings(SettingsViewModel viewModel)
28+
{
29+
_viewModel = viewModel;
30+
_settings = viewModel.Settings;
31+
DataContext = viewModel;
32+
InitializeComponent();
33+
}
34+
}
35+
36+
37+
}

plugin.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"Name": "FendCalculator",
55
"Description": "Arbitrary-precision unit-aware calculator. https://printfn.github.io/fend/",
66
"Author": "IsaacTay",
7-
"Version": "0.1.0",
7+
"Version": "0.2.0",
88
"Language": "csharp",
99
"Website": "https://github.com/IsaacTay/Flow.Launcher.Plugin.FendCalculator",
1010
"IcoPath": "Images\\calculator.png",

0 commit comments

Comments
 (0)