Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(195)

Side by Side Diff: sky/examples/editor/editable_string.dart

Issue 999553002: Make a material design Input component (Closed) Base URL: git@github.com:domokit/mojo.git@master
Patch Set: Created 5 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | sky/examples/editor/editable_text.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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:sky/services/keyboard/keyboard.mojom.dart';
6 6
7 typedef void StringChangedCallback(EditableString updated); 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) : start = position, end = position;
15 const TextRange.empty() : start = -1, end = -1; 15 const TextRange.empty() : start = -1, end = -1;
16 16
17 bool get isValid => start >= 0 && end >= 0; 17 bool get isValid => start >= 0 && end >= 0;
18 bool get isCollapsed => start == end; 18 bool get isCollapsed => start == end;
19 } 19 }
20 20
21 class EditableString implements KeyboardClient { 21 class EditableString implements KeyboardClient {
22 String text; 22 String text;
23 TextRange composing = const TextRange.empty(); 23 TextRange composing = const TextRange.empty();
24 TextRange selection = const TextRange.empty(); 24 TextRange selection = const TextRange.empty();
25 25
26 final StringChangedCallback onChanged; 26 final StringUpdated onUpdated;
27 27
28 KeyboardClientStub stub; 28 KeyboardClientStub stub;
29 29
30 EditableString({this.text: '', this.onChanged}) { 30 EditableString({this.text: '', this.onUpdated}) {
31 stub = new KeyboardClientStub.unbound()..impl = this; 31 stub = new KeyboardClientStub.unbound()..impl = this;
32 } 32 }
33 33
34 String textBefore(TextRange range) { 34 String textBefore(TextRange range) {
35 return text.substring(0, range.start); 35 return text.substring(0, range.start);
36 } 36 }
37 37
38 String textAfter(TextRange range) { 38 String textAfter(TextRange range) {
39 return text.substring(range.end); 39 return text.substring(range.end);
40 } 40 }
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
78 78
79 void commitCorrection(CorrectionData correction) { 79 void commitCorrection(CorrectionData correction) {
80 // TODO(abarth): Not implemented. 80 // TODO(abarth): Not implemented.
81 } 81 }
82 82
83 void commitText(String text, int newCursorPosition) { 83 void commitText(String text, int newCursorPosition) {
84 // TODO(abarth): Why is |newCursorPosition| always 1? 84 // TODO(abarth): Why is |newCursorPosition| always 1?
85 TextRange committedRange = _replaceOrAppend(composing, text); 85 TextRange committedRange = _replaceOrAppend(composing, text);
86 selection = new TextRange.collapsed(committedRange.end); 86 selection = new TextRange.collapsed(committedRange.end);
87 composing = const TextRange.empty(); 87 composing = const TextRange.empty();
88 onChanged(this); 88 onUpdated();
89 } 89 }
90 90
91 void deleteSurroundingText(int beforeLength, int afterLength) { 91 void deleteSurroundingText(int beforeLength, int afterLength) {
92 TextRange beforeRange = new TextRange(start: selection.start - beforeLength, 92 TextRange beforeRange = new TextRange(start: selection.start - beforeLength,
93 end: selection.start); 93 end: selection.start);
94 TextRange afterRange = new TextRange(start: selection.end, 94 TextRange afterRange = new TextRange(start: selection.end,
95 end: selection.end + afterLength); 95 end: selection.end + afterLength);
96 _delete(afterRange); 96 _delete(afterRange);
97 _delete(beforeRange); 97 _delete(beforeRange);
98 selection = new TextRange(start: selection.start - beforeLength, 98 selection = new TextRange(start: selection.start - beforeLength,
99 end: selection.end - beforeLength); 99 end: selection.end - beforeLength);
100 onChanged(this); 100 onUpdated();
101 } 101 }
102 102
103 void setComposingRegion(int start, int end) { 103 void setComposingRegion(int start, int end) {
104 composing = new TextRange(start: start, end: end); 104 composing = new TextRange(start: start, end: end);
105 onChanged(this); 105 onUpdated();
106 } 106 }
107 107
108 void setComposingText(String text, int newCursorPosition) { 108 void setComposingText(String text, int newCursorPosition) {
109 // TODO(abarth): Why is |newCursorPosition| always 1? 109 // TODO(abarth): Why is |newCursorPosition| always 1?
110 composing = _replaceOrAppend(composing, text); 110 composing = _replaceOrAppend(composing, text);
111 selection = new TextRange.collapsed(composing.end); 111 selection = new TextRange.collapsed(composing.end);
112 onChanged(this); 112 onUpdated();
113 } 113 }
114 114
115 void setSelection(int start, int end) { 115 void setSelection(int start, int end) {
116 selection = new TextRange(start: start, end: end); 116 selection = new TextRange(start: start, end: end);
117 onChanged(this); 117 onUpdated();
118 } 118 }
119 } 119 }
OLDNEW
« no previous file with comments | « no previous file | sky/examples/editor/editable_text.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698