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.
5 #include "ash/launcher/launcher.h"
10 #include "ash/focus_cycler.h"
11 #include "ash/launcher/launcher_delegate.h"
12 #include "ash/launcher/launcher_model.h"
13 #include "ash/launcher/launcher_navigator.h"
14 #include "ash/launcher/launcher_view.h"
15 #include "ash/root_window_controller.h"
16 #include "ash/screen_ash.h"
17 #include "ash/shelf/shelf_layout_manager.h"
18 #include "ash/shelf/shelf_widget.h"
19 #include "ash/shell.h"
20 #include "ash/shell_delegate.h"
21 #include "ash/shell_window_ids.h"
22 #include "ash/wm/property_util.h"
23 #include "ash/wm/window_properties.h"
24 #include "grit/ash_resources.h"
25 #include "ui/aura/client/activation_client.h"
26 #include "ui/aura/root_window.h"
27 #include "ui/aura/window.h"
28 #include "ui/aura/window_observer.h"
29 #include "ui/base/resource/resource_bundle.h"
30 #include "ui/compositor/layer.h"
31 #include "ui/gfx/canvas.h"
32 #include "ui/gfx/image/image.h"
33 #include "ui/gfx/image/image_skia_operations.h"
34 #include "ui/gfx/skbitmap_operations.h"
35 #include "ui/views/accessible_pane_view.h"
36 #include "ui/views/widget/widget.h"
37 #include "ui/views/widget/widget_delegate.h"
41 Launcher::Launcher(LauncherModel
* launcher_model
,
42 LauncherDelegate
* launcher_delegate
,
43 ShelfWidget
* shelf_widget
)
44 : launcher_view_(NULL
),
45 alignment_(shelf_widget
->GetAlignment()),
46 delegate_(launcher_delegate
),
47 shelf_widget_(shelf_widget
) {
48 launcher_view_
= new internal::LauncherView(
49 launcher_model
, delegate_
, shelf_widget_
->shelf_layout_manager());
50 launcher_view_
->Init();
51 shelf_widget_
->GetContentsView()->AddChildView(launcher_view_
);
52 shelf_widget_
->GetNativeView()->SetName("LauncherView");
53 shelf_widget_
->GetNativeView()->SetProperty(
54 internal::kStayInSameRootWindowKey
, true);
55 delegate_
->OnLauncherCreated(this);
58 Launcher::~Launcher() {
59 delegate_
->OnLauncherDestroyed(this);
63 Launcher
* Launcher::ForPrimaryDisplay() {
64 return internal::RootWindowController::ForLauncher(
65 Shell::GetPrimaryRootWindow())->shelf()->launcher();
69 Launcher
* Launcher::ForWindow(aura::Window
* window
) {
70 return internal::RootWindowController::ForLauncher(window
)->
74 void Launcher::SetAlignment(ShelfAlignment alignment
) {
75 alignment_
= alignment
;
76 launcher_view_
->OnShelfAlignmentChanged();
77 // ShelfLayoutManager will resize the launcher.
80 gfx::Rect
Launcher::GetScreenBoundsOfItemIconForWindow(aura::Window
* window
) {
81 LauncherID id
= delegate_
->GetIDByWindow(window
);
82 gfx::Rect
bounds(launcher_view_
->GetIdealBoundsOfItemIcon(id
));
86 gfx::Point screen_origin
;
87 views::View::ConvertPointToScreen(launcher_view_
, &screen_origin
);
88 return gfx::Rect(screen_origin
.x() + bounds
.x(),
89 screen_origin
.y() + bounds
.y(),
94 void Launcher::UpdateIconPositionForWindow(aura::Window
* window
) {
95 launcher_view_
->UpdatePanelIconPosition(
96 delegate_
->GetIDByWindow(window
),
97 ash::ScreenAsh::ConvertRectFromScreen(
98 shelf_widget()->GetNativeView(),
99 window
->GetBoundsInScreen()).CenterPoint());
102 void Launcher::ActivateLauncherItem(int index
) {
103 const ash::LauncherItems
& items
=
104 launcher_view_
->model()->items();
105 ui::MouseEvent
event(ui::ET_MOUSE_PRESSED
,
109 delegate_
->ItemClicked(items
[index
], event
);
112 void Launcher::CycleWindowLinear(CycleDirection direction
) {
113 int item_index
= GetNextActivatedItemIndex(
114 *(launcher_view_
->model()), direction
);
116 ActivateLauncherItem(item_index
);
119 void Launcher::AddIconObserver(LauncherIconObserver
* observer
) {
120 launcher_view_
->AddIconObserver(observer
);
123 void Launcher::RemoveIconObserver(LauncherIconObserver
* observer
) {
124 launcher_view_
->RemoveIconObserver(observer
);
127 bool Launcher::IsShowingMenu() const {
128 return launcher_view_
->IsShowingMenu();
131 void Launcher::ShowContextMenu(const gfx::Point
& location
) {
132 launcher_view_
->ShowContextMenu(location
, false);
135 bool Launcher::IsShowingOverflowBubble() const {
136 return launcher_view_
->IsShowingOverflowBubble();
139 void Launcher::SetVisible(bool visible
) const {
140 launcher_view_
->SetVisible(visible
);
143 bool Launcher::IsVisible() const {
144 return launcher_view_
->visible();
147 views::View
* Launcher::GetAppListButtonView() const {
148 return launcher_view_
->GetAppListButtonView();
151 void Launcher::SwitchToWindow(int window_index
) {
152 LauncherModel
* launcher_model
= launcher_view_
->model();
153 const LauncherItems
& items
= launcher_model
->items();
154 int item_count
= launcher_model
->item_count();
155 int indexes_left
= window_index
>= 0 ? window_index
: item_count
;
156 int found_index
= -1;
158 // Iterating until we have hit the index we are interested in which
159 // is true once indexes_left becomes negative.
160 for (int i
= 0; i
< item_count
&& indexes_left
>= 0; i
++) {
161 if (items
[i
].type
!= TYPE_APP_LIST
&&
162 items
[i
].type
!= TYPE_BROWSER_SHORTCUT
) {
168 // There are two ways how found_index can be valid: a.) the nth item was
169 // found (which is true when indexes_left is -1) or b.) the last item was
170 // requested (which is true when index was passed in as a negative number).
171 if (found_index
>= 0 && (indexes_left
== -1 || window_index
< 0) &&
172 (items
[found_index
].status
== ash::STATUS_RUNNING
||
173 items
[found_index
].status
== ash::STATUS_CLOSED
)) {
174 // Then set this one as active.
175 ActivateLauncherItem(found_index
);
179 internal::LauncherView
* Launcher::GetLauncherViewForTest() {
180 return launcher_view_
;
183 void Launcher::SetLauncherViewBounds(gfx::Rect bounds
) {
184 launcher_view_
->SetBoundsRect(bounds
);
187 gfx::Rect
Launcher::GetLauncherViewBounds() const {
188 return launcher_view_
->bounds();