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..fae5d0e838b636e59fcce89164ef47915e57dae6 |
| --- /dev/null |
| +++ b/services/keyboard_native/text_update_key.cc |
| @@ -0,0 +1,82 @@ |
| +// 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 <string> |
| + |
| +#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, |
| + const SkPaint& paint, |
| + const gfx::RectF& rect) { |
| + std::string text_to_fit = text_; |
| + SkRect bounds; |
| + SkPaint paint_copy = paint; |
| + paint_copy.measureText((const void*)(text_.c_str()), strlen(text_.c_str()), |
| + &bounds); |
| + bool text_need_scale = false; |
| + if (bounds.width() > rect.width() * 0.8) { |
| + text_need_scale = true; |
| + |
| + paint_copy.setTextScaleX(SkFloatToScalar(0.6)); |
| + paint_copy.measureText((const void*)(text_.c_str()), strlen(text_.c_str()), |
| + &bounds); |
| + paint_copy.setTextScaleX(SkIntToScalar(1)); |
| + if (bounds.width() > rect.width() * 0.8) { |
| + int dot_count = SkScalarTruncToInt((SkScalarToFloat(bounds.width()) - |
| + SkScalarToFloat(rect.width()) * 0.8) / |
| + SkScalarToFloat(bounds.width()) * |
| + strlen(text_.c_str())) + |
| + 1; |
| + std::string dots = "..."; |
| + for (int i = 3; i > 0; i--) { |
|
APW
2015/08/03 17:26:21
Still think you can do better. Maybe use dot_coun
riajiang
2015/08/03 19:39:42
Changed it to use that string constructor
|
| + if (i <= dot_count) { |
| + text_to_fit = |
| + dots + |
| + text_to_fit.substr(dot_count, text_to_fit.length() - dot_count); |
| + break; |
| + } |
| + dots.pop_back(); |
| + } |
| + } |
| + } |
| + |
| + float text_baseline_offset = rect.height() / 5.0f; |
| + if (text_need_scale) { |
| + paint_copy.setTextScaleX(SkFloatToScalar(0.6)); |
| + } |
| + 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_copy); |
| + paint_copy.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 |