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

Side by Side Diff: sky/framework/editing/editable_string.dart

Issue 1132063007: Rationalize Dart mojo and sky package structure (Closed) Base URL: https://github.com/domokit/mojo.git@master
Patch Set: Created 5 years, 7 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 | « sky/framework/debug/tracing.dart ('k') | sky/framework/editing/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:mojom/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)
15 : start = position,
16 end = position;
17 const TextRange.empty()
18 : start = -1,
19 end = -1;
20
21 bool get isValid => start >= 0 && end >= 0;
22 bool get isCollapsed => start == end;
23 }
24
25 class EditableString implements KeyboardClient {
26 String text;
27 TextRange composing = const TextRange.empty();
28 TextRange selection = const TextRange.empty();
29
30 final StringUpdated onUpdated;
31
32 KeyboardClientStub stub;
33
34 EditableString({this.text: '', this.onUpdated}) {
35 stub = new KeyboardClientStub.unbound()..impl = this;
36 }
37
38 String textBefore(TextRange range) {
39 return text.substring(0, range.start);
40 }
41
42 String textAfter(TextRange range) {
43 return text.substring(range.end);
44 }
45
46 String textInside(TextRange range) {
47 return text.substring(range.start, range.end);
48 }
49
50 void _delete(TextRange range) {
51 if (range.isCollapsed || !range.isValid) return;
52 text = textBefore(range) + textAfter(range);
53 }
54
55 TextRange _append(String newText) {
56 int start = text.length;
57 text += newText;
58 return new TextRange(start: start, end: start + newText.length);
59 }
60
61 TextRange _replace(TextRange range, String newText) {
62 assert(range.isValid);
63
64 String before = textBefore(range);
65 String after = textAfter(range);
66
67 text = before + newText + after;
68 return new TextRange(
69 start: before.length, end: before.length + newText.length);
70 }
71
72 TextRange _replaceOrAppend(TextRange range, String newText) {
73 if (!range.isValid) return _append(newText);
74 return _replace(range, newText);
75 }
76
77 void commitCompletion(CompletionData completion) {
78 // TODO(abarth): Not implemented.
79 }
80
81 void commitCorrection(CorrectionData correction) {
82 // TODO(abarth): Not implemented.
83 }
84
85 void commitText(String text, int newCursorPosition) {
86 // TODO(abarth): Why is |newCursorPosition| always 1?
87 TextRange committedRange = _replaceOrAppend(composing, text);
88 selection = new TextRange.collapsed(committedRange.end);
89 composing = const TextRange.empty();
90 onUpdated();
91 }
92
93 void deleteSurroundingText(int beforeLength, int afterLength) {
94 TextRange beforeRange = new TextRange(
95 start: selection.start - beforeLength, end: selection.start);
96 TextRange afterRange =
97 new TextRange(start: selection.end, end: selection.end + afterLength);
98 _delete(afterRange);
99 _delete(beforeRange);
100 selection = new TextRange(
101 start: selection.start - beforeLength,
102 end: selection.end - beforeLength);
103 onUpdated();
104 }
105
106 void setComposingRegion(int start, int end) {
107 composing = new TextRange(start: start, end: end);
108 onUpdated();
109 }
110
111 void setComposingText(String text, int newCursorPosition) {
112 // TODO(abarth): Why is |newCursorPosition| always 1?
113 composing = _replaceOrAppend(composing, text);
114 selection = new TextRange.collapsed(composing.end);
115 onUpdated();
116 }
117
118 void setSelection(int start, int end) {
119 selection = new TextRange(start: start, end: end);
120 onUpdated();
121 }
122 }
OLDNEW
« no previous file with comments | « sky/framework/debug/tracing.dart ('k') | sky/framework/editing/editable_text.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698