From ee43152de182f2033fbbc536271c9e605c7f0fe5 Mon Sep 17 00:00:00 2001 From: Jeremy Wu Date: Tue, 21 Jul 2020 14:40:44 +1000 Subject: [PATCH 01/15] save IconPath to settings for customisation --- .../SearchSource.cs | 23 +++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/Plugins/Flow.Launcher.Plugin.WebSearch/SearchSource.cs b/Plugins/Flow.Launcher.Plugin.WebSearch/SearchSource.cs index 20d0dd5a4..3d548b6b1 100644 --- a/Plugins/Flow.Launcher.Plugin.WebSearch/SearchSource.cs +++ b/Plugins/Flow.Launcher.Plugin.WebSearch/SearchSource.cs @@ -1,4 +1,4 @@ -using System.IO; +using System.IO; using System.Windows.Media; using JetBrains.Annotations; using Newtonsoft.Json; @@ -16,11 +16,26 @@ namespace Flow.Launcher.Plugin.WebSearch public string Icon { get; set; } = DefaultIcon; /// - /// All icon should be put under Images directory + /// Default icons are placed in Images directory in the app location. + /// Custom icons are placed in the user data directory /// [NotNull] - [JsonIgnore] - internal string IconPath => Path.Combine(Main.ImagesDirectory, Icon); + public string IconPath + { + get + { + if (string.IsNullOrEmpty(iconPath)) + return Path.Combine(Main.ImagesDirectory, Icon); + + return iconPath; + } + set + { + iconPath = value; + } + } + + private string iconPath; [JsonIgnore] public ImageSource Image => ImageLoader.Load(IconPath); From 91e7b1cfb81277a3594ba94a6af468fd504fb02f Mon Sep 17 00:00:00 2001 From: Jeremy Wu Date: Wed, 22 Jul 2020 08:17:18 +1000 Subject: [PATCH 02/15] update IconPath handling of path --- .../SearchSource.cs | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/Plugins/Flow.Launcher.Plugin.WebSearch/SearchSource.cs b/Plugins/Flow.Launcher.Plugin.WebSearch/SearchSource.cs index 3d548b6b1..1277a92fe 100644 --- a/Plugins/Flow.Launcher.Plugin.WebSearch/SearchSource.cs +++ b/Plugins/Flow.Launcher.Plugin.WebSearch/SearchSource.cs @@ -3,17 +3,20 @@ using System.Windows.Media; using JetBrains.Annotations; using Newtonsoft.Json; using Flow.Launcher.Infrastructure.Image; +using Flow.Launcher.Infrastructure; +using System.Reflection; namespace Flow.Launcher.Plugin.WebSearch { public class SearchSource : BaseModel { - public const string DefaultIcon = "web_search.png"; public string Title { get; set; } public string ActionKeyword { get; set; } [NotNull] - public string Icon { get; set; } = DefaultIcon; + public string Icon { get; set; } = "web_search.png"; + + private string iconPath; /// /// Default icons are placed in Images directory in the app location. @@ -25,7 +28,13 @@ namespace Flow.Launcher.Plugin.WebSearch get { if (string.IsNullOrEmpty(iconPath)) - return Path.Combine(Main.ImagesDirectory, Icon); + { + var pluginDirectorys = Directory.GetParent(Assembly.GetExecutingAssembly().Location.NonNull()).ToString(); + + var imagesDirectory = Path.Combine(pluginDirectorys, "Images"); + + return Path.Combine(imagesDirectory, Icon); + } return iconPath; } @@ -35,8 +44,6 @@ namespace Flow.Launcher.Plugin.WebSearch } } - private string iconPath; - [JsonIgnore] public ImageSource Image => ImageLoader.Load(IconPath); @@ -51,6 +58,7 @@ namespace Flow.Launcher.Plugin.WebSearch ActionKeyword = string.Copy(ActionKeyword), Url = string.Copy(Url), Icon = string.Copy(Icon), + IconPath = string.Copy(IconPath), Enabled = Enabled }; return webSearch; From 2c4eeb00bd0bd996601808dc148b9c90bef6a831 Mon Sep 17 00:00:00 2001 From: Jeremy Wu Date: Wed, 22 Jul 2020 08:18:36 +1000 Subject: [PATCH 03/15] add Search Source custom icon setting selection and saving --- .../SearchSourceSetting.xaml.cs | 18 ++++--- .../SearchSourceViewModel.cs | 50 ++++++++++++++++++- 2 files changed, 60 insertions(+), 8 deletions(-) diff --git a/Plugins/Flow.Launcher.Plugin.WebSearch/SearchSourceSetting.xaml.cs b/Plugins/Flow.Launcher.Plugin.WebSearch/SearchSourceSetting.xaml.cs index 9ecdda23f..ac1882cde 100644 --- a/Plugins/Flow.Launcher.Plugin.WebSearch/SearchSourceSetting.xaml.cs +++ b/Plugins/Flow.Launcher.Plugin.WebSearch/SearchSourceSetting.xaml.cs @@ -1,5 +1,4 @@ using System.Collections.Generic; -using System.IO; using System.Windows; using Microsoft.Win32; using Flow.Launcher.Core.Plugin; @@ -122,15 +121,20 @@ namespace Flow.Launcher.Plugin.WebSearch var result = dialog.ShowDialog(); if (result == true) { - var fullpath = dialog.FileName; - if (!string.IsNullOrEmpty(fullpath)) + var fullpathToSelectedImage = dialog.FileName; + if (!string.IsNullOrEmpty(fullpathToSelectedImage)) { - _searchSource.Icon = Path.GetFileName(fullpath); - if (!File.Exists(_searchSource.IconPath)) + if (!_viewModel.ImageFileExistsInLocation(fullpathToSelectedImage)) { - _searchSource.Icon = SearchSource.DefaultIcon; - MessageBox.Show($"The file should be put under {directory}"); + var fullPathToOriginalImage = _searchSource.IconPath; + _viewModel.UpdateIconPath(_searchSource, fullpathToSelectedImage); + _viewModel.CopyNewImageToUserDataDirectory(_searchSource, fullpathToSelectedImage, fullPathToOriginalImage); + + return; } + + MessageBox.Show($"An image of the same file name already exists in location {directory}. " + + $"The icon image has not been updated"); } } } diff --git a/Plugins/Flow.Launcher.Plugin.WebSearch/SearchSourceViewModel.cs b/Plugins/Flow.Launcher.Plugin.WebSearch/SearchSourceViewModel.cs index 049c50560..5bcb3938d 100644 --- a/Plugins/Flow.Launcher.Plugin.WebSearch/SearchSourceViewModel.cs +++ b/Plugins/Flow.Launcher.Plugin.WebSearch/SearchSourceViewModel.cs @@ -1,7 +1,55 @@ -namespace Flow.Launcher.Plugin.WebSearch +using Flow.Launcher.Infrastructure; +using Flow.Launcher.Infrastructure.UserSettings; +using System; +using System.IO; +using System.Windows.Forms; + +namespace Flow.Launcher.Plugin.WebSearch { public class SearchSourceViewModel { + private readonly string destinationDirectory = + Path.Combine(DataLocation.DataDirectory(), @"Settings\Plugins\Flow.Launcher.Plugin.WebSearch\IconImages"); + public SearchSource SearchSource { get; set; } + + public void UpdateIconPath(SearchSource selectedSearchSource, string fullpathToSelectedImage) + { + var iconFileName = Path.GetFileName(fullpathToSelectedImage); + selectedSearchSource.Icon = iconFileName; + selectedSearchSource.IconPath = Path.Combine(destinationDirectory, Path.GetFileName(fullpathToSelectedImage)); + } + + public void CopyNewImageToUserDataDirectory(SearchSource selectedSearchSource, string fullpathToSelectedImage, string fullPathToOriginalImage) + { + var destinationFileNameFullPath = Path.Combine(destinationDirectory, Path.GetFileName(fullpathToSelectedImage)); + + try + { + if (!Directory.Exists(destinationDirectory)) + Directory.CreateDirectory(destinationDirectory); + + File.Copy(fullpathToSelectedImage, destinationFileNameFullPath); + } + catch(Exception e) + { +#if DEBUG + throw e; +#else + MessageBox.Show(string.Format("Copying the selected image file to {0} has failed, changes will now be reverted", destinationFileNameFullPath)); + UpdateIconPath(selectedSearchSource, fullPathToOriginalImage); +#endif + } + + } + + public bool ImageFileExistsInLocation(string fullpathToSelectedImage) + { + var fileName = Path.GetFileName(fullpathToSelectedImage); + + var newImageFilePathToBe = Path.Combine(destinationDirectory, fileName); + + return File.Exists(newImageFilePathToBe); + } } } \ No newline at end of file From e2dff94f7c1f07a44f56e9c5b9384626e48c4b08 Mon Sep 17 00:00:00 2001 From: Jeremy Wu Date: Wed, 22 Jul 2020 08:19:35 +1000 Subject: [PATCH 04/15] add comment summary to clarify the shared Copy command --- Flow.Launcher.Plugin/SharedCommands/FilesFolders.cs | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/Flow.Launcher.Plugin/SharedCommands/FilesFolders.cs b/Flow.Launcher.Plugin/SharedCommands/FilesFolders.cs index 98cd777aa..c2ead6c9c 100644 --- a/Flow.Launcher.Plugin/SharedCommands/FilesFolders.cs +++ b/Flow.Launcher.Plugin/SharedCommands/FilesFolders.cs @@ -11,6 +11,12 @@ namespace Flow.Launcher.Plugin.SharedCommands private const string FileExplorerProgramEXE = "explorer.exe"; + /// + /// Copies the folder and all of its files and folders + /// including subfolders to the target location + /// + /// + /// public static void Copy(this string sourcePath, string targetPath) { // Get the subdirectories for the specified directory. From 8f8b2d7ff82b8af9dd9fbeb0e1f9a8224eb7912f Mon Sep 17 00:00:00 2001 From: Jeremy Wu Date: Wed, 22 Jul 2020 20:35:56 +1000 Subject: [PATCH 05/15] notify change of SearchSource's image after selection --- Plugins/Flow.Launcher.Plugin.WebSearch/SearchSource.cs | 2 ++ .../Flow.Launcher.Plugin.WebSearch/SearchSourceViewModel.cs | 6 ++++-- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/Plugins/Flow.Launcher.Plugin.WebSearch/SearchSource.cs b/Plugins/Flow.Launcher.Plugin.WebSearch/SearchSource.cs index 1277a92fe..e8c3b998c 100644 --- a/Plugins/Flow.Launcher.Plugin.WebSearch/SearchSource.cs +++ b/Plugins/Flow.Launcher.Plugin.WebSearch/SearchSource.cs @@ -47,6 +47,8 @@ namespace Flow.Launcher.Plugin.WebSearch [JsonIgnore] public ImageSource Image => ImageLoader.Load(IconPath); + internal void NotifyImageChange() => OnPropertyChanged(nameof(Image)); + public string Url { get; set; } public bool Enabled { get; set; } diff --git a/Plugins/Flow.Launcher.Plugin.WebSearch/SearchSourceViewModel.cs b/Plugins/Flow.Launcher.Plugin.WebSearch/SearchSourceViewModel.cs index 5bcb3938d..2ca09d408 100644 --- a/Plugins/Flow.Launcher.Plugin.WebSearch/SearchSourceViewModel.cs +++ b/Plugins/Flow.Launcher.Plugin.WebSearch/SearchSourceViewModel.cs @@ -1,4 +1,4 @@ -using Flow.Launcher.Infrastructure; +using Flow.Launcher.Infrastructure; using Flow.Launcher.Infrastructure.UserSettings; using System; using System.IO; @@ -6,7 +6,7 @@ using System.Windows.Forms; namespace Flow.Launcher.Plugin.WebSearch { - public class SearchSourceViewModel + public class SearchSourceViewModel : BaseModel { private readonly string destinationDirectory = Path.Combine(DataLocation.DataDirectory(), @"Settings\Plugins\Flow.Launcher.Plugin.WebSearch\IconImages"); @@ -30,6 +30,8 @@ namespace Flow.Launcher.Plugin.WebSearch Directory.CreateDirectory(destinationDirectory); File.Copy(fullpathToSelectedImage, destinationFileNameFullPath); + + selectedSearchSource.NotifyImageChange(); } catch(Exception e) { From dbfa2bd6c4fa361320aa67b9be5d492c62868f19 Mon Sep 17 00:00:00 2001 From: Jeremy Wu Date: Wed, 22 Jul 2020 20:41:50 +1000 Subject: [PATCH 06/15] fix typo --- Flow.Launcher.Plugin/SharedCommands/FilesFolders.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Flow.Launcher.Plugin/SharedCommands/FilesFolders.cs b/Flow.Launcher.Plugin/SharedCommands/FilesFolders.cs index c2ead6c9c..13905788a 100644 --- a/Flow.Launcher.Plugin/SharedCommands/FilesFolders.cs +++ b/Flow.Launcher.Plugin/SharedCommands/FilesFolders.cs @@ -178,7 +178,7 @@ namespace Flow.Launcher.Plugin.SharedCommands /// /// Gets the previous level directory from a path string. /// Checks that previous level directory exists and returns it - /// as a path string, or empty string if doesn't exit + /// as a path string, or empty string if doesn't exist /// public static string GetPreviousExistingDirectory(Func locationExists, string path) { From bd48ee5c90fade9dd555062dade9c82640dd34c8 Mon Sep 17 00:00:00 2001 From: Jeremy Wu Date: Wed, 22 Jul 2020 22:00:14 +1000 Subject: [PATCH 07/15] update behaviour when to copy custom icons - copy images when not in the plugin's default images directory - do not copy if in the default directory or already in user data directory --- .../Languages/en.xaml | 1 + .../SearchSourceSetting.xaml.cs | 20 ++++--- .../SearchSourceViewModel.cs | 54 +++++++++++-------- 3 files changed, 41 insertions(+), 34 deletions(-) diff --git a/Plugins/Flow.Launcher.Plugin.WebSearch/Languages/en.xaml b/Plugins/Flow.Launcher.Plugin.WebSearch/Languages/en.xaml index f1ae5bc35..c2ed7df31 100644 --- a/Plugins/Flow.Launcher.Plugin.WebSearch/Languages/en.xaml +++ b/Plugins/Flow.Launcher.Plugin.WebSearch/Languages/en.xaml @@ -25,6 +25,7 @@ Please enter a URL Action keyword already exists, please enter a different one Success + Hint: You do not need to place custom images in this directory, if Flow's version is updated they will be lost. Flow will automatically copy any images outside of this directory across to WebSearch's custom image location. Web Searches Allows to perform web searches diff --git a/Plugins/Flow.Launcher.Plugin.WebSearch/SearchSourceSetting.xaml.cs b/Plugins/Flow.Launcher.Plugin.WebSearch/SearchSourceSetting.xaml.cs index ac1882cde..a7d001197 100644 --- a/Plugins/Flow.Launcher.Plugin.WebSearch/SearchSourceSetting.xaml.cs +++ b/Plugins/Flow.Launcher.Plugin.WebSearch/SearchSourceSetting.xaml.cs @@ -38,6 +38,8 @@ namespace Flow.Launcher.Plugin.WebSearch _action = action; _context = context; _api = _context.API; + + _viewModel.SetupCustomImagesDirectory(); } private void OnCancelButtonClick(object sender, RoutedEventArgs e) @@ -114,27 +116,23 @@ namespace Flow.Launcher.Plugin.WebSearch private void OnSelectIconClick(object sender, RoutedEventArgs e) { - var directory = Main.ImagesDirectory; const string filter = "Image files (*.jpg, *.jpeg, *.gif, *.png, *.bmp) |*.jpg; *.jpeg; *.gif; *.png; *.bmp"; - var dialog = new OpenFileDialog {InitialDirectory = directory, Filter = filter}; + var dialog = new OpenFileDialog {InitialDirectory = _viewModel.DestinationDirectory, Filter = filter}; var result = dialog.ShowDialog(); if (result == true) { var fullpathToSelectedImage = dialog.FileName; + + if (_viewModel.ShouldProvideHint(fullpathToSelectedImage)) + MessageBox.Show(_api.GetTranslation("flowlauncher_plugin_websearch_iconpath_hint")); + if (!string.IsNullOrEmpty(fullpathToSelectedImage)) { - if (!_viewModel.ImageFileExistsInLocation(fullpathToSelectedImage)) - { var fullPathToOriginalImage = _searchSource.IconPath; _viewModel.UpdateIconPath(_searchSource, fullpathToSelectedImage); - _viewModel.CopyNewImageToUserDataDirectory(_searchSource, fullpathToSelectedImage, fullPathToOriginalImage); - - return; - } - - MessageBox.Show($"An image of the same file name already exists in location {directory}. " + - $"The icon image has not been updated"); + _viewModel.CopyNewImageToUserDataDirectoryIfRequired( + _searchSource, fullpathToSelectedImage, fullPathToOriginalImage); } } } diff --git a/Plugins/Flow.Launcher.Plugin.WebSearch/SearchSourceViewModel.cs b/Plugins/Flow.Launcher.Plugin.WebSearch/SearchSourceViewModel.cs index 2ca09d408..4c510bdc1 100644 --- a/Plugins/Flow.Launcher.Plugin.WebSearch/SearchSourceViewModel.cs +++ b/Plugins/Flow.Launcher.Plugin.WebSearch/SearchSourceViewModel.cs @@ -1,57 +1,65 @@ -using Flow.Launcher.Infrastructure; using Flow.Launcher.Infrastructure.UserSettings; using System; using System.IO; -using System.Windows.Forms; namespace Flow.Launcher.Plugin.WebSearch { public class SearchSourceViewModel : BaseModel { - private readonly string destinationDirectory = - Path.Combine(DataLocation.DataDirectory(), @"Settings\Plugins\Flow.Launcher.Plugin.WebSearch\IconImages"); + internal readonly string DestinationDirectory = + Path.Combine(DataLocation.DataDirectory(), @"Settings\Plugins\Flow.Launcher.Plugin.WebSearch\CustomIcons"); public SearchSource SearchSource { get; set; } public void UpdateIconPath(SearchSource selectedSearchSource, string fullpathToSelectedImage) { + var parentDirectorySelectedImg = Directory.GetParent(fullpathToSelectedImage).ToString(); + + var iconPathDirectory = parentDirectorySelectedImg == DestinationDirectory + || parentDirectorySelectedImg == Main.ImagesDirectory + ? parentDirectorySelectedImg : DestinationDirectory; + var iconFileName = Path.GetFileName(fullpathToSelectedImage); selectedSearchSource.Icon = iconFileName; - selectedSearchSource.IconPath = Path.Combine(destinationDirectory, Path.GetFileName(fullpathToSelectedImage)); + selectedSearchSource.IconPath = Path.Combine(iconPathDirectory, Path.GetFileName(fullpathToSelectedImage)); } - public void CopyNewImageToUserDataDirectory(SearchSource selectedSearchSource, string fullpathToSelectedImage, string fullPathToOriginalImage) + public void CopyNewImageToUserDataDirectoryIfRequired( + SearchSource selectedSearchSource, string fullpathToSelectedImage, string fullPathToOriginalImage) { - var destinationFileNameFullPath = Path.Combine(destinationDirectory, Path.GetFileName(fullpathToSelectedImage)); + var destinationFileNameFullPath = Path.Combine(DestinationDirectory, Path.GetFileName(fullpathToSelectedImage)); - try - { - if (!Directory.Exists(destinationDirectory)) - Directory.CreateDirectory(destinationDirectory); - - File.Copy(fullpathToSelectedImage, destinationFileNameFullPath); - - selectedSearchSource.NotifyImageChange(); - } - catch(Exception e) + var parentDirectorySelectedImg = Directory.GetParent(fullpathToSelectedImage).ToString(); + + if (parentDirectorySelectedImg != DestinationDirectory && parentDirectorySelectedImg != Main.ImagesDirectory) { + try + { + File.Copy(fullpathToSelectedImage, destinationFileNameFullPath); + } + catch (Exception e) + { #if DEBUG - throw e; + throw e; #else MessageBox.Show(string.Format("Copying the selected image file to {0} has failed, changes will now be reverted", destinationFileNameFullPath)); UpdateIconPath(selectedSearchSource, fullPathToOriginalImage); #endif + } } + selectedSearchSource.NotifyImageChange(); } - public bool ImageFileExistsInLocation(string fullpathToSelectedImage) + internal void SetupCustomImagesDirectory() { - var fileName = Path.GetFileName(fullpathToSelectedImage); + if (!Directory.Exists(DestinationDirectory)) + Directory.CreateDirectory(DestinationDirectory); + } - var newImageFilePathToBe = Path.Combine(destinationDirectory, fileName); - - return File.Exists(newImageFilePathToBe); + internal bool ShouldProvideHint(string fullPathToSelectedImage) + { + return Directory.GetParent(fullPathToSelectedImage).ToString() == Main.ImagesDirectory; } } } \ No newline at end of file From d89570ebce8f990901ab52ee4664a94cd0465fd5 Mon Sep 17 00:00:00 2001 From: Jeremy Wu Date: Wed, 22 Jul 2020 22:11:53 +1000 Subject: [PATCH 08/15] fix build fail on MessageBox --- Plugins/Flow.Launcher.Plugin.WebSearch/SearchSourceViewModel.cs | 1 + 1 file changed, 1 insertion(+) diff --git a/Plugins/Flow.Launcher.Plugin.WebSearch/SearchSourceViewModel.cs b/Plugins/Flow.Launcher.Plugin.WebSearch/SearchSourceViewModel.cs index 4c510bdc1..7eee86560 100644 --- a/Plugins/Flow.Launcher.Plugin.WebSearch/SearchSourceViewModel.cs +++ b/Plugins/Flow.Launcher.Plugin.WebSearch/SearchSourceViewModel.cs @@ -1,6 +1,7 @@ using Flow.Launcher.Infrastructure.UserSettings; using System; using System.IO; +using System.Windows; namespace Flow.Launcher.Plugin.WebSearch { From c4d1fe4436060357f0de100026e9bf7f67c580f5 Mon Sep 17 00:00:00 2001 From: Jeremy Wu Date: Tue, 4 Aug 2020 20:36:43 +1000 Subject: [PATCH 09/15] version bump WebSearch --- Plugins/Flow.Launcher.Plugin.WebSearch/plugin.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Plugins/Flow.Launcher.Plugin.WebSearch/plugin.json b/Plugins/Flow.Launcher.Plugin.WebSearch/plugin.json index 9784cfefe..a91e42f45 100644 --- a/Plugins/Flow.Launcher.Plugin.WebSearch/plugin.json +++ b/Plugins/Flow.Launcher.Plugin.WebSearch/plugin.json @@ -25,7 +25,7 @@ "Name": "Web Searches", "Description": "Provide the web search ability", "Author": "qianlifeng", - "Version": "1.0.0", + "Version": "1.0.1", "Language": "csharp", "Website": "https://github.com/Flow-Launcher/Flow.Launcher", "ExecuteFileName": "Flow.Launcher.Plugin.WebSearch.dll", From 48557aec068cd5abffa765e62e85413d266eda6b Mon Sep 17 00:00:00 2001 From: Jeremy Wu Date: Wed, 12 Aug 2020 07:08:24 +1000 Subject: [PATCH 10/15] change to dynamically loading of icon images for WebSearch --- .../UserSettings/DataLocation.cs | 2 ++ .../Flow.Launcher.Plugin.WebSearch/Main.cs | 13 ++++++---- .../SearchSource.cs | 24 ++++++------------- .../SearchSourceSetting.xaml.cs | 2 +- .../SearchSourceViewModel.cs | 20 +++++----------- 5 files changed, 25 insertions(+), 36 deletions(-) diff --git a/Flow.Launcher.Infrastructure/UserSettings/DataLocation.cs b/Flow.Launcher.Infrastructure/UserSettings/DataLocation.cs index 310c1e33a..98fa9aa64 100644 --- a/Flow.Launcher.Infrastructure/UserSettings/DataLocation.cs +++ b/Flow.Launcher.Infrastructure/UserSettings/DataLocation.cs @@ -30,5 +30,7 @@ namespace Flow.Launcher.Infrastructure.UserSettings } public static readonly string PluginsDirectory = Path.Combine(DataDirectory(), Constant.Plugins); + public static readonly string SettingsDirectory = Path.Combine(DataDirectory(), "Settings"); + public static readonly string PluginSettingsDirectory = Path.Combine(SettingsDirectory, Constant.Plugins); } } diff --git a/Plugins/Flow.Launcher.Plugin.WebSearch/Main.cs b/Plugins/Flow.Launcher.Plugin.WebSearch/Main.cs index 3fdb26c93..b9d7b4f71 100644 --- a/Plugins/Flow.Launcher.Plugin.WebSearch/Main.cs +++ b/Plugins/Flow.Launcher.Plugin.WebSearch/Main.cs @@ -8,6 +8,7 @@ using System.Threading.Tasks; using System.Windows.Controls; using Flow.Launcher.Infrastructure; using Flow.Launcher.Infrastructure.Storage; +using Flow.Launcher.Infrastructure.UserSettings; using Flow.Launcher.Plugin.SharedCommands; namespace Flow.Launcher.Plugin.WebSearch @@ -21,8 +22,9 @@ namespace Flow.Launcher.Plugin.WebSearch private CancellationTokenSource _updateSource; private CancellationToken _updateToken; - public const string Images = "Images"; - public static string ImagesDirectory; + internal const string Images = "Images"; + internal static string DefaultImagesDirectory; + internal static string CustomImagesDirectory; private readonly string SearchSourceGlobalPluginWildCardSign = "*"; @@ -172,8 +174,11 @@ namespace Flow.Launcher.Plugin.WebSearch _context = context; var pluginDirectory = _context.CurrentPluginMetadata.PluginDirectory; var bundledImagesDirectory = Path.Combine(pluginDirectory, Images); - ImagesDirectory = Path.Combine(_context.CurrentPluginMetadata.PluginDirectory, Images); - Helper.ValidateDataDirectory(bundledImagesDirectory, ImagesDirectory); + DefaultImagesDirectory = Path.Combine(_context.CurrentPluginMetadata.PluginDirectory, Images); + Helper.ValidateDataDirectory(bundledImagesDirectory, DefaultImagesDirectory); + + var name = Path.GetFileNameWithoutExtension(_context.CurrentPluginMetadata.ExecuteFileName); + CustomImagesDirectory = Path.Combine(DataLocation.PluginSettingsDirectory, $"{name}\\CustomIcons"); } #region ISettingProvider Members diff --git a/Plugins/Flow.Launcher.Plugin.WebSearch/SearchSource.cs b/Plugins/Flow.Launcher.Plugin.WebSearch/SearchSource.cs index e8c3b998c..de83cfad5 100644 --- a/Plugins/Flow.Launcher.Plugin.WebSearch/SearchSource.cs +++ b/Plugins/Flow.Launcher.Plugin.WebSearch/SearchSource.cs @@ -16,31 +16,21 @@ namespace Flow.Launcher.Plugin.WebSearch [NotNull] public string Icon { get; set; } = "web_search.png"; - private string iconPath; + public bool CustomIcon { get; set; } = false; /// /// Default icons are placed in Images directory in the app location. /// Custom icons are placed in the user data directory /// - [NotNull] + [JsonIgnore] public string IconPath - { + { get { - if (string.IsNullOrEmpty(iconPath)) - { - var pluginDirectorys = Directory.GetParent(Assembly.GetExecutingAssembly().Location.NonNull()).ToString(); + if (CustomIcon) + return Path.Combine(Main.CustomImagesDirectory, Icon); - var imagesDirectory = Path.Combine(pluginDirectorys, "Images"); - - return Path.Combine(imagesDirectory, Icon); - } - - return iconPath; - } - set - { - iconPath = value; + return Path.Combine(Main.DefaultImagesDirectory, Icon); } } @@ -60,7 +50,7 @@ namespace Flow.Launcher.Plugin.WebSearch ActionKeyword = string.Copy(ActionKeyword), Url = string.Copy(Url), Icon = string.Copy(Icon), - IconPath = string.Copy(IconPath), + CustomIcon = CustomIcon, Enabled = Enabled }; return webSearch; diff --git a/Plugins/Flow.Launcher.Plugin.WebSearch/SearchSourceSetting.xaml.cs b/Plugins/Flow.Launcher.Plugin.WebSearch/SearchSourceSetting.xaml.cs index a7d001197..69b6de617 100644 --- a/Plugins/Flow.Launcher.Plugin.WebSearch/SearchSourceSetting.xaml.cs +++ b/Plugins/Flow.Launcher.Plugin.WebSearch/SearchSourceSetting.xaml.cs @@ -117,7 +117,7 @@ namespace Flow.Launcher.Plugin.WebSearch private void OnSelectIconClick(object sender, RoutedEventArgs e) { const string filter = "Image files (*.jpg, *.jpeg, *.gif, *.png, *.bmp) |*.jpg; *.jpeg; *.gif; *.png; *.bmp"; - var dialog = new OpenFileDialog {InitialDirectory = _viewModel.DestinationDirectory, Filter = filter}; + var dialog = new OpenFileDialog {InitialDirectory = Main.CustomImagesDirectory, Filter = filter}; var result = dialog.ShowDialog(); if (result == true) diff --git a/Plugins/Flow.Launcher.Plugin.WebSearch/SearchSourceViewModel.cs b/Plugins/Flow.Launcher.Plugin.WebSearch/SearchSourceViewModel.cs index 7eee86560..3cddc59c5 100644 --- a/Plugins/Flow.Launcher.Plugin.WebSearch/SearchSourceViewModel.cs +++ b/Plugins/Flow.Launcher.Plugin.WebSearch/SearchSourceViewModel.cs @@ -1,38 +1,30 @@ -using Flow.Launcher.Infrastructure.UserSettings; using System; using System.IO; -using System.Windows; namespace Flow.Launcher.Plugin.WebSearch { public class SearchSourceViewModel : BaseModel { - internal readonly string DestinationDirectory = - Path.Combine(DataLocation.DataDirectory(), @"Settings\Plugins\Flow.Launcher.Plugin.WebSearch\CustomIcons"); - public SearchSource SearchSource { get; set; } public void UpdateIconPath(SearchSource selectedSearchSource, string fullpathToSelectedImage) { var parentDirectorySelectedImg = Directory.GetParent(fullpathToSelectedImage).ToString(); - var iconPathDirectory = parentDirectorySelectedImg == DestinationDirectory - || parentDirectorySelectedImg == Main.ImagesDirectory - ? parentDirectorySelectedImg : DestinationDirectory; + selectedSearchSource.CustomIcon = parentDirectorySelectedImg != Main.DefaultImagesDirectory; var iconFileName = Path.GetFileName(fullpathToSelectedImage); selectedSearchSource.Icon = iconFileName; - selectedSearchSource.IconPath = Path.Combine(iconPathDirectory, Path.GetFileName(fullpathToSelectedImage)); } public void CopyNewImageToUserDataDirectoryIfRequired( SearchSource selectedSearchSource, string fullpathToSelectedImage, string fullPathToOriginalImage) { - var destinationFileNameFullPath = Path.Combine(DestinationDirectory, Path.GetFileName(fullpathToSelectedImage)); + var destinationFileNameFullPath = Path.Combine(Main.CustomImagesDirectory, Path.GetFileName(fullpathToSelectedImage)); var parentDirectorySelectedImg = Directory.GetParent(fullpathToSelectedImage).ToString(); - if (parentDirectorySelectedImg != DestinationDirectory && parentDirectorySelectedImg != Main.ImagesDirectory) + if (parentDirectorySelectedImg != Main.CustomImagesDirectory && parentDirectorySelectedImg != Main.DefaultImagesDirectory) { try { @@ -54,13 +46,13 @@ namespace Flow.Launcher.Plugin.WebSearch internal void SetupCustomImagesDirectory() { - if (!Directory.Exists(DestinationDirectory)) - Directory.CreateDirectory(DestinationDirectory); + if (!Directory.Exists(Main.CustomImagesDirectory)) + Directory.CreateDirectory(Main.CustomImagesDirectory); } internal bool ShouldProvideHint(string fullPathToSelectedImage) { - return Directory.GetParent(fullPathToSelectedImage).ToString() == Main.ImagesDirectory; + return Directory.GetParent(fullPathToSelectedImage).ToString() == Main.DefaultImagesDirectory; } } } \ No newline at end of file From 203df8f1aeb396bd53c0103ae34f1c581e051188 Mon Sep 17 00:00:00 2001 From: Jeremy Wu Date: Wed, 12 Aug 2020 07:12:43 +1000 Subject: [PATCH 11/15] update method to reflect intent --- .../Flow.Launcher.Plugin.WebSearch/SearchSourceSetting.xaml.cs | 2 +- Plugins/Flow.Launcher.Plugin.WebSearch/SearchSourceViewModel.cs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Plugins/Flow.Launcher.Plugin.WebSearch/SearchSourceSetting.xaml.cs b/Plugins/Flow.Launcher.Plugin.WebSearch/SearchSourceSetting.xaml.cs index 69b6de617..7a8996943 100644 --- a/Plugins/Flow.Launcher.Plugin.WebSearch/SearchSourceSetting.xaml.cs +++ b/Plugins/Flow.Launcher.Plugin.WebSearch/SearchSourceSetting.xaml.cs @@ -130,7 +130,7 @@ namespace Flow.Launcher.Plugin.WebSearch if (!string.IsNullOrEmpty(fullpathToSelectedImage)) { var fullPathToOriginalImage = _searchSource.IconPath; - _viewModel.UpdateIconPath(_searchSource, fullpathToSelectedImage); + _viewModel.UpdateIconAttributes(_searchSource, fullpathToSelectedImage); _viewModel.CopyNewImageToUserDataDirectoryIfRequired( _searchSource, fullpathToSelectedImage, fullPathToOriginalImage); } diff --git a/Plugins/Flow.Launcher.Plugin.WebSearch/SearchSourceViewModel.cs b/Plugins/Flow.Launcher.Plugin.WebSearch/SearchSourceViewModel.cs index 3cddc59c5..17bbc2ab1 100644 --- a/Plugins/Flow.Launcher.Plugin.WebSearch/SearchSourceViewModel.cs +++ b/Plugins/Flow.Launcher.Plugin.WebSearch/SearchSourceViewModel.cs @@ -7,7 +7,7 @@ namespace Flow.Launcher.Plugin.WebSearch { public SearchSource SearchSource { get; set; } - public void UpdateIconPath(SearchSource selectedSearchSource, string fullpathToSelectedImage) + public void UpdateIconAttributes(SearchSource selectedSearchSource, string fullpathToSelectedImage) { var parentDirectorySelectedImg = Directory.GetParent(fullpathToSelectedImage).ToString(); From 6cb2cc30a3496ba6dd2ce5d94eec00cb6e6befd4 Mon Sep 17 00:00:00 2001 From: Jeremy Wu Date: Wed, 12 Aug 2020 08:23:10 +1000 Subject: [PATCH 12/15] update to WebSearch's custom icon image save behaviour Load icon image preview on select and only save changes on confirm --- .../SearchSource.cs | 5 ---- .../SearchSourceSetting.xaml | 2 +- .../SearchSourceSetting.xaml.cs | 26 ++++++++++++------- .../SearchSourceViewModel.cs | 10 +++++-- 4 files changed, 26 insertions(+), 17 deletions(-) diff --git a/Plugins/Flow.Launcher.Plugin.WebSearch/SearchSource.cs b/Plugins/Flow.Launcher.Plugin.WebSearch/SearchSource.cs index de83cfad5..c7ccb4d51 100644 --- a/Plugins/Flow.Launcher.Plugin.WebSearch/SearchSource.cs +++ b/Plugins/Flow.Launcher.Plugin.WebSearch/SearchSource.cs @@ -34,11 +34,6 @@ namespace Flow.Launcher.Plugin.WebSearch } } - [JsonIgnore] - public ImageSource Image => ImageLoader.Load(IconPath); - - internal void NotifyImageChange() => OnPropertyChanged(nameof(Image)); - public string Url { get; set; } public bool Enabled { get; set; } diff --git a/Plugins/Flow.Launcher.Plugin.WebSearch/SearchSourceSetting.xaml b/Plugins/Flow.Launcher.Plugin.WebSearch/SearchSourceSetting.xaml index 0f17d21f5..02809be3a 100644 --- a/Plugins/Flow.Launcher.Plugin.WebSearch/SearchSourceSetting.xaml +++ b/Plugins/Flow.Launcher.Plugin.WebSearch/SearchSourceSetting.xaml @@ -47,7 +47,7 @@ - +