update to WebSearch's custom icon image save behaviour

Load icon image preview on select and only save changes on confirm
This commit is contained in:
Jeremy Wu 2020-08-12 08:23:10 +10:00
parent 203df8f1ae
commit 6cb2cc30a3
4 changed files with 26 additions and 17 deletions

View file

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

View file

@ -47,7 +47,7 @@
<TextBlock Margin="10" FontSize="14" Grid.Row="4" Grid.Column="0" VerticalAlignment="Center"
HorizontalAlignment="Right" Text="{DynamicResource flowlauncher_plugin_websearch_icon}" />
<StackPanel Orientation="Horizontal" Grid.Row="4" Grid.Column="1" Margin="10">
<Image Source="{Binding SearchSource.Image ,IsAsync=True}" Width="24" Height="24" Margin="0 0 20 0" />
<Image Name="imgPreviewIcon" Width="24" Height="24" Margin="0 0 20 0" />
<Button Click="OnSelectIconClick" Height="35"
Content="{DynamicResource flowlauncher_plugin_websearch_select_icon}" />
</StackPanel>

View file

@ -14,6 +14,7 @@ namespace Flow.Launcher.Plugin.WebSearch
private PluginInitContext _context;
private IPublicAPI _api;
private SearchSourceViewModel _viewModel;
private string selectedNewIconImageFullPath;
public SearchSourceSettingWindow(IList<SearchSource> sources, PluginInitContext context, SearchSource old)
@ -40,6 +41,8 @@ namespace Flow.Launcher.Plugin.WebSearch
_api = _context.API;
_viewModel.SetupCustomImagesDirectory();
imgPreviewIcon.Source = _viewModel.LoadPreviewIcon(_searchSource.IconPath);
}
private void OnCancelButtonClick(object sender, RoutedEventArgs e)
@ -112,6 +115,14 @@ namespace Flow.Launcher.Plugin.WebSearch
var warning = _api.GetTranslation("newActionKeywordsHasBeenAssigned");
MessageBox.Show(warning);
}
if (!string.IsNullOrEmpty(selectedNewIconImageFullPath))
{
_viewModel.UpdateIconAttributes(_searchSource, selectedNewIconImageFullPath);
_viewModel.CopyNewImageToUserDataDirectoryIfRequired(
_searchSource, selectedNewIconImageFullPath, _oldSearchSource.IconPath);
}
}
private void OnSelectIconClick(object sender, RoutedEventArgs e)
@ -122,17 +133,14 @@ namespace Flow.Launcher.Plugin.WebSearch
var result = dialog.ShowDialog();
if (result == true)
{
var fullpathToSelectedImage = dialog.FileName;
selectedNewIconImageFullPath = dialog.FileName;
if (_viewModel.ShouldProvideHint(fullpathToSelectedImage))
MessageBox.Show(_api.GetTranslation("flowlauncher_plugin_websearch_iconpath_hint"));
if (!string.IsNullOrEmpty(fullpathToSelectedImage))
if (!string.IsNullOrEmpty(selectedNewIconImageFullPath))
{
var fullPathToOriginalImage = _searchSource.IconPath;
_viewModel.UpdateIconAttributes(_searchSource, fullpathToSelectedImage);
_viewModel.CopyNewImageToUserDataDirectoryIfRequired(
_searchSource, fullpathToSelectedImage, fullPathToOriginalImage);
if (_viewModel.ShouldProvideHint(selectedNewIconImageFullPath))
MessageBox.Show(_api.GetTranslation("flowlauncher_plugin_websearch_iconpath_hint"));
imgPreviewIcon.Source = _viewModel.LoadPreviewIcon(selectedNewIconImageFullPath);
}
}
}

View file

@ -1,5 +1,8 @@
using Flow.Launcher.Infrastructure.Image;
using System;
using System.Drawing;
using System.IO;
using System.Windows.Media;
namespace Flow.Launcher.Plugin.WebSearch
{
@ -40,8 +43,6 @@ namespace Flow.Launcher.Plugin.WebSearch
#endif
}
}
selectedSearchSource.NotifyImageChange();
}
internal void SetupCustomImagesDirectory()
@ -54,5 +55,10 @@ namespace Flow.Launcher.Plugin.WebSearch
{
return Directory.GetParent(fullPathToSelectedImage).ToString() == Main.DefaultImagesDirectory;
}
internal ImageSource LoadPreviewIcon(string pathToPreviewIconImage)
{
return ImageLoader.Load(pathToPreviewIconImage);
}
}
}