From 3af30f54b6f4fb5e76c9d0990126ee130c857206 Mon Sep 17 00:00:00 2001 From: leo Date: Sun, 29 Sep 2024 14:16:42 +0800 Subject: [PATCH] enhance: avoid flicker at the first time clicking on text diff view --- src/Views/TextDiffView.axaml.cs | 53 +++++++++++++++++++++++---------- 1 file changed, 38 insertions(+), 15 deletions(-) diff --git a/src/Views/TextDiffView.axaml.cs b/src/Views/TextDiffView.axaml.cs index 6ae637fc..dd3471bf 100644 --- a/src/Views/TextDiffView.axaml.cs +++ b/src/Views/TextDiffView.axaml.cs @@ -694,18 +694,6 @@ namespace SourceGit.Views return 0; } - protected override void OnApplyTemplate(TemplateAppliedEventArgs e) - { - base.OnApplyTemplate(e); - - var scroller = (ScrollViewer)e.NameScope.Find("PART_ScrollViewer"); - if (scroller != null) - { - scroller.Bind(ScrollViewer.OffsetProperty, new Binding("SyncScrollOffset", BindingMode.TwoWay)); - scroller.GotFocus += (_, _) => TrySetChunk(null); - } - } - public override void UpdateSelectedChunk(double y) { var diff = DataContext as Models.TextDiff; @@ -822,6 +810,27 @@ namespace SourceGit.Views } } + protected override void OnLoaded(RoutedEventArgs e) + { + base.OnLoaded(e); + + var scroller = this.FindDescendantOfType(); + if (scroller != null) + { + scroller.Bind(ScrollViewer.OffsetProperty, new Binding("SyncScrollOffset", BindingMode.TwoWay)); + scroller.GotFocus += OnTextViewScrollGotFocus; + } + } + + protected override void OnUnloaded(RoutedEventArgs e) + { + var scroller = this.FindDescendantOfType(); + if (scroller != null) + scroller.GotFocus -= OnTextViewScrollGotFocus; + + base.OnUnloaded(e); + } + protected override void OnDataContextChanged(EventArgs e) { base.OnDataContextChanged(e); @@ -853,6 +862,16 @@ namespace SourceGit.Views GC.Collect(); } + + private void OnTextViewScrollGotFocus(object sender, GotFocusEventArgs e) + { + if (EnableChunkSelection && sender is ScrollViewer viewer) + { + var area = viewer.FindDescendantOfType