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

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

Issue 995073002: Make Sky's EditableText mostly work (Closed) Base URL: git@github.com:domokit/mojo.git@master
Patch Set: More comments 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
(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 StringChangedCallback(EditableString updated);
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 StringChangedCallback onChanged;
27
28 KeyboardClientStub stub;
29
30 EditableString({this.text: '', this.onChanged}) {
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 // Returns the range of |newText| inside this string.
53 TextRange _replace(TextRange range, String newText) {
54 if (!range.isValid) {
55 int start = text.length;
56 int end = start + newText.length;
57 text += newText;
eseidel 2015/03/10 18:39:29 You should make this more self-documenting.
58 return new TextRange(start: start, end: end);
59 }
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 void commitCompletion(CompletionData completion) {
70 // TODO(abarth): Not implemented.
71 }
72
73 void commitCorrection(CorrectionData correction) {
74 // TODO(abarth): Not implemented.
75 }
76
77 void commitText(String text, int newCursorPosition) {
78 // TODO(abarth): Why is |newCursorPosition| always 1?
79 TextRange committedRange = _replace(composing, text);
80 selection = new TextRange.collapsed(committedRange.end);
81 composing = const TextRange.empty();
82 onChanged(this);
83 }
84
85 void deleteSurroundingText(int beforeLength, int afterLength) {
86 TextRange beforeRange = new TextRange(start: selection.start - beforeLength,
87 end: selection.start);
88 TextRange afterRange = new TextRange(start: selection.end,
89 end: selection.end + afterLength);
90 _delete(afterRange);
91 _delete(beforeRange);
92 selection = new TextRange(start: selection.start - beforeLength,
93 end: selection.end - beforeLength);
94 }
95
96 void setComposingRegion(int start, int end) {
97 composing = new TextRange(start: start, end: end);
98 onChanged(this);
99 }
100
101 void setComposingText(String text, int newCursorPosition) {
102 // TODO(abarth): Why is |newCursorPosition| always 1?
103 composing = _replace(composing, text);
104 selection = new TextRange.collapsed(composing.end);
105 onChanged(this);
106 }
107
108 void setSelection(int start, int end) {
109 selection = new TextRange(start: start, end: end);
110 }
111 }
112
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