From 382d158d7ed7cb5c9f3af56e5ec61e15ac33e297 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=81d=C3=A1m=20Kov=C3=A1cs?= Date: Sat, 19 Feb 2022 23:25:06 +0100 Subject: [PATCH] Display children immediately on container change --- src/Core/FileTime.Core/Components/Tab.cs | 19 ++++++++------- .../Application/TabContainer.cs | 24 +++++++++++-------- 2 files changed, 25 insertions(+), 18 deletions(-) diff --git a/src/Core/FileTime.Core/Components/Tab.cs b/src/Core/FileTime.Core/Components/Tab.cs index d76f3bf..82f4e21 100644 --- a/src/Core/FileTime.Core/Components/Tab.cs +++ b/src/Core/FileTime.Core/Components/Tab.cs @@ -21,7 +21,7 @@ namespace FileTime.Core.Components public bool AutoRefresh { get; set; } public AsyncEventHandler CurrentLocationChanged = new(); - public AsyncEventHandler CurrentSelectedItemChanged = new(); + public AsyncEventHandler CurrentSelectedItemChanged = new(); public async Task Init(IContainer currentPath) { @@ -47,7 +47,7 @@ namespace FileTime.Core.Components var currentLocationItems = await (await GetCurrentLocation()).GetItems(); 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); } } @@ -57,7 +57,11 @@ namespace FileTime.Core.Components return Task.FromResult(_currentSelectedItem); } - public async Task SetCurrentSelectedItem(IItem? value, bool secondary = false, CancellationToken token = default) + /*public async Task SetCurrentSelectedItem(IItem? value, bool secondary = false, CancellationToken token = default) + { + + }*/ + public async Task SetCurrentSelectedItem(IItem? value, bool locationChanged = false, CancellationToken token = default) { if (_currentlySelecting) return false; @@ -95,10 +99,9 @@ namespace FileTime.Core.Components _currentSelectedItem = itemToSelect; _lastPath = GetCommonPath(_lastPath, itemToSelect?.FullName); - var newCurrentSelectedIndex = await GetItemIndex(itemToSelect, CancellationToken.None); - CurrentSelectedIndex = newCurrentSelectedIndex; + CurrentSelectedIndex = await GetItemIndex(itemToSelect, CancellationToken.None); - await CurrentSelectedItemChanged.InvokeAsync(this, AsyncEventArgs.Empty, newToken); + await CurrentSelectedItemChanged.InvokeAsync(this, locationChanged, newToken); return !newToken.IsCancellationRequested; } @@ -290,13 +293,13 @@ namespace FileTime.Core.Components if (lastCurrentLocation is VirtualContainer lastCurrentVirtualContainer) { await SetCurrentLocation(lastCurrentVirtualContainer.CloneVirtualChainFor(parent, v => v.IsPermanent)); - await SetCurrentSelectedItem(lastCurrentVirtualContainer.GetRealContainer()); + await SetCurrentSelectedItem(lastCurrentVirtualContainer.GetRealContainer(), true); } else { await SetCurrentLocation(parent); 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()) diff --git a/src/GuiApp/FileTime.Avalonia/Application/TabContainer.cs b/src/GuiApp/FileTime.Avalonia/Application/TabContainer.cs index dc20fe2..0d2d365 100644 --- a/src/GuiApp/FileTime.Avalonia/Application/TabContainer.cs +++ b/src/GuiApp/FileTime.Avalonia/Application/TabContainer.cs @@ -55,7 +55,7 @@ namespace FileTime.Avalonia.Application { 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) { } } @@ -65,13 +65,13 @@ namespace FileTime.Avalonia.Application [Property] private ElementPreviewViewModel? _elementPreview; - public async Task SetSelectedItemAsync(IItemViewModel? value, bool fromDataBinding = false) + public async Task SetSelectedItemAsync(IItemViewModel? value) { if (_selectedItem != value) { _selectedItem = value; - await Tab.SetCurrentSelectedItem(SelectedItem?.Item, fromDataBinding); + await Tab.SetCurrentSelectedItem(SelectedItem?.Item); 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 {*/ @@ -194,11 +194,15 @@ namespace FileTime.Avalonia.Application await UpdateParents(token); var start = DateTime.Now; - while (true) + + if (!skipCancellationWaiting) { - await Task.Delay(1); - if (token.IsCancellationRequested) return; - if ((DateTime.Now - start).Milliseconds > 500) break; + do + { + await Task.Delay(1, token); + if (token.IsCancellationRequested) return; + } + while ((DateTime.Now - start).Milliseconds <= 500); } ChildContainer = newChildContainer;