mirror of
https://github.com/Flow-Launcher/Flow.Launcher.git
synced 2026-03-11 08:54:32 +00:00
refactor relaycommands with mvvmtoolkit sourcegenerator to prevent potential null reference
This commit is contained in:
parent
337e6ca427
commit
e5d95f36b3
1 changed files with 142 additions and 160 deletions
|
|
@ -77,7 +77,6 @@ namespace Flow.Launcher.ViewModel
|
||||||
_userSelectedRecord = _userSelectedRecordStorage.Load();
|
_userSelectedRecord = _userSelectedRecordStorage.Load();
|
||||||
_topMostRecord = _topMostRecordStorage.Load();
|
_topMostRecord = _topMostRecordStorage.Load();
|
||||||
|
|
||||||
InitializeKeyCommands();
|
|
||||||
|
|
||||||
ContextMenu = new ResultsViewModel(Settings)
|
ContextMenu = new ResultsViewModel(Settings)
|
||||||
{
|
{
|
||||||
|
|
@ -167,160 +166,161 @@ namespace Flow.Launcher.ViewModel
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[RelayCommand]
|
||||||
|
private async Task ReloadPluginDataAsync()
|
||||||
private void InitializeKeyCommands()
|
|
||||||
{
|
{
|
||||||
EscCommand = new RelayCommand(_ =>
|
Hide();
|
||||||
|
|
||||||
|
await PluginManager.ReloadDataAsync().ConfigureAwait(false);
|
||||||
|
Notification.Show(InternationalizationManager.Instance.GetTranslation("success"), InternationalizationManager.Instance.GetTranslation("completedSuccessfully"));
|
||||||
|
}
|
||||||
|
[RelayCommand]
|
||||||
|
private void LoadHistory()
|
||||||
|
{
|
||||||
|
if (SelectedIsFromQueryResults())
|
||||||
{
|
{
|
||||||
if (!SelectedIsFromQueryResults())
|
SelectedResults = History;
|
||||||
{
|
History.SelectedIndex = _history.Items.Count - 1;
|
||||||
SelectedResults = Results;
|
}
|
||||||
}
|
else
|
||||||
else
|
|
||||||
{
|
|
||||||
Hide();
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
ClearQueryCommand = new RelayCommand(_ =>
|
|
||||||
{
|
{
|
||||||
if (!string.IsNullOrEmpty(QueryText))
|
SelectedResults = Results;
|
||||||
{
|
}
|
||||||
ChangeQueryText(string.Empty);
|
}
|
||||||
|
[RelayCommand]
|
||||||
// Push Event to UI SystemQuery has changed
|
private void LoadContextMenu()
|
||||||
//OnPropertyChanged(nameof(SystemQueryText));
|
{
|
||||||
}
|
if (SelectedIsFromQueryResults())
|
||||||
});
|
|
||||||
|
|
||||||
SelectNextItemCommand = new RelayCommand(_ => { SelectedResults.SelectNextResult(); });
|
|
||||||
|
|
||||||
SelectPrevItemCommand = new RelayCommand(_ => { SelectedResults.SelectPrevResult(); });
|
|
||||||
|
|
||||||
SelectNextPageCommand = new RelayCommand(_ => { SelectedResults.SelectNextPage(); });
|
|
||||||
|
|
||||||
SelectPrevPageCommand = new RelayCommand(_ => { SelectedResults.SelectPrevPage(); });
|
|
||||||
|
|
||||||
SelectFirstResultCommand = new RelayCommand(_ => SelectedResults.SelectFirstResult());
|
|
||||||
|
|
||||||
StartHelpCommand = new RelayCommand(_ =>
|
|
||||||
{
|
{
|
||||||
PluginManager.API.OpenUrl("https://github.com/Flow-Launcher/Flow.Launcher/wiki/Flow-Launcher/");
|
// When switch to ContextMenu from QueryResults, but no item being chosen, should do nothing
|
||||||
});
|
// i.e. Shift+Enter/Ctrl+O right after Alt + Space should do nothing
|
||||||
OpenSettingCommand = new RelayCommand(_ => { App.API.OpenSettingDialog(); });
|
if (SelectedResults.SelectedItem != null)
|
||||||
OpenResultCommand = new AsyncRelayCommand(async _ =>
|
SelectedResults = ContextMenu;
|
||||||
|
}
|
||||||
|
else
|
||||||
{
|
{
|
||||||
var results = SelectedResults;
|
SelectedResults = Results;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
[RelayCommand]
|
||||||
|
private void Backspace(object index)
|
||||||
|
{
|
||||||
|
var query = QueryBuilder.Build(QueryText.Trim(), PluginManager.NonGlobalPlugins);
|
||||||
|
|
||||||
var result = results.SelectedItem?.Result;
|
// GetPreviousExistingDirectory does not require trailing '\', otherwise will return empty string
|
||||||
if (result == null)
|
var path = FilesFolders.GetPreviousExistingDirectory((_) => true, query.Search.TrimEnd('\\'));
|
||||||
|
|
||||||
|
var actionKeyword = string.IsNullOrEmpty(query.ActionKeyword) ? string.Empty : $"{query.ActionKeyword} ";
|
||||||
|
|
||||||
|
ChangeQueryText($"{actionKeyword}{path}");
|
||||||
|
}
|
||||||
|
[RelayCommand]
|
||||||
|
private void AutocompleteQuery()
|
||||||
|
{
|
||||||
|
var result = SelectedResults.SelectedItem?.Result;
|
||||||
|
if (result != null && SelectedIsFromQueryResults()) // SelectedItem returns null if selection is empty.
|
||||||
|
{
|
||||||
|
var autoCompleteText = result.Title;
|
||||||
|
|
||||||
|
if (!string.IsNullOrEmpty(result.AutoCompleteText))
|
||||||
{
|
{
|
||||||
return;
|
autoCompleteText = result.AutoCompleteText;
|
||||||
}
|
}
|
||||||
var hideWindow = await result.ExecuteAsync(new ActionContext
|
else if (!string.IsNullOrEmpty(SelectedResults.SelectedItem?.QuerySuggestionText))
|
||||||
|
{
|
||||||
|
autoCompleteText = SelectedResults.SelectedItem.QuerySuggestionText;
|
||||||
|
}
|
||||||
|
|
||||||
|
var specialKeyState = GlobalHotkey.CheckModifiers();
|
||||||
|
if (specialKeyState.ShiftPressed)
|
||||||
|
{
|
||||||
|
autoCompleteText = result.SubTitle;
|
||||||
|
}
|
||||||
|
|
||||||
|
ChangeQueryText(autoCompleteText);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
[RelayCommand]
|
||||||
|
private async Task OpenResult()
|
||||||
|
{
|
||||||
|
var results = SelectedResults;
|
||||||
|
|
||||||
|
var result = results.SelectedItem?.Result;
|
||||||
|
if (result == null)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
var hideWindow = await result.ExecuteAsync(new ActionContext
|
||||||
{
|
{
|
||||||
SpecialKeyState = GlobalHotkey.CheckModifiers()
|
SpecialKeyState = GlobalHotkey.CheckModifiers()
|
||||||
}).ConfigureAwait(false);
|
})
|
||||||
|
.ConfigureAwait(false);
|
||||||
|
|
||||||
if (hideWindow)
|
if (hideWindow)
|
||||||
{
|
|
||||||
Hide();
|
|
||||||
}
|
|
||||||
|
|
||||||
if (SelectedIsFromQueryResults())
|
|
||||||
{
|
|
||||||
_userSelectedRecord.Add(result);
|
|
||||||
_history.Add(result.OriginQuery.RawQuery);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
SelectedResults = Results;
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
AutocompleteQueryCommand = new RelayCommand(_ =>
|
|
||||||
{
|
|
||||||
var result = SelectedResults.SelectedItem?.Result;
|
|
||||||
if (result != null && SelectedIsFromQueryResults()) // SelectedItem returns null if selection is empty.
|
|
||||||
{
|
|
||||||
var autoCompleteText = result.Title;
|
|
||||||
|
|
||||||
if (!string.IsNullOrEmpty(result.AutoCompleteText))
|
|
||||||
{
|
|
||||||
autoCompleteText = result.AutoCompleteText;
|
|
||||||
}
|
|
||||||
else if (!string.IsNullOrEmpty(SelectedResults.SelectedItem?.QuerySuggestionText))
|
|
||||||
{
|
|
||||||
autoCompleteText = SelectedResults.SelectedItem.QuerySuggestionText;
|
|
||||||
}
|
|
||||||
|
|
||||||
var specialKeyState = GlobalHotkey.CheckModifiers();
|
|
||||||
if (specialKeyState.ShiftPressed)
|
|
||||||
{
|
|
||||||
autoCompleteText = result.SubTitle;
|
|
||||||
}
|
|
||||||
|
|
||||||
ChangeQueryText(autoCompleteText);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
BackspaceCommand = new RelayCommand(index =>
|
|
||||||
{
|
|
||||||
var query = QueryBuilder.Build(QueryText.Trim(), PluginManager.NonGlobalPlugins);
|
|
||||||
|
|
||||||
// GetPreviousExistingDirectory does not require trailing '\', otherwise will return empty string
|
|
||||||
var path = FilesFolders.GetPreviousExistingDirectory((_) => true, query.Search.TrimEnd('\\'));
|
|
||||||
|
|
||||||
var actionKeyword = string.IsNullOrEmpty(query.ActionKeyword) ? string.Empty : $"{query.ActionKeyword} ";
|
|
||||||
|
|
||||||
ChangeQueryText($"{actionKeyword}{path}");
|
|
||||||
});
|
|
||||||
|
|
||||||
LoadContextMenuCommand = new RelayCommand(_ =>
|
|
||||||
{
|
|
||||||
if (SelectedIsFromQueryResults())
|
|
||||||
{
|
|
||||||
// When switch to ContextMenu from QueryResults, but no item being chosen, should do nothing
|
|
||||||
// i.e. Shift+Enter/Ctrl+O right after Alt + Space should do nothing
|
|
||||||
if (SelectedResults.SelectedItem != null)
|
|
||||||
SelectedResults = ContextMenu;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
SelectedResults = Results;
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
LoadHistoryCommand = new RelayCommand(_ =>
|
|
||||||
{
|
|
||||||
if (SelectedIsFromQueryResults())
|
|
||||||
{
|
|
||||||
SelectedResults = History;
|
|
||||||
History.SelectedIndex = _history.Items.Count - 1;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
SelectedResults = Results;
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
ReloadPluginDataCommand = new RelayCommand(_ =>
|
|
||||||
{
|
{
|
||||||
Hide();
|
Hide();
|
||||||
|
}
|
||||||
|
|
||||||
_ = PluginManager
|
if (SelectedIsFromQueryResults())
|
||||||
.ReloadDataAsync()
|
{
|
||||||
.ContinueWith(_ =>
|
_userSelectedRecord.Add(result);
|
||||||
Application.Current.Dispatcher.Invoke(() =>
|
_history.Add(result.OriginQuery.RawQuery);
|
||||||
{
|
}
|
||||||
Notification.Show(
|
else
|
||||||
InternationalizationManager.Instance.GetTranslation("success"),
|
{
|
||||||
InternationalizationManager.Instance.GetTranslation("completedSuccessfully")
|
SelectedResults = Results;
|
||||||
);
|
}
|
||||||
}), TaskScheduler.Default)
|
}
|
||||||
.ConfigureAwait(false);
|
[RelayCommand]
|
||||||
});
|
private void OpenSetting()
|
||||||
|
{
|
||||||
|
App.API.OpenSettingDialog();
|
||||||
|
}
|
||||||
|
|
||||||
|
[RelayCommand]
|
||||||
|
private void SelectHelp()
|
||||||
|
{
|
||||||
|
PluginManager.API.OpenUrl("https://github.com/Flow-Launcher/Flow.Launcher/wiki/Flow-Launcher/");
|
||||||
|
}
|
||||||
|
|
||||||
|
[RelayCommand]
|
||||||
|
private void SelectFirstResult()
|
||||||
|
{
|
||||||
|
SelectedResults.SelectFirstResult();
|
||||||
|
}
|
||||||
|
[RelayCommand]
|
||||||
|
private void SelectPrevPage()
|
||||||
|
{
|
||||||
|
SelectedResults.SelectPrevPage();
|
||||||
|
}
|
||||||
|
|
||||||
|
[RelayCommand]
|
||||||
|
private void SelectNextPage()
|
||||||
|
{
|
||||||
|
SelectedResults.SelectNextPage();
|
||||||
|
}
|
||||||
|
[RelayCommand]
|
||||||
|
private void SelectPrevItem()
|
||||||
|
{
|
||||||
|
SelectedResults.SelectPrevResult();
|
||||||
|
}
|
||||||
|
[RelayCommand]
|
||||||
|
private void SelectNextItem()
|
||||||
|
{
|
||||||
|
SelectedResults.SelectNextResult();
|
||||||
|
}
|
||||||
|
|
||||||
|
[RelayCommand]
|
||||||
|
private void Esc()
|
||||||
|
{
|
||||||
|
if (!SelectedIsFromQueryResults())
|
||||||
|
{
|
||||||
|
SelectedResults = Results;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
Hide();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
|
|
@ -416,6 +416,7 @@ namespace Flow.Launcher.ViewModel
|
||||||
/// but we don't want to move cursor to end when query is updated from TextBox
|
/// but we don't want to move cursor to end when query is updated from TextBox
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="queryText"></param>
|
/// <param name="queryText"></param>
|
||||||
|
/// <param name="reQuery">Force query even when Query Text doesn't change</param>
|
||||||
public void ChangeQueryText(string queryText, bool reQuery = false)
|
public void ChangeQueryText(string queryText, bool reQuery = false)
|
||||||
{
|
{
|
||||||
if (QueryText != queryText)
|
if (QueryText != queryText)
|
||||||
|
|
@ -494,25 +495,6 @@ namespace Flow.Launcher.ViewModel
|
||||||
|
|
||||||
public string PluginIconPath { get; set; } = null;
|
public string PluginIconPath { get; set; } = null;
|
||||||
|
|
||||||
public ICommand EscCommand { get; set; }
|
|
||||||
public ICommand BackspaceCommand { get; set; }
|
|
||||||
public ICommand SelectNextItemCommand { get; set; }
|
|
||||||
public ICommand SelectPrevItemCommand { get; set; }
|
|
||||||
public ICommand SelectNextPageCommand { get; set; }
|
|
||||||
public ICommand SelectPrevPageCommand { get; set; }
|
|
||||||
public ICommand SelectFirstResultCommand { get; set; }
|
|
||||||
public ICommand StartHelpCommand { get; set; }
|
|
||||||
public ICommand LoadContextMenuCommand { get; set; }
|
|
||||||
public ICommand LoadHistoryCommand { get; set; }
|
|
||||||
public ICommand OpenResultCommand { get; set; }
|
|
||||||
public ICommand OpenSettingCommand { get; set; }
|
|
||||||
public ICommand ReloadPluginDataCommand { get; set; }
|
|
||||||
public ICommand ClearQueryCommand { get; private set; }
|
|
||||||
|
|
||||||
public ICommand CopyToClipboard { get; set; }
|
|
||||||
|
|
||||||
public ICommand AutocompleteQueryCommand { get; set; }
|
|
||||||
|
|
||||||
public string OpenResultCommandModifiers { get; private set; }
|
public string OpenResultCommandModifiers { get; private set; }
|
||||||
|
|
||||||
public string Image => Constant.QueryTextBoxIconImagePath;
|
public string Image => Constant.QueryTextBoxIconImagePath;
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue