OLD | NEW |
---|---|
(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 #include <string> | |
6 | |
7 #include "services/keyboard_native/text_update_key.h" | |
8 #include "third_party/skia/include/core/SkCanvas.h" | |
9 #include "third_party/skia/include/core/SkPaint.h" | |
10 #include "third_party/skia/include/core/SkScalar.h" | |
11 #include "ui/gfx/geometry/rect_f.h" | |
12 | |
13 namespace keyboard { | |
14 | |
15 TextUpdateKey::TextUpdateKey( | |
16 std::string text, | |
17 base::Callback<void(const TextUpdateKey&)> touch_up_callback) | |
18 : text_(text), touch_up_callback_(touch_up_callback) { | |
19 } | |
20 | |
21 TextUpdateKey::~TextUpdateKey() { | |
22 } | |
23 | |
24 // Key implementation. | |
25 void TextUpdateKey::Draw(SkCanvas* canvas, | |
26 const SkPaint& paint, | |
27 const gfx::RectF& rect) { | |
28 std::string text_to_fit = text_; | |
29 SkRect bounds; | |
30 SkPaint paint_copy = paint; | |
31 paint_copy.measureText((const void*)(text_.c_str()), strlen(text_.c_str()), | |
32 &bounds); | |
33 bool text_need_scale = false; | |
34 if (bounds.width() > rect.width() * 0.8) { | |
35 text_need_scale = true; | |
36 | |
37 paint_copy.setTextScaleX(SkFloatToScalar(0.6)); | |
38 paint_copy.measureText((const void*)(text_.c_str()), strlen(text_.c_str()), | |
39 &bounds); | |
40 paint_copy.setTextScaleX(SkIntToScalar(1)); | |
41 if (bounds.width() > rect.width() * 0.8) { | |
42 int dot_count = SkScalarTruncToInt((SkScalarToFloat(bounds.width()) - | |
43 SkScalarToFloat(rect.width()) * 0.8) / | |
44 SkScalarToFloat(bounds.width()) * | |
45 strlen(text_.c_str())) + | |
46 1; | |
47 std::string dots = "..."; | |
48 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
| |
49 if (i <= dot_count) { | |
50 text_to_fit = | |
51 dots + | |
52 text_to_fit.substr(dot_count, text_to_fit.length() - dot_count); | |
53 break; | |
54 } | |
55 dots.pop_back(); | |
56 } | |
57 } | |
58 } | |
59 | |
60 float text_baseline_offset = rect.height() / 5.0f; | |
61 if (text_need_scale) { | |
62 paint_copy.setTextScaleX(SkFloatToScalar(0.6)); | |
63 } | |
64 canvas->drawText(text_to_fit.c_str(), strlen(text_to_fit.c_str()), | |
65 rect.x() + (rect.width() / 2.0f), | |
66 rect.y() + rect.height() - text_baseline_offset, paint_copy); | |
67 paint_copy.setTextScaleX(SkIntToScalar(1)); | |
68 } | |
69 | |
70 const char* TextUpdateKey::ToText() const { | |
71 const char* text_char = text_.c_str(); | |
72 return text_char; | |
73 } | |
74 | |
75 void TextUpdateKey::OnTouchUp() { | |
76 touch_up_callback_.Run(*this); | |
77 } | |
78 | |
79 void TextUpdateKey::ChangeText(std::string new_text) { | |
80 text_ = new_text; | |
81 } | |
82 } // namespace keyboard | |
OLD | NEW |