[Windows][GestureManager] Unsubscribe control tap events only if they were subscribed (#23976)

* Optimize

* feedback

* More

* fix build
This commit is contained in:
MartyIX 2024-08-09 14:57:13 +02:00 коммит произвёл GitHub
Родитель fd6332899a
Коммит 534ed023c6
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: B5690EEEBB952194
1 изменённых файлов: 37 добавлений и 31 удалений

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

@ -103,10 +103,17 @@ namespace Microsoft.Maui.Controls.Platform
if (_control == value)
return;
if (_control != null)
if (_control is not null)
{
_control.Tapped -= HandleTapped;
_control.DoubleTapped -= HandleDoubleTapped;
if ((_subscriptionFlags & SubscriptionFlags.ControlTapEventSubscribed) != 0)
{
_control.Tapped -= HandleTapped;
}
if ((_subscriptionFlags & SubscriptionFlags.ControlDoubleTapEventSubscribed) != 0)
{
_control.DoubleTapped -= HandleDoubleTapped;
}
}
_control = value;
@ -277,26 +284,16 @@ namespace Microsoft.Maui.Controls.Platform
if (_element == value)
return;
if (_element != null)
if (_element is View && ElementGestureRecognizers is {} gestureRecognizersBefore)
{
var view = _element as View;
if (view != null)
{
if (ElementGestureRecognizers != null)
ElementGestureRecognizers.CollectionChanged -= _collectionChangedHandler;
}
gestureRecognizersBefore.CollectionChanged -= _collectionChangedHandler;
}
_element = value;
if (_element != null)
if (_element is View && ElementGestureRecognizers is {} gestureRecognizersAfter)
{
var view = _element as View;
if (view != null)
{
if (ElementGestureRecognizers != null)
ElementGestureRecognizers.CollectionChanged += _collectionChangedHandler;
}
gestureRecognizersAfter.CollectionChanged += _collectionChangedHandler;
}
}
}
@ -371,30 +368,35 @@ namespace Microsoft.Maui.Controls.Platform
protected virtual void Dispose(bool disposing)
{
if (_isDisposed)
{
return;
}
_isDisposed = true;
if (!disposing)
{
return;
}
ClearContainerEventHandlers();
if (_element != null)
if (_element is View && ElementGestureRecognizers is {} gestureRecognizers)
{
var view = _element as View;
if (view != null)
{
if (ElementGestureRecognizers != null)
ElementGestureRecognizers.CollectionChanged -= _collectionChangedHandler;
}
gestureRecognizers.CollectionChanged -= _collectionChangedHandler;
}
if (_control != null)
if (_control is not null)
{
_control.Tapped -= HandleTapped;
_control.DoubleTapped -= HandleDoubleTapped;
if ((_subscriptionFlags & SubscriptionFlags.ControlTapEventSubscribed) != 0)
{
_control.Tapped -= HandleTapped;
}
if ((_subscriptionFlags & SubscriptionFlags.ControlDoubleTapEventSubscribed) != 0)
{
_control.DoubleTapped -= HandleDoubleTapped;
}
}
Control = null;
@ -783,8 +785,9 @@ namespace Microsoft.Maui.Controls.Platform
}
else
{
if (_control != null && PreventGestureBubbling)
if (_control is not null && PreventGestureBubbling)
{
_subscriptionFlags |= SubscriptionFlags.ControlTapEventSubscribed;
_control.Tapped += HandleTapped;
}
}
@ -798,8 +801,9 @@ namespace Microsoft.Maui.Controls.Platform
}
else
{
if (_control != null && PreventGestureBubbling)
if (_control is not null && PreventGestureBubbling)
{
_subscriptionFlags |= SubscriptionFlags.ControlDoubleTapEventSubscribed;
_control.DoubleTapped += HandleDoubleTapped;
}
}
@ -885,7 +889,9 @@ namespace Microsoft.Maui.Controls.Platform
ContainerPgrPointerEventsSubscribed = 1 << 2,
ContainerManipulationAndPointerEventsSubscribed = 1 << 3,
ContainerTapAndRightTabEventSubscribed = 1 << 4,
ContainerDoubleTapEventSubscribed = 1 << 5
ContainerDoubleTapEventSubscribed = 1 << 5,
ControlTapEventSubscribed = 1 << 6,
ControlDoubleTapEventSubscribed = 1 << 7
}
}
}