Skip to content

Commit

Permalink
feat: Add setting for fend command
Browse files Browse the repository at this point in the history
  • Loading branch information
IsaacTay committed Mar 8, 2023
1 parent 83a3e6b commit ecbde92
Show file tree
Hide file tree
Showing 9 changed files with 252 additions and 25 deletions.
5 changes: 5 additions & 0 deletions Flow.Launcher.Plugin.FendCalculator.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@
<GenerateDocumentationFile>true</GenerateDocumentationFile>
</PropertyGroup>

<PropertyGroup>
<UseWPF>true</UseWPF>
<UseWindowsForms>true</UseWindowsForms>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
<DebugSymbols>false</DebugSymbols>
<DebugType>None</DebugType>
Expand Down
76 changes: 52 additions & 24 deletions Main.cs
Original file line number Diff line number Diff line change
@@ -1,18 +1,29 @@
using System.Runtime.InteropServices;
using System.Diagnostics;
using System.Windows;
using System.Windows.Controls;
using System.Text;
using System.Collections.Generic;
using Flow.Launcher.Plugin.FendCalculator.ViewModels;
using Flow.Launcher.Plugin.FendCalculator.Views;

namespace Flow.Launcher.Plugin.FendCalculator
{
public class FendCalculator : IPlugin
public class FendCalculator : IPlugin, ISettingProvider
{
private PluginInitContext _context;
private Settings _settings;
private static SettingsViewModel _viewModel;

public void Init(PluginInitContext context)
{
_context = context;
_settings = context.API.LoadSettingJsonStorage<Settings>();
_viewModel = new SettingsViewModel(_settings);
if (string.IsNullOrEmpty(_settings.FendCommand))
{
_settings.FendCommand = "fend";
}
}

public List<Result> Query(Query query)
Expand All @@ -31,39 +42,56 @@ public List<Result> Query(Query query)
UseShellExecute = false,
RedirectStandardOutput = true,
StandardOutputEncoding = Encoding.UTF8,
FileName = "fend",
FileName = $"{_settings.FendCommand}",
Arguments = $"\"{query.Search}\""
};
Process process = Process.Start(startInfo);

string output = process.StandardOutput.ReadToEnd().TrimEnd();
if (process.ExitCode == 0 && !string.IsNullOrEmpty(output))
try
{
var result = new Result
Process process = Process.Start(startInfo);
string output = process.StandardOutput.ReadToEnd().TrimEnd();
if (process.ExitCode == 0 && !string.IsNullOrEmpty(output))
{
Title = output,
SubTitle = "Copy result to clipboard",
IcoPath = "Images/calculator.png",
Score = 300,
CopyText = output,
Action = c =>
var result = new Result
{
try
Title = output,
SubTitle = "Copy result to clipboard",
IcoPath = "Images/calculator.png",
Score = 300,
CopyText = output,
Action = c =>
{
Clipboard.SetDataObject(output);
return true;
try
{
Clipboard.SetDataObject(output);
return true;
}
catch (ExternalException)
{
MessageBox.Show("Copy failed, please try later");
return false;
}
}
catch (ExternalException)
{
MessageBox.Show("Copy failed, please try later");
return false;
}
}
};
results.Add(result);
};
results.Add(result);
}
}
catch (ExternalException)
{
results.Add(new Result
{
Title = "Error",
SubTitle = "fend command error. Check installation path or fend config file",
IcoPath = "Images/calculator.png",
Score = 300
});
return results;
}

return results;
}
public Control CreateSettingPanel()
{
return new FendCalculatorSettings(_viewModel);
}
}
}
5 changes: 5 additions & 0 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,10 @@ Flow.Launcher.Plugin.FendCalculator

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

## Settings
| Setting | Type | Default | Description |
| ------------ | ------ | ------- | ---------------------------------------------- |
| Fend Command | string | fend | Command to run fend or path to fend executable |

## WIP
This plugin is still being worked on, some features are yet to be implemented.
7 changes: 7 additions & 0 deletions Settings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace Flow.Launcher.Plugin.FendCalculator
{
public class Settings
{
public string FendCommand { get; set; } = "fend";
}
}
27 changes: 27 additions & 0 deletions ViewModels/RelayCommand.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using System;
using System.Windows.Input;

namespace Flow.Launcher.Plugin.FendCalculator.ViewModels
{
internal class RelayCommand : ICommand
{
private Action<object> _action;

public RelayCommand(Action<object> action)
{
_action = action;
}

public virtual bool CanExecute(object parameter)
{
return true;
}

public event EventHandler CanExecuteChanged;

public virtual void Execute(object parameter)
{
_action?.Invoke(parameter);
}
}
}
63 changes: 63 additions & 0 deletions ViewModels/SettingsViewModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
using System.Collections.Generic;
using System.Linq;

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Linq;
using System.Windows;
using System.Windows.Forms;
using System.Windows.Input;

namespace Flow.Launcher.Plugin.FendCalculator.ViewModels
{
public class SettingsViewModel : BaseModel
{
public SettingsViewModel(Settings settings)
{
Settings = settings;
}

public Settings Settings { get; init; }

public string FendCommand
{
get => Settings.FendCommand;
set
{
Settings.FendCommand = value;
OnPropertyChanged();
}
}

private string? PromptUserSelectPath(string? initialDirectory = null)
{
string? path = null;

var openFileDialog = new OpenFileDialog();
if (initialDirectory is not null)
openFileDialog.InitialDirectory = initialDirectory;

if (openFileDialog.ShowDialog() != DialogResult.OK)
return path;

path = openFileDialog.FileName;

return path;
}

private ICommand? _openFendPathCommand;

public ICommand OpenFendPath => _openFendPathCommand ??= new RelayCommand(_ =>
{
var path = PromptUserSelectPath(Settings.FendCommand != null ? Path.GetDirectoryName(Settings.FendCommand) : null);
if (path is null)
return;
FendCommand = path;
});
}
}
55 changes: 55 additions & 0 deletions Views/FendCalculatorSettings.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<UserControl
x:Class="Flow.Launcher.Plugin.FendCalculator.Views.FendCalculatorSettings"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:ui="http://schemas.modernwpf.com/2019"
xmlns:viewModels="clr-namespace:Flow.Launcher.Plugin.FendCalculator.ViewModels"
xmlns:views="clr-namespace:Flow.Launcher.Plugin.FendCalculator.Views"
d:DataContext="{d:DesignInstance viewModels:SettingsViewModel}"
d:DesignHeight="450"
d:DesignWidth="800"
mc:Ignorable="d">

<!-- <UserControl.Resources>
<core:LocalizationConverter x:Key="LocalizationConverter" />
</UserControl.Resources> -->

<Grid Margin="70,14,0,14">
<Grid.RowDefinitions>
<RowDefinition Height="auto" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="3*" />
</Grid.ColumnDefinitions>

<TextBlock
Grid.Row="0"
Grid.Column="0"
Margin="0,0,10,0"
VerticalAlignment="Center"
FontSize="14"
Text="Fend Command" />
<StackPanel
Grid.Row="0"
Grid.Column="1"
Margin="0,6,6,6"
Orientation="Horizontal">
<TextBox
Width="250"
HorizontalAlignment="Left"
VerticalAlignment="Center"
Text="{Binding FendCommand}"
TextWrapping="NoWrap" />
<Button
MinWidth="50"
Margin="5,0,0,0"
HorizontalAlignment="Left"
VerticalAlignment="Center"
Command="{Binding OpenFendPath}"
Content="..." />
</StackPanel>
</Grid>
</UserControl>
37 changes: 37 additions & 0 deletions Views/FendCalculatorSettings.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Flow.Launcher.Plugin.FendCalculator.ViewModels;

namespace Flow.Launcher.Plugin.FendCalculator.Views
{
/// <summary>
/// Interaction logic for FendCalculatorSettings.xaml
/// </summary>
public partial class FendCalculatorSettings : UserControl
{
private readonly SettingsViewModel _viewModel;
private readonly Settings _settings;

public FendCalculatorSettings(SettingsViewModel viewModel)
{
_viewModel = viewModel;
_settings = viewModel.Settings;
DataContext = viewModel;
InitializeComponent();
}
}


}
2 changes: 1 addition & 1 deletion plugin.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"Name": "FendCalculator",
"Description": "Arbitrary-precision unit-aware calculator. https://printfn.github.io/fend/",
"Author": "IsaacTay",
"Version": "0.1.0",
"Version": "0.2.0",
"Language": "csharp",
"Website": "https://github.com/IsaacTay/Flow.Launcher.Plugin.FendCalculator",
"IcoPath": "Images\\calculator.png",
Expand Down

0 comments on commit ecbde92

Please sign in to comment.