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