Skip to content

Commit

Permalink
fix(tooltip): Fix tooltip popup panel created too early
Browse files Browse the repository at this point in the history
Improves tooltip creation performance, fixes iOS 11/12 crash.
  • Loading branch information
jeromelaban committed Sep 24, 2020
1 parent 1a0ee79 commit 3473a4d
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 15 deletions.
41 changes: 27 additions & 14 deletions src/Uno.UI/UI/Xaml/Controls/ToolTip/ToolTip.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System;
#nullable enable

using System;
using System.Diagnostics;
using Microsoft.Extensions.Logging;
using Uno.Extensions;
Expand All @@ -24,11 +26,25 @@ public partial class ToolTip : ContentControl

internal const PlacementMode DefaultPlacementMode = PlacementMode.Top;

private readonly Popup _popup = new Popup { IsLightDismissEnabled = false };
private Popup? _popup;

private DependencyObject _owner;
private DependencyObject? _owner;

internal Popup Popup => _popup;
internal Popup Popup
{
get
{
if(_popup == null)
{
_popup = new Popup { IsLightDismissEnabled = false };
_popup.PopupPanel = new PopupPanel(_popup);
_popup.Opened += OnPopupOpened;
_popup.Closed += (sender, e) => IsOpen = false;
}

return _popup;
}
}

#pragma warning disable CS0649
#pragma warning disable CS0169
Expand All @@ -43,15 +59,12 @@ public partial class ToolTip : ContentControl
#pragma warning restore CS0169
#pragma warning restore CS0414

private FrameworkElement Target => _owner as FrameworkElement;
private FrameworkElement? Target => _owner as FrameworkElement;

public ToolTip()
{
DefaultStyleKey = typeof(ToolTip);

_popup.PopupPanel = new PopupPanel(_popup);
_popup.Opened += OnPopupOpened;
_popup.Closed += (sender, e) => IsOpen = false;
SizeChanged += OnToolTipSizeChanged;
Loading += (sender, e) => PerformPlacementInternal(); // Update placement on Loading, because this is the point at which Uno sets the default Style
}
Expand Down Expand Up @@ -102,7 +115,7 @@ private Size GetTooltipSize()
private void OnOpenChanged(bool isOpen)
{
PerformPlacementInternal();
_popup.IsOpen = isOpen;
Popup.IsOpen = isOpen;
if (isOpen)
{
AttachToPopup();
Expand All @@ -121,7 +134,7 @@ private void AttachToPopup()
{
if (Parent == null)
{
_popup.Child = this;
Popup.Child = this;
}
else if (!ReferenceEquals(Parent, _popup))
{
Expand All @@ -131,9 +144,9 @@ private void AttachToPopup()

public void SetAnchor(UIElement element) => _owner = element;

public event RoutedEventHandler Closed;
public event RoutedEventHandler? Closed;

public event RoutedEventHandler Opened;
public event RoutedEventHandler? Opened;

// Sets the location of the ToolTip's Popup.
//
Expand Down Expand Up @@ -251,7 +264,7 @@ private void PerformMousePlacementWithPopup(
}
}

var spPopup = _popup;
var spPopup = Popup;

var lastPointerEnteredPoint = CoreWindow.GetForCurrentThread().PointerPosition;

Expand Down Expand Up @@ -451,7 +464,7 @@ private void PerformNonMousePlacementWithPopup(
getDockToRectFromTargetElement = true;
}

if (getDockToRectFromTargetElement)
if (getDockToRectFromTargetElement && Target != null)
{
var targetTopLeft = default(Point);
Target.TransformToVisual(null).TransformPoint(targetTopLeft);
Expand Down
2 changes: 1 addition & 1 deletion src/Uno.UI/WasmScripts/Uno.UI.js
Original file line number Diff line number Diff line change
Expand Up @@ -3081,7 +3081,7 @@ var Windows;
class AssetManager {
static DownloadAssetsManifest(path) {
return __awaiter(this, void 0, void 0, function* () {
var response = yield fetch(path);
const response = yield fetch(path);
return response.text();
});
}
Expand Down

0 comments on commit 3473a4d

Please sign in to comment.