diff --git a/sky/packages/sky/lib/src/services/keyboard.dart b/sky/packages/sky/lib/src/services/keyboard.dart index a56df6a0219..a6c2b2afff1 100644 --- a/sky/packages/sky/lib/src/services/keyboard.dart +++ b/sky/packages/sky/lib/src/services/keyboard.dart @@ -2,6 +2,8 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. +import 'dart:async'; + import 'package:mojo_services/keyboard/keyboard.mojom.dart'; import 'shell.dart'; @@ -32,6 +34,8 @@ class Keyboard { KeyboardHandle _currentHandle; + bool _hidePending = false; + KeyboardHandle show(KeyboardClientStub stub, KeyboardType keyboardType) { assert(stub != null); if (_currentHandle != null) { @@ -43,6 +47,20 @@ class Keyboard { return _currentHandle; } + void _scheduleHide() { + if (_hidePending) return; + _hidePending = true; + + // Schedule a deferred task that hides the keyboard. If someone else shows + // the keyboard during this update cycle, then the task will do nothing. + scheduleMicrotask(() { + _hidePending = false; + if (_currentHandle == null) { + service.hide(); + } + }); + } + } class KeyboardHandle { @@ -70,9 +88,9 @@ class KeyboardHandle { void release() { if (_attached) { assert(_keyboard._currentHandle == this); - _keyboard.service.hide(); _attached = false; _keyboard._currentHandle = null; + _keyboard._scheduleHide(); } assert(_keyboard._currentHandle != this); }