优化动画弹出的一个误差

This commit is contained in:
polarboy 2024-07-07 13:50:37 +08:00
parent b37517deb6
commit d9554269cf
6 changed files with 19 additions and 17 deletions

View File

@ -53,8 +53,8 @@ public partial class ArrowDecoratedBox : IControlCustomStyle
// 生命周期一样,可以不用管理
if (_container is not null) {
if (Child?.Parent is not null) {
UiStructureUtils.SetLogicalParent(Child, null);
UiStructureUtils.SetVisualParent(Child, null);
UiStructureUtils.ClearLogicalParentRecursive(Child, null);
UiStructureUtils.ClearVisualParentRecursive(Child, null);
}
_compositeDisposable.Add(BindUtils.RelayBind(this, BackgroundSizingProperty, _container));
_compositeDisposable.Add(BindUtils.RelayBind(this, BackgroundProperty, _container));

View File

@ -278,7 +278,6 @@ public class Flyout : PopupFlyoutBase
offsetY += offset.Y;
Popup.HorizontalOffset = offsetX;
Popup.VerticalOffset = offsetY;
Console.WriteLine($"NotifyPositionPopup-{offsetY}");
}
protected override bool ShowAtCore(Control placementTarget, bool showAtPointer = false)
@ -291,13 +290,16 @@ public class Flyout : PopupFlyoutBase
bool result = default;
if (presenter is FlyoutPresenter flyoutPresenter) {
_animating = true;
if (flyoutPresenter.Child?.Parent is null) {
// 为了获取 token 资源
UiStructureUtils.SetLogicalParent(flyoutPresenter, placementTarget);
flyoutPresenter.Opacity = 0;
if (flyoutPresenter.Child is not null) {
var placementToplevel = TopLevel.GetTopLevel(placementTarget);
UiStructureUtils.ClearLogicalParentRecursive(flyoutPresenter, null);
UiStructureUtils.ClearVisualParentRecursive(flyoutPresenter, null);
UiStructureUtils.SetLogicalParent(flyoutPresenter, placementToplevel);
var positionInfo = AtomPopup.CalculatePositionInfo(placementTarget, presenter);
flyoutPresenter.Opacity = 1;
PlayShowUpMotion(positionInfo, placementTarget, flyoutPresenter, showAtPointer);
}
var positionInfo = AtomPopup.CalculatePositionInfo(placementTarget, presenter);
Console.WriteLine(positionInfo.Offset);
PlayShowUpMotion(positionInfo, placementTarget, flyoutPresenter, showAtPointer);
result = true;
} else {
result = base.ShowAtCore(placementTarget, showAtPointer);
@ -332,8 +334,11 @@ public class Flyout : PopupFlyoutBase
UiStructureUtils.ClearLogicalParentRecursive(child, null);
UiStructureUtils.ClearVisualParentRecursive(child, null);
}
base.ShowAtCore(placementTarget, showAtPointer);
if (Popup.Host is WindowBase window) {
window.PlatformImpl!.SetTopmost(true);
}
_animating = false;
base.ShowAtCore(placementTarget, showAtPointer);
};
director?.Schedule(motionActor);

View File

@ -388,12 +388,9 @@ public class Popup : AbstractPopup
{
using var ignoreSyncOriginHandling = IgnoreSyncOriginValueHandling();
var offsetX = _originOffsetX;
var offsetY = _originOffsetY;
Console.WriteLine(offsetY);
var offsetY = _originOffsetY + 0.5; // TODO 不知道为什么会出现 0.5 的误差
var marginToAnchorOffset = CalculateMarginToAnchorOffset(Placement);
Console.WriteLine(marginToAnchorOffset);
offsetX += marginToAnchorOffset.X;
offsetY += marginToAnchorOffset.Y;
Point offset = default;

View File

@ -8,6 +8,7 @@ using Avalonia.Controls;
using Avalonia.Controls.Primitives;
using Avalonia.Controls.Primitives.PopupPositioning;
using Avalonia.Media;
using Avalonia.Threading;
namespace AtomUI.Controls;

View File

@ -164,6 +164,7 @@ internal class MotionGhostControl : Control, INotifyCaptureGhostBitmap
{
if (_ghostBitmap is not null) {
var scaling = TopLevel.GetTopLevel(this)?.RenderScaling ?? 1.0;
context.DrawImage(_ghostBitmap, new Rect(new Point(0, 0), DesiredSize * scaling),
new Rect(new Point(0, 0), DesiredSize));
}

View File

@ -1,8 +1,6 @@
using System.Reactive.Disposables;
using AtomUI.MotionScene;
using Avalonia;
using Avalonia.Animation;
using Avalonia.Styling;
using Avalonia.Threading;
namespace AtomUI.Controls.MotionScene;
@ -42,7 +40,7 @@ public class Director : IDirector
if (sceneLayer is not null) {
Dispatcher.UIThread.InvokeAsync(async () =>
{
await Task.Delay(100);
await Task.Delay(300);
sceneLayer.Hide();
sceneLayer.Dispose();
});