Skip to content

Commit

Permalink
Simplify keyboard tracking logic.
Browse files Browse the repository at this point in the history
  • Loading branch information
ChebanovDD committed Aug 22, 2022
1 parent 0a142af commit 4959416
Showing 1 changed file with 20 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ namespace UIElements
{
public class MobileInputAdaptivePage : VisualElement, IDisposable
{
private bool _isActivated;
private float _parentHeight;
private float _initialPaddingBottom;
private float _defaultPaddingBottom;
private float? _paddingBottomWithKeyboard;
private MobileInputDialogController _inputDialog;

private AsyncLazy _trackKeyboardActivityTask;
private CancellationTokenSource _cancellationTokenSource;

public MobileInputAdaptivePage()
Expand All @@ -29,14 +29,21 @@ public MobileInputAdaptivePage()
}
}

public bool IsActivated => _isActivated;
public float OffsetFromKeyboardPx { get; set; }
public bool IsActivated => _trackKeyboardActivityTask is { Task: { Status: UniTaskStatus.Pending } };


public async UniTask ActivateAsync()
{
if (_isActivated)
{
return;
}

_isActivated = true;

if (IsScreenKeyboardSupported())
{
ActivateKeyboardTrackingAsync().Forget();
TrackKeyboardActivityAsync().Forget();
}

SetVisible(true);
Expand All @@ -48,6 +55,13 @@ public async UniTask ActivateAsync()

public async UniTask DeactivateAsync()
{
if (_isActivated == false)
{
return;
}

_isActivated = false;

if (IsScreenKeyboardSupported())
{
_inputDialog.HideScreenKeyboard();
Expand Down Expand Up @@ -89,25 +103,15 @@ private void OnLayoutCalculated(GeometryChangedEvent evt)
parent.visible = value;
}

private async UniTaskVoid ActivateKeyboardTrackingAsync()
{
if (_trackKeyboardActivityTask?.Task.Status.IsCompleted() ?? true)
{
_trackKeyboardActivityTask = TrackKeyboardActivityAsync().ToAsyncLazy();
}

await _trackKeyboardActivityTask;
}

private async UniTask TrackKeyboardActivityAsync()
private async UniTaskVoid TrackKeyboardActivityAsync()
{
_cancellationTokenSource = new CancellationTokenSource();

try
{
var cancellationToken = _cancellationTokenSource.Token;

while (cancellationToken.IsCancellationRequested == false)
while (_isActivated)
{
if (TouchScreenKeyboard.visible == false)
{
Expand Down

0 comments on commit 4959416

Please sign in to comment.