"Recent" menu implemented (#585)
* Refactoring CommandHistoryService to prepare "Recent" menu item feature. * "Recent" menu implemented, but still not bound to commands. * "Recent" menu implemented.
This commit is contained in:
1 parent
5bc679d8e6
commit
a872d126ce
15 files changed
+510
-313
No files matched your search
@@ -18,12 +18,19 @@ namespace FluentTerminal.App.ViewModels
|
||||
{
|
||||
public class MainViewModel : ViewModelBase
|
||||
{
|
||||
#region Constants
|
||||
|
||||
private const int RecentItemsMaxCount = 10;
|
||||
|
||||
#endregion Constants
|
||||
|
||||
private readonly IClipboardService _clipboardService;
|
||||
private readonly IDialogService _dialogService;
|
||||
private readonly IDispatcherTimer _dispatcherTimer;
|
||||
private readonly IKeyboardCommandService _keyboardCommandService;
|
||||
private readonly ISettingsService _settingsService;
|
||||
private readonly ITrayProcessCommunicationService _trayProcessCommunicationService;
|
||||
private readonly ICommandHistoryService _commandHistoryService;
|
||||
private ApplicationSettings _applicationSettings;
|
||||
private string _background;
|
||||
private double _backgroundOpacity;
|
||||
@@ -32,8 +39,8 @@ namespace FluentTerminal.App.ViewModels
|
||||
private string _windowTitle;
|
||||
private List<KeyBinding> _keyBindings;
|
||||
|
||||
public MainViewModel(ISettingsService settingsService, ITrayProcessCommunicationService trayProcessCommunicationService, IDialogService dialogService, IKeyboardCommandService keyboardCommandService,
|
||||
IApplicationView applicationView, IDispatcherTimer dispatcherTimer, IClipboardService clipboardService)
|
||||
public MainViewModel(ISettingsService settingsService, ITrayProcessCommunicationService trayProcessCommunicationService, IDialogService dialogService, IKeyboardCommandService keyboardCommandService,
|
||||
IApplicationView applicationView, IDispatcherTimer dispatcherTimer, IClipboardService clipboardService, ICommandHistoryService commandHistoryService)
|
||||
{
|
||||
MessengerInstance.Register<ApplicationSettingsChangedMessage>(this, OnApplicationSettingsChanged);
|
||||
MessengerInstance.Register<CurrentThemeChangedMessage>(this, OnCurrentThemeChanged);
|
||||
@@ -42,6 +49,7 @@ namespace FluentTerminal.App.ViewModels
|
||||
MessengerInstance.Register<SshProfileAddedMessage>(this, OnSshProfileAdded);
|
||||
MessengerInstance.Register<SshProfileDeletedMessage>(this, OnSshProfileDeleted);
|
||||
MessengerInstance.Register<TerminalOptionsChangedMessage>(this, OnTerminalOptionsChanged);
|
||||
MessengerInstance.Register<CommandHistoryChangedMessage>(this, OnCommandHistoryChanged);
|
||||
|
||||
_settingsService = settingsService;
|
||||
|
||||
@@ -51,6 +59,8 @@ namespace FluentTerminal.App.ViewModels
|
||||
_dispatcherTimer = dispatcherTimer;
|
||||
_clipboardService = clipboardService;
|
||||
_keyboardCommandService = keyboardCommandService;
|
||||
_commandHistoryService = commandHistoryService;
|
||||
|
||||
_keyboardCommandService.RegisterCommandHandler(nameof(Command.NewTab), async () => await AddLocalTabAsync());
|
||||
_keyboardCommandService.RegisterCommandHandler(nameof(Command.NewSshTab), async () => await AddSshTabAsync());
|
||||
_keyboardCommandService.RegisterCommandHandler(nameof(Command.NewCustomCommandTab), async () => await AddCustomCommandTabAsync());
|
||||
@@ -113,6 +123,24 @@ namespace FluentTerminal.App.ViewModels
|
||||
MessengerInstance.Register<KeyBindingsChangedMessage>(this, message => LoadKeyBindings());
|
||||
}
|
||||
|
||||
private IList<ProfileCommandViewModel> _recentCommands;
|
||||
|
||||
public IList<ProfileCommandViewModel> RecentCommands
|
||||
{
|
||||
get => _recentCommands ?? (_recentCommands = _commandHistoryService
|
||||
.GetHistoryRecentFirst(top: RecentItemsMaxCount).Select(c =>
|
||||
new ProfileCommandViewModel(c.ShellProfile,
|
||||
new RelayCommand(() => AddTerminalAsync(c.ShellProfile)))).ToList());
|
||||
private set => Set(ref _recentCommands, value);
|
||||
}
|
||||
|
||||
private void OnCommandHistoryChanged(CommandHistoryChangedMessage message)
|
||||
{
|
||||
_recentCommands = null;
|
||||
|
||||
ApplicationView.RunOnDispatcherThread(() => RaisePropertyChanged(nameof(RecentCommands)), false);
|
||||
}
|
||||
|
||||
private void LoadKeyBindings() =>
|
||||
_keyBindings = _settingsService.GetCommandKeyBindings().Values.SelectMany(v => v).ToList();
|
||||
|
||||
@@ -371,6 +399,8 @@ namespace FluentTerminal.App.ViewModels
|
||||
|
||||
public Task AddTerminalAsync(ShellProfile profile, string terminalState, int position)
|
||||
{
|
||||
profile.Tag = new DelayedHistorySaver(() => _commandHistoryService.MarkUsed(profile));
|
||||
|
||||
return ApplicationView.RunOnDispatcherThread(() =>
|
||||
{
|
||||
var terminal = new TerminalViewModel(_settingsService, _trayProcessCommunicationService, _dialogService, _keyboardCommandService,
|
||||
|
||||
Reference in new issue
Block a user