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 #include "base/bind.h" | 5 #include "base/bind.h" |
6 #include "base/macros.h" | 6 #include "base/macros.h" |
7 #include "mojo/application/application_runner_chromium.h" | 7 #include "mojo/application/application_runner_chromium.h" |
8 #include "mojo/gpu/gl_texture.h" | 8 #include "mojo/gpu/gl_texture.h" |
9 #include "mojo/gpu/texture_cache.h" | 9 #include "mojo/gpu/texture_cache.h" |
10 #include "mojo/gpu/texture_uploader.h" | 10 #include "mojo/gpu/texture_uploader.h" |
(...skipping 201 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
212 void CommitCompletion(keyboard::CompletionDataPtr completion) override { | 212 void CommitCompletion(keyboard::CompletionDataPtr completion) override { |
213 DrawText(); | 213 DrawText(); |
214 } | 214 } |
215 | 215 |
216 void CommitCorrection(keyboard::CorrectionDataPtr correction) override { | 216 void CommitCorrection(keyboard::CorrectionDataPtr correction) override { |
217 DrawText(); | 217 DrawText(); |
218 } | 218 } |
219 | 219 |
220 void CommitText(const mojo::String& text, | 220 void CommitText(const mojo::String& text, |
221 int32_t new_cursor_position) override { | 221 int32_t new_cursor_position) override { |
222 std::string combined(text_[1]); | 222 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(); | 223 DrawText(); |
233 } | 224 } |
234 | 225 |
235 void DeleteSurroundingText(int32_t before_length, | 226 void DeleteSurroundingText(int32_t before_length, |
236 int32_t after_length) override { | 227 int32_t after_length) override { |
237 // treat negative and zero |before_length| values as no-op. | 228 // treat negative and zero |before_length| values as no-op. |
238 if (before_length > 0) { | 229 if (before_length > 0) { |
239 if (before_length > static_cast<int32_t>(text_[1].size())) { | 230 if (before_length > static_cast<int32_t>(text_.size())) { |
240 before_length = text_[1].size(); | 231 before_length = text_.size(); |
241 } | 232 } |
242 text_[1].erase(text_[1].end() - before_length, text_[1].end()); | 233 text_.erase(text_.end() - before_length, text_.end()); |
243 } | 234 } |
244 DrawText(); | 235 DrawText(); |
245 } | 236 } |
246 | 237 |
247 void SetComposingRegion(int32_t start, int32_t end) override { DrawText(); } | 238 void SetComposingRegion(int32_t start, int32_t end) override { DrawText(); } |
248 | 239 |
249 void SetComposingText(const mojo::String& text, | 240 void SetComposingText(const mojo::String& text, |
250 int32_t new_cursor_position) override { | 241 int32_t new_cursor_position) override { |
251 DrawText(); | 242 DrawText(); |
252 } | 243 } |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
309 if (text_view_texture_uploader_) { | 300 if (text_view_texture_uploader_) { |
310 text_view_texture_uploader_->Draw(text_view_surface_id_); | 301 text_view_texture_uploader_->Draw(text_view_surface_id_); |
311 } | 302 } |
312 } | 303 } |
313 | 304 |
314 void DrawTextView(SkCanvas* canvas) { | 305 void DrawTextView(SkCanvas* canvas) { |
315 canvas->clear(SK_ColorRED); | 306 canvas->clear(SK_ColorRED); |
316 | 307 |
317 float row_height = text_view_height_ / 2.0f; | 308 float row_height = text_view_height_ / 2.0f; |
318 float text_baseline_offset = row_height / 5.0f; | 309 float text_baseline_offset = row_height / 5.0f; |
310 if (!text_.empty()) { | |
311 SkRect sk_rect; | |
312 text_paint_.measureText((const void*)(text_.c_str()), | |
313 strlen(text_.c_str()), &sk_rect); | |
319 | 314 |
320 if (!text_[0].empty()) { | 315 if (sk_rect.width() > text_view_->bounds().width) { |
321 canvas->drawText(text_[0].data(), text_[0].size(), 0.0f, | 316 char reverse_text[text_.length() + 1]; |
322 row_height - text_baseline_offset, text_paint_); | 317 for (unsigned int i = 0; i < text_.length(); i++) { |
323 } | 318 reverse_text[i] = text_[text_.length() - i - 1]; |
APW
2015/07/31 22:36:43
use either a reverse iterator or create a copy of
riajiang
2015/08/01 01:23:08
Done.
| |
319 } | |
320 reverse_text[text_.length()] = '\0'; | |
324 | 321 |
325 if (!text_[1].empty()) { | 322 size_t processed1 = text_paint_.breakText((const void*)(reverse_text), |
326 canvas->drawText(text_[1].data(), text_[1].size(), 0.0f, | 323 strlen(reverse_text), |
327 (2.0f * row_height) - text_baseline_offset, text_paint_); | 324 text_view_->bounds().width); |
325 size_t processed2 = text_paint_.breakText( | |
326 (const void*)(reverse_text + processed1), | |
327 strlen(reverse_text) - processed1, text_view_->bounds().width); | |
328 if (processed1 + processed2 < text_.length()) { | |
329 canvas->drawText( | |
330 text_.data() + (text_.length() - processed1), processed1, 0.0f, | |
331 (2.0f * row_height) - text_baseline_offset, text_paint_); | |
332 canvas->drawText( | |
333 text_.data() + (text_.length() - processed1 - processed2), | |
334 processed2, 0.0f, row_height - text_baseline_offset, text_paint_); | |
335 } else { | |
336 size_t processed3 = text_paint_.breakText( | |
337 (const void*)(text_.c_str()), strlen(text_.c_str()), | |
338 text_view_->bounds().width); | |
339 canvas->drawText(text_.data(), processed3, 0.0f, | |
340 row_height - text_baseline_offset, text_paint_); | |
341 canvas->drawText( | |
342 text_.data() + processed3, strlen(text_.c_str()) - processed3, | |
343 0.0f, (2.0f * row_height) - text_baseline_offset, text_paint_); | |
344 } | |
345 } else { | |
346 canvas->drawText(text_.data(), text_.size(), 0.0f, | |
APW
2015/07/31 22:36:43
just to make things a bit easier to understand, ca
riajiang
2015/08/01 01:23:08
Done.
| |
347 (2.0f * row_height) - text_baseline_offset, | |
348 text_paint_); | |
349 } | |
328 } | 350 } |
329 | 351 |
330 canvas->flush(); | 352 canvas->flush(); |
331 } | 353 } |
332 | 354 |
333 void DrawRootView(SkCanvas* canvas) { | 355 void DrawRootView(SkCanvas* canvas) { |
334 canvas->clear(SK_ColorDKGRAY); | 356 canvas->clear(SK_ColorDKGRAY); |
335 canvas->flush(); | 357 canvas->flush(); |
336 } | 358 } |
337 | 359 |
338 private: | 360 private: |
339 mojo::Binding<keyboard::KeyboardClient> binding_; | 361 mojo::Binding<keyboard::KeyboardClient> binding_; |
340 keyboard::KeyboardServicePtr keyboard_; | 362 keyboard::KeyboardServicePtr keyboard_; |
341 scoped_ptr<mojo::ViewManagerClientFactory> view_manager_client_factory_; | 363 scoped_ptr<mojo::ViewManagerClientFactory> view_manager_client_factory_; |
342 mojo::Shell* shell_; | 364 mojo::Shell* shell_; |
343 mojo::View* keyboard_view_; | 365 mojo::View* keyboard_view_; |
344 mojo::View* text_view_; | 366 mojo::View* text_view_; |
345 mojo::View* root_view_; | 367 mojo::View* root_view_; |
346 uint32_t root_view_surface_id_; | 368 uint32_t root_view_surface_id_; |
347 uint32_t text_view_surface_id_; | 369 uint32_t text_view_surface_id_; |
348 uint32_t id_namespace_; | 370 uint32_t id_namespace_; |
349 base::WeakPtr<mojo::GLContext> gl_context_; | 371 base::WeakPtr<mojo::GLContext> gl_context_; |
350 mojo::SurfacePtr surface_; | 372 mojo::SurfacePtr surface_; |
351 scoped_ptr<mojo::TextureCache> texture_cache_; | 373 scoped_ptr<mojo::TextureCache> texture_cache_; |
352 scoped_ptr<ViewTextureUploader> text_view_texture_uploader_; | 374 scoped_ptr<ViewTextureUploader> text_view_texture_uploader_; |
353 scoped_ptr<ViewTextureUploader> root_view_texture_uploader_; | 375 scoped_ptr<ViewTextureUploader> root_view_texture_uploader_; |
354 int text_view_height_; | 376 int text_view_height_; |
355 std::string text_[2]; | 377 std::string text_; |
356 SkPaint text_paint_; | 378 SkPaint text_paint_; |
357 base::WeakPtrFactory<KeyboardDelegate> weak_factory_; | 379 base::WeakPtrFactory<KeyboardDelegate> weak_factory_; |
358 | 380 |
359 DISALLOW_COPY_AND_ASSIGN(KeyboardDelegate); | 381 DISALLOW_COPY_AND_ASSIGN(KeyboardDelegate); |
360 }; | 382 }; |
361 | 383 |
362 } // namespace examples | 384 } // namespace examples |
363 | 385 |
364 MojoResult MojoMain(MojoHandle application_request) { | 386 MojoResult MojoMain(MojoHandle application_request) { |
365 mojo::ApplicationRunnerChromium runner(new examples::KeyboardDelegate); | 387 mojo::ApplicationRunnerChromium runner(new examples::KeyboardDelegate); |
366 return runner.Run(application_request); | 388 return runner.Run(application_request); |
367 } | 389 } |
OLD | NEW |