浏览代码

Mark editable box for repaint on showCursor notification only if it contains caret

Anatoly Pulyaevskiy 6 年前
父节点
当前提交
aa30def386
共有 1 个文件被更改,包括 16 次插入6 次删除
  1. 16
    6
      packages/zefyr/lib/src/widgets/editable_box.dart

+ 16
- 6
packages/zefyr/lib/src/widgets/editable_box.dart 查看文件

102
   set showCursor(ValueNotifier<bool> value) {
102
   set showCursor(ValueNotifier<bool> value) {
103
     assert(value != null);
103
     assert(value != null);
104
     if (_showCursor == value) return;
104
     if (_showCursor == value) return;
105
-    // TODO: only mark to repaint if cursor is collapsed and inside this box.
106
-    if (attached) _showCursor.removeListener(markNeedsPaint);
105
+    if (attached) _showCursor.removeListener(markNeedsCursorPaint);
107
     _showCursor = value;
106
     _showCursor = value;
108
-    if (attached) _showCursor.addListener(markNeedsPaint);
107
+    if (attached) _showCursor.addListener(markNeedsCursorPaint);
109
     markNeedsPaint();
108
     markNeedsPaint();
110
   }
109
   }
111
 
110
 
131
   /// Returns `true` if current selection is collapsed, located within
130
   /// Returns `true` if current selection is collapsed, located within
132
   /// this paragraph and is visible according to tick timer.
131
   /// this paragraph and is visible according to tick timer.
133
   bool get isCaretVisible {
132
   bool get isCaretVisible {
133
+    return _showCursor.value && containsCaret;
134
+  }
135
+
136
+  /// Returns `true` if current selection is collapsed and located
137
+  /// within this paragraph.
138
+  bool get containsCaret {
134
     if (!_selection.isCollapsed) return false;
139
     if (!_selection.isCollapsed) return false;
135
-    if (!_showCursor.value) return false;
136
 
140
 
137
     final int start = node.documentOffset;
141
     final int start = node.documentOffset;
138
     final int end = start + node.length;
142
     final int end = start + node.length;
147
     return intersectsWithSelection(_selection);
151
     return intersectsWithSelection(_selection);
148
   }
152
   }
149
 
153
 
154
+  void markNeedsCursorPaint() {
155
+    if (containsCaret) {
156
+      markNeedsPaint();
157
+    }
158
+  }
159
+
150
   //
160
   //
151
   // Overridden members of RenderBox
161
   // Overridden members of RenderBox
152
   //
162
   //
154
   @override
164
   @override
155
   void attach(PipelineOwner owner) {
165
   void attach(PipelineOwner owner) {
156
     super.attach(owner);
166
     super.attach(owner);
157
-    _showCursor.addListener(markNeedsPaint);
167
+    _showCursor.addListener(markNeedsCursorPaint);
158
     _renderContext.addBox(this);
168
     _renderContext.addBox(this);
159
   }
169
   }
160
 
170
 
161
   @override
171
   @override
162
   void detach() {
172
   void detach() {
163
-    _showCursor.removeListener(markNeedsPaint);
173
+    _showCursor.removeListener(markNeedsCursorPaint);
164
     _renderContext.removeBox(this);
174
     _renderContext.removeBox(this);
165
     super.detach();
175
     super.detach();
166
   }
176
   }