Display children immediately on container change

This commit is contained in:
2022-02-19 23:25:06 +01:00
parent cd04e992f1
commit 382d158d7e
2 changed files with 25 additions and 18 deletions

View File

@@ -21,7 +21,7 @@ namespace FileTime.Core.Components
public bool AutoRefresh { get; set; } public bool AutoRefresh { get; set; }
public AsyncEventHandler CurrentLocationChanged = new(); public AsyncEventHandler CurrentLocationChanged = new();
public AsyncEventHandler CurrentSelectedItemChanged = new(); public AsyncEventHandler<bool> CurrentSelectedItemChanged = new();
public async Task Init(IContainer currentPath) public async Task Init(IContainer currentPath)
{ {
@@ -47,7 +47,7 @@ namespace FileTime.Core.Components
var currentLocationItems = await (await GetCurrentLocation()).GetItems(); var currentLocationItems = await (await GetCurrentLocation()).GetItems();
if (currentLocationItems == null) throw new Exception("Could not get current location items."); if (currentLocationItems == null) throw new Exception("Could not get current location items.");
await SetCurrentSelectedItem(await GetItemByLastPath() ?? (currentLocationItems.Count > 0 ? currentLocationItems[0] : null)); await SetCurrentSelectedItem(await GetItemByLastPath() ?? (currentLocationItems.Count > 0 ? currentLocationItems[0] : null), true);
_currentLocation.Refreshed.Add(HandleCurrentLocationRefresh); _currentLocation.Refreshed.Add(HandleCurrentLocationRefresh);
} }
} }
@@ -57,7 +57,11 @@ namespace FileTime.Core.Components
return Task.FromResult(_currentSelectedItem); return Task.FromResult(_currentSelectedItem);
} }
public async Task<bool> SetCurrentSelectedItem(IItem? value, bool secondary = false, CancellationToken token = default) /*public async Task<bool> SetCurrentSelectedItem(IItem? value, bool secondary = false, CancellationToken token = default)
{
}*/
public async Task<bool> SetCurrentSelectedItem(IItem? value, bool locationChanged = false, CancellationToken token = default)
{ {
if (_currentlySelecting) return false; if (_currentlySelecting) return false;
@@ -95,10 +99,9 @@ namespace FileTime.Core.Components
_currentSelectedItem = itemToSelect; _currentSelectedItem = itemToSelect;
_lastPath = GetCommonPath(_lastPath, itemToSelect?.FullName); _lastPath = GetCommonPath(_lastPath, itemToSelect?.FullName);
var newCurrentSelectedIndex = await GetItemIndex(itemToSelect, CancellationToken.None); CurrentSelectedIndex = await GetItemIndex(itemToSelect, CancellationToken.None);
CurrentSelectedIndex = newCurrentSelectedIndex;
await CurrentSelectedItemChanged.InvokeAsync(this, AsyncEventArgs.Empty, newToken); await CurrentSelectedItemChanged.InvokeAsync(this, locationChanged, newToken);
return !newToken.IsCancellationRequested; return !newToken.IsCancellationRequested;
} }
@@ -290,13 +293,13 @@ namespace FileTime.Core.Components
if (lastCurrentLocation is VirtualContainer lastCurrentVirtualContainer) if (lastCurrentLocation is VirtualContainer lastCurrentVirtualContainer)
{ {
await SetCurrentLocation(lastCurrentVirtualContainer.CloneVirtualChainFor(parent, v => v.IsPermanent)); await SetCurrentLocation(lastCurrentVirtualContainer.CloneVirtualChainFor(parent, v => v.IsPermanent));
await SetCurrentSelectedItem(lastCurrentVirtualContainer.GetRealContainer()); await SetCurrentSelectedItem(lastCurrentVirtualContainer.GetRealContainer(), true);
} }
else else
{ {
await SetCurrentLocation(parent); await SetCurrentLocation(parent);
var newCurrentLocation = (await (await GetCurrentLocation()).GetItems())?.FirstOrDefault(i => i.Name == lastCurrentLocation.Name); var newCurrentLocation = (await (await GetCurrentLocation()).GetItems())?.FirstOrDefault(i => i.Name == lastCurrentLocation.Name);
await SetCurrentSelectedItem(newCurrentLocation); await SetCurrentSelectedItem(newCurrentLocation, true);
} }
foreach (var lastLocationItem in currentLocationItems.OfType<IContainer>()) foreach (var lastLocationItem in currentLocationItems.OfType<IContainer>())

View File

@@ -55,7 +55,7 @@ namespace FileTime.Avalonia.Application
{ {
try try
{ {
Task.Run(async () => await SetSelectedItemAsync(value, true)).Wait(); Task.Run(async () => await SetSelectedItemAsync(value)).Wait();
} }
catch (AggregateException e) when (e.InnerExceptions.Count == 1 && e.InnerExceptions[0] is IndexOutOfRangeException) { } catch (AggregateException e) when (e.InnerExceptions.Count == 1 && e.InnerExceptions[0] is IndexOutOfRangeException) { }
} }
@@ -65,13 +65,13 @@ namespace FileTime.Avalonia.Application
[Property] [Property]
private ElementPreviewViewModel? _elementPreview; private ElementPreviewViewModel? _elementPreview;
public async Task SetSelectedItemAsync(IItemViewModel? value, bool fromDataBinding = false) public async Task SetSelectedItemAsync(IItemViewModel? value)
{ {
if (_selectedItem != value) if (_selectedItem != value)
{ {
_selectedItem = value; _selectedItem = value;
await Tab.SetCurrentSelectedItem(SelectedItem?.Item, fromDataBinding); await Tab.SetCurrentSelectedItem(SelectedItem?.Item);
OnPropertyChanged(nameof(SelectedItem)); OnPropertyChanged(nameof(SelectedItem));
} }
} }
@@ -153,12 +153,12 @@ namespace FileTime.Avalonia.Application
} }
} }
private async Task Tab_CurrentSelectedItemChanged(object? sender, AsyncEventArgs e, CancellationToken token = default) private async Task Tab_CurrentSelectedItemChanged(object? sender, bool locationChanged, CancellationToken token = default)
{ {
await UpdateCurrentSelectedItem(token); await UpdateCurrentSelectedItem(skipCancellationWaiting: locationChanged, token: token);
} }
public async Task UpdateCurrentSelectedItem(CancellationToken token = default) public async Task UpdateCurrentSelectedItem(bool skipCancellationWaiting = false, CancellationToken token = default)
{ {
/*try /*try
{*/ {*/
@@ -194,11 +194,15 @@ namespace FileTime.Avalonia.Application
await UpdateParents(token); await UpdateParents(token);
var start = DateTime.Now; var start = DateTime.Now;
while (true)
if (!skipCancellationWaiting)
{ {
await Task.Delay(1); do
{
await Task.Delay(1, token);
if (token.IsCancellationRequested) return; if (token.IsCancellationRequested) return;
if ((DateTime.Now - start).Milliseconds > 500) break; }
while ((DateTime.Now - start).Milliseconds <= 500);
} }
ChildContainer = newChildContainer; ChildContainer = newChildContainer;