feat: implement scroll detection.
This commit is contained in:
@@ -1,7 +1,8 @@
|
||||
<ResourceDictionary
|
||||
xmlns="https://github.com/avaloniaui"
|
||||
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}">
|
||||
<Setter Property="Template">
|
||||
<ControlTemplate>
|
||||
@@ -18,10 +19,13 @@
|
||||
<Setter Property="Template">
|
||||
<ControlTemplate TargetType="u:AnchorItem">
|
||||
<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}" />
|
||||
</StackPanel>
|
||||
</ControlTemplate>
|
||||
</Setter>
|
||||
<Style Selector="^:selected /template/ ContentPresenter#PART_HeaderPresenter">
|
||||
<Setter Property="Foreground" Value="{DynamicResource SemiBlue5}" />
|
||||
</Style>
|
||||
</ControlTheme>
|
||||
</ResourceDictionary>
|
||||
|
||||
13
src/Ursa/Common/VisualHelpers.cs
Normal file
13
src/Ursa/Common/VisualHelpers.cs
Normal 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;
|
||||
}
|
||||
}
|
||||
@@ -1,15 +1,23 @@
|
||||
using System.Diagnostics;
|
||||
using Avalonia;
|
||||
using Avalonia.Animation;
|
||||
using Avalonia.Animation.Easings;
|
||||
using Avalonia.Controls;
|
||||
using Avalonia.Controls.Primitives;
|
||||
using Avalonia.Input;
|
||||
using Avalonia.Interactivity;
|
||||
using Avalonia.Styling;
|
||||
using Avalonia.VisualTree;
|
||||
using Ursa.Common;
|
||||
|
||||
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?>(
|
||||
nameof(TargetContainer));
|
||||
@@ -40,6 +48,7 @@ public class Anchor: SelectingItemsControl
|
||||
|
||||
internal void ScrollToAnchor(string anchorId)
|
||||
{
|
||||
return;
|
||||
if (TargetContainer is null)
|
||||
return;
|
||||
var target = TargetContainer.GetVisualDescendants().FirstOrDefault(a=>Anchor.GetAnchorId(a) == anchorId);
|
||||
@@ -48,11 +57,13 @@ public class Anchor: SelectingItemsControl
|
||||
}
|
||||
|
||||
private CancellationTokenSource _cts = new();
|
||||
private bool _scrollingFromSelection = false;
|
||||
|
||||
private void ScrollToAnchor(Visual target)
|
||||
{
|
||||
if (TargetContainer is null)
|
||||
return;
|
||||
|
||||
var targetPosition = target.TranslatePoint(new Point(0, 0), TargetContainer);
|
||||
if (targetPosition.HasValue)
|
||||
{
|
||||
@@ -89,7 +100,10 @@ public class Anchor: SelectingItemsControl
|
||||
};
|
||||
_cts.Cancel();
|
||||
_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)
|
||||
{
|
||||
|
||||
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)
|
||||
{
|
||||
return CreateContainerForItemOverride(item, index, recycleKey);
|
||||
@@ -134,4 +169,8 @@ public class Anchor: SelectingItemsControl
|
||||
{
|
||||
ContainerForItemPreparedOverride(container, item, index);
|
||||
}
|
||||
|
||||
internal AnchorItem? _selectedContainer;
|
||||
|
||||
|
||||
}
|
||||
@@ -59,6 +59,7 @@ public class AnchorItem : HeaderedItemsControl, ISelectable
|
||||
{
|
||||
// var item = new TreeViewItem();
|
||||
base.OnPointerPressed(e);
|
||||
return;
|
||||
if (e.Handled) return;
|
||||
if (_root is null)
|
||||
return;
|
||||
|
||||
Reference in New Issue
Block a user