From 24a7efccf4c395a1f7e238523be41f1f0efaa820 Mon Sep 17 00:00:00 2001 From: MountainFlash Date: Mon, 18 Oct 2021 10:21:46 +0530 Subject: [PATCH] remember caret offset for all open tabs i hope this works fine. --- FModel/ViewModels/TabControlViewModel.cs | 10 +++- .../Resources/Controls/AvalonEditor.xaml.cs | 56 +++++++++++++------ 2 files changed, 48 insertions(+), 18 deletions(-) diff --git a/FModel/ViewModels/TabControlViewModel.cs b/FModel/ViewModels/TabControlViewModel.cs index a70d30f1..3cb5efb8 100644 --- a/FModel/ViewModels/TabControlViewModel.cs +++ b/FModel/ViewModels/TabControlViewModel.cs @@ -1,4 +1,5 @@ -using FModel.Extensions; +using System; +using FModel.Extensions; using FModel.Framework; using FModel.Settings; using FModel.ViewModels.Commands; @@ -349,9 +350,16 @@ namespace FModel.ViewModels } _tabItems.Remove(tabToDelete); + OnTabRemove?.Invoke(this, new TabEventArgs(tabToDelete)); }); } + public class TabEventArgs : EventArgs + { + public TabItem TabToRemove { get; set; } + public TabEventArgs(TabItem tab) { TabToRemove = tab; } + } + public event EventHandler OnTabRemove; public void GoLeftTab() => SelectedTab = _tabItems.Previous(SelectedTab); public void GoRightTab() => SelectedTab = _tabItems.Next(SelectedTab); diff --git a/FModel/Views/Resources/Controls/AvalonEditor.xaml.cs b/FModel/Views/Resources/Controls/AvalonEditor.xaml.cs index aced1b7c..f325b16a 100644 --- a/FModel/Views/Resources/Controls/AvalonEditor.xaml.cs +++ b/FModel/Views/Resources/Controls/AvalonEditor.xaml.cs @@ -1,10 +1,13 @@ using System; +using System.Collections.Generic; using System.Text.RegularExpressions; using System.Windows; using System.Windows.Input; using System.Windows.Media; +using CUE4Parse.Utils; using FModel.Extensions; using FModel.Framework; +using FModel.Services; using FModel.ViewModels; using ICSharpCode.AvalonEdit; using SkiaSharp; @@ -21,7 +24,17 @@ namespace FModel.Views.Resources.Controls private readonly Regex _hexColorRegex = new("\"Hex\": \"(?'target'[0-9A-Fa-f]{3,8})\"$", RegexOptions.Compiled | RegexOptions.Singleline | RegexOptions.IgnoreCase | RegexOptions.CultureInvariant); private readonly System.Windows.Controls.ToolTip _toolTip = new(); - private readonly NavigationList _caretList = new(); + private Dictionary> _savedCarets= new(); + private NavigationList _caretsOffsets + { + get + { + if (MyAvalonEditor.Document != null) + return _savedCarets.GetOrAdd(MyAvalonEditor.Document.FileName, () => new NavigationList()); + else + return new NavigationList(); + } + } private bool _ignoreCaret = true; public AvalonEditor() @@ -33,6 +46,8 @@ namespace FModel.Views.Resources.Controls YesWeSearch = WpfSuckMyDick; MyAvalonEditor.TextArea.TextView.ElementGenerators.Add(new GamePathElementGenerator()); MyAvalonEditor.TextArea.TextView.ElementGenerators.Add(new HexColorElementGenerator()); + + ApplicationService.ApplicationView.CUE4Parse.TabControl.OnTabRemove += OnTabClose; } private void OnPreviewKeyDown(object sender, KeyEventArgs e) @@ -55,17 +70,13 @@ namespace FModel.Views.Resources.Controls case Key.System: // Alt if (Keyboard.IsKeyDown(Key.Left)) { - if (_caretList.Count == 0) - return; - var movep = _caretList.MovePrevious; - MyAvalonEditor.CaretOffset = movep; + if (_caretsOffsets.Count == 0) return; + MyAvalonEditor.CaretOffset = _caretsOffsets.MovePrevious; MyAvalonEditor.TextArea.Caret.BringCaretToView(); } else if ((Keyboard.IsKeyDown(Key.Right))) { - if (_caretList.Count == 0) - return; - var move = _caretList.MoveNext; - MyAvalonEditor.CaretOffset = move; + if (_caretsOffsets.Count == 0) return; + MyAvalonEditor.CaretOffset = _caretsOffsets.MoveNext; MyAvalonEditor.TextArea.Caret.BringCaretToView(); } break; @@ -108,9 +119,11 @@ namespace FModel.Views.Resources.Controls if (sender is not TextEditor avalonEditor || DataContext is not TabItem tabItem || avalonEditor.Document == null || string.IsNullOrEmpty(avalonEditor.Document.Text)) return; - _caretList.Clear(); - _ignoreCaret = true; - avalonEditor.Document.FileName = tabItem.Directory + '/' + tabItem.Header.SubstringBeforeLast('.'); + avalonEditor.Document.FileName = tabItem.Directory + '/' + StringExtensions.SubstringBeforeLast(tabItem.Header, '.'); + + if (!_savedCarets.ContainsKey(avalonEditor.Document.FileName)) + _ignoreCaret = true; + if (!tabItem.ShouldScroll) return; var lineNumber = avalonEditor.Document.Text.GetLineNumber(tabItem.ScrollTrigger); @@ -208,15 +221,24 @@ namespace FModel.Views.Resources.Controls ((TabItem) DataContext).HasSearchOpen = false; } + private void OnTabClose(object sender, EventArgs eventArgs) + { + if (sender is not TabItem tab|| eventArgs is not TabControlViewModel.TabEventArgs e) + return; + var fileName = e.TabToRemove.Document.FileName; + if (_savedCarets.ContainsKey(fileName)) + _savedCarets.Remove(fileName); + } + private void SaveCaretLoc(int offset) { if (_ignoreCaret) { _ignoreCaret = false; return;} // first always point to the end of the file for some reason - if (_caretList.Count >= 10) - _caretList.RemoveAt(0); - if (!_caretList.Contains(offset)) + if (_caretsOffsets.Count >= 10) + _caretsOffsets.RemoveAt(0); + if (!_caretsOffsets.Contains(offset)) { - _caretList.Add(offset); - _caretList.CurrentIndex = _caretList.Count-1; + _caretsOffsets.Add(offset); + _caretsOffsets.CurrentIndex = _caretsOffsets.Count-1; } }