OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2011 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 "ui/aura_shell/app_list.h" |
| 6 |
| 7 #include "base/bind.h" |
| 8 #include "ui/aura/event.h" |
| 9 #include "ui/aura/window.h" |
| 10 #include "ui/aura_shell/shell.h" |
| 11 #include "ui/aura_shell/shell_delegate.h" |
| 12 #include "ui/aura_shell/shell_window_ids.h" |
| 13 #include "ui/gfx/screen.h" |
| 14 |
| 15 namespace aura_shell { |
| 16 namespace internal { |
| 17 |
| 18 namespace { |
| 19 |
| 20 // Gets preferred bounds of app list window in show/hide state. |
| 21 gfx::Rect GetPreferredBounds(bool show) { |
| 22 // The y-axis offset used at the beginning of showing animation. |
| 23 static const int kMoveUpAnimationOffset = 50; |
| 24 |
| 25 gfx::Point cursor = gfx::Screen::GetCursorScreenPoint(); |
| 26 gfx::Rect work_area = gfx::Screen::GetMonitorWorkAreaNearestPoint(cursor); |
| 27 gfx::Rect widget_bounds(work_area); |
| 28 widget_bounds.Inset(150, 100); |
| 29 if (!show) |
| 30 widget_bounds.Offset(0, kMoveUpAnimationOffset); |
| 31 |
| 32 return widget_bounds; |
| 33 } |
| 34 |
| 35 ui::Layer* GetLayer(views::Widget* widget) { |
| 36 return widget->GetNativeView()->layer(); |
| 37 } |
| 38 |
| 39 } // namespace |
| 40 |
| 41 //////////////////////////////////////////////////////////////////////////////// |
| 42 // AppList, public: |
| 43 |
| 44 AppList::AppList() |
| 45 : aura::EventFilter(NULL), |
| 46 is_visible_(false), |
| 47 widget_(NULL), |
| 48 ALLOW_THIS_IN_INITIALIZER_LIST(set_widget_factory_(this)) { |
| 49 } |
| 50 |
| 51 AppList::~AppList() { |
| 52 ResetWidget(); |
| 53 } |
| 54 |
| 55 void AppList::SetVisible(bool visible) { |
| 56 if (visible == is_visible_) |
| 57 return; |
| 58 |
| 59 is_visible_ = visible; |
| 60 |
| 61 if (widget_) { |
| 62 ScheduleAnimation(); |
| 63 } else if (is_visible_ && !set_widget_factory_.HasWeakPtrs()) { |
| 64 Shell::GetInstance()->delegate()->RequestAppListWidget( |
| 65 base::Bind(&AppList::SetWidget, set_widget_factory_.GetWeakPtr())); |
| 66 } |
| 67 } |
| 68 |
| 69 bool AppList::IsVisible() { |
| 70 return widget_ && widget_->IsVisible(); |
| 71 } |
| 72 |
| 73 //////////////////////////////////////////////////////////////////////////////// |
| 74 // AppList, private: |
| 75 |
| 76 void AppList::SetWidget(views::Widget* widget) { |
| 77 DCHECK(widget_ == NULL); |
| 78 set_widget_factory_.InvalidateWeakPtrs(); |
| 79 |
| 80 if (is_visible_) { |
| 81 widget_ = widget; |
| 82 widget_->AddObserver(this); |
| 83 GetLayer(widget_)->GetAnimator()->AddObserver(this); |
| 84 Shell::GetInstance()->AddDesktopEventFilter(this); |
| 85 |
| 86 widget_->SetBounds(GetPreferredBounds(false)); |
| 87 widget_->SetOpacity(0); |
| 88 ScheduleAnimation(); |
| 89 |
| 90 widget_->Show(); |
| 91 widget_->Activate(); |
| 92 } else { |
| 93 widget->Close(); |
| 94 } |
| 95 } |
| 96 |
| 97 void AppList::ResetWidget() { |
| 98 if (!widget_) |
| 99 return; |
| 100 |
| 101 widget_->RemoveObserver(this); |
| 102 GetLayer(widget_)->GetAnimator()->RemoveObserver(this); |
| 103 Shell::GetInstance()->RemoveDesktopEventFilter(this); |
| 104 widget_ = NULL; |
| 105 } |
| 106 |
| 107 void AppList::ScheduleAnimation() { |
| 108 ui::Layer* layer = GetLayer(widget_); |
| 109 ui::LayerAnimator::ScopedSettings app_list_animation(layer->GetAnimator()); |
| 110 layer->SetBounds(GetPreferredBounds(is_visible_)); |
| 111 layer->SetOpacity(is_visible_ ? 1.0 : 0.0); |
| 112 |
| 113 ui::Layer* default_container_layer = Shell::GetInstance()->GetContainer( |
| 114 internal::kShellWindowId_DefaultContainer)->layer(); |
| 115 ui::LayerAnimator::ScopedSettings default_container_animation( |
| 116 default_container_layer->GetAnimator()); |
| 117 default_container_layer->SetOpacity(is_visible_ ? 0.0 : 1.0); |
| 118 } |
| 119 |
| 120 //////////////////////////////////////////////////////////////////////////////// |
| 121 // AppList, aura::EventFilter implementation: |
| 122 |
| 123 bool AppList::PreHandleKeyEvent(aura::Window* target, |
| 124 aura::KeyEvent* event) { |
| 125 return false; |
| 126 } |
| 127 |
| 128 bool AppList::PreHandleMouseEvent(aura::Window* target, |
| 129 aura::MouseEvent* event) { |
| 130 if (widget_ && is_visible_ && event->type() == ui::ET_MOUSE_PRESSED) { |
| 131 aura::MouseEvent translated(*event, target, widget_->GetNativeView()); |
| 132 if (!widget_->GetNativeView()->ContainsPoint(translated.location())) |
| 133 SetVisible(false); |
| 134 } |
| 135 return false; |
| 136 } |
| 137 |
| 138 ui::TouchStatus AppList::PreHandleTouchEvent(aura::Window* target, |
| 139 aura::TouchEvent* event) { |
| 140 return ui::TOUCH_STATUS_UNKNOWN; |
| 141 } |
| 142 |
| 143 //////////////////////////////////////////////////////////////////////////////// |
| 144 // AppList, ui::LayerAnimationObserver implementation: |
| 145 |
| 146 void AppList::OnLayerAnimationEnded( |
| 147 const ui::LayerAnimationSequence* sequence) { |
| 148 if (!is_visible_ ) |
| 149 widget_->Close(); |
| 150 } |
| 151 |
| 152 void AppList::OnLayerAnimationAborted( |
| 153 const ui::LayerAnimationSequence* sequence) { |
| 154 } |
| 155 |
| 156 void AppList::OnLayerAnimationScheduled( |
| 157 const ui::LayerAnimationSequence* sequence) { |
| 158 } |
| 159 |
| 160 //////////////////////////////////////////////////////////////////////////////// |
| 161 // AppList, views::Widget::Observer implementation: |
| 162 |
| 163 void AppList::OnWidgetClosing(views::Widget* widget) { |
| 164 DCHECK(widget_ == widget); |
| 165 ResetWidget(); |
| 166 } |
| 167 |
| 168 void AppList::OnWidgetActivationChanged(views::Widget* widget, bool active) { |
| 169 DCHECK(widget_ == widget); |
| 170 if (widget_ && is_visible_ && !active) |
| 171 SetVisible(false); |
| 172 } |
| 173 |
| 174 } // namespace internal |
| 175 } // namespace aura_shell |
OLD | NEW |