1 // Copyright 2014 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.
5 #include "ash/ime/infolist_window.h"
10 #include "ash/ime/candidate_window_constants.h"
11 #include "base/logging.h"
12 #include "grit/ash_strings.h"
13 #include "ui/base/l10n/l10n_util.h"
14 #include "ui/gfx/color_utils.h"
15 #include "ui/native_theme/native_theme.h"
16 #include "ui/views/background.h"
17 #include "ui/views/border.h"
18 #include "ui/views/bubble/bubble_border.h"
19 #include "ui/views/bubble/bubble_frame_view.h"
20 #include "ui/views/controls/label.h"
21 #include "ui/views/corewm/window_animations.h"
22 #include "ui/views/layout/box_layout.h"
23 #include "ui/views/widget/widget.h"
29 // The width of an info-list.
30 const int kInfolistEntryWidth = 200;
32 // The milliseconds of the delay to show the infolist window.
33 const int kInfolistShowDelayMilliSeconds = 500;
34 // The milliseconds of the delay to hide the infolist window.
35 const int kInfolistHideDelayMilliSeconds = 500;
37 ///////////////////////////////////////////////////////////////////////////////
39 // The BubbleBorder subclass to draw the border and determine its position.
40 class InfolistBorder : public views::BubbleBorder {
43 virtual ~InfolistBorder();
45 // views::BubbleBorder implementation.
46 virtual gfx::Rect GetBounds(const gfx::Rect& anchor_rect,
47 const gfx::Size& contents_size) const OVERRIDE;
48 virtual gfx::Insets GetInsets() const OVERRIDE;
51 DISALLOW_COPY_AND_ASSIGN(InfolistBorder);
54 InfolistBorder::InfolistBorder()
55 : views::BubbleBorder(views::BubbleBorder::LEFT_CENTER,
56 views::BubbleBorder::NO_SHADOW,
57 SK_ColorTRANSPARENT) {
58 set_paint_arrow(views::BubbleBorder::PAINT_NONE);
61 InfolistBorder::~InfolistBorder() {}
63 gfx::Rect InfolistBorder::GetBounds(const gfx::Rect& anchor_rect,
64 const gfx::Size& contents_size) const {
65 gfx::Rect bounds(contents_size);
66 bounds.set_x(is_arrow_on_left(arrow()) ?
67 anchor_rect.right() : anchor_rect.x() - contents_size.width());
68 // InfolistBorder modifies the vertical position based on the arrow offset
69 // although it doesn't draw the arrow. The arrow offset is the half of
70 // |contents_size| by default but can be modified through the off-screen logic
71 // in BubbleFrameView.
72 bounds.set_y(anchor_rect.y() + contents_size.height() / 2 -
73 GetArrowOffset(contents_size));
77 gfx::Insets InfolistBorder::GetInsets() const {
78 // This has to be specified and return empty insets to place the infolist
79 // window without the gap.
85 // InfolistRow renderes a row of a infolist.
86 class InfolistEntryView : public views::View {
88 InfolistEntryView(const ui::InfolistEntry& entry,
89 const gfx::FontList& title_font_list,
90 const gfx::FontList& description_font_list);
91 virtual ~InfolistEntryView();
93 void SetEntry(const ui::InfolistEntry& entry);
96 // views::View implementation.
97 virtual gfx::Size GetPreferredSize() OVERRIDE;
99 void UpdateBackground();
101 ui::InfolistEntry entry_;
103 // The title label. Owned by views hierarchy.
104 views::Label* title_label_;
106 // The description label. Owned by views hierarchy.
107 views::Label* description_label_;
109 DISALLOW_COPY_AND_ASSIGN(InfolistEntryView);
112 InfolistEntryView::InfolistEntryView(const ui::InfolistEntry& entry,
113 const gfx::FontList& title_font_list,
114 const gfx::FontList& description_font_list)
116 SetLayoutManager(new views::BoxLayout(views::BoxLayout::kVertical, 0, 0, 0));
118 title_label_ = new views::Label(entry.title, title_font_list);
119 title_label_->SetPosition(gfx::Point(0, 0));
120 title_label_->SetHorizontalAlignment(gfx::ALIGN_LEFT);
121 title_label_->SetBorder(views::Border::CreateEmptyBorder(4, 7, 2, 4));
123 description_label_ = new views::Label(entry.body, description_font_list);
124 description_label_->SetPosition(gfx::Point(0, 0));
125 description_label_->SetHorizontalAlignment(gfx::ALIGN_LEFT);
126 description_label_->SetMultiLine(true);
127 description_label_->SizeToFit(kInfolistEntryWidth);
128 description_label_->SetBorder(views::Border::CreateEmptyBorder(2, 17, 4, 4));
129 AddChildView(title_label_);
130 AddChildView(description_label_);
134 InfolistEntryView::~InfolistEntryView() {}
136 void InfolistEntryView::SetEntry(const ui::InfolistEntry& entry) {
141 title_label_->SetText(entry_.title);
142 description_label_->SetText(entry_.body);
146 gfx::Size InfolistEntryView::GetPreferredSize() {
147 return gfx::Size(kInfolistEntryWidth, GetHeightForWidth(kInfolistEntryWidth));
150 void InfolistEntryView::UpdateBackground() {
151 if (entry_.highlighted) {
153 views::Background::CreateSolidBackground(GetNativeTheme()->GetSystemColor(
154 ui::NativeTheme::kColorId_TextfieldSelectionBackgroundFocused)));
155 SetBorder(views::Border::CreateSolidBorder(
157 GetNativeTheme()->GetSystemColor(
158 ui::NativeTheme::kColorId_FocusedBorderColor)));
160 set_background(NULL);
161 SetBorder(views::Border::CreateEmptyBorder(1, 1, 1, 1));
166 ///////////////////////////////////////////////////////////////////////////////
169 InfolistWindow::InfolistWindow(views::View* candidate_window,
170 const std::vector<ui::InfolistEntry>& entries)
171 : views::BubbleDelegateView(candidate_window, views::BubbleBorder::NONE),
172 title_font_list_(gfx::Font(kJapaneseFontName, kFontSizeDelta + 15)),
173 description_font_list_(gfx::Font(kJapaneseFontName,
174 kFontSizeDelta + 11)) {
175 set_move_with_anchor(true);
176 set_margins(gfx::Insets());
179 views::Background::CreateSolidBackground(GetNativeTheme()->GetSystemColor(
180 ui::NativeTheme::kColorId_WindowBackground)));
181 SetBorder(views::Border::CreateSolidBorder(
183 GetNativeTheme()->GetSystemColor(
184 ui::NativeTheme::kColorId_MenuBorderColor)));
186 SetLayoutManager(new views::BoxLayout(views::BoxLayout::kVertical, 0, 0, 0));
188 views::Label* caption_label = new views::Label(
189 l10n_util::GetStringUTF16(IDS_ASH_IME_INFOLIST_WINDOW_TITLE));
190 caption_label->SetHorizontalAlignment(gfx::ALIGN_LEFT);
191 caption_label->SetEnabledColor(GetNativeTheme()->GetSystemColor(
192 ui::NativeTheme::kColorId_LabelEnabledColor));
193 caption_label->SetBorder(views::Border::CreateEmptyBorder(2, 2, 2, 2));
194 caption_label->set_background(views::Background::CreateSolidBackground(
195 color_utils::AlphaBlend(SK_ColorBLACK,
196 GetNativeTheme()->GetSystemColor(
197 ui::NativeTheme::kColorId_WindowBackground),
200 AddChildView(caption_label);
202 for (size_t i = 0; i < entries.size(); ++i) {
203 entry_views_.push_back(new InfolistEntryView(
204 entries[i], title_font_list_, description_font_list_));
205 AddChildView(entry_views_.back());
209 InfolistWindow::~InfolistWindow() {
212 void InfolistWindow::InitWidget() {
213 views::Widget* widget = views::BubbleDelegateView::CreateBubble(this);
214 views::corewm::SetWindowVisibilityAnimationType(
215 widget->GetNativeView(),
216 views::corewm::WINDOW_VISIBILITY_ANIMATION_TYPE_FADE);
218 // BubbleFrameView will be initialized through CreateBubble.
219 GetBubbleFrameView()->SetBubbleBorder(
220 scoped_ptr<views::BubbleBorder>(new InfolistBorder()));
224 void InfolistWindow::Relayout(const std::vector<ui::InfolistEntry>& entries) {
226 for (; i < entries.size(); ++i) {
227 if (i < entry_views_.size()) {
228 entry_views_[i]->SetEntry(entries[i]);
230 InfolistEntryView* new_entry = new InfolistEntryView(
231 entries[i], title_font_list_, description_font_list_);
232 AddChildView(new_entry);
233 entry_views_.push_back(new_entry);
237 if (i < entry_views_.size()) {
238 for (; i < entry_views_.size(); ++i)
239 delete entry_views_[i];
240 entry_views_.resize(entries.size());
244 GetBubbleFrameView()->bubble_border()->set_arrow_offset(0);
248 void InfolistWindow::ShowWithDelay() {
249 show_hide_timer_.Start(
251 base::TimeDelta::FromMilliseconds(kInfolistShowDelayMilliSeconds),
253 &views::Widget::Show);
256 void InfolistWindow::HideWithDelay() {
257 show_hide_timer_.Start(
259 base::TimeDelta::FromMilliseconds(kInfolistHideDelayMilliSeconds),
261 &views::Widget::Close);
264 void InfolistWindow::ShowImmediately() {
265 show_hide_timer_.Stop();
269 void InfolistWindow::HideImmediately() {
270 show_hide_timer_.Stop();
271 GetWidget()->Close();
274 void InfolistWindow::WindowClosing() {
275 show_hide_timer_.Stop();