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 20d0dd5a454..3d548b6b1f0 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 @@ public class SearchSource : BaseModel 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 3d548b6b1f0..1277a92fe0c 100644 --- a/Plugins/Flow.Launcher.Plugin.WebSearch/SearchSource.cs +++ b/Plugins/Flow.Launcher.Plugin.WebSearch/SearchSource.cs @@ -3,17 +3,20 @@ 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 @@ public string IconPath 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 @@ public string IconPath } } - private string iconPath; - [JsonIgnore] public ImageSource Image => ImageLoader.Load(IconPath); @@ -51,6 +58,7 @@ public SearchSource DeepCopy() 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 9ecdda23f00..ac1882cde05 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 @@ private void OnSelectIconClick(object sender, RoutedEventArgs e) 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 049c50560bb..5bcb3938da5 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 98cd777aac4..c2ead6c9c5c 100644 --- a/Flow.Launcher.Plugin/SharedCommands/FilesFolders.cs +++ b/Flow.Launcher.Plugin/SharedCommands/FilesFolders.cs @@ -11,6 +11,12 @@ public static class FilesFolders 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 1277a92fe0c..e8c3b998c53 100644 --- a/Plugins/Flow.Launcher.Plugin.WebSearch/SearchSource.cs +++ b/Plugins/Flow.Launcher.Plugin.WebSearch/SearchSource.cs @@ -47,6 +47,8 @@ public string IconPath [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 5bcb3938da5..2ca09d408d6 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 @@ 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 @@ public void CopyNewImageToUserDataDirectory(SearchSource selectedSearchSource, s 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 c2ead6c9c5c..13905788a90 100644 --- a/Flow.Launcher.Plugin/SharedCommands/FilesFolders.cs +++ b/Flow.Launcher.Plugin/SharedCommands/FilesFolders.cs @@ -178,7 +178,7 @@ public static bool IsLocationPathString(string querySearchString) /// /// 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 | 52 +++++++++++-------- 3 files changed, 40 insertions(+), 33 deletions(-) diff --git a/Plugins/Flow.Launcher.Plugin.WebSearch/Languages/en.xaml b/Plugins/Flow.Launcher.Plugin.WebSearch/Languages/en.xaml index f1ae5bc3541..c2ed7df3140 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 ac1882cde05..a7d00119767 100644 --- a/Plugins/Flow.Launcher.Plugin.WebSearch/SearchSourceSetting.xaml.cs +++ b/Plugins/Flow.Launcher.Plugin.WebSearch/SearchSourceSetting.xaml.cs @@ -38,6 +38,8 @@ private void Initilize(IList sources, PluginInitContext context, A _action = action; _context = context; _api = _context.API; + + _viewModel.SetupCustomImagesDirectory(); } private void OnCancelButtonClick(object sender, RoutedEventArgs e) @@ -114,27 +116,23 @@ private void EditSearchSource() 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 2ca09d408d6..4c510bdc16e 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)); - - try - { - if (!Directory.Exists(destinationDirectory)) - Directory.CreateDirectory(destinationDirectory); + var destinationFileNameFullPath = Path.Combine(DestinationDirectory, Path.GetFileName(fullpathToSelectedImage)); - File.Copy(fullpathToSelectedImage, destinationFileNameFullPath); + var parentDirectorySelectedImg = Directory.GetParent(fullpathToSelectedImage).ToString(); - selectedSearchSource.NotifyImageChange(); - } - catch(Exception e) + 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); - - var newImageFilePathToBe = Path.Combine(destinationDirectory, fileName); + if (!Directory.Exists(DestinationDirectory)) + Directory.CreateDirectory(DestinationDirectory); + } - 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 4c510bdc16e..7eee86560b6 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 9784cfefecb..a91e42f45e1 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 310c1e33af2..98fa9aa6487 100644 --- a/Flow.Launcher.Infrastructure/UserSettings/DataLocation.cs +++ b/Flow.Launcher.Infrastructure/UserSettings/DataLocation.cs @@ -30,5 +30,7 @@ public static bool PortableDataLocationInUse() } 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 3fdb26c93c6..b9d7b4f7129 100644 --- a/Plugins/Flow.Launcher.Plugin.WebSearch/Main.cs +++ b/Plugins/Flow.Launcher.Plugin.WebSearch/Main.cs @@ -8,6 +8,7 @@ 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 @@ public class Main : IPlugin, ISettingProvider, IPluginI18n, ISavable, IResultUpd 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 @@ public void Init(PluginInitContext context) _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 e8c3b998c53..de83cfad5bf 100644 --- a/Plugins/Flow.Launcher.Plugin.WebSearch/SearchSource.cs +++ b/Plugins/Flow.Launcher.Plugin.WebSearch/SearchSource.cs @@ -16,31 +16,21 @@ public class SearchSource : BaseModel [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(); - - var imagesDirectory = Path.Combine(pluginDirectorys, "Images"); + if (CustomIcon) + return Path.Combine(Main.CustomImagesDirectory, Icon); - return Path.Combine(imagesDirectory, Icon); - } - - return iconPath; - } - set - { - iconPath = value; + return Path.Combine(Main.DefaultImagesDirectory, Icon); } } @@ -60,7 +50,7 @@ public SearchSource DeepCopy() 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 a7d00119767..69b6de617bf 100644 --- a/Plugins/Flow.Launcher.Plugin.WebSearch/SearchSourceSetting.xaml.cs +++ b/Plugins/Flow.Launcher.Plugin.WebSearch/SearchSourceSetting.xaml.cs @@ -117,7 +117,7 @@ private void EditSearchSource() 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 7eee86560b6..3cddc59c5f5 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 @@ public void CopyNewImageToUserDataDirectoryIfRequired( 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 69b6de617bf..7a899694345 100644 --- a/Plugins/Flow.Launcher.Plugin.WebSearch/SearchSourceSetting.xaml.cs +++ b/Plugins/Flow.Launcher.Plugin.WebSearch/SearchSourceSetting.xaml.cs @@ -130,7 +130,7 @@ private void OnSelectIconClick(object sender, RoutedEventArgs e) 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 3cddc59c5f5..17bbc2ab117 100644 --- a/Plugins/Flow.Launcher.Plugin.WebSearch/SearchSourceViewModel.cs +++ b/Plugins/Flow.Launcher.Plugin.WebSearch/SearchSourceViewModel.cs @@ -7,7 +7,7 @@ public class SearchSourceViewModel : BaseModel { 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 de83cfad5bf..c7ccb4d51fe 100644 --- a/Plugins/Flow.Launcher.Plugin.WebSearch/SearchSource.cs +++ b/Plugins/Flow.Launcher.Plugin.WebSearch/SearchSource.cs @@ -34,11 +34,6 @@ public string IconPath } } - [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 0f17d21f5ca..02809be3ad9 100644 --- a/Plugins/Flow.Launcher.Plugin.WebSearch/SearchSourceSetting.xaml +++ b/Plugins/Flow.Launcher.Plugin.WebSearch/SearchSourceSetting.xaml @@ -47,7 +47,7 @@ - +