Flow.Launcher/Plugins/Flow.Launcher.Plugin.Explorer/Views/ActionKeywordSetting.xaml.cs

117 lines
3.8 KiB
C#
Raw Normal View History

2025-06-17 08:55:23 +00:00
using System.Linq;
using System.Windows;
using System.Windows.Input;
2025-06-17 08:55:23 +00:00
using CommunityToolkit.Mvvm.ComponentModel;
using Flow.Launcher.Plugin.Explorer.ViewModels;
namespace Flow.Launcher.Plugin.Explorer.Views
{
2025-06-17 08:55:23 +00:00
[INotifyPropertyChanged]
public partial class ActionKeywordSetting
{
private ActionKeywordModel CurrentActionKeyword { get; }
public string ActionKeyword
{
2021-09-01 11:52:32 +00:00
get => actionKeyword;
set
{
// Set Enable to be true only when the ActionKeyword value actually changes
if (SetProperty(ref actionKeyword, value))
KeywordEnabled = true;
}
}
public bool KeywordEnabled
{
get => _keywordEnabled;
2025-06-17 08:55:23 +00:00
set => _ = SetProperty(ref _keywordEnabled, value);
}
2021-06-05 08:44:16 +00:00
2021-09-01 11:52:32 +00:00
private string actionKeyword;
private bool _keywordEnabled;
public ActionKeywordSetting(ActionKeywordModel selectedActionKeyword)
{
CurrentActionKeyword = selectedActionKeyword;
// Initialize backing fields directly to avoid triggering the auto-enable side-effect
actionKeyword = selectedActionKeyword.Keyword;
_keywordEnabled = selectedActionKeyword.Enabled;
2021-06-05 08:44:16 +00:00
InitializeComponent();
TxtCurrentActionKeyword.Focus();
}
2021-06-05 08:44:16 +00:00
private void OnDoneButtonClick(object sender, RoutedEventArgs e)
{
if (string.IsNullOrEmpty(ActionKeyword))
ActionKeyword = Query.GlobalPluginWildcardSign;
if (CurrentActionKeyword.Keyword == ActionKeyword && CurrentActionKeyword.Enabled == KeywordEnabled)
{
DialogResult = false;
Close();
return;
}
2021-08-06 08:52:09 +00:00
if (ActionKeyword == Query.GlobalPluginWildcardSign)
switch (CurrentActionKeyword.KeywordProperty, KeywordEnabled)
2021-07-30 10:44:29 +00:00
{
case (Settings.ActionKeyword.FileContentSearchActionKeyword, true):
Main.Context.API.ShowMsgBox(Localize.plugin_explorer_globalActionKeywordInvalid());
2021-08-06 08:52:09 +00:00
return;
case (Settings.ActionKeyword.QuickAccessActionKeyword, true):
Main.Context.API.ShowMsgBox(Localize.plugin_explorer_quickaccess_globalActionKeywordInvalid());
2021-08-06 08:52:09 +00:00
return;
2021-07-30 10:44:29 +00:00
}
if (!KeywordEnabled || !Main.Context.API.ActionKeywordAssigned(ActionKeyword))
{
DialogResult = true;
Close();
return;
}
// The keyword is not valid, so show message
Main.Context.API.ShowMsgBox(Localize.plugin_explorer_new_action_keyword_assigned());
}
private void BtnCancel_OnClick(object sender, RoutedEventArgs e)
{
DialogResult = false;
Close();
}
private void TxtCurrentActionKeyword_OnKeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Enter)
{
DownButton.Focus();
OnDoneButtonClick(sender, e);
e.Handled = true;
}
if (e.Key == Key.Space)
{
e.Handled = true;
}
}
private void TextBox_Pasting(object sender, DataObjectPastingEventArgs e)
{
if (e.DataObject.GetDataPresent(DataFormats.Text))
{
string text = e.DataObject.GetData(DataFormats.Text) as string;
if (!string.IsNullOrEmpty(text) && text.Any(char.IsWhiteSpace))
{
e.CancelCommand();
}
}
else
{
e.CancelCommand();
}
}
}
}