Skip to content

Commit 48557ae

Browse files
committed
change to dynamically loading of icon images for WebSearch
1 parent c4d1fe4 commit 48557ae

File tree

5 files changed

+25
-36
lines changed

5 files changed

+25
-36
lines changed

Flow.Launcher.Infrastructure/UserSettings/DataLocation.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,5 +30,7 @@ public static bool PortableDataLocationInUse()
3030
}
3131

3232
public static readonly string PluginsDirectory = Path.Combine(DataDirectory(), Constant.Plugins);
33+
public static readonly string SettingsDirectory = Path.Combine(DataDirectory(), "Settings");
34+
public static readonly string PluginSettingsDirectory = Path.Combine(SettingsDirectory, Constant.Plugins);
3335
}
3436
}

Plugins/Flow.Launcher.Plugin.WebSearch/Main.cs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
using System.Windows.Controls;
99
using Flow.Launcher.Infrastructure;
1010
using Flow.Launcher.Infrastructure.Storage;
11+
using Flow.Launcher.Infrastructure.UserSettings;
1112
using Flow.Launcher.Plugin.SharedCommands;
1213

1314
namespace Flow.Launcher.Plugin.WebSearch
@@ -21,8 +22,9 @@ public class Main : IPlugin, ISettingProvider, IPluginI18n, ISavable, IResultUpd
2122
private CancellationTokenSource _updateSource;
2223
private CancellationToken _updateToken;
2324

24-
public const string Images = "Images";
25-
public static string ImagesDirectory;
25+
internal const string Images = "Images";
26+
internal static string DefaultImagesDirectory;
27+
internal static string CustomImagesDirectory;
2628

2729
private readonly string SearchSourceGlobalPluginWildCardSign = "*";
2830

@@ -172,8 +174,11 @@ public void Init(PluginInitContext context)
172174
_context = context;
173175
var pluginDirectory = _context.CurrentPluginMetadata.PluginDirectory;
174176
var bundledImagesDirectory = Path.Combine(pluginDirectory, Images);
175-
ImagesDirectory = Path.Combine(_context.CurrentPluginMetadata.PluginDirectory, Images);
176-
Helper.ValidateDataDirectory(bundledImagesDirectory, ImagesDirectory);
177+
DefaultImagesDirectory = Path.Combine(_context.CurrentPluginMetadata.PluginDirectory, Images);
178+
Helper.ValidateDataDirectory(bundledImagesDirectory, DefaultImagesDirectory);
179+
180+
var name = Path.GetFileNameWithoutExtension(_context.CurrentPluginMetadata.ExecuteFileName);
181+
CustomImagesDirectory = Path.Combine(DataLocation.PluginSettingsDirectory, $"{name}\\CustomIcons");
177182
}
178183

179184
#region ISettingProvider Members

Plugins/Flow.Launcher.Plugin.WebSearch/SearchSource.cs

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -16,31 +16,21 @@ public class SearchSource : BaseModel
1616
[NotNull]
1717
public string Icon { get; set; } = "web_search.png";
1818

19-
private string iconPath;
19+
public bool CustomIcon { get; set; } = false;
2020

2121
/// <summary>
2222
/// Default icons are placed in Images directory in the app location.
2323
/// Custom icons are placed in the user data directory
2424
/// </summary>
25-
[NotNull]
25+
[JsonIgnore]
2626
public string IconPath
27-
{
27+
{
2828
get
2929
{
30-
if (string.IsNullOrEmpty(iconPath))
31-
{
32-
var pluginDirectorys = Directory.GetParent(Assembly.GetExecutingAssembly().Location.NonNull()).ToString();
33-
34-
var imagesDirectory = Path.Combine(pluginDirectorys, "Images");
30+
if (CustomIcon)
31+
return Path.Combine(Main.CustomImagesDirectory, Icon);
3532

36-
return Path.Combine(imagesDirectory, Icon);
37-
}
38-
39-
return iconPath;
40-
}
41-
set
42-
{
43-
iconPath = value;
33+
return Path.Combine(Main.DefaultImagesDirectory, Icon);
4434
}
4535
}
4636

@@ -60,7 +50,7 @@ public SearchSource DeepCopy()
6050
ActionKeyword = string.Copy(ActionKeyword),
6151
Url = string.Copy(Url),
6252
Icon = string.Copy(Icon),
63-
IconPath = string.Copy(IconPath),
53+
CustomIcon = CustomIcon,
6454
Enabled = Enabled
6555
};
6656
return webSearch;

Plugins/Flow.Launcher.Plugin.WebSearch/SearchSourceSetting.xaml.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ private void EditSearchSource()
117117
private void OnSelectIconClick(object sender, RoutedEventArgs e)
118118
{
119119
const string filter = "Image files (*.jpg, *.jpeg, *.gif, *.png, *.bmp) |*.jpg; *.jpeg; *.gif; *.png; *.bmp";
120-
var dialog = new OpenFileDialog {InitialDirectory = _viewModel.DestinationDirectory, Filter = filter};
120+
var dialog = new OpenFileDialog {InitialDirectory = Main.CustomImagesDirectory, Filter = filter};
121121

122122
var result = dialog.ShowDialog();
123123
if (result == true)
Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,30 @@
1-
using Flow.Launcher.Infrastructure.UserSettings;
21
using System;
32
using System.IO;
4-
using System.Windows;
53

64
namespace Flow.Launcher.Plugin.WebSearch
75
{
86
public class SearchSourceViewModel : BaseModel
97
{
10-
internal readonly string DestinationDirectory =
11-
Path.Combine(DataLocation.DataDirectory(), @"Settings\Plugins\Flow.Launcher.Plugin.WebSearch\CustomIcons");
12-
138
public SearchSource SearchSource { get; set; }
149

1510
public void UpdateIconPath(SearchSource selectedSearchSource, string fullpathToSelectedImage)
1611
{
1712
var parentDirectorySelectedImg = Directory.GetParent(fullpathToSelectedImage).ToString();
1813

19-
var iconPathDirectory = parentDirectorySelectedImg == DestinationDirectory
20-
|| parentDirectorySelectedImg == Main.ImagesDirectory
21-
? parentDirectorySelectedImg : DestinationDirectory;
14+
selectedSearchSource.CustomIcon = parentDirectorySelectedImg != Main.DefaultImagesDirectory;
2215

2316
var iconFileName = Path.GetFileName(fullpathToSelectedImage);
2417
selectedSearchSource.Icon = iconFileName;
25-
selectedSearchSource.IconPath = Path.Combine(iconPathDirectory, Path.GetFileName(fullpathToSelectedImage));
2618
}
2719

2820
public void CopyNewImageToUserDataDirectoryIfRequired(
2921
SearchSource selectedSearchSource, string fullpathToSelectedImage, string fullPathToOriginalImage)
3022
{
31-
var destinationFileNameFullPath = Path.Combine(DestinationDirectory, Path.GetFileName(fullpathToSelectedImage));
23+
var destinationFileNameFullPath = Path.Combine(Main.CustomImagesDirectory, Path.GetFileName(fullpathToSelectedImage));
3224

3325
var parentDirectorySelectedImg = Directory.GetParent(fullpathToSelectedImage).ToString();
3426

35-
if (parentDirectorySelectedImg != DestinationDirectory && parentDirectorySelectedImg != Main.ImagesDirectory)
27+
if (parentDirectorySelectedImg != Main.CustomImagesDirectory && parentDirectorySelectedImg != Main.DefaultImagesDirectory)
3628
{
3729
try
3830
{
@@ -54,13 +46,13 @@ public void CopyNewImageToUserDataDirectoryIfRequired(
5446

5547
internal void SetupCustomImagesDirectory()
5648
{
57-
if (!Directory.Exists(DestinationDirectory))
58-
Directory.CreateDirectory(DestinationDirectory);
49+
if (!Directory.Exists(Main.CustomImagesDirectory))
50+
Directory.CreateDirectory(Main.CustomImagesDirectory);
5951
}
6052

6153
internal bool ShouldProvideHint(string fullPathToSelectedImage)
6254
{
63-
return Directory.GetParent(fullPathToSelectedImage).ToString() == Main.ImagesDirectory;
55+
return Directory.GetParent(fullPathToSelectedImage).ToString() == Main.DefaultImagesDirectory;
6456
}
6557
}
6658
}

0 commit comments

Comments
 (0)