Skip to content

Commit 091bea1

Browse files
committed
Add comments
1 parent 0c43c8a commit 091bea1

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

lib/src/widgets/raw_editor.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -513,7 +513,7 @@ class RawEditorState extends EditorState
513513
}
514514

515515
void _updateSelectionOverlayForScroll() {
516-
_selectionOverlay?.markNeedsBuild();
516+
_selectionOverlay?.updateForScroll();
517517
}
518518

519519
void _didChangeTextEditingValue([bool ignoreFocus = false]) {

lib/src/widgets/text_selection.dart

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,8 @@ class EditorTextSelectionOverlay {
187187
return;
188188
}
189189
handlesVisible = visible;
190+
// If we are in build state, it will be too late to update visibility.
191+
// We will need to schedule the build in next frame.
190192
if (SchedulerBinding.instance!.schedulerPhase ==
191193
SchedulerPhase.persistentCallbacks) {
192194
SchedulerBinding.instance!.addPostFrameCallback(markNeedsBuild);
@@ -195,6 +197,7 @@ class EditorTextSelectionOverlay {
195197
}
196198
}
197199

200+
/// Destroys the handles by removing them from overlay.
198201
void hideHandles() {
199202
if (_handles == null) {
200203
return;
@@ -204,13 +207,17 @@ class EditorTextSelectionOverlay {
204207
_handles = null;
205208
}
206209

210+
/// Hides the toolbar part of the overlay.
211+
///
212+
/// To hide the whole overlay, see [hide].
207213
void hideToolbar() {
208214
assert(toolbar != null);
209215
_toolbarController.stop();
210216
toolbar!.remove();
211217
toolbar = null;
212218
}
213219

220+
/// Shows the toolbar by inserting it into the [context]'s overlay.
214221
void showToolbar() {
215222
assert(toolbar == null);
216223
toolbar = OverlayEntry(builder: _buildToolbar);
@@ -242,6 +249,15 @@ class EditorTextSelectionOverlay {
242249
));
243250
}
244251

252+
/// Updates the overlay after the selection has changed.
253+
///
254+
/// If this method is called while the [SchedulerBinding.schedulerPhase] is
255+
/// [SchedulerPhase.persistentCallbacks], i.e. during the build, layout, or
256+
/// paint phases (see [WidgetsBinding.drawFrame]), then the update is delayed
257+
/// until the post-frame callbacks phase. Otherwise the update is done
258+
/// synchronously. This means that it is safe to call during builds, but also
259+
/// that if you do call this during a build, the UI will not update until the
260+
/// next frame (i.e. many milliseconds later).
245261
void update(TextEditingValue newValue) {
246262
if (value == newValue) {
247263
return;
@@ -291,6 +307,7 @@ class EditorTextSelectionOverlay {
291307
}
292308

293309
Widget _buildToolbar(BuildContext context) {
310+
// Find the horizontal midpoint, just above the selected text.
294311
final endpoints = renderObject!.getEndpointsForSelection(_selection);
295312

296313
final editingRegion = Rect.fromPoints(
@@ -341,6 +358,7 @@ class EditorTextSelectionOverlay {
341358
toolbar?.markNeedsBuild();
342359
}
343360

361+
/// Hides the entire overlay including the toolbar and the handles.
344362
void hide() {
345363
if (_handles != null) {
346364
_handles![0].remove();
@@ -352,11 +370,13 @@ class EditorTextSelectionOverlay {
352370
}
353371
}
354372

373+
/// Final cleanup.
355374
void dispose() {
356375
hide();
357376
_toolbarController.dispose();
358377
}
359378

379+
/// Builds the handles by inserting them into the [context]'s overlay.
360380
void showHandles() {
361381
assert(_handles == null);
362382
_handles = <OverlayEntry>[
@@ -371,8 +391,17 @@ class EditorTextSelectionOverlay {
371391
Overlay.of(context, rootOverlay: true, debugRequiredFor: debugRequiredFor)!
372392
.insertAll(_handles!);
373393
}
394+
395+
/// Causes the overlay to update its rendering.
396+
///
397+
/// This is intended to be called when the [renderObject] may have changed its
398+
/// text metrics (e.g. because the text was scrolled).
399+
void updateForScroll() {
400+
markNeedsBuild();
401+
}
374402
}
375403

404+
/// This widget represents a single draggable text selection handle.
376405
class _TextSelectionHandleOverlay extends StatefulWidget {
377406
const _TextSelectionHandleOverlay({
378407
required this.selection,

0 commit comments

Comments
 (0)