| OLD | NEW |
| (Empty) |
| 1 // Copyright 2013 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 #include "chrome/browser/chromeos/input_method/mode_indicator_view.h" | |
| 5 | |
| 6 #include "base/strings/utf_string_conversions.h" | |
| 7 #include "ui/gfx/color_utils.h" | |
| 8 #include "ui/native_theme/native_theme.h" | |
| 9 #include "ui/views/bubble/bubble_border.h" | |
| 10 #include "ui/views/controls/label.h" | |
| 11 | |
| 12 namespace chromeos { | |
| 13 namespace input_method { | |
| 14 | |
| 15 ModeIndicatorView::ModeIndicatorView() | |
| 16 : label_(NULL) { | |
| 17 Init(); | |
| 18 } | |
| 19 | |
| 20 ModeIndicatorView::~ModeIndicatorView() {} | |
| 21 | |
| 22 void ModeIndicatorView::SetLabelTextUtf8(const std::string& text_utf8) { | |
| 23 DCHECK(label_); | |
| 24 | |
| 25 label_->SetText(UTF8ToUTF16(text_utf8)); | |
| 26 | |
| 27 // If Layout is not called here, the size of the view is not updated. | |
| 28 // TODO(komatsu): Investigate a proper way not to call Layout. | |
| 29 Layout(); | |
| 30 } | |
| 31 | |
| 32 namespace { | |
| 33 const int kMinSize = 43; | |
| 34 } // namespace | |
| 35 | |
| 36 void ModeIndicatorView::Layout() { | |
| 37 DCHECK(label_); | |
| 38 | |
| 39 // Resize label (simply use the preferred size). | |
| 40 label_->SizeToPreferredSize(); | |
| 41 const gfx::Size label_size = label_->size(); | |
| 42 | |
| 43 // Calculate the view's base size to fit to the label size or the | |
| 44 // minimum size. | |
| 45 gfx::Size base_size = label_size; | |
| 46 base_size.SetToMax(gfx::Size(kMinSize, kMinSize)); | |
| 47 | |
| 48 // Resize view considering the insets of the border. | |
| 49 gfx::Size view_size = base_size; | |
| 50 const gfx::Insets insets = border()->GetInsets(); | |
| 51 view_size.Enlarge(insets.width(), insets.height()); | |
| 52 SetSize(view_size); | |
| 53 | |
| 54 // Relocate label (center of the view considering the insets). | |
| 55 const int x = insets.left() + (base_size.width() - label_size.width()) / 2; | |
| 56 const int y = insets.top() + (base_size.height() - label_size.height()) / 2; | |
| 57 label_->SetX(x); | |
| 58 label_->SetY(y); | |
| 59 } | |
| 60 | |
| 61 void ModeIndicatorView::Init() { | |
| 62 // Initialize view. | |
| 63 views::BubbleBorder* border = new views::BubbleBorder( | |
| 64 views::BubbleBorder::TOP_CENTER, | |
| 65 views::BubbleBorder::NO_SHADOW, | |
| 66 SK_ColorWHITE); | |
| 67 set_border(border); | |
| 68 set_background(new views::BubbleBackground(border)); | |
| 69 | |
| 70 // Initialize label. | |
| 71 label_ = new views::Label; | |
| 72 label_->set_border(views::Border::CreateEmptyBorder(2, 2, 2, 2)); | |
| 73 label_->set_background( | |
| 74 views::Background::CreateSolidBackground( | |
| 75 GetNativeTheme()->GetSystemColor( | |
| 76 ui::NativeTheme::kColorId_WindowBackground))); | |
| 77 label_->SetBackgroundColor(label_->background()->get_color()); | |
| 78 | |
| 79 // Pass the ownership. | |
| 80 AddChildView(label_); | |
| 81 } | |
| 82 | |
| 83 } // namespace input_method | |
| 84 } // namespace chromeos | |
| OLD | NEW |