From 9661155cdd0357f9524a65890383568eed9ae61a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=81d=C3=A1m=20Kov=C3=A1cs?= Date: Sun, 13 Feb 2022 21:08:57 +0100 Subject: [PATCH] Fixes, UI improvements child container, preview, etc --- .../Application/TabContainer.cs | 123 ++++++++++-------- .../Services/KeyInputHandlerService.cs | 4 +- .../ViewModels/ContainerViewModel.cs | 65 --------- .../FileTime.Avalonia/Views/MainWindow.axaml | 3 +- .../Views/MainWindow.axaml.cs | 6 + 5 files changed, 78 insertions(+), 123 deletions(-) diff --git a/src/GuiApp/FileTime.Avalonia/Application/TabContainer.cs b/src/GuiApp/FileTime.Avalonia/Application/TabContainer.cs index c60070f..4007ac7 100644 --- a/src/GuiApp/FileTime.Avalonia/Application/TabContainer.cs +++ b/src/GuiApp/FileTime.Avalonia/Application/TabContainer.cs @@ -33,7 +33,7 @@ namespace FileTime.Avalonia.Application private ContainerViewModel _currentLocation; [Property] - private ContainerViewModel _childContainer; + private ContainerViewModel? _childContainer; [Property] private int _tabNumber; @@ -51,16 +51,16 @@ namespace FileTime.Avalonia.Application { if (!_updateFromCode && value != null) { - try - { - /*var task = SetSelectedItemAsync(value, true); - Task.WaitAll(new Task[] { task }, 100);*/ - SetSelectedItemAsync(value, true); - } + /*try + {*/ + /*var task = SetSelectedItemAsync(value, true); + Task.WaitAll(new Task[] { task }, 100);*/ + SetSelectedItemAsync(value, true); + /*} catch { //TODO: Debug, linux start after restore 3 tabs - } + }*/ } } } @@ -74,21 +74,8 @@ namespace FileTime.Avalonia.Application { _selectedItem = value; - if (value is ElementViewModel elementViewModel) - { - var elementPreview = new ElementPreviewViewModel(); - await elementPreview.Init(elementViewModel.Element); - ElementPreview = elementPreview; - } - else - { - ElementPreview = null; - } - - if (await Tab.SetCurrentSelectedItem(SelectedItem?.Item, fromDataBinding)) - { - OnPropertyChanged(nameof(SelectedItem)); - } + await Tab.SetCurrentSelectedItem(SelectedItem?.Item, fromDataBinding); + OnPropertyChanged(nameof(SelectedItem)); } } @@ -161,40 +148,68 @@ namespace FileTime.Avalonia.Application public async Task UpdateCurrentSelectedItem(CancellationToken token = default) { - try + /*try + {*/ + + if (token.IsCancellationRequested) return; + + var tabCurrentSelectenItem = await Tab.GetCurrentSelectedItem(); + ContainerViewModel? newChildContainer = null; + + IItemViewModel? currentSelectenItem = null; + if (tabCurrentSelectenItem == null) { - var tabCurrentSelectenItem = await Tab.GetCurrentSelectedItem(); - - if (token.IsCancellationRequested) return; - - IItemViewModel? currentSelectenItem = null; - if (tabCurrentSelectenItem == null) + await SetSelectedItemAsync(null); + } + else + { + currentSelectenItem = (await _currentLocation.GetItems(token)).FirstOrDefault(i => i.Item.Name == tabCurrentSelectenItem.Name); + if (currentSelectenItem is ContainerViewModel currentSelectedContainer) { - await SetSelectedItemAsync(null); - ChildContainer = null; + await SetSelectedItemAsync(currentSelectedContainer); + newChildContainer = currentSelectedContainer; + } + else if (currentSelectenItem is ElementViewModel element) + { + await SetSelectedItemAsync(element); } else { - currentSelectenItem = (await _currentLocation.GetItems(token)).FirstOrDefault(i => i.Item.Name == tabCurrentSelectenItem.Name); - if (currentSelectenItem is ContainerViewModel currentSelectedContainer) - { - await SetSelectedItemAsync(currentSelectedContainer); - ChildContainer = currentSelectedContainer; - } - else if (currentSelectenItem is ElementViewModel element) - { - await SetSelectedItemAsync(element); - ChildContainer = null; - } - else - { - await SetSelectedItemAsync(null); - ChildContainer = null; - } + await SetSelectedItemAsync(null); } + } + await UpdateParents(token); + + var start = DateTime.Now; + while (true) + { + await Task.Delay(1); if (token.IsCancellationRequested) return; + if ((DateTime.Now - start).Milliseconds > 500) break; + } + ChildContainer = newChildContainer; + + if (currentSelectenItem is ElementViewModel elementViewModel) + { + var elementPreview = new ElementPreviewViewModel(); + await elementPreview.Init(elementViewModel.Element); + ElementPreview = elementPreview; + } + else + { + ElementPreview = null; + } + /*} + catch + { + //INFO collection modified exception on: currentSelectenItem = (await _currentLocation.GetItems()).FirstOrDefault(i => i.Item.Name == tabCurrentSelectenItem.Name); + //TODO: handle or error message + }*/ + + async Task UpdateParents(CancellationToken token = default) + { var items = await _currentLocation.GetItems(token); if (items?.Count > 0) { @@ -244,11 +259,6 @@ namespace FileTime.Avalonia.Application } } } - catch - { - //INFO collection modified exception on: currentSelectenItem = (await _currentLocation.GetItems()).FirstOrDefault(i => i.Item.Name == tabCurrentSelectenItem.Name); - //TODO: handle or error message - } } public async Task SetCurrentSelectedItem(IItem newItem) @@ -269,14 +279,17 @@ namespace FileTime.Avalonia.Application } catch { - _updateFromCode = false; throw; } + finally + { + _updateFromCode = false; + } } private CancellationToken CancelAndGenerateNextMovementToken() { - if(_moveCancellationTokenSource != null) _moveCancellationTokenSource.Cancel(); + if (_moveCancellationTokenSource != null) _moveCancellationTokenSource.Cancel(); _moveCancellationTokenSource = new CancellationTokenSource(); return _moveCancellationTokenSource.Token; } diff --git a/src/GuiApp/FileTime.Avalonia/Services/KeyInputHandlerService.cs b/src/GuiApp/FileTime.Avalonia/Services/KeyInputHandlerService.cs index 4f7662a..b09b5a6 100644 --- a/src/GuiApp/FileTime.Avalonia/Services/KeyInputHandlerService.cs +++ b/src/GuiApp/FileTime.Avalonia/Services/KeyInputHandlerService.cs @@ -80,9 +80,9 @@ namespace FileTime.Avalonia.Services else if (selectedCommandBinding != null) { setHandled(true); - await _commandHandlerService.HandleCommandAsync(selectedCommandBinding.Command); _appState.PreviousKeys.Clear(); _appState.PossibleCommands = new(); + await _commandHandlerService.HandleCommandAsync(selectedCommandBinding.Command); } else if (_keysToSkip.Any(k => AreKeysEqual(k, _appState.PreviousKeys))) { @@ -99,6 +99,7 @@ namespace FileTime.Avalonia.Services } else { + setHandled(true); var possibleCommands = _keyboardConfigurationService.AllShortcut.Where(c => AreKeysEqual(c.Keys[0], keyWithModifiers)).ToList(); if (possibleCommands.Count == 0) @@ -110,7 +111,6 @@ namespace FileTime.Avalonia.Services { _appState.PossibleCommands = possibleCommands; } - setHandled(true); } } else diff --git a/src/GuiApp/FileTime.Avalonia/ViewModels/ContainerViewModel.cs b/src/GuiApp/FileTime.Avalonia/ViewModels/ContainerViewModel.cs index c845ec9..be6be80 100644 --- a/src/GuiApp/FileTime.Avalonia/ViewModels/ContainerViewModel.cs +++ b/src/GuiApp/FileTime.Avalonia/ViewModels/ContainerViewModel.cs @@ -67,71 +67,6 @@ namespace FileTime.Avalonia.ViewModels public List DisplayName => ItemNameConverterService.GetDisplayName(this); - /*[Obsolete($"This property is for databinding only, use {nameof(GetContainers)} method instead.")] - public ObservableCollection Containers - { - get - { - try - { - if (!_isInitialized) Task.Run(Refresh).Wait(); - } - catch(Exception e) { } - return _containers; - } - set - { - if (value != _containers) - { - _containers = value; - OnPropertyChanged(nameof(Containers)); - } - } - } - - [Obsolete($"This property is for databinding only, use {nameof(GetElements)} method instead.")] - public ObservableCollection Elements - { - get - { - try - { - if (!_isInitialized) Task.Run(Refresh).Wait(); - } - catch(Exception e) { } - return _elements; - } - set - { - if (value != _elements) - { - _elements = value; - OnPropertyChanged(nameof(Elements)); - } - } - } - [Obsolete($"This property is for databinding only, use {nameof(GetItems)} method instead.")] - public ObservableCollection Items - { - get - { - try - { - if (!_isInitialized) Task.Run(Refresh).Wait(); - } - catch(Exception e) { } - return _items; - } - set - { - if (value != _items) - { - _items = value; - OnPropertyChanged(nameof(Items)); - } - } - }*/ - public Task Containers => GetContainers(); public Task Elements => GetElements(); public Task Items => GetItems(); diff --git a/src/GuiApp/FileTime.Avalonia/Views/MainWindow.axaml b/src/GuiApp/FileTime.Avalonia/Views/MainWindow.axaml index e34bd09..a5ef93b 100644 --- a/src/GuiApp/FileTime.Avalonia/Views/MainWindow.axaml +++ b/src/GuiApp/FileTime.Avalonia/Views/MainWindow.axaml @@ -248,9 +248,10 @@ AutoScrollToSelectedItem="True" IsTabStop="True" Items="{Binding AppState.SelectedTab.CurrentLocation.Items^}" - ScrollViewer.HorizontalScrollBarVisibility="Disabled" + ScrollViewer.HorizontalScrollBarVisibility="Hidden" ScrollViewer.VerticalScrollBarVisibility="Hidden" SelectedItem="{Binding AppState.SelectedTab.SelectedItem, Mode=TwoWay}" + SelectionChanged="OnCurrentItemsSelectionChanged" Classes="ContentListView"> diff --git a/src/GuiApp/FileTime.Avalonia/Views/MainWindow.axaml.cs b/src/GuiApp/FileTime.Avalonia/Views/MainWindow.axaml.cs index 7949132..473ba9e 100644 --- a/src/GuiApp/FileTime.Avalonia/Views/MainWindow.axaml.cs +++ b/src/GuiApp/FileTime.Avalonia/Views/MainWindow.axaml.cs @@ -154,5 +154,11 @@ namespace FileTime.Avalonia.Views BeginMoveDrag(e); } } + + private void OnCurrentItemsSelectionChanged(object sender, SelectionChangedEventArgs e) + { + var listBox = this.FindControl("CurrentItems"); + listBox.ScrollIntoView(listBox.SelectedIndex); + } } } \ No newline at end of file