Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(234)

Side by Side Diff: ui/app_list/search_box_view.cc

Issue 10386224: app_list: Add search box and search result view for v2. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: rebase, rename and add chrome search Created 8 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
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
3 // found in the LICENSE file.
4
5 #include "ui/app_list/search_box_view.h"
6
7 #include <algorithm>
8
9 #include "ui/app_list/search_box_model.h"
10 #include "ui/app_list/search_box_view_delegate.h"
11 #include "ui/app_list/view_ids.h"
12 #include "ui/base/resource/resource_bundle.h"
13 #include "ui/views/controls/image_view.h"
14 #include "ui/views/controls/textfield/textfield.h"
15
16 namespace app_list {
17
18 namespace {
19
20 const int kPadding = 9;
21 const int kIconDimension = 32;
22
23 // Amount of time to wait so that we coalesce query text changes.
24 const int kQueryTextChangeCoalescingTimeMs = 100;
25
26 } // namespace
27
28 SearchBoxView::SearchBoxView(SearchBoxViewDelegate* delegate)
29 : delegate_(delegate),
30 model_(NULL) {
sky 2012/05/22 22:16:06 I think it's worth initializing icon_view_ and sea
xiyuan 2012/05/23 21:25:22 Done. I agree and I don't know why I forgot this.
31 set_id(VIEW_ID_SEARCH_BOX);
32
33 icon_view_ = new views::ImageView;
34 AddChildView(icon_view_);
35
36 search_box_ = new views::Textfield;
37 search_box_->RemoveBorder();
38 ResourceBundle& rb = ResourceBundle::GetSharedInstance();
39 search_box_->SetFont(rb.GetFont(ResourceBundle::BaseFont).DeriveFont(
40 2, gfx::Font::BOLD));
41 search_box_->SetController(this);
42 AddChildView(search_box_);
43 }
44
45 SearchBoxView::~SearchBoxView() {
46 if (model_)
47 model_->RemoveObserver(this);
48 }
49
50 void SearchBoxView::SetModel(SearchBoxModel* model) {
51 if (model_ == model)
52 return;
53
54 if (model_)
55 model_->RemoveObserver(this);
56
57 model_ = model;
58 if (model_) {
59 model_->AddObserver(this);
60 IconChanged();
61 HintTextChanged();
62 }
63 }
64
65 void SearchBoxView::UpdateModel() {
66 // Temporarily remove from observer to ignore notifications caused by us.
67 model_->RemoveObserver(this);
68 model_->SetText(search_box_->text());
69
70 gfx::SelectionModel sel;
71 search_box_->GetSelectionModel(&sel);
72 model_->SetSelectionModel(sel);
73 model_->AddObserver(this);
74 }
75
76 void SearchBoxView::NotifyQueryChanged() {
77 UpdateModel();
sky 2012/05/22 22:16:06 Shouldn't this only update if the contents actuall
xiyuan 2012/05/23 21:25:22 Done. Moved this call to ContentsChanged handler.
78 delegate_->QueryChanged(this);
79 }
80
81 gfx::Size SearchBoxView::GetPreferredSize() {
82 if (!parent())
83 return gfx::Size();
84
85 return gfx::Size(parent()->GetContentsBounds().width(),
86 std::max(kIconDimension,
87 search_box_->GetPreferredSize().height()));
88 }
89
90 void SearchBoxView::Layout() {
91 gfx::Rect rect(GetContentsBounds());
92 if (rect.IsEmpty())
93 return;
94
95 gfx::Size icon_size(kIconDimension, kIconDimension);
96 gfx::Rect icon_frame(rect);
97 icon_frame.set_width(icon_size.width() + 2 * kPadding);
98 icon_view_->SetBoundsRect(icon_frame);
99
100 gfx::Rect edit_frame(rect);
101 edit_frame.set_x(icon_frame.right());
102 edit_frame.set_width(rect.width() - icon_frame.width() - kPadding);
103 search_box_->SetBoundsRect(edit_frame);
104 }
105
106 void SearchBoxView::ContentsChanged(views::Textfield* sender,
107 const string16& new_contents) {
108 query_timer_.Stop();
sky 2012/05/22 22:16:06 If the delegate wants to throttle queries, shouldn
xiyuan 2012/05/23 21:25:22 Makes sense. Removed.
109 query_timer_.Start(
110 FROM_HERE,
111 base::TimeDelta::FromMilliseconds(kQueryTextChangeCoalescingTimeMs),
112 this,
113 &SearchBoxView::NotifyQueryChanged);
114 }
115
116 bool SearchBoxView::HandleKeyEvent(views::Textfield* sender,
117 const views::KeyEvent& key_event) {
118 bool has_query = !search_box_->text().empty();
119
120 // Escape with non-empty query text clears the search box.
121 if (has_query && key_event.key_code() == ui::VKEY_ESCAPE) {
122 search_box_->SetText(string16());
123 return true;
124 }
125
126 bool handled = false;
127 if (has_query) {
128 views::View* results_view = parent()->GetViewByID(VIEW_ID_SEARCH_RESULTS);
sky 2012/05/22 22:16:06 This strikes me as very brittle. Could you explici
xiyuan 2012/05/23 21:25:22 Done.
129 if (results_view && results_view->visible())
130 handled = results_view->OnKeyPressed(key_event);
131 } else {
132 views::View* grid_view = parent()->GetViewByID(VIEW_ID_APPS_GRID);
133 if (grid_view && grid_view->visible())
134 handled = grid_view->OnKeyPressed(key_event);
135 }
136
137 return handled;
138 }
139
140 void SearchBoxView::IconChanged() {
141 icon_view_->SetImage(model_->icon());
142 }
143
144 void SearchBoxView::HintTextChanged() {
145 search_box_->set_placeholder_text(model_->hint_text());
146 }
147
148 void SearchBoxView::SelectionModelChanged() {
149 search_box_->SelectSelectionModel(model_->selection_model());
150 }
151
152 void SearchBoxView::TextChanged() {
153 search_box_->SetText(model_->text());
154 }
155
156 } // namespace app_list
157
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698