Files
sdk/samples-dev/swarm/swarm_ui_lib/touch/EventUtil.dart
T
Leaf Petersen 745f112ef6 Opt out some files from null safety.
Opt an initial batch of files under samples, samples-dev,
utils, and runtime/tools/dartfuzz out of null safety in preparation
for switching the flag on by default.

Change-Id: Icdfd52a5a969e678a7205903332f73fe3841c223
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/166960
Reviewed-by: Daco Harkes <dacoharkes@google.com>
Commit-Queue: Leaf Petersen <leafp@google.com>
2020-10-12 22:15:42 +00:00

48 lines
1.4 KiB
Dart

// Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
// @dart = 2.9
part of touch;
/**
* Common events related helpers.
*/
class EventUtil {
/**
* Add an event listener to an element.
* The event callback is specified by [handler].
* If [capture] is true, the listener gets events on the capture phase.
* If [removeHandlerOnFocus] is true the handler is removed when there is any
* focus event, and added back on blur events.
*/
static void observe(
/*Element or Document*/ element, Stream stream, Function handler,
[bool removeHandlerOnFocus = false]) {
var subscription = stream.listen(handler);
// TODO(jacobr): this remove on focus behavior seems really ugly.
if (removeHandlerOnFocus) {
element.onFocus.listen((e) {
subscription.cancel();
});
element.onBlur.listen((e) {
subscription.cancel();
});
}
}
/**
* Clear the keyboard focus of the currently focused element (if there is
* one). If there is no currently focused element then this function will do
* nothing. For most browsers this will cause the keyboard to be dismissed.
*/
static void blurFocusedElement() {
Element focusedEl = document.querySelector("*:focus");
if (focusedEl != null) {
focusedEl.blur();
}
}
}