OLD | NEW |
(Empty) | |
| 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/wm/app_list_shower_delegate.h" |
| 6 |
| 7 #include "ash/ash_switches.h" |
| 8 #include "ash/root_window_controller.h" |
| 9 #include "ash/screen_util.h" |
| 10 #include "ash/shelf/shelf.h" |
| 11 #include "ash/shelf/shelf_layout_manager.h" |
| 12 #include "ash/shell.h" |
| 13 #include "ash/shell_delegate.h" |
| 14 #include "ash/shell_window_ids.h" |
| 15 #include "ash/wm/app_list_view_delegate_factory.h" |
| 16 #include "ash/wm/maximize_mode/maximize_mode_controller.h" |
| 17 #include "base/command_line.h" |
| 18 #include "ui/app_list/app_list_constants.h" |
| 19 #include "ui/app_list/app_list_switches.h" |
| 20 #include "ui/app_list/shower/app_list_shower.h" |
| 21 #include "ui/app_list/views/app_list_view.h" |
| 22 #include "ui/aura/window.h" |
| 23 #include "ui/events/event.h" |
| 24 #include "ui/keyboard/keyboard_controller.h" |
| 25 #include "ui/views/widget/widget.h" |
| 26 |
| 27 namespace ash { |
| 28 namespace { |
| 29 |
| 30 // Offset in pixels to animation away/towards the shelf. |
| 31 const int kAnimationOffset = 8; |
| 32 |
| 33 // The minimal anchor position offset to make sure that the bubble is still on |
| 34 // the screen with 8 pixels spacing on the left / right. This constant is a |
| 35 // result of minimal bubble arrow sizes and offsets. |
| 36 const int kMinimalAnchorPositionOffset = 57; |
| 37 |
| 38 // The minimal margin (in pixels) around the app list when in centered mode. |
| 39 const int kMinimalCenteredAppListMargin = 10; |
| 40 |
| 41 // Gets arrow location based on shelf alignment. |
| 42 views::BubbleBorder::Arrow GetBubbleArrow(aura::Window* window) { |
| 43 DCHECK(Shell::HasInstance()); |
| 44 return Shelf::ForWindow(window)->SelectValueForShelfAlignment( |
| 45 views::BubbleBorder::BOTTOM_CENTER, views::BubbleBorder::LEFT_CENTER, |
| 46 views::BubbleBorder::RIGHT_CENTER, views::BubbleBorder::TOP_CENTER); |
| 47 } |
| 48 |
| 49 // Using |button_bounds|, determine the anchor offset so that the bubble gets |
| 50 // shown above the shelf (used for the alternate shelf theme). |
| 51 gfx::Vector2d GetAnchorPositionOffsetToShelf( |
| 52 const gfx::Rect& button_bounds, views::Widget* widget) { |
| 53 DCHECK(Shell::HasInstance()); |
| 54 ShelfAlignment shelf_alignment = Shell::GetInstance()->GetShelfAlignment( |
| 55 widget->GetNativeView()->GetRootWindow()); |
| 56 gfx::Point anchor(button_bounds.CenterPoint()); |
| 57 switch (shelf_alignment) { |
| 58 case SHELF_ALIGNMENT_TOP: |
| 59 case SHELF_ALIGNMENT_BOTTOM: |
| 60 if (base::i18n::IsRTL()) { |
| 61 int screen_width = widget->GetWorkAreaBoundsInScreen().width(); |
| 62 return gfx::Vector2d( |
| 63 std::min(screen_width - kMinimalAnchorPositionOffset - anchor.x(), |
| 64 0), 0); |
| 65 } |
| 66 return gfx::Vector2d( |
| 67 std::max(kMinimalAnchorPositionOffset - anchor.x(), 0), 0); |
| 68 case SHELF_ALIGNMENT_LEFT: |
| 69 return gfx::Vector2d( |
| 70 0, std::max(kMinimalAnchorPositionOffset - anchor.y(), 0)); |
| 71 case SHELF_ALIGNMENT_RIGHT: |
| 72 return gfx::Vector2d( |
| 73 0, std::max(kMinimalAnchorPositionOffset - anchor.y(), 0)); |
| 74 default: |
| 75 NOTREACHED(); |
| 76 return gfx::Vector2d(); |
| 77 } |
| 78 } |
| 79 |
| 80 // Gets the point at the center of the display that a particular view is on. |
| 81 // This calculation excludes the virtual keyboard area. If the height of the |
| 82 // display area is less than |minimum_height|, its bottom will be extended to |
| 83 // that height (so that the app list never starts above the top of the screen). |
| 84 gfx::Point GetCenterOfDisplayForView(const views::View* view, |
| 85 int minimum_height) { |
| 86 aura::Window* window = view->GetWidget()->GetNativeView(); |
| 87 gfx::Rect bounds = ScreenUtil::GetShelfDisplayBoundsInRoot(window); |
| 88 bounds = ScreenUtil::ConvertRectToScreen(window->GetRootWindow(), bounds); |
| 89 |
| 90 // If the virtual keyboard is active, subtract it from the display bounds, so |
| 91 // that the app list is centered in the non-keyboard area of the display. |
| 92 // (Note that work_area excludes the keyboard, but it doesn't get updated |
| 93 // until after this function is called.) |
| 94 keyboard::KeyboardController* keyboard_controller = |
| 95 keyboard::KeyboardController::GetInstance(); |
| 96 if (keyboard_controller && keyboard_controller->keyboard_visible()) |
| 97 bounds.Subtract(keyboard_controller->current_keyboard_bounds()); |
| 98 |
| 99 // Apply the |minimum_height|. |
| 100 if (bounds.height() < minimum_height) |
| 101 bounds.set_height(minimum_height); |
| 102 |
| 103 return bounds.CenterPoint(); |
| 104 } |
| 105 |
| 106 // Gets the minimum height of the rectangle to center the app list in. |
| 107 int GetMinimumBoundsHeightForAppList(const app_list::AppListView* app_list) { |
| 108 return app_list->bounds().height() + 2 * kMinimalCenteredAppListMargin; |
| 109 } |
| 110 |
| 111 bool IsFullscreenAppListEnabled() { |
| 112 #if defined(OS_CHROMEOS) |
| 113 return base::CommandLine::ForCurrentProcess()->HasSwitch( |
| 114 switches::kAshEnableFullscreenAppList) && |
| 115 app_list::switches::IsExperimentalAppListEnabled(); |
| 116 #else |
| 117 return false; |
| 118 #endif |
| 119 } |
| 120 |
| 121 } // namespace |
| 122 |
| 123 //////////////////////////////////////////////////////////////////////////////// |
| 124 // AppListShowerDelegate, public: |
| 125 |
| 126 AppListShowerDelegate::AppListShowerDelegate( |
| 127 app_list::AppListShower* shower, |
| 128 AppListViewDelegateFactory* view_delegate_factory) |
| 129 : shower_(shower), view_delegate_factory_(view_delegate_factory) { |
| 130 Shell::GetInstance()->AddShellObserver(this); |
| 131 } |
| 132 |
| 133 AppListShowerDelegate::~AppListShowerDelegate() { |
| 134 DCHECK(view_); |
| 135 keyboard::KeyboardController* keyboard_controller = |
| 136 keyboard::KeyboardController::GetInstance(); |
| 137 if (keyboard_controller) |
| 138 keyboard_controller->RemoveObserver(this); |
| 139 views::Widget* widget = view_->GetWidget(); |
| 140 Shell::GetInstance()->RemovePreTargetHandler(this); |
| 141 Shelf::ForWindow(widget->GetNativeWindow())->RemoveIconObserver(this); |
| 142 Shell::GetInstance()->RemoveShellObserver(this); |
| 143 } |
| 144 |
| 145 app_list::AppListViewDelegate* AppListShowerDelegate::GetViewDelegate() { |
| 146 return view_delegate_factory_->GetDelegate(); |
| 147 } |
| 148 |
| 149 void AppListShowerDelegate::Init(app_list::AppListView* view, |
| 150 aura::Window* root_window, |
| 151 int current_apps_page) { |
| 152 // App list needs to know the new shelf layout in order to calculate its |
| 153 // UI layout when AppListView visibility changes. |
| 154 ash::Shell::GetPrimaryRootWindowController() |
| 155 ->GetShelfLayoutManager() |
| 156 ->UpdateAutoHideState(); |
| 157 view_ = view; |
| 158 aura::Window* container = GetRootWindowController(root_window) |
| 159 ->GetContainer(kShellWindowId_AppListContainer); |
| 160 views::View* applist_button = |
| 161 Shelf::ForWindow(container)->GetAppListButtonView(); |
| 162 is_centered_ = view->ShouldCenterWindow(); |
| 163 bool is_fullscreen = IsFullscreenAppListEnabled() && |
| 164 Shell::GetInstance() |
| 165 ->maximize_mode_controller() |
| 166 ->IsMaximizeModeWindowManagerEnabled(); |
| 167 if (is_fullscreen) { |
| 168 view->InitAsFramelessWindow( |
| 169 container, current_apps_page, |
| 170 ScreenUtil::GetDisplayWorkAreaBoundsInParent(container)); |
| 171 } else if (is_centered_) { |
| 172 // Note: We can't center the app list until we have its dimensions, so we |
| 173 // init at (0, 0) and then reset its anchor point. |
| 174 view->InitAsBubbleAtFixedLocation(container, |
| 175 current_apps_page, |
| 176 gfx::Point(), |
| 177 views::BubbleBorder::FLOAT, |
| 178 true /* border_accepts_events */); |
| 179 // The experimental app list is centered over the display of the app list |
| 180 // button that was pressed (if triggered via keyboard, this is the display |
| 181 // with the currently focused window). |
| 182 view->SetAnchorPoint(GetCenterOfDisplayForView( |
| 183 applist_button, GetMinimumBoundsHeightForAppList(view))); |
| 184 } else { |
| 185 gfx::Rect applist_button_bounds = applist_button->GetBoundsInScreen(); |
| 186 // We need the location of the button within the local screen. |
| 187 applist_button_bounds = ScreenUtil::ConvertRectFromScreen( |
| 188 root_window, |
| 189 applist_button_bounds); |
| 190 view->InitAsBubbleAttachedToAnchor( |
| 191 container, |
| 192 current_apps_page, |
| 193 Shelf::ForWindow(container)->GetAppListButtonView(), |
| 194 GetAnchorPositionOffsetToShelf( |
| 195 applist_button_bounds, |
| 196 Shelf::ForWindow(container)->GetAppListButtonView()->GetWidget()), |
| 197 GetBubbleArrow(container), |
| 198 true /* border_accepts_events */); |
| 199 view->SetArrowPaintType(views::BubbleBorder::PAINT_NONE); |
| 200 } |
| 201 |
| 202 keyboard::KeyboardController* keyboard_controller = |
| 203 keyboard::KeyboardController::GetInstance(); |
| 204 if (keyboard_controller) |
| 205 keyboard_controller->AddObserver(this); |
| 206 Shell::GetInstance()->AddPreTargetHandler(this); |
| 207 views::Widget* widget = view->GetWidget(); |
| 208 Shelf::ForWindow(widget->GetNativeWindow())->AddIconObserver(this); |
| 209 |
| 210 // By setting us as DnD recipient, the app list knows that we can |
| 211 // handle items. |
| 212 view->SetDragAndDropHostOfCurrentAppList( |
| 213 Shelf::ForWindow(root_window)->GetDragAndDropHostForAppList()); |
| 214 } |
| 215 |
| 216 void AppListShowerDelegate::OnShown(aura::Window* root_window) { |
| 217 is_visible_ = true; |
| 218 // Update applist button status when app list visibility is changed. |
| 219 Shelf::ForWindow(root_window)->GetAppListButtonView()->SchedulePaint(); |
| 220 } |
| 221 |
| 222 void AppListShowerDelegate::OnDismissed() { |
| 223 DCHECK(is_visible_); |
| 224 DCHECK(view_); |
| 225 |
| 226 is_visible_ = false; |
| 227 |
| 228 // App list needs to know the new shelf layout in order to calculate its |
| 229 // UI layout when AppListView visibility changes. |
| 230 Shell::GetPrimaryRootWindowController() |
| 231 ->GetShelfLayoutManager() |
| 232 ->UpdateAutoHideState(); |
| 233 |
| 234 // Update applist button status when app list visibility is changed. |
| 235 Shelf::ForWindow(view_->GetWidget()->GetNativeView()) |
| 236 ->GetAppListButtonView() |
| 237 ->SchedulePaint(); |
| 238 } |
| 239 |
| 240 void AppListShowerDelegate::UpdateBounds() { |
| 241 if (!view_ || !is_visible_) |
| 242 return; |
| 243 |
| 244 view_->UpdateBounds(); |
| 245 |
| 246 if (is_centered_) |
| 247 view_->SetAnchorPoint(GetCenterOfDisplayForView( |
| 248 view_, GetMinimumBoundsHeightForAppList(view_))); |
| 249 } |
| 250 |
| 251 gfx::Vector2d AppListShowerDelegate::GetVisibilityAnimationOffset( |
| 252 aura::Window* root_window) { |
| 253 DCHECK(Shell::HasInstance()); |
| 254 |
| 255 // App list needs to know the new shelf layout in order to calculate its |
| 256 // UI layout when AppListView visibility changes. |
| 257 Shell::GetPrimaryRootWindowController() |
| 258 ->GetShelfLayoutManager() |
| 259 ->UpdateAutoHideState(); |
| 260 |
| 261 ShelfAlignment shelf_alignment = |
| 262 Shell::GetInstance()->GetShelfAlignment(root_window); |
| 263 switch (shelf_alignment) { |
| 264 case SHELF_ALIGNMENT_BOTTOM: |
| 265 return gfx::Vector2d(0, kAnimationOffset); |
| 266 case SHELF_ALIGNMENT_LEFT: |
| 267 return gfx::Vector2d(-kAnimationOffset, 0); |
| 268 case SHELF_ALIGNMENT_RIGHT: |
| 269 return gfx::Vector2d(kAnimationOffset, 0); |
| 270 case SHELF_ALIGNMENT_TOP: |
| 271 return gfx::Vector2d(0, -kAnimationOffset); |
| 272 } |
| 273 NOTREACHED(); |
| 274 return gfx::Vector2d(); |
| 275 } |
| 276 |
| 277 //////////////////////////////////////////////////////////////////////////////// |
| 278 // AppListShowerDelegate, private: |
| 279 |
| 280 void AppListShowerDelegate::ProcessLocatedEvent(ui::LocatedEvent* event) { |
| 281 if (!view_ || !is_visible_) |
| 282 return; |
| 283 |
| 284 // If the event happened on a menu, then the event should not close the app |
| 285 // list. |
| 286 aura::Window* target = static_cast<aura::Window*>(event->target()); |
| 287 if (target) { |
| 288 RootWindowController* root_controller = |
| 289 GetRootWindowController(target->GetRootWindow()); |
| 290 if (root_controller) { |
| 291 aura::Window* menu_container = |
| 292 root_controller->GetContainer(kShellWindowId_MenuContainer); |
| 293 if (menu_container->Contains(target)) |
| 294 return; |
| 295 aura::Window* keyboard_container = root_controller->GetContainer( |
| 296 kShellWindowId_VirtualKeyboardContainer); |
| 297 if (keyboard_container->Contains(target)) |
| 298 return; |
| 299 } |
| 300 } |
| 301 |
| 302 aura::Window* window = view_->GetWidget()->GetNativeView()->parent(); |
| 303 if (!window->Contains(target) && |
| 304 !app_list::switches::ShouldNotDismissOnBlur()) { |
| 305 shower_->Dismiss(); |
| 306 } |
| 307 } |
| 308 |
| 309 //////////////////////////////////////////////////////////////////////////////// |
| 310 // AppListShowerDelegate, aura::EventFilter implementation: |
| 311 |
| 312 void AppListShowerDelegate::OnMouseEvent(ui::MouseEvent* event) { |
| 313 if (event->type() == ui::ET_MOUSE_PRESSED) |
| 314 ProcessLocatedEvent(event); |
| 315 } |
| 316 |
| 317 void AppListShowerDelegate::OnGestureEvent(ui::GestureEvent* event) { |
| 318 if (event->type() == ui::ET_GESTURE_TAP_DOWN) |
| 319 ProcessLocatedEvent(event); |
| 320 } |
| 321 |
| 322 //////////////////////////////////////////////////////////////////////////////// |
| 323 // AppListShowerDelegate, keyboard::KeyboardControllerObserver implementation: |
| 324 |
| 325 void AppListShowerDelegate::OnKeyboardBoundsChanging( |
| 326 const gfx::Rect& new_bounds) { |
| 327 UpdateBounds(); |
| 328 } |
| 329 |
| 330 //////////////////////////////////////////////////////////////////////////////// |
| 331 // AppListShowerDelegate, ShellObserver implementation: |
| 332 void AppListShowerDelegate::OnShelfAlignmentChanged(aura::Window* root_window) { |
| 333 if (view_) |
| 334 view_->SetBubbleArrow(GetBubbleArrow(view_->GetWidget()->GetNativeView())); |
| 335 } |
| 336 |
| 337 void AppListShowerDelegate::OnMaximizeModeStarted() { |
| 338 // The "fullscreen" app-list is initialized as in a different type of window, |
| 339 // therefore we can't switch between the fullscreen status and the normal |
| 340 // app-list bubble. App-list should be dismissed for the transition between |
| 341 // maximize mode (touch-view mode) and non-maximize mode, otherwise the app |
| 342 // list tries to behave as a bubble which leads to a crash. crbug.com/510062 |
| 343 if (IsFullscreenAppListEnabled() && is_visible_) |
| 344 shower_->Dismiss(); |
| 345 } |
| 346 |
| 347 void AppListShowerDelegate::OnMaximizeModeEnded() { |
| 348 // See the comments of OnMaximizeModeStarted(). |
| 349 if (IsFullscreenAppListEnabled() && is_visible_) |
| 350 shower_->Dismiss(); |
| 351 } |
| 352 |
| 353 //////////////////////////////////////////////////////////////////////////////// |
| 354 // AppListShowerDelegate, ShelfIconObserver implementation: |
| 355 |
| 356 void AppListShowerDelegate::OnShelfIconPositionsChanged() { |
| 357 UpdateBounds(); |
| 358 } |
| 359 |
| 360 } // namespace ash |
OLD | NEW |