Chromium Code Reviews| Index: services/keyboard_native/text_update_key.cc |
| diff --git a/services/keyboard_native/text_update_key.cc b/services/keyboard_native/text_update_key.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..c29deff01b5efc9eb1e4645f69d4366b810a71b1 |
| --- /dev/null |
| +++ b/services/keyboard_native/text_update_key.cc |
| @@ -0,0 +1,80 @@ |
| +// Copyright 2015 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "services/keyboard_native/text_update_key.h" |
| +#include "third_party/skia/include/core/SkCanvas.h" |
| +#include "third_party/skia/include/core/SkPaint.h" |
| +#include "third_party/skia/include/core/SkScalar.h" |
| +#include "ui/gfx/geometry/rect_f.h" |
| + |
| +namespace keyboard { |
| + |
| +TextUpdateKey::TextUpdateKey( |
| + std::string text, |
| + base::Callback<void(const TextUpdateKey&)> touch_up_callback) |
| + : text_(text), touch_up_callback_(touch_up_callback) { |
| +} |
| + |
| +TextUpdateKey::~TextUpdateKey() { |
| +} |
| + |
| +// Key implementation. |
| +void TextUpdateKey::Draw(SkCanvas* canvas, |
| + SkPaint& paint, |
| + const gfx::RectF& rect) { |
| + std::string text_to_fit = text_; |
| + SkRect bounds; |
| + paint.measureText((const void*)(text_.c_str()), strlen(text_.c_str()), |
| + &bounds); |
| + int text_need_scale = 0; |
|
APW
2015/07/31 22:36:42
boolean?
riajiang
2015/08/01 01:23:08
Done.
|
| + if (bounds.width() > rect.width() * 0.8) { |
| + text_need_scale = 1; |
| + |
| + paint.setTextScaleX(SkFloatToScalar(0.6)); |
|
APW
2015/07/31 22:36:43
Can you try using copy constructor to make copy of
riajiang
2015/08/01 01:23:08
Made a copy of the paint and changed it back to co
|
| + paint.measureText((const void*)(text_.c_str()), strlen(text_.c_str()), |
| + &bounds); |
| + paint.setTextScaleX(SkIntToScalar(1)); |
| + if (bounds.width() > rect.width() * 0.8) { |
| + int dot_size = SkScalarTruncToInt((SkScalarToFloat(bounds.width()) - |
|
APW
2015/07/31 22:36:43
dot_count
riajiang
2015/08/01 01:23:08
Done.
|
| + SkScalarToFloat(rect.width()) * 0.8) / |
| + SkScalarToFloat(bounds.width()) * |
| + strlen(text_.c_str())) + |
| + 1; |
| + if (dot_size >= 3) { |
|
APW
2015/07/31 22:36:43
You can do this better in a loop.
riajiang
2015/08/01 01:23:08
Done.
|
| + text_to_fit = |
| + "..." + |
| + text_to_fit.substr(dot_size, text_to_fit.length() - dot_size); |
| + } else if (dot_size == 1) { |
| + text_to_fit = |
| + "." + text_to_fit.substr(dot_size, text_to_fit.length() - dot_size); |
| + } else if (dot_size == 2) { |
| + text_to_fit = |
| + ".." + |
| + text_to_fit.substr(dot_size, text_to_fit.length() - dot_size); |
| + } |
| + } |
| + } |
| + |
| + float text_baseline_offset = rect.height() / 5.0f; |
| + if (text_need_scale) |
| + paint.setTextScaleX(SkFloatToScalar(0.6)); |
|
APW
2015/07/31 22:36:42
use braces around this if
riajiang
2015/08/01 01:23:08
Done.
|
| + canvas->drawText(text_to_fit.c_str(), strlen(text_to_fit.c_str()), |
| + rect.x() + (rect.width() / 2.0f), |
| + rect.y() + rect.height() - text_baseline_offset, paint); |
| + paint.setTextScaleX(SkIntToScalar(1)); |
| +} |
| + |
| +const char* TextUpdateKey::ToText() const { |
| + const char* text_char = text_.c_str(); |
| + return text_char; |
| +} |
| + |
| +void TextUpdateKey::OnTouchUp() { |
| + touch_up_callback_.Run(*this); |
| +} |
| + |
| +void TextUpdateKey::ChangeText(std::string new_text) { |
| + text_ = new_text; |
| +} |
| +} // namespace keyboard |