zefyr

scope.dart 7.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. import 'package:flutter/foundation.dart';
  2. import 'package:flutter/material.dart';
  3. import 'package:notus/notus.dart';
  4. import 'controller.dart';
  5. import 'cursor_timer.dart';
  6. import 'editor.dart';
  7. import 'image.dart';
  8. import 'mode.dart';
  9. import 'render_context.dart';
  10. import 'view.dart';
  11. /// Provides access to shared state of [ZefyrEditor] or [ZefyrView].
  12. ///
  13. /// A scope object can be created by an editable widget like [ZefyrEditor] in
  14. /// which case it provides access to editing state, including focus nodes,
  15. /// selection and such. Editable scope can be created using
  16. /// [ZefyrScope.editable] constructor.
  17. ///
  18. /// If a scope object is created by a view-only widget like [ZefyrView] then
  19. /// it only provides access to [imageDelegate].
  20. ///
  21. /// Can be retrieved using [ZefyrScope.of].
  22. class ZefyrScope extends ChangeNotifier {
  23. /// Creates a view-only scope.
  24. ///
  25. /// Normally used in [ZefyrView].
  26. ZefyrScope.view({ZefyrImageDelegate imageDelegate})
  27. : isEditable = false,
  28. _mode = ZefyrMode.view,
  29. _imageDelegate = imageDelegate;
  30. /// Creates editable scope.
  31. ///
  32. /// Normally used in [ZefyrEditor].
  33. ZefyrScope.editable({
  34. @required ZefyrMode mode,
  35. @required ZefyrController controller,
  36. @required FocusNode focusNode,
  37. @required FocusScopeNode focusScope,
  38. ZefyrImageDelegate imageDelegate,
  39. }) : assert(mode != null),
  40. assert(controller != null),
  41. assert(focusNode != null),
  42. assert(focusScope != null),
  43. isEditable = true,
  44. _mode = mode,
  45. _controller = controller,
  46. _imageDelegate = imageDelegate,
  47. _focusNode = focusNode,
  48. _focusScope = focusScope,
  49. _cursorTimer = CursorTimer(),
  50. _renderContext = ZefyrRenderContext() {
  51. _selectionStyle = _controller.getSelectionStyle();
  52. _selection = _controller.selection;
  53. _controller.addListener(_handleControllerChange);
  54. _focusNode.addListener(_handleFocusChange);
  55. }
  56. static ZefyrScope of(BuildContext context) {
  57. final ZefyrScopeAccess widget =
  58. context.dependOnInheritedWidgetOfExactType<ZefyrScopeAccess>();
  59. return widget.scope;
  60. }
  61. ZefyrImageDelegate _imageDelegate;
  62. ZefyrImageDelegate get imageDelegate => _imageDelegate;
  63. set imageDelegate(ZefyrImageDelegate value) {
  64. if (_imageDelegate != value) {
  65. _imageDelegate = value;
  66. notifyListeners();
  67. }
  68. }
  69. ZefyrMode _mode;
  70. ZefyrMode get mode => _mode;
  71. set mode(ZefyrMode value) {
  72. assert(value != null);
  73. if (_mode != value) {
  74. _mode = value;
  75. notifyListeners();
  76. }
  77. }
  78. ZefyrController _controller;
  79. ZefyrController get controller => _controller;
  80. set controller(ZefyrController value) {
  81. assert(isEditable && value != null);
  82. if (_controller != value) {
  83. _controller.removeListener(_handleControllerChange);
  84. _controller = value;
  85. _selectionStyle = _controller.getSelectionStyle();
  86. _selection = _controller.selection;
  87. _controller.addListener(_handleControllerChange);
  88. notifyListeners();
  89. }
  90. }
  91. FocusNode _focusNode;
  92. FocusNode get focusNode => _focusNode;
  93. set focusNode(FocusNode value) {
  94. assert(isEditable && value != null);
  95. if (_focusNode != value) {
  96. _focusNode.removeListener(_handleFocusChange);
  97. _focusNode = value;
  98. _focusNode.addListener(_handleFocusChange);
  99. notifyListeners();
  100. }
  101. }
  102. FocusScopeNode _focusScope;
  103. FocusScopeNode get focusScope => _focusScope;
  104. set focusScope(FocusScopeNode value) {
  105. assert(isEditable && value != null);
  106. if (_focusScope != value) {
  107. _focusScope = value;
  108. }
  109. }
  110. CursorTimer _cursorTimer;
  111. CursorTimer get cursorTimer => _cursorTimer;
  112. ValueNotifier<bool> get showCursor => cursorTimer.value;
  113. ZefyrRenderContext _renderContext;
  114. ZefyrRenderContext get renderContext => _renderContext;
  115. NotusStyle get selectionStyle => _selectionStyle;
  116. NotusStyle _selectionStyle;
  117. TextSelection get selection => _selection;
  118. TextSelection _selection;
  119. bool _disposed = false;
  120. FocusNode _toolbarFocusNode;
  121. /// Whether this scope is backed by editable Zefyr widgets or read-only view.
  122. ///
  123. /// Returns `true` if this scope provides Zefyr interface that allows editing
  124. /// (e.g. created by [ZefyrEditor]). Returns `false` if this scope provides
  125. /// read-only view (e.g. created by [ZefyrView]).
  126. ///
  127. /// Editable scope provides access to corresponding [controller], [focusNode],
  128. /// [focusScope], [showCursor], [renderContext] and other shared objects. For
  129. /// non-editable scopes these are set to `null`. You can still access
  130. /// objects which are not dependent on editing flow, e.g. [imageDelegate].
  131. final bool isEditable;
  132. set toolbarFocusNode(FocusNode node) {
  133. assert(isEditable);
  134. assert(!_disposed || node == null);
  135. if (_toolbarFocusNode != node) {
  136. _toolbarFocusNode?.removeListener(_handleFocusChange);
  137. _toolbarFocusNode = node;
  138. _toolbarFocusNode?.addListener(_handleFocusChange);
  139. // We do not notify listeners here because it will happen when
  140. // focus state changes, see [_handleFocusChange].
  141. }
  142. }
  143. FocusOwner get focusOwner {
  144. assert(isEditable);
  145. assert(!_disposed);
  146. if (_focusNode.hasFocus) {
  147. return FocusOwner.editor;
  148. } else if (_toolbarFocusNode?.hasFocus == true) {
  149. return FocusOwner.toolbar;
  150. } else {
  151. return FocusOwner.none;
  152. }
  153. }
  154. void updateSelection(TextSelection value,
  155. {ChangeSource source = ChangeSource.remote}) {
  156. assert(isEditable);
  157. assert(!_disposed);
  158. _controller.updateSelection(value, source: source);
  159. }
  160. void formatSelection(NotusAttribute value) {
  161. assert(isEditable);
  162. assert(!_disposed);
  163. _controller.formatSelection(value);
  164. }
  165. void focus() {
  166. assert(isEditable);
  167. assert(!_disposed);
  168. _focusScope.requestFocus(_focusNode);
  169. }
  170. void hideKeyboard() {
  171. assert(isEditable);
  172. assert(!_disposed);
  173. _focusNode.unfocus();
  174. }
  175. @override
  176. void dispose() {
  177. assert(!_disposed);
  178. _controller?.removeListener(_handleControllerChange);
  179. _focusNode?.removeListener(_handleFocusChange);
  180. _disposed = true;
  181. super.dispose();
  182. }
  183. void _handleControllerChange() {
  184. assert(!_disposed);
  185. final attrs = _controller.getSelectionStyle();
  186. final selection = _controller.selection;
  187. if (_selectionStyle != attrs || _selection != selection) {
  188. _selectionStyle = attrs;
  189. _selection = selection;
  190. notifyListeners();
  191. }
  192. }
  193. void _handleFocusChange() {
  194. assert(!_disposed);
  195. if (focusOwner == FocusOwner.none && !_selection.isCollapsed) {
  196. // Collapse selection if there is nothing focused.
  197. _controller.updateSelection(_selection.copyWith(
  198. baseOffset: _selection.extentOffset,
  199. extentOffset: _selection.extentOffset,
  200. ));
  201. }
  202. notifyListeners();
  203. }
  204. @override
  205. String toString() {
  206. return '$ZefyrScope#${shortHash(this)}';
  207. }
  208. }
  209. class ZefyrScopeAccess extends InheritedWidget {
  210. final ZefyrScope scope;
  211. ZefyrScopeAccess({Key key, @required this.scope, @required Widget child})
  212. : super(key: key, child: child);
  213. @override
  214. bool updateShouldNotify(ZefyrScopeAccess oldWidget) {
  215. return scope != oldWidget.scope;
  216. }
  217. }