diff --git a/Flow.Launcher.Plugin.FendCalculator.csproj b/Flow.Launcher.Plugin.FendCalculator.csproj index d640600..a115492 100644 --- a/Flow.Launcher.Plugin.FendCalculator.csproj +++ b/Flow.Launcher.Plugin.FendCalculator.csproj @@ -13,6 +13,11 @@ true + + true + true + + false None diff --git a/Main.cs b/Main.cs index 2fa43ff..2e36d4c 100644 --- a/Main.cs +++ b/Main.cs @@ -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(); + _viewModel = new SettingsViewModel(_settings); + if (string.IsNullOrEmpty(_settings.FendCommand)) + { + _settings.FendCommand = "fend"; + } } public List Query(Query query) @@ -31,39 +42,56 @@ public List 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); + } } } \ No newline at end of file diff --git a/Readme.md b/Readme.md index 17ed0bc..8995c0b 100644 --- a/Readme.md +++ b/Readme.md @@ -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. \ No newline at end of file diff --git a/Settings.cs b/Settings.cs new file mode 100644 index 0000000..a3c2a27 --- /dev/null +++ b/Settings.cs @@ -0,0 +1,7 @@ +namespace Flow.Launcher.Plugin.FendCalculator +{ + public class Settings + { + public string FendCommand { get; set; } = "fend"; + } +} \ No newline at end of file diff --git a/ViewModels/RelayCommand.cs b/ViewModels/RelayCommand.cs new file mode 100644 index 0000000..bb0a940 --- /dev/null +++ b/ViewModels/RelayCommand.cs @@ -0,0 +1,27 @@ +using System; +using System.Windows.Input; + +namespace Flow.Launcher.Plugin.FendCalculator.ViewModels +{ + internal class RelayCommand : ICommand + { + private Action _action; + + public RelayCommand(Action action) + { + _action = action; + } + + public virtual bool CanExecute(object parameter) + { + return true; + } + + public event EventHandler CanExecuteChanged; + + public virtual void Execute(object parameter) + { + _action?.Invoke(parameter); + } + } +} diff --git a/ViewModels/SettingsViewModel.cs b/ViewModels/SettingsViewModel.cs new file mode 100644 index 0000000..b9cabff --- /dev/null +++ b/ViewModels/SettingsViewModel.cs @@ -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; + }); + } +} \ No newline at end of file diff --git a/Views/FendCalculatorSettings.xaml b/Views/FendCalculatorSettings.xaml new file mode 100644 index 0000000..aaadfb3 --- /dev/null +++ b/Views/FendCalculatorSettings.xaml @@ -0,0 +1,55 @@ + + + + + + + + + + + + + + + + +