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.
7 #include "ash/session_state_delegate.h"
9 #include "ash/shell/example_factory.h"
10 #include "ash/shell/toplevel_window.h"
11 #include "base/basictypes.h"
12 #include "base/callback.h"
13 #include "base/files/file_path.h"
14 #include "base/i18n/case_conversion.h"
15 #include "base/i18n/string_search.h"
16 #include "base/strings/string_util.h"
17 #include "base/strings/utf_string_conversions.h"
18 #include "ui/app_list/app_list_item_model.h"
19 #include "ui/app_list/app_list_model.h"
20 #include "ui/app_list/app_list_view_delegate.h"
21 #include "ui/app_list/search_box_model.h"
22 #include "ui/app_list/search_result.h"
23 #include "ui/gfx/canvas.h"
24 #include "ui/gfx/font.h"
25 #include "ui/gfx/image/image_skia.h"
26 #include "ui/gfx/rect.h"
27 #include "ui/views/examples/examples_window_with_content.h"
34 // WindowTypeLauncherItem is an app item of app list. It carries a window
35 // launch type and launches corresponding example window when activated.
36 class WindowTypeLauncherItem
: public app_list::AppListItemModel
{
47 explicit WindowTypeLauncherItem(Type type
) : type_(type
) {
48 SetIcon(GetIcon(type
), false);
49 SetTitle(GetTitle(type
));
52 static gfx::ImageSkia
GetIcon(Type type
) {
53 static const SkColor kColors
[] = {
61 const int kIconSize
= 128;
63 icon
.setConfig(SkBitmap::kARGB_8888_Config
, kIconSize
, kIconSize
);
65 icon
.eraseColor(kColors
[static_cast<int>(type
) % arraysize(kColors
)]);
66 return gfx::ImageSkia::CreateFrom1xBitmap(icon
);
69 // The text below is not localized as this is an example code.
70 static std::string
GetTitle(Type type
) {
73 return "Create Window";
74 case NON_RESIZABLE_WINDOW
:
75 return "Create Non-Resizable Window";
79 return "Show Example Widgets";
81 return "Open Views Examples Window";
83 return "Unknown window type.";
87 // The text below is not localized as this is an example code.
88 static std::string
GetDetails(Type type
) {
89 // Assigns details only to some types so that we see both one-line
90 // and two-line results.
93 return "Creates a window to show example widgets";
95 return "Creates a window to show views example.";
101 static void Activate(Type type
, int event_flags
) {
103 case TOPLEVEL_WINDOW
: {
104 ToplevelWindow::CreateParams params
;
105 params
.can_resize
= true;
106 ToplevelWindow::CreateToplevelWindow(params
);
109 case NON_RESIZABLE_WINDOW
: {
110 ToplevelWindow::CreateToplevelWindow(ToplevelWindow::CreateParams());
114 Shell::GetInstance()->session_state_delegate()->LockScreen();
117 case WIDGETS_WINDOW
: {
118 CreateWidgetsWindow();
121 case EXAMPLES_WINDOW
: {
122 #if !defined(OS_MACOSX)
123 views::examples::ShowExamplesWindowWithContent(
124 views::examples::DO_NOTHING_ON_CLOSE
,
125 ash::Shell::GetInstance()->browser_context());
134 void Activate(int event_flags
) {
135 Activate(type_
, event_flags
);
141 DISALLOW_COPY_AND_ASSIGN(WindowTypeLauncherItem
);
144 // ExampleSearchResult is an app list search result. It provides what icon to
145 // show, what should title and details text look like. It also carries the
146 // matching window launch type so that AppListViewDelegate knows how to open
148 class ExampleSearchResult
: public app_list::SearchResult
{
150 ExampleSearchResult(WindowTypeLauncherItem::Type type
,
151 const base::string16
& query
)
153 SetIcon(WindowTypeLauncherItem::GetIcon(type_
));
155 base::string16 title
= UTF8ToUTF16(WindowTypeLauncherItem::GetTitle(type_
));
159 const size_t match_len
= query
.length();
161 // Highlight matching parts in title with bold.
162 // Note the following is not a proper way to handle i18n string.
163 title
= base::i18n::ToLower(title
);
164 size_t match_start
= title
.find(query
);
165 while (match_start
!= base::string16::npos
) {
166 title_tags
.push_back(Tag(Tag::MATCH
,
168 match_start
+ match_len
));
169 match_start
= title
.find(query
, match_start
+ match_len
);
171 set_title_tags(title_tags
);
173 base::string16 details
=
174 UTF8ToUTF16(WindowTypeLauncherItem::GetDetails(type_
));
175 set_details(details
);
177 details_tags
.push_back(Tag(Tag::DIM
, 0, details
.length()));
178 set_details_tags(details_tags
);
181 WindowTypeLauncherItem::Type
type() const { return type_
; }
184 WindowTypeLauncherItem::Type type_
;
186 DISALLOW_COPY_AND_ASSIGN(ExampleSearchResult
);
189 class ExampleAppListViewDelegate
: public app_list::AppListViewDelegate
{
191 ExampleAppListViewDelegate() : model_(NULL
) {}
194 void PopulateApps(app_list::AppListModel::Apps
* apps
) {
196 i
< static_cast<int>(WindowTypeLauncherItem::LAST_TYPE
);
198 WindowTypeLauncherItem::Type type
=
199 static_cast<WindowTypeLauncherItem::Type
>(i
);
200 apps
->Add(new WindowTypeLauncherItem(type
));
204 gfx::ImageSkia
CreateSearchBoxIcon() {
205 const base::string16 icon_text
= ASCIIToUTF16("ash");
206 const gfx::Size
icon_size(32, 32);
208 gfx::Canvas
canvas(icon_size
, ui::SCALE_FACTOR_100P
,
209 false /* is_opaque */);
210 canvas
.DrawStringInt(icon_text
,
213 0, 0, icon_size
.width(), icon_size
.height(),
214 gfx::Canvas::TEXT_ALIGN_CENTER
|
215 gfx::Canvas::NO_SUBPIXEL_RENDERING
);
217 return gfx::ImageSkia(canvas
.ExtractImageRep());
220 void DecorateSearchBox(app_list::SearchBoxModel
* search_box_model
) {
221 search_box_model
->SetIcon(CreateSearchBoxIcon());
222 search_box_model
->SetHintText(ASCIIToUTF16("Type to search..."));
225 // Overridden from ash::AppListViewDelegate:
226 virtual void SetModel(app_list::AppListModel
* model
) OVERRIDE
{
228 PopulateApps(model_
->apps());
229 DecorateSearchBox(model_
->search_box());
232 virtual app_list::SigninDelegate
* GetSigninDelegate() OVERRIDE
{
236 virtual void GetShortcutPathForApp(
237 const std::string
& app_id
,
238 const base::Callback
<void(const base::FilePath
&)>& callback
) OVERRIDE
{
241 virtual void ActivateAppListItem(app_list::AppListItemModel
* item
,
242 int event_flags
) OVERRIDE
{
243 static_cast<WindowTypeLauncherItem
*>(item
)->Activate(event_flags
);
246 virtual void OpenSearchResult(app_list::SearchResult
* result
,
247 int event_flags
) OVERRIDE
{
248 const ExampleSearchResult
* example_result
=
249 static_cast<const ExampleSearchResult
*>(result
);
250 WindowTypeLauncherItem::Activate(example_result
->type(), event_flags
);
253 virtual void InvokeSearchResultAction(app_list::SearchResult
* result
,
255 int event_flags
) OVERRIDE
{
259 virtual void StartSearch() OVERRIDE
{
260 base::string16 query
;
261 TrimWhitespace(model_
->search_box()->text(), TRIM_ALL
, &query
);
262 query
= base::i18n::ToLower(query
);
264 model_
->results()->DeleteAll();
269 i
< static_cast<int>(WindowTypeLauncherItem::LAST_TYPE
);
271 WindowTypeLauncherItem::Type type
=
272 static_cast<WindowTypeLauncherItem::Type
>(i
);
274 base::string16 title
=
275 UTF8ToUTF16(WindowTypeLauncherItem::GetTitle(type
));
276 if (base::i18n::StringSearchIgnoringCaseAndAccents(
277 query
, title
, NULL
, NULL
)) {
278 model_
->results()->Add(new ExampleSearchResult(type
, query
));
283 virtual void StopSearch() OVERRIDE
{
284 // Nothing needs to be done.
287 virtual void Dismiss() OVERRIDE
{
288 DCHECK(ash::Shell::HasInstance());
289 if (Shell::GetInstance()->GetAppListTargetVisibility())
290 Shell::GetInstance()->ToggleAppList(NULL
);
293 virtual void ViewClosing() OVERRIDE
{
294 // Nothing needs to be done.
297 virtual void ViewActivationChanged(bool active
) OVERRIDE
{
298 // Nothing needs to be done.
301 virtual gfx::ImageSkia
GetWindowIcon() OVERRIDE
{
302 return gfx::ImageSkia();
305 virtual base::string16
GetCurrentUserName() OVERRIDE
{
306 return base::string16();
309 virtual base::string16
GetCurrentUserEmail() OVERRIDE
{
310 return base::string16();
313 virtual void OpenSettings() OVERRIDE
{
314 // Nothing needs to be done.
317 virtual void OpenHelp() OVERRIDE
{
318 // Nothing needs to be done.
321 virtual void OpenFeedback() OVERRIDE
{
322 // Nothing needs to be done.
325 app_list::AppListModel
* model_
;
327 DISALLOW_COPY_AND_ASSIGN(ExampleAppListViewDelegate
);
332 app_list::AppListViewDelegate
* CreateAppListViewDelegate() {
333 return new ExampleAppListViewDelegate
;