| 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 |
| 31 // WindowTypeLauncherItem is an app item of app list. It carries a window |
| 32 // launch type and launches corresponding example window when activated. |
| 20 class WindowTypeLauncherItem : public app_list::AppListItemModel { | 33 class WindowTypeLauncherItem : public app_list::AppListItemModel { |
| 21 public: | 34 public: |
| 22 enum Type { | 35 enum Type { |
| 23 TOPLEVEL_WINDOW = 0, | 36 TOPLEVEL_WINDOW = 0, |
| 24 NON_RESIZABLE_WINDOW, | 37 NON_RESIZABLE_WINDOW, |
| 25 LOCK_SCREEN, | 38 LOCK_SCREEN, |
| 26 WIDGETS_WINDOW, | 39 WIDGETS_WINDOW, |
| 27 EXAMPLES_WINDOW, | 40 EXAMPLES_WINDOW, |
| 28 LAST_TYPE, | 41 LAST_TYPE, |
| 29 }; | 42 }; |
| (...skipping 13 matching lines...) Expand all Loading... |
| 43 }; | 56 }; |
| 44 | 57 |
| 45 const int kIconSize = 128; | 58 const int kIconSize = 128; |
| 46 SkBitmap icon; | 59 SkBitmap icon; |
| 47 icon.setConfig(SkBitmap::kARGB_8888_Config, kIconSize, kIconSize); | 60 icon.setConfig(SkBitmap::kARGB_8888_Config, kIconSize, kIconSize); |
| 48 icon.allocPixels(); | 61 icon.allocPixels(); |
| 49 icon.eraseColor(kColors[static_cast<int>(type) % arraysize(kColors)]); | 62 icon.eraseColor(kColors[static_cast<int>(type) % arraysize(kColors)]); |
| 50 return icon; | 63 return icon; |
| 51 } | 64 } |
| 52 | 65 |
| 66 // The text below is not localized as this is an example code. |
| 53 static std::string GetTitle(Type type) { | 67 static std::string GetTitle(Type type) { |
| 54 switch (type) { | 68 switch (type) { |
| 55 case TOPLEVEL_WINDOW: | 69 case TOPLEVEL_WINDOW: |
| 56 return "Create Window"; | 70 return "Create Window"; |
| 57 case NON_RESIZABLE_WINDOW: | 71 case NON_RESIZABLE_WINDOW: |
| 58 return "Create Non-Resizable Window"; | 72 return "Create Non-Resizable Window"; |
| 59 case LOCK_SCREEN: | 73 case LOCK_SCREEN: |
| 60 return "Lock Screen"; | 74 return "Lock Screen"; |
| 61 case WIDGETS_WINDOW: | 75 case WIDGETS_WINDOW: |
| 62 return "Show Example Widgets"; | 76 return "Show Example Widgets"; |
| 63 case EXAMPLES_WINDOW: | 77 case EXAMPLES_WINDOW: |
| 64 return "Open Views Examples Window"; | 78 return "Open Views Examples Window"; |
| 65 default: | 79 default: |
| 66 return "Unknown window type."; | 80 return "Unknown window type."; |
| 67 } | 81 } |
| 68 } | 82 } |
| 69 | 83 |
| 70 void Activate(int event_flags) { | 84 // The text below is not localized as this is an example code. |
| 71 switch (type_) { | 85 static std::string GetDetails(Type type) { |
| 86 // Assigns details only to some types so that we see both one-line |
| 87 // and two-line results. |
| 88 switch (type) { |
| 89 case WIDGETS_WINDOW: |
| 90 return "Creates a window to show example widgets"; |
| 91 case EXAMPLES_WINDOW: |
| 92 return "Creates a window to show views example."; |
| 93 default: |
| 94 return std::string(); |
| 95 } |
| 96 } |
| 97 |
| 98 static void Activate(Type type, int event_flags) { |
| 99 switch (type) { |
| 72 case TOPLEVEL_WINDOW: { | 100 case TOPLEVEL_WINDOW: { |
| 73 ToplevelWindow::CreateParams params; | 101 ToplevelWindow::CreateParams params; |
| 74 params.can_resize = true; | 102 params.can_resize = true; |
| 75 ToplevelWindow::CreateToplevelWindow(params); | 103 ToplevelWindow::CreateToplevelWindow(params); |
| 76 break; | 104 break; |
| 77 } | 105 } |
| 78 case NON_RESIZABLE_WINDOW: { | 106 case NON_RESIZABLE_WINDOW: { |
| 79 ToplevelWindow::CreateToplevelWindow(ToplevelWindow::CreateParams()); | 107 ToplevelWindow::CreateToplevelWindow(ToplevelWindow::CreateParams()); |
| 80 break; | 108 break; |
| 81 } | 109 } |
| (...skipping 11 matching lines...) Expand all Loading... |
| 93 views::examples::DO_NOTHING_ON_CLOSE, | 121 views::examples::DO_NOTHING_ON_CLOSE, |
| 94 ash::Shell::GetInstance()->browser_context()); | 122 ash::Shell::GetInstance()->browser_context()); |
| 95 #endif | 123 #endif |
| 96 break; | 124 break; |
| 97 } | 125 } |
| 98 default: | 126 default: |
| 99 break; | 127 break; |
| 100 } | 128 } |
| 101 } | 129 } |
| 102 | 130 |
| 131 void Activate(int event_flags) { |
| 132 Activate(type_, event_flags); |
| 133 } |
| 134 |
| 103 private: | 135 private: |
| 104 Type type_; | 136 Type type_; |
| 105 | 137 |
| 106 DISALLOW_COPY_AND_ASSIGN(WindowTypeLauncherItem); | 138 DISALLOW_COPY_AND_ASSIGN(WindowTypeLauncherItem); |
| 107 }; | 139 }; |
| 108 | 140 |
| 141 // ExampleSearchResult is an app list search result. It provides what icon to |
| 142 // show, what should title and details text look like. It also carries the |
| 143 // matching window launch type so that AppListViewDelegate knows how to open |
| 144 // it. |
| 145 class ExampleSearchResult : public app_list::SearchResult { |
| 146 public: |
| 147 ExampleSearchResult(WindowTypeLauncherItem::Type type, |
| 148 const string16& query) |
| 149 : type_(type) { |
| 150 set_icon(WindowTypeLauncherItem::GetIcon(type_)); |
| 151 |
| 152 string16 title = UTF8ToUTF16(WindowTypeLauncherItem::GetTitle(type_)); |
| 153 set_title(title); |
| 154 |
| 155 Tags title_tags; |
| 156 const size_t match_len = query.length(); |
| 157 |
| 158 // Highlight matching parts in title with bold. |
| 159 // Note the following is not a proper way to handle i18n string. |
| 160 title = base::i18n::ToLower(title); |
| 161 size_t match_start = title.find(query); |
| 162 while (match_start != string16::npos) { |
| 163 title_tags.push_back(Tag(Tag::MATCH, |
| 164 match_start, |
| 165 match_start + match_len)); |
| 166 match_start = title.find(query, match_start + match_len); |
| 167 } |
| 168 set_title_tags(title_tags); |
| 169 |
| 170 string16 details = UTF8ToUTF16(WindowTypeLauncherItem::GetDetails(type_)); |
| 171 set_details(details); |
| 172 Tags details_tags; |
| 173 details_tags.push_back(Tag(Tag::DIM, 0, details.length())); |
| 174 set_details_tags(details_tags); |
| 175 } |
| 176 |
| 177 WindowTypeLauncherItem::Type type() const { return type_; } |
| 178 |
| 179 private: |
| 180 WindowTypeLauncherItem::Type type_; |
| 181 |
| 182 DISALLOW_COPY_AND_ASSIGN(ExampleSearchResult); |
| 183 }; |
| 184 |
| 109 class ExampleAppListViewDelegate : public app_list::AppListViewDelegate { | 185 class ExampleAppListViewDelegate : public app_list::AppListViewDelegate { |
| 110 public: | 186 public: |
| 111 ExampleAppListViewDelegate() : model_(NULL) {} | 187 ExampleAppListViewDelegate() : model_(NULL) {} |
| 112 | 188 |
| 113 private: | 189 private: |
| 190 void PopulateApps(app_list::AppListModel::Apps* apps) { |
| 191 for (int i = 0; |
| 192 i < static_cast<int>(WindowTypeLauncherItem::LAST_TYPE); |
| 193 ++i) { |
| 194 WindowTypeLauncherItem::Type type = |
| 195 static_cast<WindowTypeLauncherItem::Type>(i); |
| 196 |
| 197 std::string title = WindowTypeLauncherItem::GetTitle(type); |
| 198 apps->Add(new WindowTypeLauncherItem(type)); |
| 199 } |
| 200 } |
| 201 |
| 202 SkBitmap CreateSearchBoxIcon() { |
| 203 const string16 icon_text = ASCIIToUTF16("ash"); |
| 204 const gfx::Size icon_size(32, 32); |
| 205 |
| 206 gfx::Canvas canvas(icon_size, false /* is_opaque */); |
| 207 canvas.DrawStringInt(icon_text, |
| 208 gfx::Font(), |
| 209 SK_ColorBLACK, |
| 210 0, 0, icon_size.width(), icon_size.height(), |
| 211 gfx::Canvas::TEXT_ALIGN_CENTER | |
| 212 gfx::Canvas::TEXT_VALIGN_MIDDLE | |
| 213 gfx::Canvas::NO_SUBPIXEL_RENDERING); |
| 214 |
| 215 return canvas.ExtractBitmap(); |
| 216 } |
| 217 |
| 218 void DecorateSearchBox(app_list::SearchBoxModel* search_box_model) { |
| 219 search_box_model->SetIcon(CreateSearchBoxIcon()); |
| 220 search_box_model->SetHintText(ASCIIToUTF16("Type to search...")); |
| 221 } |
| 222 |
| 114 // Overridden from ash::AppListViewDelegate: | 223 // Overridden from ash::AppListViewDelegate: |
| 115 virtual void SetModel(app_list::AppListModel* model) OVERRIDE { | 224 virtual void SetModel(app_list::AppListModel* model) OVERRIDE { |
| 116 model_ = model; | 225 model_ = model; |
| 226 PopulateApps(model_->apps()); |
| 227 DecorateSearchBox(model_->search_box()); |
| 117 } | 228 } |
| 118 | 229 |
| 119 virtual void UpdateModel(const std::string& query) OVERRIDE { | 230 virtual void ActivateAppListItem(app_list::AppListItemModel* item, |
| 120 DCHECK(model_ && model_->item_count() == 0); | 231 int event_flags) OVERRIDE { |
| 232 static_cast<WindowTypeLauncherItem*>(item)->Activate(event_flags); |
| 233 } |
| 234 |
| 235 virtual void OpenSearchResult(const app_list::SearchResult& result, |
| 236 int event_flags) OVERRIDE { |
| 237 const ExampleSearchResult* example_result = |
| 238 static_cast<const ExampleSearchResult*>(&result); |
| 239 WindowTypeLauncherItem::Activate(example_result->type(), event_flags); |
| 240 } |
| 241 |
| 242 virtual void StartSearch() OVERRIDE { |
| 243 string16 query; |
| 244 TrimWhitespace(model_->search_box()->text(), TRIM_ALL, &query); |
| 245 query = base::i18n::ToLower(query); |
| 246 |
| 247 model_->results()->DeleteAll(); |
| 248 if (query.empty()) |
| 249 return; |
| 121 | 250 |
| 122 for (int i = 0; | 251 for (int i = 0; |
| 123 i < static_cast<int>(WindowTypeLauncherItem::LAST_TYPE); | 252 i < static_cast<int>(WindowTypeLauncherItem::LAST_TYPE); |
| 124 ++i) { | 253 ++i) { |
| 125 WindowTypeLauncherItem::Type type = | 254 WindowTypeLauncherItem::Type type = |
| 126 static_cast<WindowTypeLauncherItem::Type>(i); | 255 static_cast<WindowTypeLauncherItem::Type>(i); |
| 127 | 256 |
| 128 std::string title = WindowTypeLauncherItem::GetTitle(type); | 257 string16 title = UTF8ToUTF16(WindowTypeLauncherItem::GetTitle(type)); |
| 129 if (title.find(query) != std::string::npos) | 258 if (base::i18n::StringSearchIgnoringCaseAndAccents(query, title)) |
| 130 model_->AddItem(new WindowTypeLauncherItem(type)); | 259 model_->results()->Add(new ExampleSearchResult(type, query)); |
| 131 } | 260 } |
| 132 } | 261 } |
| 133 | 262 |
| 134 virtual void OnAppListItemActivated(app_list::AppListItemModel* item, | 263 virtual void StopSearch() OVERRIDE { |
| 135 int event_flags) OVERRIDE { | 264 // Nothing needs to be done. |
| 136 static_cast<WindowTypeLauncherItem*>(item)->Activate(event_flags); | |
| 137 } | 265 } |
| 138 | 266 |
| 139 virtual void Close() OVERRIDE { | 267 virtual void Close() OVERRIDE { |
| 140 DCHECK(ash::Shell::HasInstance()); | 268 DCHECK(ash::Shell::HasInstance()); |
| 141 if (Shell::GetInstance()->GetAppListTargetVisibility()) | 269 if (Shell::GetInstance()->GetAppListTargetVisibility()) |
| 142 Shell::GetInstance()->ToggleAppList(); | 270 Shell::GetInstance()->ToggleAppList(); |
| 143 } | 271 } |
| 144 | 272 |
| 145 app_list::AppListModel* model_; | 273 app_list::AppListModel* model_; |
| 146 | 274 |
| 147 DISALLOW_COPY_AND_ASSIGN(ExampleAppListViewDelegate); | 275 DISALLOW_COPY_AND_ASSIGN(ExampleAppListViewDelegate); |
| 148 }; | 276 }; |
| 149 | 277 |
| 150 } // namespace | 278 } // namespace |
| 151 | 279 |
| 152 app_list::AppListViewDelegate* CreateAppListViewDelegate() { | 280 app_list::AppListViewDelegate* CreateAppListViewDelegate() { |
| 153 return new ExampleAppListViewDelegate; | 281 return new ExampleAppListViewDelegate; |
| 154 } | 282 } |
| 155 | 283 |
| 156 } // namespace shell | 284 } // namespace shell |
| 157 } // namespace ash | 285 } // namespace ash |
| OLD | NEW |