Frequency navigation

This commit is contained in:
2023-02-27 23:19:04 +01:00
parent 650b650cd8
commit 05c1f026e6
10 changed files with 130 additions and 42 deletions

View File

@@ -11,4 +11,8 @@
<ProjectReference Include="..\FileTime.App.Core.Abstraction\FileTime.App.Core.Abstraction.csproj" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Avalonia" Version="11.0.0-preview5" />
</ItemGroup>
</Project>

View File

@@ -1,3 +1,4 @@
using Avalonia.Input;
using FileTime.App.Core.ViewModels;
namespace FileTime.App.FrequencyNavigation.ViewModels;
@@ -9,4 +10,5 @@ public interface IFrequencyNavigationViewModel : IModalViewModel
string SearchText { get; set; }
string SelectedItem { get; set; }
void Close();
void HandleKeyDown(KeyEventArgs keyEventArgs);
}

View File

@@ -89,7 +89,7 @@ public partial class FrequencyNavigationService : IFrequencyNavigationService, I
if (TryAgeContainerScores() || DateTime.Now - _lastSave > TimeSpan.FromMinutes(5))
{
}
//TODO: move to if above
await SaveStateAsync();
}
catch (Exception e)
@@ -136,14 +136,18 @@ public partial class FrequencyNavigationService : IFrequencyNavigationService, I
return new List<string>();
_saveLock.Wait();
var matchingContainers = _containerScores
try
{
return _containerScores
.Where(c => c.Key.Contains(searchText, StringComparison.OrdinalIgnoreCase))
.OrderBy(c => GetWeightedScore(c.Value.Score, c.Value.LastAccessed))
.Select(c => c.Key)
.ToList();
}
finally
{
_saveLock.Release();
return matchingContainers;
}
}
private int GetWeightedScore(int score, DateTime lastAccess)
@@ -159,36 +163,48 @@ public partial class FrequencyNavigationService : IFrequencyNavigationService, I
};
}
public async Task InitAsync()
{
await LoadStateAsync();
}
public async Task InitAsync() => await LoadStateAsync();
private async Task LoadStateAsync()
{
if (!File.Exists(_dbPath))
return;
try
{
await _saveLock.WaitAsync();
_logger.LogTrace("Loading frequency navigation state from file '{DbPath}'", _dbPath);
await using var dbStream = File.OpenRead(_dbPath);
var containerScores = await JsonSerializer.DeserializeAsync<Dictionary<string, ContainerFrequencyData>>(dbStream);
if (containerScores is null) return;
_containerScores = containerScores;
}
catch (Exception e)
{
_logger.LogError(e, "Error loading frequency navigation state");
}
finally
{
_saveLock.Release();
}
public async Task ExitAsync()
{
await SaveStateAsync();
}
public async Task ExitAsync() => await SaveStateAsync();
private async Task SaveStateAsync()
{
await _saveLock.WaitAsync();
try
{
_lastSave = DateTime.Now;
await using var dbStream = File.OpenWrite(_dbPath);
await using var dbStream = File.Create(_dbPath);
await JsonSerializer.SerializeAsync(dbStream, _containerScores);
dbStream.Flush();
}
finally
{
_saveLock.Release();
}
}
}

View File

@@ -1,11 +1,18 @@
using Avalonia.Input;
using FileTime.App.Core.Services;
using FileTime.App.Core.UserCommand;
using FileTime.App.Core.ViewModels;
using FileTime.App.FrequencyNavigation.Services;
using FileTime.Core.Models;
using FileTime.Core.Timeline;
using MvvmGen;
namespace FileTime.App.FrequencyNavigation.ViewModels;
[ViewModel]
[Inject(typeof(IFrequencyNavigationService), "_frequencyNavigationService")]
[Inject(typeof(IUserCommandHandlerService), "_userCommandHandlerService")]
[Inject(typeof(ITimelessContentProvider), "_timelessContentProvider")]
public partial class FrequencyNavigationViewModel : IFrequencyNavigationViewModel
{
private string _searchText;
@@ -29,18 +36,48 @@ public partial class FrequencyNavigationViewModel : IFrequencyNavigationViewMode
}
public void Close()
=> _frequencyNavigationService.CloseNavigationWindow();
public async void HandleKeyDown(KeyEventArgs keyEventArgs)
{
_frequencyNavigationService.CloseNavigationWindow();
if (keyEventArgs.Key == Key.Down)
{
var nextItem = FilteredMatches.SkipWhile(i => i != SelectedItem).Skip(1).FirstOrDefault();
if (nextItem is not null)
{
SelectedItem = nextItem;
}
}
else if (keyEventArgs.Key == Key.Up)
{
var previousItem = FilteredMatches.TakeWhile(i => i != SelectedItem).LastOrDefault();
if (previousItem is not null)
{
SelectedItem = previousItem;
}
}
else if (keyEventArgs.Key == Key.Enter)
{
var targetContainer = await _timelessContentProvider.GetItemByFullNameAsync(new FullName(SelectedItem), PointInTime.Present);
var openContainerCommand = new OpenContainerCommand(new AbsolutePath(_timelessContentProvider, targetContainer));
await _userCommandHandlerService.HandleCommandAsync(openContainerCommand);
Close();
}
}
partial void OnInitialize()
{
_showWindow = _frequencyNavigationService.ShowWindow;
}
=> _showWindow = _frequencyNavigationService.ShowWindow;
private void UpdateFilteredMatches()
{
FilteredMatches = new List<string>(_frequencyNavigationService.GetMatchingContainers(_searchText));
if (FilteredMatches.Contains(SelectedItem)) return;
SelectedItem = FilteredMatches.Count > 0
? FilteredMatches[0]
: null;
}
string IModalViewModel.Name => "FrequencyNavigation";

View File

@@ -1,7 +1,7 @@
{
"Serilog": {
"MinimumLevel": {
"Default": "Debug",
"Default": "Verbose",
"Override": {
"Microsoft": "Information",
"System": "Warning"

View File

@@ -18,6 +18,8 @@ public class ToastMessageSink : ILogEventSink
if (logEvent.Level >= LogEventLevel.Error)
{
var message = logEvent.RenderMessage();
if (logEvent.Exception is not null)
message += $" {logEvent.Exception.Message}";
dialogService.ShowToastMessage(message);
}
}

View File

@@ -40,6 +40,21 @@
</Setter>
</Style>
<Style Selector="ListBox.CommandPalette">
<Setter Property="Background" Value="Transparent" />
<Setter Property="HorizontalAlignment" Value="Stretch" />
</Style>
<Style Selector="ListBox.CommandPalette &gt; ListBoxItem">
<Setter Property="Margin" Value="0" />
<Setter Property="Padding" Value="0" />
<Setter Property="Background" Value="Transparent" />
<Setter Property="HorizontalContentAlignment" Value="Stretch" />
<Setter Property="HorizontalAlignment" Value="Stretch" />
</Style>
<Style Selector="ListBox.CommandPalette &gt; ListBoxItem[IsSelected=true] TextBlock">
<Setter Property="Foreground" Value="{DynamicResource SelectedItemForegroundBrush}" />
</Style>
<Style Selector="Grid.SidebarContainerPresenter">
<Setter Property="Background" Value="#01000000" />
</Style>

View File

@@ -9,21 +9,28 @@
d:DesignWidth="800"
x:CompileBindings="True"
x:DataType="viewModels:IFrequencyNavigationViewModel"
Background="{DynamicResource ContainerBackgroundColor}"
mc:Ignorable="d">
<UserControl.Styles>
<StyleInclude Source="avares://FileTime.GuiApp/Resources/Styles.axaml" />
</UserControl.Styles>
<Grid RowDefinitions="Auto,*">
<TextBox
Focusable="True"
KeyDown="Search_OnKeyDown"
Text="{Binding SearchText, Mode=TwoWay}" />
<ItemsRepeater
<ListBox
Grid.Row="1"
Items="{Binding FilteredMatches}">
<ItemsRepeater.ItemTemplate>
Classes="CommandPalette"
Items="{Binding FilteredMatches}"
SelectedItem="{Binding SelectedItem}">
<ListBox.ItemTemplate>
<DataTemplate x:DataType="x:String">
<Grid Margin="5">
<TextBlock Text="{Binding}" />
</Grid>
</DataTemplate>
</ItemsRepeater.ItemTemplate>
</ItemsRepeater>
</ListBox.ItemTemplate>
</ListBox>
</Grid>
</UserControl>

View File

@@ -26,5 +26,9 @@ public partial class FrequencyNavigation : UserControl
{
viewModel.Close();
}
else
{
viewModel.HandleKeyDown(e);
}
}
}

View File

@@ -719,12 +719,13 @@
<Border
DataContext="{Binding FrequencyNavigationService.CurrentModal}"
Margin="100"
HorizontalAlignment="Stretch"
VerticalAlignment="Stretch"
Background="{DynamicResource BarelyTransparentBackgroundColor}"
IsVisible="{Binding ShowWindow^, FallbackValue=False}">
<Grid Margin="100">
<local:FrequencyNavigation />
</Grid>
</Border>
</Grid>