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

Side by Side Diff: examples/keyboard_client/keyboard_client.cc

Issue 1247903003: Add spellcheck and word suggestion to the prediction service (Closed) Base URL: https://github.com/domokit/mojo.git@master
Patch Set: fixed build problem Created 5 years, 4 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
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 #include <algorithm>
6
5 #include "base/bind.h" 7 #include "base/bind.h"
6 #include "base/macros.h" 8 #include "base/macros.h"
7 #include "mojo/application/application_runner_chromium.h" 9 #include "mojo/application/application_runner_chromium.h"
8 #include "mojo/gpu/gl_texture.h" 10 #include "mojo/gpu/gl_texture.h"
9 #include "mojo/gpu/texture_cache.h" 11 #include "mojo/gpu/texture_cache.h"
10 #include "mojo/gpu/texture_uploader.h" 12 #include "mojo/gpu/texture_uploader.h"
11 #include "mojo/public/c/system/main.h" 13 #include "mojo/public/c/system/main.h"
12 #include "mojo/public/cpp/application/application_delegate.h" 14 #include "mojo/public/cpp/application/application_delegate.h"
13 #include "mojo/public/cpp/application/application_impl.h" 15 #include "mojo/public/cpp/application/application_impl.h"
14 #include "mojo/public/cpp/application/connect.h" 16 #include "mojo/public/cpp/application/connect.h"
(...skipping 197 matching lines...) Expand 10 before | Expand all | Expand 10 after
212 void CommitCompletion(keyboard::CompletionDataPtr completion) override { 214 void CommitCompletion(keyboard::CompletionDataPtr completion) override {
213 DrawText(); 215 DrawText();
214 } 216 }
215 217
216 void CommitCorrection(keyboard::CorrectionDataPtr correction) override { 218 void CommitCorrection(keyboard::CorrectionDataPtr correction) override {
217 DrawText(); 219 DrawText();
218 } 220 }
219 221
220 void CommitText(const mojo::String& text, 222 void CommitText(const mojo::String& text,
221 int32_t new_cursor_position) override { 223 int32_t new_cursor_position) override {
222 std::string combined(text_[1]); 224 text_.append(text);
223 combined.append(text);
224 SkRect bounds;
225 text_paint_.measureText(combined.data(), combined.size(), &bounds);
226 if (bounds.width() > text_view_->bounds().width) {
227 text_[0] = text_[1];
228 text_[1] = text;
229 } else {
230 text_[1].append(text);
231 }
232 DrawText(); 225 DrawText();
233 } 226 }
234 227
235 void DeleteSurroundingText(int32_t before_length, 228 void DeleteSurroundingText(int32_t before_length,
236 int32_t after_length) override { 229 int32_t after_length) override {
237 // treat negative and zero |before_length| values as no-op. 230 // treat negative and zero |before_length| values as no-op.
238 if (before_length > 0) { 231 if (before_length > 0) {
239 if (before_length > static_cast<int32_t>(text_[1].size())) { 232 if (before_length > static_cast<int32_t>(text_.size())) {
240 before_length = text_[1].size(); 233 before_length = text_.size();
241 } 234 }
242 text_[1].erase(text_[1].end() - before_length, text_[1].end()); 235 text_.erase(text_.end() - before_length, text_.end());
243 } 236 }
244 DrawText(); 237 DrawText();
245 } 238 }
246 239
247 void SetComposingRegion(int32_t start, int32_t end) override { DrawText(); } 240 void SetComposingRegion(int32_t start, int32_t end) override { DrawText(); }
248 241
249 void SetComposingText(const mojo::String& text, 242 void SetComposingText(const mojo::String& text,
250 int32_t new_cursor_position) override { 243 int32_t new_cursor_position) override {
251 DrawText(); 244 DrawText();
252 } 245 }
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after
309 if (text_view_texture_uploader_) { 302 if (text_view_texture_uploader_) {
310 text_view_texture_uploader_->Draw(text_view_surface_id_); 303 text_view_texture_uploader_->Draw(text_view_surface_id_);
311 } 304 }
312 } 305 }
313 306
314 void DrawTextView(SkCanvas* canvas) { 307 void DrawTextView(SkCanvas* canvas) {
315 canvas->clear(SK_ColorRED); 308 canvas->clear(SK_ColorRED);
316 309
317 float row_height = text_view_height_ / 2.0f; 310 float row_height = text_view_height_ / 2.0f;
318 float text_baseline_offset = row_height / 5.0f; 311 float text_baseline_offset = row_height / 5.0f;
312 if (!text_.empty()) {
313 SkRect sk_rect;
314 text_paint_.measureText((const void*)(text_.c_str()), text_.length(),
315 &sk_rect);
319 316
320 if (!text_[0].empty()) { 317 if (sk_rect.width() > text_view_->bounds().width) {
321 canvas->drawText(text_[0].data(), text_[0].size(), 0.0f, 318 std::string reverse_text = text_;
322 row_height - text_baseline_offset, text_paint_); 319 std::reverse(reverse_text.begin(), reverse_text.end());
323 }
324 320
325 if (!text_[1].empty()) { 321 size_t processed1 = text_paint_.breakText(
326 canvas->drawText(text_[1].data(), text_[1].size(), 0.0f, 322 (const void*)(reverse_text.c_str()), strlen(reverse_text.c_str()),
327 (2.0f * row_height) - text_baseline_offset, text_paint_); 323 text_view_->bounds().width);
324 size_t processed2 = text_paint_.breakText(
325 (const void*)(reverse_text.c_str() + processed1),
326 strlen(reverse_text.c_str()) - processed1,
327 text_view_->bounds().width);
328 if (processed1 + processed2 < text_.length()) {
329 DrawSecondLine(canvas, text_.length() - processed1, processed1,
330 row_height, text_baseline_offset);
331 DrawFirstLine(canvas, text_.length() - processed1 - processed2,
332 processed2, row_height, text_baseline_offset);
333 } else {
334 size_t processed3 =
335 text_paint_.breakText((const void*)(text_.c_str()),
336 text_.length(), text_view_->bounds().width);
337 DrawFirstLine(canvas, 0, processed3, row_height,
338 text_baseline_offset);
339 DrawSecondLine(canvas, processed3, text_.length() - processed3,
340 row_height, text_baseline_offset);
341 }
342 } else {
343 DrawSecondLine(canvas, 0, text_.length(), row_height,
344 text_baseline_offset);
345 }
328 } 346 }
329 347
330 canvas->flush(); 348 canvas->flush();
331 } 349 }
332 350
351 void DrawFirstLine(SkCanvas* canvas,
352 const size_t offset,
353 const size_t length,
354 const float row_height,
355 const float text_baseline_offset) {
356 canvas->drawText(text_.data() + offset, length, 0.0f,
357 row_height - text_baseline_offset, text_paint_);
358 }
359
360 void DrawSecondLine(SkCanvas* canvas,
361 const size_t offset,
362 const size_t length,
363 const float row_height,
364 const float text_baseline_offset) {
365 canvas->drawText(text_.data() + offset, length, 0.0f,
366 (2.0f * row_height) - text_baseline_offset, text_paint_);
367 }
368
333 void DrawRootView(SkCanvas* canvas) { 369 void DrawRootView(SkCanvas* canvas) {
334 canvas->clear(SK_ColorDKGRAY); 370 canvas->clear(SK_ColorDKGRAY);
335 canvas->flush(); 371 canvas->flush();
336 } 372 }
337 373
338 private: 374 private:
339 mojo::Binding<keyboard::KeyboardClient> binding_; 375 mojo::Binding<keyboard::KeyboardClient> binding_;
340 keyboard::KeyboardServicePtr keyboard_; 376 keyboard::KeyboardServicePtr keyboard_;
341 scoped_ptr<mojo::ViewManagerClientFactory> view_manager_client_factory_; 377 scoped_ptr<mojo::ViewManagerClientFactory> view_manager_client_factory_;
342 mojo::Shell* shell_; 378 mojo::Shell* shell_;
343 mojo::View* keyboard_view_; 379 mojo::View* keyboard_view_;
344 mojo::View* text_view_; 380 mojo::View* text_view_;
345 mojo::View* root_view_; 381 mojo::View* root_view_;
346 uint32_t root_view_surface_id_; 382 uint32_t root_view_surface_id_;
347 uint32_t text_view_surface_id_; 383 uint32_t text_view_surface_id_;
348 uint32_t id_namespace_; 384 uint32_t id_namespace_;
349 base::WeakPtr<mojo::GLContext> gl_context_; 385 base::WeakPtr<mojo::GLContext> gl_context_;
350 mojo::SurfacePtr surface_; 386 mojo::SurfacePtr surface_;
351 scoped_ptr<mojo::TextureCache> texture_cache_; 387 scoped_ptr<mojo::TextureCache> texture_cache_;
352 scoped_ptr<ViewTextureUploader> text_view_texture_uploader_; 388 scoped_ptr<ViewTextureUploader> text_view_texture_uploader_;
353 scoped_ptr<ViewTextureUploader> root_view_texture_uploader_; 389 scoped_ptr<ViewTextureUploader> root_view_texture_uploader_;
354 int text_view_height_; 390 int text_view_height_;
355 std::string text_[2]; 391 std::string text_;
356 SkPaint text_paint_; 392 SkPaint text_paint_;
357 base::WeakPtrFactory<KeyboardDelegate> weak_factory_; 393 base::WeakPtrFactory<KeyboardDelegate> weak_factory_;
358 394
359 DISALLOW_COPY_AND_ASSIGN(KeyboardDelegate); 395 DISALLOW_COPY_AND_ASSIGN(KeyboardDelegate);
360 }; 396 };
361 397
362 } // namespace examples 398 } // namespace examples
363 399
364 MojoResult MojoMain(MojoHandle application_request) { 400 MojoResult MojoMain(MojoHandle application_request) {
365 mojo::ApplicationRunnerChromium runner(new examples::KeyboardDelegate); 401 mojo::ApplicationRunnerChromium runner(new examples::KeyboardDelegate);
366 return runner.Run(application_request); 402 return runner.Run(application_request);
367 } 403 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698