feat: implement scroll detection.

This commit is contained in:
Dong Bin
2025-07-05 01:55:17 +08:00
parent dbc41249d8
commit 85605b5faf
4 changed files with 63 additions and 6 deletions

View File

@@ -1,7 +1,8 @@
<ResourceDictionary <ResourceDictionary
xmlns="https://github.com/avaloniaui" xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:u="https://irihi.tech/ursa"> xmlns:u="https://irihi.tech/ursa"
xmlns:iri="https://irihi.tech/shared">
<ControlTheme x:Key="{x:Type u:Anchor}" TargetType="{x:Type u:Anchor}"> <ControlTheme x:Key="{x:Type u:Anchor}" TargetType="{x:Type u:Anchor}">
<Setter Property="Template"> <Setter Property="Template">
<ControlTemplate> <ControlTemplate>
@@ -18,10 +19,13 @@
<Setter Property="Template"> <Setter Property="Template">
<ControlTemplate TargetType="u:AnchorItem"> <ControlTemplate TargetType="u:AnchorItem">
<StackPanel> <StackPanel>
<ContentPresenter Content="{TemplateBinding Header}" ContentTemplate="{TemplateBinding HeaderTemplate}" /> <ContentPresenter Name="{x:Static iri:PartNames.PART_HeaderPresenter}" Content="{TemplateBinding Header}" ContentTemplate="{TemplateBinding HeaderTemplate}" />
<ItemsPresenter Margin="8 0 0 0" ItemsPanel="{TemplateBinding ItemsPanel}" /> <ItemsPresenter Margin="8 0 0 0" ItemsPanel="{TemplateBinding ItemsPanel}" />
</StackPanel> </StackPanel>
</ControlTemplate> </ControlTemplate>
</Setter> </Setter>
<Style Selector="^:selected /template/ ContentPresenter#PART_HeaderPresenter">
<Setter Property="Foreground" Value="{DynamicResource SemiBlue5}" />
</Style>
</ControlTheme> </ControlTheme>
</ResourceDictionary> </ResourceDictionary>

View File

@@ -0,0 +1,13 @@
using Avalonia;
using Avalonia.VisualTree;
namespace Ursa.Common;
public static class VisualHelpers
{
public static T? GetContainerFromEventSource<T>(this Visual? source)
{
var item = source.GetSelfAndVisualAncestors().OfType<T>().FirstOrDefault();
return item;
}
}

View File

@@ -1,15 +1,23 @@
using System.Diagnostics;
using Avalonia; using Avalonia;
using Avalonia.Animation; using Avalonia.Animation;
using Avalonia.Animation.Easings; using Avalonia.Animation.Easings;
using Avalonia.Controls; using Avalonia.Controls;
using Avalonia.Controls.Primitives; using Avalonia.Input;
using Avalonia.Interactivity; using Avalonia.Interactivity;
using Avalonia.Styling; using Avalonia.Styling;
using Avalonia.VisualTree; using Avalonia.VisualTree;
using Ursa.Common;
namespace Ursa.Controls; namespace Ursa.Controls;
public class Anchor: SelectingItemsControl /// <summary>
/// Some basic assumptions: This should not be a regular SelectingItemsControl, because it does not support multiple selections.
/// Selection should not be exposed to the user, it is only used to determine which item is currently selected.
/// The manipulation of container selection should be simplified.
/// Scroll event of TargetContainer also triggers selection change.
/// </summary>
public class Anchor: ItemsControl
{ {
public static readonly StyledProperty<ScrollViewer?> TargetContainerProperty = AvaloniaProperty.Register<Anchor, ScrollViewer?>( public static readonly StyledProperty<ScrollViewer?> TargetContainerProperty = AvaloniaProperty.Register<Anchor, ScrollViewer?>(
nameof(TargetContainer)); nameof(TargetContainer));
@@ -40,6 +48,7 @@ public class Anchor: SelectingItemsControl
internal void ScrollToAnchor(string anchorId) internal void ScrollToAnchor(string anchorId)
{ {
return;
if (TargetContainer is null) if (TargetContainer is null)
return; return;
var target = TargetContainer.GetVisualDescendants().FirstOrDefault(a=>Anchor.GetAnchorId(a) == anchorId); var target = TargetContainer.GetVisualDescendants().FirstOrDefault(a=>Anchor.GetAnchorId(a) == anchorId);
@@ -48,11 +57,13 @@ public class Anchor: SelectingItemsControl
} }
private CancellationTokenSource _cts = new(); private CancellationTokenSource _cts = new();
private bool _scrollingFromSelection = false;
private void ScrollToAnchor(Visual target) private void ScrollToAnchor(Visual target)
{ {
if (TargetContainer is null) if (TargetContainer is null)
return; return;
var targetPosition = target.TranslatePoint(new Point(0, 0), TargetContainer); var targetPosition = target.TranslatePoint(new Point(0, 0), TargetContainer);
if (targetPosition.HasValue) if (targetPosition.HasValue)
{ {
@@ -89,7 +100,10 @@ public class Anchor: SelectingItemsControl
}; };
_cts.Cancel(); _cts.Cancel();
_cts = new CancellationTokenSource(); _cts = new CancellationTokenSource();
animation.RunAsync(TargetContainer, _cts.Token); var token = _cts.Token;
token.Register(_ => _scrollingFromSelection = false, null);
_scrollingFromSelection = true;
animation.RunAsync(TargetContainer, token).ContinueWith(_ => _scrollingFromSelection = false, token);
} }
} }
@@ -112,9 +126,30 @@ public class Anchor: SelectingItemsControl
private void OnScrollChanged(object? sender, ScrollChangedEventArgs e) private void OnScrollChanged(object? sender, ScrollChangedEventArgs e)
{ {
if (_scrollingFromSelection) return;
Debug.WriteLine("Scroll changed");
} }
protected override void OnPointerPressed(PointerPressedEventArgs e)
{
base.OnPointerPressed(e);
var source = (e.Source as Visual).GetContainerFromEventSource<AnchorItem>();
if (source is null) return;
if (_selectedContainer is not null)
{
_selectedContainer.IsSelected = false;
}
source.IsSelected = true;
_selectedContainer = source;
var target = TargetContainer?.GetVisualDescendants()
.FirstOrDefault(a => Anchor.GetAnchorId(a) == source?.AnchorId);
if (target is null) return;
ScrollToAnchor(target);
}
/// <summary>
/// This method is used to expose the protected CreateContainerForItemOverride method to the AnchorItem class.
/// </summary>
internal Control CreateContainerForItemOverride_INTERNAL(object? item, int index, object? recycleKey) internal Control CreateContainerForItemOverride_INTERNAL(object? item, int index, object? recycleKey)
{ {
return CreateContainerForItemOverride(item, index, recycleKey); return CreateContainerForItemOverride(item, index, recycleKey);
@@ -134,4 +169,8 @@ public class Anchor: SelectingItemsControl
{ {
ContainerForItemPreparedOverride(container, item, index); ContainerForItemPreparedOverride(container, item, index);
} }
internal AnchorItem? _selectedContainer;
} }

View File

@@ -59,6 +59,7 @@ public class AnchorItem : HeaderedItemsControl, ISelectable
{ {
// var item = new TreeViewItem(); // var item = new TreeViewItem();
base.OnPointerPressed(e); base.OnPointerPressed(e);
return;
if (e.Handled) return; if (e.Handled) return;
if (_root is null) if (_root is null)
return; return;