OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "ui/app_list/views/search_box_view.h" | 5 #include "ui/app_list/views/search_box_view.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 | 8 |
| 9 #include "grit/ui_resources.h" |
9 #include "ui/app_list/search_box_model.h" | 10 #include "ui/app_list/search_box_model.h" |
10 #include "ui/app_list/search_box_view_delegate.h" | 11 #include "ui/app_list/search_box_view_delegate.h" |
11 #include "ui/base/events/event.h" | 12 #include "ui/base/events/event.h" |
12 #include "ui/base/resource/resource_bundle.h" | 13 #include "ui/base/resource/resource_bundle.h" |
| 14 #include "ui/views/controls/button/menu_button.h" |
13 #include "ui/views/controls/image_view.h" | 15 #include "ui/views/controls/image_view.h" |
14 #include "ui/views/controls/textfield/textfield.h" | 16 #include "ui/views/controls/textfield/textfield.h" |
15 | 17 |
16 namespace app_list { | 18 namespace app_list { |
17 | 19 |
18 namespace { | 20 namespace { |
19 | 21 |
20 const int kPadding = 14; | 22 const int kPadding = 14; |
21 const int kIconDimension = 32; | 23 const int kIconDimension = 32; |
22 const int kPreferredWidth = 360; | 24 const int kPreferredWidth = 360; |
23 const int kPreferredHeight = 48; | 25 const int kPreferredHeight = 48; |
24 const int kEditHeight = 19; | 26 const int kEditHeight = 19; |
| 27 const int kMenuButtonDimension = 29; |
25 | 28 |
26 const SkColor kHintTextColor = SkColorSetRGB(0xA0, 0xA0, 0xA0); | 29 const SkColor kHintTextColor = SkColorSetRGB(0xA0, 0xA0, 0xA0); |
27 | 30 |
28 } // namespace | 31 } // namespace |
29 | 32 |
30 SearchBoxView::SearchBoxView(SearchBoxViewDelegate* delegate) | 33 SearchBoxView::SearchBoxView(SearchBoxViewDelegate* delegate, |
| 34 AppListViewDelegate* view_delegate) |
31 : delegate_(delegate), | 35 : delegate_(delegate), |
32 model_(NULL), | 36 model_(NULL), |
| 37 menu_(view_delegate), |
33 icon_view_(new views::ImageView), | 38 icon_view_(new views::ImageView), |
34 user_icon_view_(new views::ImageView), | |
35 search_box_(new views::Textfield), | 39 search_box_(new views::Textfield), |
36 contents_view_(NULL) { | 40 contents_view_(NULL) { |
37 AddChildView(icon_view_); | 41 AddChildView(icon_view_); |
38 AddChildView(user_icon_view_); | 42 |
| 43 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); |
| 44 |
| 45 #if !defined(OS_CHROMEOS) |
| 46 menu_button_ = new views::MenuButton(NULL, string16(), this, false); |
| 47 menu_button_->set_border(NULL); |
| 48 menu_button_->SetIcon(*rb.GetImageSkiaNamed(IDR_APP_LIST_TOOLS_NORMAL)); |
| 49 menu_button_->SetHoverIcon(*rb.GetImageSkiaNamed(IDR_APP_LIST_TOOLS_HOVER)); |
| 50 menu_button_->SetPushedIcon(*rb.GetImageSkiaNamed( |
| 51 IDR_APP_LIST_TOOLS_PRESSED)); |
| 52 AddChildView(menu_button_); |
| 53 #endif |
39 | 54 |
40 search_box_->RemoveBorder(); | 55 search_box_->RemoveBorder(); |
41 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); | |
42 search_box_->SetFont(rb.GetFont(ui::ResourceBundle::MediumFont)); | 56 search_box_->SetFont(rb.GetFont(ui::ResourceBundle::MediumFont)); |
43 search_box_->set_placeholder_text_color(kHintTextColor); | 57 search_box_->set_placeholder_text_color(kHintTextColor); |
44 search_box_->SetController(this); | 58 search_box_->SetController(this); |
45 AddChildView(search_box_); | 59 AddChildView(search_box_); |
46 } | 60 } |
47 | 61 |
48 SearchBoxView::~SearchBoxView() { | 62 SearchBoxView::~SearchBoxView() { |
49 if (model_) | 63 if (model_) |
50 model_->RemoveObserver(this); | 64 model_->RemoveObserver(this); |
51 } | 65 } |
52 | 66 |
53 void SearchBoxView::SetModel(SearchBoxModel* model) { | 67 void SearchBoxView::SetModel(SearchBoxModel* model) { |
54 if (model_ == model) | 68 if (model_ == model) |
55 return; | 69 return; |
56 | 70 |
57 if (model_) | 71 if (model_) |
58 model_->RemoveObserver(this); | 72 model_->RemoveObserver(this); |
59 | 73 |
60 model_ = model; | 74 model_ = model; |
61 if (model_) { | 75 if (model_) { |
62 model_->AddObserver(this); | 76 model_->AddObserver(this); |
63 IconChanged(); | 77 IconChanged(); |
64 UserIconChanged(); | |
65 HintTextChanged(); | 78 HintTextChanged(); |
66 UserIconTooltipChanged(); | |
67 } | 79 } |
68 } | 80 } |
69 | 81 |
70 gfx::Size SearchBoxView::GetPreferredSize() { | 82 gfx::Size SearchBoxView::GetPreferredSize() { |
71 return gfx::Size(kPreferredWidth, kPreferredHeight); | 83 return gfx::Size(kPreferredWidth, kPreferredHeight); |
72 } | 84 } |
73 | 85 |
74 void SearchBoxView::Layout() { | 86 void SearchBoxView::Layout() { |
75 gfx::Rect rect(GetContentsBounds()); | 87 gfx::Rect rect(GetContentsBounds()); |
76 if (rect.IsEmpty()) | 88 if (rect.IsEmpty()) |
77 return; | 89 return; |
78 | 90 |
79 gfx::Size icon_size(kIconDimension, kIconDimension); | |
80 gfx::Rect icon_frame(rect); | 91 gfx::Rect icon_frame(rect); |
81 icon_frame.set_width(icon_size.width() + 2 * kPadding); | 92 icon_frame.set_width(kIconDimension + 2 * kPadding); |
82 icon_view_->SetBoundsRect(icon_frame); | 93 icon_view_->SetBoundsRect(icon_frame); |
83 | 94 |
84 gfx::Rect user_icon_frame(rect); | 95 gfx::Rect menu_button_frame(rect); |
85 user_icon_frame.set_width(icon_size.width() + 2 * kPadding); | 96 #if !defined(OS_CHROMEOS) |
86 user_icon_frame.set_x(rect.right() - user_icon_frame.width()); | 97 menu_button_frame.set_width(kMenuButtonDimension); |
87 if (!model_->user_icon_enabled()) { | 98 menu_button_frame.set_x(rect.right() - menu_button_frame.width() - kPadding); |
88 user_icon_frame.set_width(0); | 99 menu_button_frame.ClampToCenteredSize(gfx::Size(menu_button_frame.width(), |
89 } | 100 kMenuButtonDimension)); |
90 user_icon_view_->SetVisible(model_->user_icon_enabled()); | 101 menu_button_->SetBoundsRect(menu_button_frame); |
91 user_icon_view_->SetBoundsRect(user_icon_frame); | 102 #else |
| 103 menu_button_frame.set_width(0); |
| 104 #endif |
92 | 105 |
93 gfx::Rect edit_frame(rect); | 106 gfx::Rect edit_frame(rect); |
94 edit_frame.set_x(icon_frame.right()); | 107 edit_frame.set_x(icon_frame.right()); |
95 edit_frame.set_width( | 108 edit_frame.set_width( |
96 rect.width() - icon_frame.width() - kPadding - user_icon_frame.width()); | 109 rect.width() - icon_frame.width() - kPadding - menu_button_frame.width()); |
97 edit_frame.ClampToCenteredSize(gfx::Size(edit_frame.width(), kEditHeight)); | 110 edit_frame.ClampToCenteredSize(gfx::Size(edit_frame.width(), kEditHeight)); |
98 search_box_->SetBoundsRect(edit_frame); | 111 search_box_->SetBoundsRect(edit_frame); |
99 } | 112 } |
100 | 113 |
101 bool SearchBoxView::OnMouseWheel(const ui::MouseWheelEvent& event) { | 114 bool SearchBoxView::OnMouseWheel(const ui::MouseWheelEvent& event) { |
102 if (contents_view_) | 115 if (contents_view_) |
103 return contents_view_->OnMouseWheel(event); | 116 return contents_view_->OnMouseWheel(event); |
104 | 117 |
105 return false; | 118 return false; |
106 } | 119 } |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
138 return true; | 151 return true; |
139 } | 152 } |
140 | 153 |
141 bool handled = false; | 154 bool handled = false; |
142 if (contents_view_ && contents_view_->visible()) | 155 if (contents_view_ && contents_view_->visible()) |
143 handled = contents_view_->OnKeyPressed(key_event); | 156 handled = contents_view_->OnKeyPressed(key_event); |
144 | 157 |
145 return handled; | 158 return handled; |
146 } | 159 } |
147 | 160 |
| 161 void SearchBoxView::OnMenuButtonClicked(View* source, const gfx::Point& point) { |
| 162 menu_.RunMenuAt(menu_button_, |
| 163 menu_button_->GetBoundsInScreen().bottom_right()); |
| 164 } |
| 165 |
148 void SearchBoxView::IconChanged() { | 166 void SearchBoxView::IconChanged() { |
149 icon_view_->SetImage(model_->icon()); | 167 icon_view_->SetImage(model_->icon()); |
150 } | 168 } |
151 | 169 |
152 void SearchBoxView::HintTextChanged() { | 170 void SearchBoxView::HintTextChanged() { |
153 search_box_->set_placeholder_text(model_->hint_text()); | 171 search_box_->set_placeholder_text(model_->hint_text()); |
154 } | 172 } |
155 | 173 |
156 void SearchBoxView::SelectionModelChanged() { | 174 void SearchBoxView::SelectionModelChanged() { |
157 search_box_->SelectSelectionModel(model_->selection_model()); | 175 search_box_->SelectSelectionModel(model_->selection_model()); |
158 } | 176 } |
159 | 177 |
160 void SearchBoxView::TextChanged() { | 178 void SearchBoxView::TextChanged() { |
161 search_box_->SetText(model_->text()); | 179 search_box_->SetText(model_->text()); |
162 } | 180 } |
163 | 181 |
164 void SearchBoxView::UserIconChanged() { | |
165 user_icon_view_->SetImage(model_->user_icon()); | |
166 } | |
167 | |
168 void SearchBoxView::UserIconTooltipChanged() { | |
169 user_icon_view_->SetTooltipText(model_->user_icon_tooltip()); | |
170 } | |
171 | |
172 void SearchBoxView::UserIconEnabledChanged() { | |
173 InvalidateLayout(); | |
174 } | |
175 | |
176 } // namespace app_list | 182 } // namespace app_list |
OLD | NEW |