Chromium Code Reviews| 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 <string> | |
| 6 | |
| 5 #include "ash/shell.h" | 7 #include "ash/shell.h" |
| 6 #include "ash/shell_delegate.h" | 8 #include "ash/shell_delegate.h" |
| 7 #include "ash/shell/example_factory.h" | 9 #include "ash/shell/example_factory.h" |
| 8 #include "ash/shell/toplevel_window.h" | 10 #include "ash/shell/toplevel_window.h" |
| 9 #include "base/basictypes.h" | 11 #include "base/basictypes.h" |
| 12 #include "base/i18n/case_conversion.h" | |
| 13 #include "base/i18n/string_search.h" | |
| 14 #include "base/string_util.h" | |
| 15 #include "base/utf_string_conversions.h" | |
| 10 #include "ui/app_list/app_list_item_model.h" | 16 #include "ui/app_list/app_list_item_model.h" |
| 11 #include "ui/app_list/app_list_model.h" | 17 #include "ui/app_list/app_list_model.h" |
| 12 #include "ui/app_list/app_list_view_delegate.h" | 18 #include "ui/app_list/app_list_view_delegate.h" |
| 19 #include "ui/app_list/search_box_model.h" | |
| 20 #include "ui/app_list/search_result.h" | |
| 21 #include "ui/gfx/canvas.h" | |
| 22 #include "ui/gfx/font.h" | |
| 23 #include "ui/gfx/rect.h" | |
| 13 #include "ui/views/examples/examples_window.h" | 24 #include "ui/views/examples/examples_window.h" |
| 14 | 25 |
| 15 namespace ash { | 26 namespace ash { |
| 16 namespace shell { | 27 namespace shell { |
| 17 | 28 |
| 18 namespace { | 29 namespace { |
| 19 | 30 |
| 20 class WindowTypeLauncherItem : public app_list::AppListItemModel { | 31 class WindowTypeLauncherItem : public app_list::AppListItemModel { |
| 21 public: | 32 public: |
| 22 enum Type { | 33 enum Type { |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 60 return "Lock Screen"; | 71 return "Lock Screen"; |
| 61 case WIDGETS_WINDOW: | 72 case WIDGETS_WINDOW: |
| 62 return "Show Example Widgets"; | 73 return "Show Example Widgets"; |
| 63 case EXAMPLES_WINDOW: | 74 case EXAMPLES_WINDOW: |
| 64 return "Open Views Examples Window"; | 75 return "Open Views Examples Window"; |
| 65 default: | 76 default: |
| 66 return "Unknown window type."; | 77 return "Unknown window type."; |
| 67 } | 78 } |
| 68 } | 79 } |
| 69 | 80 |
| 70 void Activate(int event_flags) { | 81 static std::string GetDetails(Type type) { |
|
sky
2012/05/22 22:16:06
Add a comment as to why this doesn't need to be lo
xiyuan
2012/05/23 21:25:22
Done.
| |
| 71 switch (type_) { | 82 // Assigns details only to some types so that we see both one-line |
| 83 // and two-line results. | |
| 84 switch (type) { | |
| 85 case WIDGETS_WINDOW: | |
| 86 return "Creates a window to show example widgets"; | |
| 87 case EXAMPLES_WINDOW: | |
| 88 return "Creates a window to show views example."; | |
| 89 default: | |
| 90 return std::string(); | |
| 91 } | |
| 92 } | |
| 93 | |
| 94 static void Activate(Type type, int event_flags) { | |
| 95 switch (type) { | |
| 72 case TOPLEVEL_WINDOW: { | 96 case TOPLEVEL_WINDOW: { |
| 73 ToplevelWindow::CreateParams params; | 97 ToplevelWindow::CreateParams params; |
| 74 params.can_resize = true; | 98 params.can_resize = true; |
| 75 ToplevelWindow::CreateToplevelWindow(params); | 99 ToplevelWindow::CreateToplevelWindow(params); |
| 76 break; | 100 break; |
| 77 } | 101 } |
| 78 case NON_RESIZABLE_WINDOW: { | 102 case NON_RESIZABLE_WINDOW: { |
| 79 ToplevelWindow::CreateToplevelWindow(ToplevelWindow::CreateParams()); | 103 ToplevelWindow::CreateToplevelWindow(ToplevelWindow::CreateParams()); |
| 80 break; | 104 break; |
| 81 } | 105 } |
| (...skipping 11 matching lines...) Expand all Loading... | |
| 93 views::examples::DO_NOTHING_ON_CLOSE, | 117 views::examples::DO_NOTHING_ON_CLOSE, |
| 94 ash::Shell::GetInstance()->browser_context()); | 118 ash::Shell::GetInstance()->browser_context()); |
| 95 #endif | 119 #endif |
| 96 break; | 120 break; |
| 97 } | 121 } |
| 98 default: | 122 default: |
| 99 break; | 123 break; |
| 100 } | 124 } |
| 101 } | 125 } |
| 102 | 126 |
| 127 void Activate(int event_flags) { | |
| 128 Activate(type_, event_flags); | |
| 129 } | |
| 130 | |
| 103 private: | 131 private: |
| 104 Type type_; | 132 Type type_; |
| 105 | 133 |
| 106 DISALLOW_COPY_AND_ASSIGN(WindowTypeLauncherItem); | 134 DISALLOW_COPY_AND_ASSIGN(WindowTypeLauncherItem); |
| 107 }; | 135 }; |
| 108 | 136 |
| 137 class ExampleSearchResult : public app_list::SearchResult { | |
|
sky
2012/05/22 22:16:06
Add a description
xiyuan
2012/05/23 21:25:22
Done.
| |
| 138 public: | |
| 139 ExampleSearchResult(WindowTypeLauncherItem::Type type, | |
| 140 const string16& query) | |
| 141 : type_(type) { | |
| 142 set_icon(WindowTypeLauncherItem::GetIcon(type_)); | |
| 143 | |
| 144 string16 title = UTF8ToUTF16(WindowTypeLauncherItem::GetTitle(type_)); | |
| 145 set_title(title); | |
| 146 | |
| 147 Tags title_tags; | |
| 148 const size_t match_len = query.length(); | |
| 149 | |
| 150 // Hightlight matching parts in title with bold. | |
| 151 // Note the following is not a proper way to handle i18n string. | |
| 152 title = base::i18n::ToLower(title); | |
| 153 size_t match_start = title.find(query); | |
| 154 while (match_start != string16::npos) { | |
| 155 title_tags.push_back(Tag(Tag::MATCH, | |
| 156 match_start, | |
| 157 match_start + match_len)); | |
| 158 match_start = title.find(query, match_start + match_len); | |
| 159 } | |
| 160 set_title_tags(title_tags); | |
| 161 | |
| 162 string16 details = UTF8ToUTF16(WindowTypeLauncherItem::GetDetails(type_)); | |
| 163 set_details(details); | |
| 164 Tags details_tags; | |
| 165 details_tags.push_back(Tag(Tag::DIM, 0, details.length())); | |
| 166 set_details_tags(details_tags); | |
| 167 } | |
| 168 | |
| 169 WindowTypeLauncherItem::Type type() const { return type_; } | |
| 170 | |
| 171 private: | |
| 172 WindowTypeLauncherItem::Type type_; | |
| 173 | |
| 174 DISALLOW_COPY_AND_ASSIGN(ExampleSearchResult); | |
| 175 }; | |
| 176 | |
| 109 class ExampleAppListViewDelegate : public app_list::AppListViewDelegate { | 177 class ExampleAppListViewDelegate : public app_list::AppListViewDelegate { |
| 110 public: | 178 public: |
| 111 ExampleAppListViewDelegate() : model_(NULL) {} | 179 ExampleAppListViewDelegate() : model_(NULL) {} |
| 112 | 180 |
| 113 private: | 181 private: |
| 182 void PopulateApps(app_list::AppListModel::Apps* apps) { | |
| 183 for (int i = 0; | |
| 184 i < static_cast<int>(WindowTypeLauncherItem::LAST_TYPE); | |
| 185 ++i) { | |
| 186 WindowTypeLauncherItem::Type type = | |
| 187 static_cast<WindowTypeLauncherItem::Type>(i); | |
| 188 | |
| 189 std::string title = WindowTypeLauncherItem::GetTitle(type); | |
| 190 apps->Add(new WindowTypeLauncherItem(type)); | |
| 191 } | |
| 192 } | |
| 193 | |
| 194 SkBitmap CreateSearchBoxIcon() { | |
| 195 const string16 icon_text = ASCIIToUTF16("ash"); | |
| 196 const gfx::Size icon_size(32, 32); | |
| 197 | |
| 198 gfx::Canvas canvas(icon_size, false /* is_opaque */); | |
| 199 canvas.DrawStringInt(icon_text, | |
| 200 gfx::Font(), | |
| 201 SK_ColorBLACK, | |
| 202 0, 0, icon_size.width(), icon_size.height(), | |
| 203 gfx::Canvas::TEXT_ALIGN_CENTER | | |
| 204 gfx::Canvas::TEXT_VALIGN_MIDDLE | | |
| 205 gfx::Canvas::NO_SUBPIXEL_RENDERING); | |
| 206 | |
| 207 return canvas.ExtractBitmap(); | |
| 208 } | |
| 209 | |
| 210 void DecorateSearchBox(app_list::SearchBoxModel* search_box_model) { | |
| 211 search_box_model->SetIcon(CreateSearchBoxIcon()); | |
| 212 search_box_model->SetHintText(ASCIIToUTF16("Type to search...")); | |
| 213 } | |
| 214 | |
| 114 // Overridden from ash::AppListViewDelegate: | 215 // Overridden from ash::AppListViewDelegate: |
| 115 virtual void SetModel(app_list::AppListModel* model) OVERRIDE { | 216 virtual void SetModel(app_list::AppListModel* model) OVERRIDE { |
| 116 model_ = model; | 217 model_ = model; |
| 218 PopulateApps(model_->apps()); | |
| 219 DecorateSearchBox(model_->search_box()); | |
| 117 } | 220 } |
| 118 | 221 |
| 119 virtual void UpdateModel(const std::string& query) OVERRIDE { | 222 virtual void ActivateAppListItem(app_list::AppListItemModel* item, |
| 120 DCHECK(model_ && model_->item_count() == 0); | 223 int event_flags) OVERRIDE { |
| 224 static_cast<WindowTypeLauncherItem*>(item)->Activate(event_flags); | |
| 225 } | |
| 226 | |
| 227 virtual void OpenSearchResult(const app_list::SearchResult* result, | |
| 228 int event_flags) OVERRIDE { | |
| 229 const ExampleSearchResult* example_result = | |
| 230 static_cast<const ExampleSearchResult*>(result); | |
| 231 WindowTypeLauncherItem::Activate(example_result->type(), event_flags); | |
| 232 } | |
| 233 | |
| 234 virtual void StartSearch() OVERRIDE { | |
| 235 string16 query; | |
| 236 TrimWhitespace(model_->search_box()->text(), TRIM_ALL, &query); | |
| 237 query = base::i18n::ToLower(query); | |
| 238 | |
| 239 model_->results()->DeleteAll(); | |
| 240 if (query.empty()) | |
| 241 return; | |
| 121 | 242 |
| 122 for (int i = 0; | 243 for (int i = 0; |
| 123 i < static_cast<int>(WindowTypeLauncherItem::LAST_TYPE); | 244 i < static_cast<int>(WindowTypeLauncherItem::LAST_TYPE); |
| 124 ++i) { | 245 ++i) { |
| 125 WindowTypeLauncherItem::Type type = | 246 WindowTypeLauncherItem::Type type = |
| 126 static_cast<WindowTypeLauncherItem::Type>(i); | 247 static_cast<WindowTypeLauncherItem::Type>(i); |
| 127 | 248 |
| 128 std::string title = WindowTypeLauncherItem::GetTitle(type); | 249 string16 title = UTF8ToUTF16(WindowTypeLauncherItem::GetTitle(type)); |
| 129 if (title.find(query) != std::string::npos) | 250 if (base::i18n::StringSearchIgnoringCaseAndAccents(query, title)) |
| 130 model_->AddItem(new WindowTypeLauncherItem(type)); | 251 model_->results()->Add(new ExampleSearchResult(type, query)); |
| 131 } | 252 } |
| 132 } | 253 } |
| 133 | 254 |
| 134 virtual void OnAppListItemActivated(app_list::AppListItemModel* item, | 255 virtual void StopSearch() OVERRIDE { |
| 135 int event_flags) OVERRIDE { | 256 // Nothing needs to be done. |
| 136 static_cast<WindowTypeLauncherItem*>(item)->Activate(event_flags); | |
| 137 } | 257 } |
| 138 | 258 |
| 139 virtual void Close() OVERRIDE { | 259 virtual void Close() OVERRIDE { |
| 140 DCHECK(ash::Shell::HasInstance()); | 260 DCHECK(ash::Shell::HasInstance()); |
| 141 if (Shell::GetInstance()->GetAppListTargetVisibility()) | 261 if (Shell::GetInstance()->GetAppListTargetVisibility()) |
| 142 Shell::GetInstance()->ToggleAppList(); | 262 Shell::GetInstance()->ToggleAppList(); |
| 143 } | 263 } |
| 144 | 264 |
| 145 app_list::AppListModel* model_; | 265 app_list::AppListModel* model_; |
| 146 | 266 |
| 147 DISALLOW_COPY_AND_ASSIGN(ExampleAppListViewDelegate); | 267 DISALLOW_COPY_AND_ASSIGN(ExampleAppListViewDelegate); |
| 148 }; | 268 }; |
| 149 | 269 |
| 150 } // namespace | 270 } // namespace |
| 151 | 271 |
| 152 app_list::AppListViewDelegate* CreateAppListViewDelegate() { | 272 app_list::AppListViewDelegate* CreateAppListViewDelegate() { |
| 153 return new ExampleAppListViewDelegate; | 273 return new ExampleAppListViewDelegate; |
| 154 } | 274 } |
| 155 | 275 |
| 156 } // namespace shell | 276 } // namespace shell |
| 157 } // namespace ash | 277 } // namespace ash |
| OLD | NEW |