focus.dart 7.69 KB
Newer Older
Eric Seidel's avatar
Eric Seidel committed
1 2 3 4
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

5
import 'package:sky/src/widgets/framework.dart';
Eric Seidel's avatar
Eric Seidel committed
6

7
typedef void FocusChanged(GlobalKey key);
Eric Seidel's avatar
Eric Seidel committed
8

9 10 11 12 13
// _noFocusedScope is used by Focus to track the case where none of the Focus
// component's subscopes (e.g. dialogs) are focused. This is distinct from the
// focused scope being null, which means that we haven't yet decided which scope
// is focused and whichever is the first scope to ask for focus will get it.
final GlobalKey _noFocusedScope = new GlobalKey();
Eric Seidel's avatar
Eric Seidel committed
14

15 16 17 18 19 20 21
class _FocusScope extends Inherited {

  _FocusScope({
    Key key,
    this.scopeFocused: true, // are we focused in our ancestor scope?
    this.focusedScope, // which of our descendant scopes is focused, if any?
    this.focusedWidget,
Eric Seidel's avatar
Eric Seidel committed
22 23 24
    Widget child
  }) : super(key: key, child: child);

25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
  final bool scopeFocused;

  // These are mutable because we implicitly changed them when they're null in
  // certain cases, basically pretending retroactively that we were constructed
  // with the right keys.
  GlobalKey focusedScope;
  GlobalKey focusedWidget;

  // The ...IfUnset() methods don't need to notify descendants because by
  // definition they are only going to make a change the very first time that
  // our state is checked.

  void _setFocusedWidgetIfUnset(GlobalKey key) {
    assert(parent is Focus);
    (parent as Focus)._setFocusedWidgetIfUnset(key); // TODO(ianh): remove cast once analyzer is cleverer
    focusedWidget = (parent as Focus)._focusedWidget;
    focusedScope = (parent as Focus)._focusedScope == _noFocusedScope ? null : (parent as Focus)._focusedScope;
  }

  void _setFocusedScopeIfUnset(GlobalKey key) {
    assert(parent is Focus);
    (parent as Focus)._setFocusedScopeIfUnset(key); // TODO(ianh): remove cast once analyzer is cleverer
    assert(focusedWidget == (parent as Focus)._focusedWidget);
    focusedScope = (parent as Focus)._focusedScope == _noFocusedScope ? null : (parent as Focus)._focusedScope;
  }

  bool syncShouldNotify(_FocusScope old) {
    assert(parent is Focus);
    if (scopeFocused != old.scopeFocused)
      return true;
    if (!scopeFocused)
      return false;
    if (focusedScope != old.focusedScope)
      return true;
    if (focusedScope != null)
      return false;
    if (focusedWidget != old.focusedWidget)
      return true;
    return false;
  }

}

class Focus extends StatefulComponent {

  Focus({
    GlobalKey key, // key is required if this is a nested Focus scope
    this.autofocus: false,
    this.child
  }) : super(key: key) {
    assert(!autofocus || key != null);
  }

  bool autofocus;
  Widget child;

81
  void syncConstructorArguments(Focus source) {
82 83 84 85 86 87 88
    autofocus = source.autofocus;
    child = source.child;
  }


  GlobalKey _focusedWidget; // when null, the first component to ask if it's focused will get the focus
  GlobalKey _currentlyRegisteredWidgetRemovalListenerKey;
Eric Seidel's avatar
Eric Seidel committed
89

90 91 92 93 94 95 96
  void _setFocusedWidget(GlobalKey key) {
    setState(() {
      _focusedWidget = key;
      if (_focusedScope == null)
        _focusedScope = _noFocusedScope;
    });
    _updateWidgetRemovalListener(key);
Eric Seidel's avatar
Eric Seidel committed
97
  }
98 99 100 101 102 103

  void _setFocusedWidgetIfUnset(GlobalKey key) {
    if (_focusedWidget == null && (_focusedScope == null || _focusedScope == _noFocusedScope)) {
      _focusedWidget = key;
      _focusedScope = _noFocusedScope;
      _updateWidgetRemovalListener(key);
Eric Seidel's avatar
Eric Seidel committed
104 105 106
    }
  }

107
  void _handleWidgetRemoved(GlobalKey key) {
108
    assert(_focusedWidget == key);
109
    _updateWidgetRemovalListener(null);
110 111 112 113 114 115 116 117
    setState(() {
      _focusedWidget = null;
    });
  }

  void _updateWidgetRemovalListener(GlobalKey key) {
    if (_currentlyRegisteredWidgetRemovalListenerKey != key) {
      if (_currentlyRegisteredWidgetRemovalListenerKey != null)
118
        GlobalKey.unregisterRemoveListener(_currentlyRegisteredWidgetRemovalListenerKey, _handleWidgetRemoved);
119
      if (key != null)
120
        GlobalKey.registerRemoveListener(key, _handleWidgetRemoved);
121 122 123 124 125 126 127 128 129 130 131 132 133 134
      _currentlyRegisteredWidgetRemovalListenerKey = key;
    }
  }


  GlobalKey _focusedScope; // when null, the first scope to ask if it's focused will get the focus
  GlobalKey _currentlyRegisteredScopeRemovalListenerKey;

  void _setFocusedScope(GlobalKey key) {
    setState(() {
      _focusedScope = key;
    });
    _updateScopeRemovalListener(key);
  }
Eric Seidel's avatar
Eric Seidel committed
135

136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
  void _setFocusedScopeIfUnset(GlobalKey key) {
    if (_focusedScope == null) {
      _focusedScope = key;
      _updateScopeRemovalListener(key);
    }
  }

  void _scopeRemoved(GlobalKey key) {
    assert(_focusedScope == key);
    _currentlyRegisteredScopeRemovalListenerKey = null;
    setState(() {
      _focusedScope = null;
    });
  }

  void _updateScopeRemovalListener(GlobalKey key) {
    if (_currentlyRegisteredScopeRemovalListenerKey != key) {
      if (_currentlyRegisteredScopeRemovalListenerKey != null)
154
        GlobalKey.unregisterRemoveListener(_currentlyRegisteredScopeRemovalListenerKey, _scopeRemoved);
155
      if (key != null)
156
        GlobalKey.registerRemoveListener(key, _scopeRemoved);
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
      _currentlyRegisteredScopeRemovalListenerKey = key;
    }
  }


  bool _didAutoFocus = false;
  void didMount() {
    if (autofocus && !_didAutoFocus) {
      _didAutoFocus = true;
      Focus._moveScopeTo(this);
    }
    _updateWidgetRemovalListener(_focusedWidget);
    _updateScopeRemovalListener(_focusedScope);
    super.didMount();
  }

  void didUnmount() {
    _updateWidgetRemovalListener(null);
    _updateScopeRemovalListener(null);
    super.didUnmount();
  }

  Widget build() {
    return new _FocusScope(
      scopeFocused: Focus._atScope(this),
      focusedScope: _focusedScope == _noFocusedScope ? null : _focusedScope,
      focusedWidget: _focusedWidget,
      child: child
    );
  }

  static bool at(Component component, { bool autofocus: true }) {
Eric Seidel's avatar
Eric Seidel committed
189 190
    assert(component != null);
    assert(component.key is GlobalKey);
191 192 193 194 195 196 197 198 199
    _FocusScope focusScope = component.inheritedOfType(_FocusScope);
    if (focusScope != null) {
      if (autofocus)
        focusScope._setFocusedWidgetIfUnset(component.key);
      return focusScope.scopeFocused &&
             focusScope.focusedScope == null &&
             focusScope.focusedWidget == component.key;
    }
    return true;
Eric Seidel's avatar
Eric Seidel committed
200 201
  }

202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217
  static bool _atScope(Focus component, { bool autofocus: true }) {
    assert(component != null);
    _FocusScope focusScope = component.inheritedOfType(_FocusScope);
    if (focusScope != null) {
      if (autofocus)
        focusScope._setFocusedScopeIfUnset(component.key);
      assert(component.key != null);
      return focusScope.scopeFocused &&
             focusScope.focusedScope == component.key;
    }
    return true;
  }

  // Don't call moveTo() from your build() function, it's intended to be called
  // from event listeners, e.g. in response to a finger tap or tab key.

Eric Seidel's avatar
Eric Seidel committed
218 219 220
  static void moveTo(Component component) {
    assert(component != null);
    assert(component.key is GlobalKey);
221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237
    _FocusScope focusScope = component.inheritedOfType(_FocusScope);
    if (focusScope != null) {
      assert(focusScope.parent is Focus);
      (focusScope.parent as Focus)._setFocusedWidget(component.key); // TODO(ianh): remove cast once analyzer is cleverer
    }
  }

  static void _moveScopeTo(Focus component) {
    assert(component != null);
    assert(component.key != null);
    _FocusScope focusScope = component.inheritedOfType(_FocusScope);
    if (focusScope != null) {
      assert(focusScope.parent is Focus);
      (focusScope.parent as Focus)._setFocusedScope(component.key); // TODO(ianh): remove cast once analyzer is cleverer
    }
  }

238 239 240 241
  void debugAddDetails(List<String> details) {
    super.debugAddDetails(details);
    details.add('focusedScope=$_focusedScope');
    details.add('focusedWidget=$_focusedWidget');
Eric Seidel's avatar
Eric Seidel committed
242 243 244
  }

}