From 48557aec068cd5abffa765e62e85413d266eda6b Mon Sep 17 00:00:00 2001 From: Jeremy Wu Date: Wed, 12 Aug 2020 07:08:24 +1000 Subject: [PATCH] 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