fix enable not working at the first time (also refactor a little bit code)

This commit is contained in:
Hongtao Zhang 2022-12-04 01:47:48 -06:00
parent 435144b440
commit 281f4db5f8
No known key found for this signature in database
GPG key ID: 75F655B91C7AC9BB
3 changed files with 41 additions and 30 deletions

View file

@ -1,5 +1,4 @@
using Flow.Launcher.Core.Plugin;
using Flow.Launcher.Infrastructure.Storage;
#nullable enable
using Flow.Launcher.Plugin.Explorer.Search;
using Flow.Launcher.Plugin.Explorer.Search.Everything;
using Flow.Launcher.Plugin.Explorer.Search.Everything.Exceptions;
@ -11,7 +10,6 @@ using System.Collections.ObjectModel;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Forms;
using System.Windows.Input;
@ -90,9 +88,9 @@ namespace Flow.Launcher.Plugin.Explorer.ViewModels
ContentIndexSearchEngines = EnumBindingModel<Settings.ContentIndexSearchEngineOption>.CreateList();
PathEnumerationEngines = EnumBindingModel<Settings.PathEnumerationEngineOption>.CreateList();
SelectedIndexSearchEngine = IndexSearchEngines.FirstOrDefault(x => x.Value == Settings.IndexSearchEngine);
_selectedContentSearchEngine = ContentIndexSearchEngines.FirstOrDefault(x => x.Value == Settings.ContentSearchEngine);
_selectedPathEnumerationEngine = PathEnumerationEngines.FirstOrDefault(x => x.Value == Settings.PathEnumerationEngine);
SelectedIndexSearchEngine = IndexSearchEngines.First(x => x.Value == Settings.IndexSearchEngine);
SelectedContentSearchEngine = ContentIndexSearchEngines.First(x => x.Value == Settings.ContentSearchEngine);
SelectedPathEnumerationEngine = PathEnumerationEngines.First(x => x.Value == Settings.PathEnumerationEngine);
}
#endregion
@ -120,13 +118,13 @@ namespace Flow.Launcher.Plugin.Explorer.ViewModels
public IReadOnlyList<ActionKeywordModel> ActionKeywordsModels { get; set; }
public ActionKeywordModel SelectedActionKeyword { get; set; }
public ActionKeywordModel? SelectedActionKeyword { get; set; }
public ICommand EditActionKeywordCommand => new RelayCommand(EditActionKeyword);
private void EditActionKeyword(object obj)
{
if (SelectedActionKeyword is not ActionKeywordModel actionKeyword)
if (SelectedActionKeyword is not { } actionKeyword)
{
ShowUnselectedMessage();
return;
@ -150,7 +148,7 @@ namespace Flow.Launcher.Plugin.Explorer.ViewModels
Context.API.AddActionKeyword(Context.CurrentPluginMetadata.ID, actionKeywordWindow.ActionKeyword);
break;
case (false, true):
Context.API.AddActionKeyword(Context.CurrentPluginMetadata.ID, actionKeyword.Keyword);
Context.API.AddActionKeyword(Context.CurrentPluginMetadata.ID, actionKeywordWindow.ActionKeyword);
break;
case (false, false):
throw new ArgumentException(
@ -165,8 +163,8 @@ namespace Flow.Launcher.Plugin.Explorer.ViewModels
#region AccessLinks
public AccessLink SelectedQuickAccessLink { get; set; }
public AccessLink SelectedIndexSearchExcludedPath { get; set; }
public AccessLink? SelectedQuickAccessLink { get; set; }
public AccessLink? SelectedIndexSearchExcludedPath { get; set; }
@ -187,7 +185,7 @@ namespace Flow.Launcher.Plugin.Explorer.ViewModels
private void EditLink(object commandParameter)
{
(AccessLink selectedLink, ObservableCollection<AccessLink> collection) = commandParameter switch
var (selectedLink, collection) = commandParameter switch
{
"QuickAccessLink" => (SelectedQuickAccessLink, Settings.QuickAccessLinks),
"IndexSearchExcludedPaths" => (SelectedIndexSearchExcludedPath, Settings.IndexSearchExcludedSubdirectoryPaths),
@ -266,9 +264,9 @@ namespace Flow.Launcher.Plugin.Explorer.ViewModels
#endregion
private string? PromptUserSelectPath(ResultType type, string initialDirectory = null)
private string? PromptUserSelectPath(ResultType type, string? initialDirectory = null)
{
string path = null;
string? path = null;
if (type is ResultType.Folder)
{

View file

@ -77,8 +77,6 @@
Text="{DynamicResource plugin_explorer_actionkeyword_current}" />
<TextBox
Name="TxtCurrentActionKeyword"
Grid.Row="0"
Grid.Column="1"
Width="135"
HorizontalAlignment="Left"
VerticalAlignment="Center"
@ -97,7 +95,7 @@
Name="ChkActionKeywordEnabled"
Width="auto"
VerticalAlignment="Center"
IsChecked="{Binding KeywordEnabled}"
IsChecked="{Binding KeywordEnabled, Mode=TwoWay}"
ToolTip="{DynamicResource plugin_explorer_actionkeyword_enabled_tooltip}" />
</StackPanel>
</StackPanel>

View file

@ -2,7 +2,9 @@ using Flow.Launcher.Plugin.Explorer.ViewModels;
using ICSharpCode.SharpZipLib.Zip;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Windows;
using System.Windows.Input;
@ -11,9 +13,9 @@ namespace Flow.Launcher.Plugin.Explorer.Views
/// <summary>
/// Interaction logic for ActionKeywordSetting.xaml
/// </summary>
public partial class ActionKeywordSetting : Window
public partial class ActionKeywordSetting : INotifyPropertyChanged
{
public ActionKeywordModel CurrentActionKeyword { get; set; }
private ActionKeywordModel CurrentActionKeyword { get; }
public string ActionKeyword
{
@ -22,14 +24,19 @@ namespace Flow.Launcher.Plugin.Explorer.Views
{
// Set Enable to be true if user change ActionKeyword
KeywordEnabled = true;
actionKeyword = value;
_ = SetField(ref actionKeyword, value);
}
}
public bool KeywordEnabled { get; set; }
public bool KeywordEnabled
{
get => _keywordEnabled;
set => SetField(ref _keywordEnabled, value);
}
private string actionKeyword;
private readonly IPublicAPI api;
private bool _keywordEnabled;
public ActionKeywordSetting(ActionKeywordModel selectedActionKeyword, IPublicAPI api)
{
@ -55,18 +62,13 @@ namespace Flow.Launcher.Plugin.Explorer.Views
return;
}
if (ActionKeyword == "")
{
ActionKeyword = "*";
}
if (ActionKeyword == Query.GlobalPluginWildcardSign)
switch (CurrentActionKeyword.KeywordProperty)
switch (CurrentActionKeyword.KeywordProperty, KeywordEnabled)
{
case Settings.ActionKeyword.FileContentSearchActionKeyword:
case (Settings.ActionKeyword.FileContentSearchActionKeyword, true):
MessageBox.Show(api.GetTranslation("plugin_explorer_globalActionKeywordInvalid"));
return;
case Settings.ActionKeyword.QuickAccessActionKeyword:
case (Settings.ActionKeyword.QuickAccessActionKeyword, true):
MessageBox.Show(api.GetTranslation("plugin_explorer_quickaccess_globalActionKeywordInvalid"));
return;
}
@ -96,5 +98,18 @@ namespace Flow.Launcher.Plugin.Explorer.Views
e.Handled = true;
}
}
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
private bool SetField<T>(ref T field, T value, [CallerMemberName] string propertyName = null)
{
if (EqualityComparer<T>.Default.Equals(field, value))
return false;
field = value;
OnPropertyChanged(propertyName);
return true;
}
}
}
}