| 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 <memory> | 5 #include <memory> |
| 6 #include <string> | 6 #include <string> |
| 7 #include <utility> | 7 #include <utility> |
| 8 #include <vector> | 8 #include <vector> |
| 9 | 9 |
| 10 #include "ash/common/session/session_state_delegate.h" | 10 #include "ash/common/session/session_state_delegate.h" |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 50 WIDGETS_WINDOW, | 50 WIDGETS_WINDOW, |
| 51 EXAMPLES_WINDOW, | 51 EXAMPLES_WINDOW, |
| 52 LAST_TYPE, | 52 LAST_TYPE, |
| 53 }; | 53 }; |
| 54 | 54 |
| 55 WindowTypeShelfItem(const std::string& id, Type type); | 55 WindowTypeShelfItem(const std::string& id, Type type); |
| 56 ~WindowTypeShelfItem() override; | 56 ~WindowTypeShelfItem() override; |
| 57 | 57 |
| 58 static gfx::ImageSkia GetIcon(Type type) { | 58 static gfx::ImageSkia GetIcon(Type type) { |
| 59 static const SkColor kColors[] = { | 59 static const SkColor kColors[] = { |
| 60 SK_ColorRED, | 60 SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE, SK_ColorYELLOW, SK_ColorCYAN, |
| 61 SK_ColorGREEN, | |
| 62 SK_ColorBLUE, | |
| 63 SK_ColorYELLOW, | |
| 64 SK_ColorCYAN, | |
| 65 }; | 61 }; |
| 66 | 62 |
| 67 const int kIconSize = 128; | 63 const int kIconSize = 128; |
| 68 SkBitmap icon; | 64 SkBitmap icon; |
| 69 icon.allocN32Pixels(kIconSize, kIconSize); | 65 icon.allocN32Pixels(kIconSize, kIconSize); |
| 70 icon.eraseColor(kColors[static_cast<int>(type) % arraysize(kColors)]); | 66 icon.eraseColor(kColors[static_cast<int>(type) % arraysize(kColors)]); |
| 71 return gfx::ImageSkia::CreateFrom1xBitmap(icon); | 67 return gfx::ImageSkia::CreateFrom1xBitmap(icon); |
| 72 } | 68 } |
| 73 | 69 |
| 74 // The text below is not localized as this is an example code. | 70 // The text below is not localized as this is an example code. |
| (...skipping 22 matching lines...) Expand all Loading... |
| 97 case WIDGETS_WINDOW: | 93 case WIDGETS_WINDOW: |
| 98 return "Creates a window to show example widgets"; | 94 return "Creates a window to show example widgets"; |
| 99 case EXAMPLES_WINDOW: | 95 case EXAMPLES_WINDOW: |
| 100 return "Creates a window to show views example."; | 96 return "Creates a window to show views example."; |
| 101 default: | 97 default: |
| 102 return std::string(); | 98 return std::string(); |
| 103 } | 99 } |
| 104 } | 100 } |
| 105 | 101 |
| 106 static void ActivateItem(Type type, int event_flags) { | 102 static void ActivateItem(Type type, int event_flags) { |
| 107 switch (type) { | 103 switch (type) { |
| 108 case TOPLEVEL_WINDOW: { | 104 case TOPLEVEL_WINDOW: { |
| 109 ToplevelWindow::CreateParams params; | 105 ToplevelWindow::CreateParams params; |
| 110 params.can_resize = true; | 106 params.can_resize = true; |
| 111 ToplevelWindow::CreateToplevelWindow(params); | 107 ToplevelWindow::CreateToplevelWindow(params); |
| 112 break; | 108 break; |
| 113 } | 109 } |
| 114 case NON_RESIZABLE_WINDOW: { | 110 case NON_RESIZABLE_WINDOW: { |
| 115 ToplevelWindow::CreateToplevelWindow(ToplevelWindow::CreateParams()); | 111 ToplevelWindow::CreateToplevelWindow(ToplevelWindow::CreateParams()); |
| 116 break; | 112 break; |
| 117 } | 113 } |
| (...skipping 25 matching lines...) Expand all Loading... |
| 143 DISALLOW_COPY_AND_ASSIGN(WindowTypeShelfItem); | 139 DISALLOW_COPY_AND_ASSIGN(WindowTypeShelfItem); |
| 144 }; | 140 }; |
| 145 | 141 |
| 146 WindowTypeShelfItem::WindowTypeShelfItem(const std::string& id, Type type) | 142 WindowTypeShelfItem::WindowTypeShelfItem(const std::string& id, Type type) |
| 147 : app_list::AppListItem(id), type_(type) { | 143 : app_list::AppListItem(id), type_(type) { |
| 148 std::string title(GetTitle(type)); | 144 std::string title(GetTitle(type)); |
| 149 SetIcon(GetIcon(type)); | 145 SetIcon(GetIcon(type)); |
| 150 SetName(title); | 146 SetName(title); |
| 151 } | 147 } |
| 152 | 148 |
| 153 WindowTypeShelfItem::~WindowTypeShelfItem() { | 149 WindowTypeShelfItem::~WindowTypeShelfItem() {} |
| 154 } | |
| 155 | 150 |
| 156 // ExampleSearchResult is an app list search result. It provides what icon to | 151 // ExampleSearchResult is an app list search result. It provides what icon to |
| 157 // show, what should title and details text look like. It also carries the | 152 // show, what should title and details text look like. It also carries the |
| 158 // matching window launch type so that AppListViewDelegate knows how to open | 153 // matching window launch type so that AppListViewDelegate knows how to open |
| 159 // it. | 154 // it. |
| 160 class ExampleSearchResult : public app_list::SearchResult { | 155 class ExampleSearchResult : public app_list::SearchResult { |
| 161 public: | 156 public: |
| 162 ExampleSearchResult(WindowTypeShelfItem::Type type, | 157 ExampleSearchResult(WindowTypeShelfItem::Type type, |
| 163 const base::string16& query) | 158 const base::string16& query) |
| 164 : type_(type) { | 159 : type_(type) { |
| 165 SetIcon(WindowTypeShelfItem::GetIcon(type_)); | 160 SetIcon(WindowTypeShelfItem::GetIcon(type_)); |
| 166 | 161 |
| 167 base::string16 title = | 162 base::string16 title = |
| 168 base::UTF8ToUTF16(WindowTypeShelfItem::GetTitle(type_)); | 163 base::UTF8ToUTF16(WindowTypeShelfItem::GetTitle(type_)); |
| 169 set_title(title); | 164 set_title(title); |
| 170 | 165 |
| 171 Tags title_tags; | 166 Tags title_tags; |
| 172 const size_t match_len = query.length(); | 167 const size_t match_len = query.length(); |
| 173 | 168 |
| 174 // Highlight matching parts in title with bold. | 169 // Highlight matching parts in title with bold. |
| 175 // Note the following is not a proper way to handle i18n string. | 170 // Note the following is not a proper way to handle i18n string. |
| 176 title = base::i18n::ToLower(title); | 171 title = base::i18n::ToLower(title); |
| 177 size_t match_start = title.find(query); | 172 size_t match_start = title.find(query); |
| 178 while (match_start != base::string16::npos) { | 173 while (match_start != base::string16::npos) { |
| 179 title_tags.push_back(Tag(Tag::MATCH, | 174 title_tags.push_back( |
| 180 match_start, | 175 Tag(Tag::MATCH, match_start, match_start + match_len)); |
| 181 match_start + match_len)); | |
| 182 match_start = title.find(query, match_start + match_len); | 176 match_start = title.find(query, match_start + match_len); |
| 183 } | 177 } |
| 184 set_title_tags(title_tags); | 178 set_title_tags(title_tags); |
| 185 | 179 |
| 186 base::string16 details = | 180 base::string16 details = |
| 187 base::UTF8ToUTF16(WindowTypeShelfItem::GetDetails(type_)); | 181 base::UTF8ToUTF16(WindowTypeShelfItem::GetDetails(type_)); |
| 188 set_details(details); | 182 set_details(details); |
| 189 Tags details_tags; | 183 Tags details_tags; |
| 190 details_tags.push_back(Tag(Tag::DIM, 0, details.length())); | 184 details_tags.push_back(Tag(Tag::DIM, 0, details.length())); |
| 191 set_details_tags(details_tags); | 185 set_details_tags(details_tags); |
| 192 } | 186 } |
| 193 | 187 |
| 194 WindowTypeShelfItem::Type type() const { return type_; } | 188 WindowTypeShelfItem::Type type() const { return type_; } |
| 195 | 189 |
| 196 // app_list::SearchResult: | 190 // app_list::SearchResult: |
| 197 std::unique_ptr<SearchResult> Duplicate() const override { | 191 std::unique_ptr<SearchResult> Duplicate() const override { |
| 198 return std::unique_ptr<SearchResult>(); | 192 return std::unique_ptr<SearchResult>(); |
| 199 } | 193 } |
| 200 | 194 |
| 201 private: | 195 private: |
| 202 WindowTypeShelfItem::Type type_; | 196 WindowTypeShelfItem::Type type_; |
| 203 | 197 |
| 204 DISALLOW_COPY_AND_ASSIGN(ExampleSearchResult); | 198 DISALLOW_COPY_AND_ASSIGN(ExampleSearchResult); |
| 205 }; | 199 }; |
| 206 | 200 |
| 207 class ExampleAppListViewDelegate : public app_list::AppListViewDelegate { | 201 class ExampleAppListViewDelegate : public app_list::AppListViewDelegate { |
| 208 public: | 202 public: |
| 209 ExampleAppListViewDelegate() | 203 ExampleAppListViewDelegate() : model_(new app_list::AppListModel) { |
| 210 : model_(new app_list::AppListModel) { | |
| 211 PopulateApps(); | 204 PopulateApps(); |
| 212 DecorateSearchBox(model_->search_box()); | 205 DecorateSearchBox(model_->search_box()); |
| 213 } | 206 } |
| 214 | 207 |
| 215 private: | 208 private: |
| 216 void PopulateApps() { | 209 void PopulateApps() { |
| 217 for (int i = 0; i < static_cast<int>(WindowTypeShelfItem::LAST_TYPE); ++i) { | 210 for (int i = 0; i < static_cast<int>(WindowTypeShelfItem::LAST_TYPE); ++i) { |
| 218 WindowTypeShelfItem::Type type = | 211 WindowTypeShelfItem::Type type = |
| 219 static_cast<WindowTypeShelfItem::Type>(i); | 212 static_cast<WindowTypeShelfItem::Type>(i); |
| 220 std::string id = base::IntToString(i); | 213 std::string id = base::IntToString(i); |
| 221 std::unique_ptr<WindowTypeShelfItem> shelf_item( | 214 std::unique_ptr<WindowTypeShelfItem> shelf_item( |
| 222 new WindowTypeShelfItem(id, type)); | 215 new WindowTypeShelfItem(id, type)); |
| 223 model_->AddItem(std::move(shelf_item)); | 216 model_->AddItem(std::move(shelf_item)); |
| 224 } | 217 } |
| 225 } | 218 } |
| 226 | 219 |
| 227 gfx::ImageSkia CreateSearchBoxIcon() { | 220 gfx::ImageSkia CreateSearchBoxIcon() { |
| 228 const base::string16 icon_text = base::ASCIIToUTF16("ash"); | 221 const base::string16 icon_text = base::ASCIIToUTF16("ash"); |
| 229 const gfx::Size icon_size(32, 32); | 222 const gfx::Size icon_size(32, 32); |
| 230 | 223 |
| 231 gfx::Canvas canvas(icon_size, 1.0f, false /* is_opaque */); | 224 gfx::Canvas canvas(icon_size, 1.0f, false /* is_opaque */); |
| 232 canvas.DrawStringRectWithFlags( | 225 canvas.DrawStringRectWithFlags( |
| 233 icon_text, | 226 icon_text, gfx::FontList(), SK_ColorBLACK, gfx::Rect(icon_size), |
| 234 gfx::FontList(), | |
| 235 SK_ColorBLACK, | |
| 236 gfx::Rect(icon_size), | |
| 237 gfx::Canvas::TEXT_ALIGN_CENTER | gfx::Canvas::NO_SUBPIXEL_RENDERING); | 227 gfx::Canvas::TEXT_ALIGN_CENTER | gfx::Canvas::NO_SUBPIXEL_RENDERING); |
| 238 | 228 |
| 239 return gfx::ImageSkia(canvas.ExtractImageRep()); | 229 return gfx::ImageSkia(canvas.ExtractImageRep()); |
| 240 } | 230 } |
| 241 | 231 |
| 242 void DecorateSearchBox(app_list::SearchBoxModel* search_box_model) { | 232 void DecorateSearchBox(app_list::SearchBoxModel* search_box_model) { |
| 243 search_box_model->SetIcon(CreateSearchBoxIcon()); | 233 search_box_model->SetIcon(CreateSearchBoxIcon()); |
| 244 search_box_model->SetHintText(base::ASCIIToUTF16("Type to search...")); | 234 search_box_model->SetHintText(base::ASCIIToUTF16("Type to search...")); |
| 245 } | 235 } |
| 246 | 236 |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 291 model_->results()->DeleteAll(); | 281 model_->results()->DeleteAll(); |
| 292 if (query.empty()) | 282 if (query.empty()) |
| 293 return; | 283 return; |
| 294 | 284 |
| 295 for (int i = 0; i < static_cast<int>(WindowTypeShelfItem::LAST_TYPE); ++i) { | 285 for (int i = 0; i < static_cast<int>(WindowTypeShelfItem::LAST_TYPE); ++i) { |
| 296 WindowTypeShelfItem::Type type = | 286 WindowTypeShelfItem::Type type = |
| 297 static_cast<WindowTypeShelfItem::Type>(i); | 287 static_cast<WindowTypeShelfItem::Type>(i); |
| 298 | 288 |
| 299 base::string16 title = | 289 base::string16 title = |
| 300 base::UTF8ToUTF16(WindowTypeShelfItem::GetTitle(type)); | 290 base::UTF8ToUTF16(WindowTypeShelfItem::GetTitle(type)); |
| 301 if (base::i18n::StringSearchIgnoringCaseAndAccents( | 291 if (base::i18n::StringSearchIgnoringCaseAndAccents(query, title, NULL, |
| 302 query, title, NULL, NULL)) { | 292 NULL)) { |
| 303 model_->results()->Add(new ExampleSearchResult(type, query)); | 293 model_->results()->Add(new ExampleSearchResult(type, query)); |
| 304 } | 294 } |
| 305 } | 295 } |
| 306 } | 296 } |
| 307 | 297 |
| 308 void StopSearch() override { | 298 void StopSearch() override { |
| 309 // Nothing needs to be done. | 299 // Nothing needs to be done. |
| 310 } | 300 } |
| 311 | 301 |
| 312 void ViewInitialized() override { | 302 void ViewInitialized() override { |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 364 }; | 354 }; |
| 365 | 355 |
| 366 } // namespace | 356 } // namespace |
| 367 | 357 |
| 368 app_list::AppListViewDelegate* CreateAppListViewDelegate() { | 358 app_list::AppListViewDelegate* CreateAppListViewDelegate() { |
| 369 return new ExampleAppListViewDelegate; | 359 return new ExampleAppListViewDelegate; |
| 370 } | 360 } |
| 371 | 361 |
| 372 } // namespace shell | 362 } // namespace shell |
| 373 } // namespace ash | 363 } // namespace ash |
| OLD | NEW |