| OLD | NEW |
| (Empty) |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 import 'package:sky/services/keyboard/keyboard.mojom.dart'; | |
| 6 | |
| 7 typedef void StringUpdated(); | |
| 8 | |
| 9 class TextRange { | |
| 10 final int start; | |
| 11 final int end; | |
| 12 | |
| 13 TextRange({this.start, this.end}); | |
| 14 TextRange.collapsed(int position) : start = position, end = position; | |
| 15 const TextRange.empty() : start = -1, end = -1; | |
| 16 | |
| 17 bool get isValid => start >= 0 && end >= 0; | |
| 18 bool get isCollapsed => start == end; | |
| 19 } | |
| 20 | |
| 21 class EditableString implements KeyboardClient { | |
| 22 String text; | |
| 23 TextRange composing = const TextRange.empty(); | |
| 24 TextRange selection = const TextRange.empty(); | |
| 25 | |
| 26 final StringUpdated onUpdated; | |
| 27 | |
| 28 KeyboardClientStub stub; | |
| 29 | |
| 30 EditableString({this.text: '', this.onUpdated}) { | |
| 31 stub = new KeyboardClientStub.unbound()..impl = this; | |
| 32 } | |
| 33 | |
| 34 String textBefore(TextRange range) { | |
| 35 return text.substring(0, range.start); | |
| 36 } | |
| 37 | |
| 38 String textAfter(TextRange range) { | |
| 39 return text.substring(range.end); | |
| 40 } | |
| 41 | |
| 42 String textInside(TextRange range) { | |
| 43 return text.substring(range.start, range.end); | |
| 44 } | |
| 45 | |
| 46 void _delete(TextRange range) { | |
| 47 if (range.isCollapsed || !range.isValid) | |
| 48 return; | |
| 49 text = textBefore(range) + textAfter(range); | |
| 50 } | |
| 51 | |
| 52 TextRange _append(String newText) { | |
| 53 int start = text.length; | |
| 54 text += newText; | |
| 55 return new TextRange(start: start, end: start + newText.length); | |
| 56 } | |
| 57 | |
| 58 TextRange _replace(TextRange range, String newText) { | |
| 59 assert(range.isValid); | |
| 60 | |
| 61 String before = textBefore(range); | |
| 62 String after = textAfter(range); | |
| 63 | |
| 64 text = before + newText + after; | |
| 65 return new TextRange(start: before.length, | |
| 66 end: before.length + newText.length); | |
| 67 } | |
| 68 | |
| 69 TextRange _replaceOrAppend(TextRange range, String newText) { | |
| 70 if (!range.isValid) | |
| 71 return _append(newText); | |
| 72 return _replace(range, newText); | |
| 73 } | |
| 74 | |
| 75 void commitCompletion(CompletionData completion) { | |
| 76 // TODO(abarth): Not implemented. | |
| 77 } | |
| 78 | |
| 79 void commitCorrection(CorrectionData correction) { | |
| 80 // TODO(abarth): Not implemented. | |
| 81 } | |
| 82 | |
| 83 void commitText(String text, int newCursorPosition) { | |
| 84 // TODO(abarth): Why is |newCursorPosition| always 1? | |
| 85 TextRange committedRange = _replaceOrAppend(composing, text); | |
| 86 selection = new TextRange.collapsed(committedRange.end); | |
| 87 composing = const TextRange.empty(); | |
| 88 onUpdated(); | |
| 89 } | |
| 90 | |
| 91 void deleteSurroundingText(int beforeLength, int afterLength) { | |
| 92 TextRange beforeRange = new TextRange(start: selection.start - beforeLength, | |
| 93 end: selection.start); | |
| 94 TextRange afterRange = new TextRange(start: selection.end, | |
| 95 end: selection.end + afterLength); | |
| 96 _delete(afterRange); | |
| 97 _delete(beforeRange); | |
| 98 selection = new TextRange(start: selection.start - beforeLength, | |
| 99 end: selection.end - beforeLength); | |
| 100 onUpdated(); | |
| 101 } | |
| 102 | |
| 103 void setComposingRegion(int start, int end) { | |
| 104 composing = new TextRange(start: start, end: end); | |
| 105 onUpdated(); | |
| 106 } | |
| 107 | |
| 108 void setComposingText(String text, int newCursorPosition) { | |
| 109 // TODO(abarth): Why is |newCursorPosition| always 1? | |
| 110 composing = _replaceOrAppend(composing, text); | |
| 111 selection = new TextRange.collapsed(composing.end); | |
| 112 onUpdated(); | |
| 113 } | |
| 114 | |
| 115 void setSelection(int start, int end) { | |
| 116 selection = new TextRange(start: start, end: end); | |
| 117 onUpdated(); | |
| 118 } | |
| 119 } | |
| OLD | NEW |