launcher.cc revision 424c4d7b64af9d0d8fd9624f381f469654d5e3d2
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. 4 5#include "ash/launcher/launcher.h" 6 7#include <algorithm> 8#include <cmath> 9 10#include "ash/focus_cycler.h" 11#include "ash/launcher/launcher_delegate.h" 12#include "ash/launcher/launcher_item_delegate.h" 13#include "ash/launcher/launcher_item_delegate_manager.h" 14#include "ash/launcher/launcher_model.h" 15#include "ash/launcher/launcher_navigator.h" 16#include "ash/launcher/launcher_view.h" 17#include "ash/root_window_controller.h" 18#include "ash/screen_ash.h" 19#include "ash/shelf/shelf_layout_manager.h" 20#include "ash/shelf/shelf_widget.h" 21#include "ash/shell.h" 22#include "ash/shell_delegate.h" 23#include "ash/shell_window_ids.h" 24#include "ash/wm/property_util.h" 25#include "ash/wm/window_properties.h" 26#include "grit/ash_resources.h" 27#include "ui/aura/client/activation_client.h" 28#include "ui/aura/root_window.h" 29#include "ui/aura/window.h" 30#include "ui/aura/window_observer.h" 31#include "ui/base/resource/resource_bundle.h" 32#include "ui/compositor/layer.h" 33#include "ui/gfx/canvas.h" 34#include "ui/gfx/image/image.h" 35#include "ui/gfx/image/image_skia_operations.h" 36#include "ui/gfx/skbitmap_operations.h" 37#include "ui/views/accessible_pane_view.h" 38#include "ui/views/widget/widget.h" 39#include "ui/views/widget/widget_delegate.h" 40 41namespace ash { 42 43const char Launcher::kNativeViewName[] = "LauncherView"; 44 45Launcher::Launcher(LauncherModel* launcher_model, 46 LauncherDelegate* launcher_delegate, 47 ShelfWidget* shelf_widget) 48 : launcher_view_(NULL), 49 alignment_(shelf_widget->GetAlignment()), 50 delegate_(launcher_delegate), 51 shelf_widget_(shelf_widget) { 52 launcher_view_ = new internal::LauncherView( 53 launcher_model, delegate_, shelf_widget_->shelf_layout_manager()); 54 launcher_view_->Init(); 55 shelf_widget_->GetContentsView()->AddChildView(launcher_view_); 56 shelf_widget_->GetNativeView()->SetName(kNativeViewName); 57 delegate_->OnLauncherCreated(this); 58} 59 60Launcher::~Launcher() { 61 delegate_->OnLauncherDestroyed(this); 62} 63 64// static 65Launcher* Launcher::ForPrimaryDisplay() { 66 ShelfWidget* shelf_widget = internal::RootWindowController::ForLauncher( 67 Shell::GetPrimaryRootWindow())->shelf(); 68 return shelf_widget ? shelf_widget->launcher() : NULL; 69} 70 71// static 72Launcher* Launcher::ForWindow(aura::Window* window) { 73 ShelfWidget* shelf_widget = 74 internal::RootWindowController::ForLauncher(window)->shelf(); 75 return shelf_widget ? shelf_widget->launcher() : NULL; 76} 77 78void Launcher::SetAlignment(ShelfAlignment alignment) { 79 alignment_ = alignment; 80 launcher_view_->OnShelfAlignmentChanged(); 81 // ShelfLayoutManager will resize the launcher. 82} 83 84gfx::Rect Launcher::GetScreenBoundsOfItemIconForWindow(aura::Window* window) { 85 LauncherID id = delegate_->GetIDByWindow(window); 86 gfx::Rect bounds(launcher_view_->GetIdealBoundsOfItemIcon(id)); 87 gfx::Point screen_origin; 88 views::View::ConvertPointToScreen(launcher_view_, &screen_origin); 89 return gfx::Rect(screen_origin.x() + bounds.x(), 90 screen_origin.y() + bounds.y(), 91 bounds.width(), 92 bounds.height()); 93} 94 95void Launcher::UpdateIconPositionForWindow(aura::Window* window) { 96 launcher_view_->UpdatePanelIconPosition( 97 delegate_->GetIDByWindow(window), 98 ash::ScreenAsh::ConvertRectFromScreen( 99 shelf_widget()->GetNativeView(), 100 window->GetBoundsInScreen()).CenterPoint()); 101} 102 103void Launcher::ActivateLauncherItem(int index) { 104 // We pass in a keyboard event which will then trigger a switch to the 105 // next item if the current one is already active. 106 ui::KeyEvent event(ui::ET_KEY_RELEASED, 107 ui::VKEY_UNKNOWN, // The actual key gets ignored. 108 ui::EF_NONE, 109 false); 110 111 const ash::LauncherItem& item = launcher_view_->model()->items()[index]; 112 Shell::GetInstance()->launcher_item_delegate_manager()-> 113 GetLauncherItemDelegate(item.type)->ItemSelected(item, event); 114} 115 116void Launcher::CycleWindowLinear(CycleDirection direction) { 117 int item_index = GetNextActivatedItemIndex( 118 *(launcher_view_->model()), direction); 119 if (item_index >= 0) 120 ActivateLauncherItem(item_index); 121} 122 123void Launcher::AddIconObserver(LauncherIconObserver* observer) { 124 launcher_view_->AddIconObserver(observer); 125} 126 127void Launcher::RemoveIconObserver(LauncherIconObserver* observer) { 128 launcher_view_->RemoveIconObserver(observer); 129} 130 131bool Launcher::IsShowingMenu() const { 132 return launcher_view_->IsShowingMenu(); 133} 134 135bool Launcher::IsShowingOverflowBubble() const { 136 return launcher_view_->IsShowingOverflowBubble(); 137} 138 139void Launcher::SetVisible(bool visible) const { 140 launcher_view_->SetVisible(visible); 141} 142 143bool Launcher::IsVisible() const { 144 return launcher_view_->visible(); 145} 146 147void Launcher::SchedulePaint() { 148 launcher_view_->SchedulePaintForAllButtons(); 149} 150 151views::View* Launcher::GetAppListButtonView() const { 152 return launcher_view_->GetAppListButtonView(); 153} 154 155void Launcher::LaunchAppIndexAt(int item_index) { 156 LauncherModel* launcher_model = launcher_view_->model(); 157 const LauncherItems& items = launcher_model->items(); 158 int item_count = launcher_model->item_count(); 159 int indexes_left = item_index >= 0 ? item_index : item_count; 160 int found_index = -1; 161 162 // Iterating until we have hit the index we are interested in which 163 // is true once indexes_left becomes negative. 164 for (int i = 0; i < item_count && indexes_left >= 0; i++) { 165 if (items[i].type != TYPE_APP_LIST) { 166 found_index = i; 167 indexes_left--; 168 } 169 } 170 171 // There are two ways how found_index can be valid: a.) the nth item was 172 // found (which is true when indexes_left is -1) or b.) the last item was 173 // requested (which is true when index was passed in as a negative number). 174 if (found_index >= 0 && (indexes_left == -1 || item_index < 0)) { 175 // Then set this one as active (or advance to the next item of its kind). 176 ActivateLauncherItem(found_index); 177 } 178} 179 180internal::LauncherView* Launcher::GetLauncherViewForTest() { 181 return launcher_view_; 182} 183 184void Launcher::SetLauncherViewBounds(gfx::Rect bounds) { 185 launcher_view_->SetBoundsRect(bounds); 186} 187 188gfx::Rect Launcher::GetLauncherViewBounds() const { 189 return launcher_view_->bounds(); 190} 191 192app_list::ApplicationDragAndDropHost* Launcher::GetDragAndDropHostForAppList() { 193 return launcher_view_; 194} 195 196} // namespace ash 197