better way to handle on hovered position

This commit is contained in:
Tung Huynh 2021-07-13 10:24:29 -07:00
Родитель b150736401
Коммит 9d66f1a4e1
7 изменённых файлов: 35 добавлений и 37 удалений

Просмотреть файл

@ -1,6 +1,11 @@
private async void SuggestingBox_OnTokenHovered(RichSuggestBox sender, RichSuggestTokenHoveredEventArgs args)
private void SuggestingBox_OnPointerMoved(object sender, PointerRoutedEventArgs e)
{
await Task.Delay(1);
this._pointerPoint = e.GetCurrentPoint((UIElement)sender);
}
private async void SuggestingBox_OnTokenHovered(RichSuggestBox sender, RichSuggestTokenEventArgs args)
{
await Task.Delay(200);
var flyout = (Flyout)FlyoutBase.GetAttachedFlyout(sender);
if (flyout?.Content is ContentPresenter cp && sender.TextDocument.Selection.Type != SelectionType.Normal &&
(!flyout.IsOpen || cp.Content != args.Token.Item))
@ -8,7 +13,7 @@
cp.Content = args.Token.Item;
flyout.ShowAt(sender, new FlyoutShowOptions
{
Position = args.CurrentPoint.Position,
Position = this._pointerPoint.Position,
ExclusionRect = args.Rect,
ShowMode = FlyoutShowMode.TransientWithDismissOnPointerMoveAway
});

Просмотреть файл

@ -8,12 +8,13 @@ using System.Linq;
using System.Threading.Tasks;
using Microsoft.Toolkit.Uwp.UI;
using Microsoft.Toolkit.Uwp.UI.Controls;
using Windows.System;
using Windows.UI;
using Windows.UI.Input;
using Windows.UI.Text;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Input;
// The Blank Page item template is documented at https://go.microsoft.com/fwlink/?LinkId=234238
@ -90,17 +91,18 @@ namespace Microsoft.Toolkit.Uwp.SampleApp.SamplePages
private RichSuggestBox _rsb;
private RichSuggestBox _tsb;
private DispatcherQueueTimer _hoveringTimer;
private PointerPoint _pointerPoint;
public RichSuggestBoxPage()
{
this.InitializeComponent();
this._hoveringTimer = DispatcherQueue.GetForCurrentThread().CreateTimer();
Loaded += (sender, e) => { this.OnXamlRendered(this); };
}
public void OnXamlRendered(FrameworkElement control)
{
PointerEventHandler pointerMovedHandler = SuggestingBox_OnPointerMoved;
if (this._rsb != null)
{
this._rsb.SuggestionChosen -= this.SuggestingBox_OnSuggestionChosen;
@ -112,6 +114,7 @@ namespace Microsoft.Toolkit.Uwp.SampleApp.SamplePages
this._tsb.SuggestionChosen -= this.SuggestingBox_OnSuggestionChosen;
this._tsb.SuggestionsRequested -= this.SuggestingBox_OnSuggestionsRequested;
this._tsb.TokenHovered -= SuggestingBox_OnTokenHovered;
this._tsb.RemoveHandler(PointerMovedEvent, pointerMovedHandler);
}
if (control.FindChild("SuggestingBox") is RichSuggestBox rsb)
@ -127,6 +130,7 @@ namespace Microsoft.Toolkit.Uwp.SampleApp.SamplePages
this._tsb.SuggestionChosen += this.SuggestingBox_OnSuggestionChosen;
this._tsb.SuggestionsRequested += this.SuggestingBox_OnSuggestionsRequested;
this._tsb.TokenHovered += this.SuggestingBox_OnTokenHovered;
this._tsb.AddHandler(PointerMovedEvent, pointerMovedHandler, true);
}
if (control.FindChild("TokenListView1") is ListView tls1)
@ -140,9 +144,14 @@ namespace Microsoft.Toolkit.Uwp.SampleApp.SamplePages
}
}
private async void SuggestingBox_OnTokenHovered(RichSuggestBox sender, RichSuggestTokenHoveredEventArgs args)
private void SuggestingBox_OnPointerMoved(object sender, PointerRoutedEventArgs e)
{
await Task.Delay(1);
this._pointerPoint = e.GetCurrentPoint((UIElement)sender);
}
private async void SuggestingBox_OnTokenHovered(RichSuggestBox sender, RichSuggestTokenEventArgs args)
{
await Task.Delay(200);
var flyout = (Flyout)FlyoutBase.GetAttachedFlyout(sender);
if (flyout?.Content is ContentPresenter cp && sender.TextDocument.Selection.Type != SelectionType.Normal &&
(!flyout.IsOpen || cp.Content != args.Token.Item))
@ -150,7 +159,7 @@ namespace Microsoft.Toolkit.Uwp.SampleApp.SamplePages
cp.Content = args.Token.Item;
flyout.ShowAt(sender, new FlyoutShowOptions
{
Position = args.CurrentPoint.Position,
Position = this._pointerPoint.Position,
ExclusionRect = args.Rect,
ShowMode = FlyoutShowMode.TransientWithDismissOnPointerMoveAway
});

Просмотреть файл

@ -27,12 +27,12 @@ namespace Microsoft.Toolkit.Uwp.UI.Controls
/// <summary>
/// Event raised when a token is fully highlighted.
/// </summary>
public event TypedEventHandler<RichSuggestBox, RichSuggestTokenSelectedEventArgs> TokenSelected;
public event TypedEventHandler<RichSuggestBox, RichSuggestTokenEventArgs> TokenSelected;
/// <summary>
/// Event raised when a pointer is hovering over a token.
/// </summary>
public event TypedEventHandler<RichSuggestBox, RichSuggestTokenHoveredEventArgs> TokenHovered;
public event TypedEventHandler<RichSuggestBox, RichSuggestTokenEventArgs> TokenHovered;
/// <summary>
/// Event raised when text is changed, either by user or by internal formatting.

Просмотреть файл

@ -13,7 +13,6 @@ using Windows.ApplicationModel.DataTransfer;
using Windows.Foundation;
using Windows.Foundation.Metadata;
using Windows.System;
using Windows.UI.Input;
using Windows.UI.Text;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
@ -240,7 +239,7 @@ namespace Microsoft.Toolkit.Uwp.UI.Controls
private void RichEditBox_OnPointerMoved(object sender, PointerRoutedEventArgs e)
{
var pointer = e.GetCurrentPoint((UIElement)sender);
InvokeTokenHovered(pointer.Position, e.GetCurrentPoint(this));
InvokeTokenHovered(pointer.Position);
}
private void RichEditBox_SelectionChanging(RichEditBox sender, RichEditBoxSelectionChangingEventArgs args)
@ -440,7 +439,7 @@ namespace Microsoft.Toolkit.Uwp.UI.Controls
}
var tokenRect = GetTokenRect(selection);
TokenSelected?.Invoke(this, new RichSuggestTokenSelectedEventArgs
TokenSelected?.Invoke(this, new RichSuggestTokenEventArgs
{
Token = token,
Rect = tokenRect,
@ -448,7 +447,7 @@ namespace Microsoft.Toolkit.Uwp.UI.Controls
});
}
private void InvokeTokenHovered(Point pointerPosition, PointerPoint passingPointerPoint)
private void InvokeTokenHovered(Point pointerPosition)
{
var padding = _richEditBox.Padding;
pointerPosition.X -= padding.Left;
@ -458,12 +457,11 @@ namespace Microsoft.Toolkit.Uwp.UI.Controls
if (range.Expand(TextRangeUnit.Link) > 0 && TryGetTokenFromRange(range, out token) &&
token != _hoveringToken)
{
TokenHovered?.Invoke(this, new RichSuggestTokenHoveredEventArgs
TokenHovered?.Invoke(this, new RichSuggestTokenEventArgs
{
Token = token,
Rect = GetTokenRect(range),
Range = range,
CurrentPoint = passingPointerPoint
});
}

Просмотреть файл

@ -4,6 +4,11 @@
xmlns:contract7Present="http://schemas.microsoft.com/winfx/2006/xaml/presentation?IsApiContractPresent(Windows.Foundation.UniversalApiContract,7)"
xmlns:contract8Present="http://schemas.microsoft.com/winfx/2006/xaml/presentation?IsApiContractPresent(Windows.Foundation.UniversalApiContract,8)"
xmlns:controls="using:Microsoft.Toolkit.Uwp.UI.Controls">
<ResourceDictionary.MergedDictionaries>
<!-- WinUI dependency for testing -->
<XamlControlsResources xmlns="using:Microsoft.UI.Xaml.Controls" />
</ResourceDictionary.MergedDictionaries>
<!-- Default style for RichSuggestBox -->
<Style BasedOn="{StaticResource DefaultRichSuggestBoxStyle}"
TargetType="controls:RichSuggestBox" />

Просмотреть файл

@ -11,7 +11,7 @@ namespace Microsoft.Toolkit.Uwp.UI.Controls
/// <summary>
/// Provides data for <see cref="RichSuggestBox.TokenSelected"/> event.
/// </summary>
public class RichSuggestTokenSelectedEventArgs : EventArgs
public class RichSuggestTokenEventArgs : EventArgs
{
/// <summary>
/// Gets or sets the selected token.

Просмотреть файл

@ -1,19 +0,0 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using Windows.UI.Input;
namespace Microsoft.Toolkit.Uwp.UI.Controls
{
/// <summary>
/// Provides data for <see cref="RichSuggestBox.TokenHovered"/> event.
/// </summary>
public class RichSuggestTokenHoveredEventArgs : RichSuggestTokenSelectedEventArgs
{
/// <summary>
/// Gets or sets a <see cref="PointerPoint"/> object with position relative to the <see cref="RichSuggestBox"/> instance.
/// </summary>
public PointerPoint CurrentPoint { get; set; }
}
}