change to dynamically loading of icon images for WebSearch

This commit is contained in:
Jeremy Wu 2020-08-12 07:08:24 +10:00
parent c4d1fe4436
commit 48557aec06
5 changed files with 25 additions and 36 deletions

View file

@ -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);
}
}

View file

@ -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

View file

@ -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;
/// <summary>
/// Default icons are placed in Images directory in the app location.
/// Custom icons are placed in the user data directory
/// </summary>
[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;

View file

@ -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)

View file

@ -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;
}
}
}