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/shelf/shelf_widget.h" |
| 6 |
| 7 #include "ash/focus_cycler.h" |
| 8 #include "ash/launcher/launcher_delegate.h" |
| 9 #include "ash/launcher/launcher_model.h" |
| 10 #include "ash/launcher/launcher_navigator.h" |
| 11 #include "ash/launcher/launcher_view.h" |
| 12 #include "ash/root_window_controller.h" |
| 13 #include "ash/shelf/shelf_layout_manager.h" |
| 14 #include "ash/shelf/shelf_widget.h" |
| 15 #include "ash/shell.h" |
| 16 #include "ash/shell_delegate.h" |
| 17 #include "ash/shell_window_ids.h" |
| 18 #include "ash/wm/property_util.h" |
| 19 #include "ash/wm/status_area_layout_manager.h" |
| 20 #include "ash/wm/window_properties.h" |
| 21 #include "ash/wm/workspace_controller.h" |
| 22 #include "grit/ash_resources.h" |
| 23 #include "ui/aura/client/activation_client.h" |
| 24 #include "ui/aura/root_window.h" |
| 25 #include "ui/aura/window.h" |
| 26 #include "ui/aura/window_observer.h" |
| 27 #include "ui/base/resource/resource_bundle.h" |
| 28 #include "ui/compositor/layer.h" |
| 29 #include "ui/gfx/canvas.h" |
| 30 #include "ui/gfx/image/image.h" |
| 31 #include "ui/gfx/image/image_skia_operations.h" |
| 32 #include "ui/gfx/skbitmap_operations.h" |
| 33 #include "ui/views/accessible_pane_view.h" |
| 34 #include "ui/views/widget/widget.h" |
| 35 #include "ui/views/widget/widget_delegate.h" |
| 36 |
| 37 namespace { |
| 38 // Size of black border at bottom (or side) of launcher. |
| 39 const int kNumBlackPixels = 3; |
| 40 // Alpha to paint dimming image with. |
| 41 const int kDimAlpha = 96; |
| 42 } |
| 43 |
| 44 namespace ash { |
| 45 |
| 46 // The contents view of the Shelf. This view contains LauncherView and |
| 47 // sizes it to the width of the shelf minus the size of the status area. |
| 48 class ShelfWidget::DelegateView : public views::WidgetDelegate, |
| 49 public views::AccessiblePaneView, |
| 50 public internal::BackgroundAnimatorDelegate { |
| 51 public: |
| 52 explicit DelegateView(ShelfWidget* shelf); |
| 53 virtual ~DelegateView(); |
| 54 |
| 55 void set_focus_cycler(internal::FocusCycler* focus_cycler) { |
| 56 focus_cycler_ = focus_cycler; |
| 57 } |
| 58 internal::FocusCycler* focus_cycler() { |
| 59 return focus_cycler_; |
| 60 } |
| 61 |
| 62 void SetDimmed(bool dimmed) { |
| 63 if (dimmed_ != dimmed) { |
| 64 dimmed_ = dimmed; |
| 65 SchedulePaint(); |
| 66 } |
| 67 } |
| 68 bool GetDimmed() const { return dimmed_; } |
| 69 // views::View overrides |
| 70 virtual void OnPaintBackground(gfx::Canvas* canvas) OVERRIDE; |
| 71 |
| 72 // views::WidgetDelegateView overrides: |
| 73 virtual views::Widget* GetWidget() OVERRIDE { |
| 74 return View::GetWidget(); |
| 75 } |
| 76 virtual const views::Widget* GetWidget() const OVERRIDE { |
| 77 return View::GetWidget(); |
| 78 } |
| 79 virtual bool CanActivate() const OVERRIDE { |
| 80 // Allow to activate as fallback. |
| 81 if (shelf_->activating_as_fallback_) |
| 82 return true; |
| 83 // Allow to activate from the focus cycler. |
| 84 if (focus_cycler_ && focus_cycler_->widget_activating() == GetWidget()) |
| 85 return true; |
| 86 // Disallow activating in other cases, especially when using mouse. |
| 87 return false; |
| 88 } |
| 89 |
| 90 virtual void Layout() OVERRIDE { |
| 91 for(int i = 0; i < this->child_count(); ++i) { |
| 92 if (shelf_->shelf_layout_manager()->IsHorizontalAlignment()) |
| 93 child_at(i)->SetBounds(child_at(i)->x(),child_at(i)->y(), |
| 94 child_at(i)->width(),height()); |
| 95 else |
| 96 child_at(i)->SetBounds(child_at(i)->x(),child_at(i)->y(), |
| 97 width(),child_at(i)->height()); |
| 98 } |
| 99 } |
| 100 |
| 101 // BackgroundAnimatorDelegate overrides: |
| 102 virtual void UpdateBackground(int alpha) OVERRIDE; |
| 103 |
| 104 private: |
| 105 ShelfWidget* shelf_; |
| 106 internal::FocusCycler* focus_cycler_; |
| 107 int alpha_; |
| 108 bool dimmed_; |
| 109 |
| 110 DISALLOW_COPY_AND_ASSIGN(DelegateView); |
| 111 }; |
| 112 |
| 113 ShelfWidget::DelegateView::DelegateView(ShelfWidget* shelf) |
| 114 : shelf_(shelf), |
| 115 focus_cycler_(NULL), |
| 116 alpha_(0), |
| 117 dimmed_(false) { |
| 118 } |
| 119 |
| 120 ShelfWidget::DelegateView::~DelegateView() { |
| 121 } |
| 122 |
| 123 void ShelfWidget::DelegateView::OnPaintBackground(gfx::Canvas* canvas) { |
| 124 ResourceBundle& rb = ResourceBundle::GetSharedInstance(); |
| 125 gfx::ImageSkia launcher_background = |
| 126 *rb.GetImageSkiaNamed(IDR_AURA_LAUNCHER_BACKGROUND); |
| 127 if (SHELF_ALIGNMENT_BOTTOM != shelf_->GetAlignment()) |
| 128 launcher_background = gfx::ImageSkiaOperations::CreateRotatedImage( |
| 129 launcher_background, |
| 130 shelf_->shelf_layout_manager()->SelectValueForShelfAlignment( |
| 131 SkBitmapOperations::ROTATION_90_CW, |
| 132 SkBitmapOperations::ROTATION_90_CW, |
| 133 SkBitmapOperations::ROTATION_270_CW, |
| 134 SkBitmapOperations::ROTATION_180_CW)); |
| 135 |
| 136 gfx::Rect black_rect = |
| 137 shelf_->shelf_layout_manager()->SelectValueForShelfAlignment( |
| 138 gfx::Rect(0, height() - kNumBlackPixels, width(), kNumBlackPixels), |
| 139 gfx::Rect(0, 0, kNumBlackPixels, height()), |
| 140 gfx::Rect(width() - kNumBlackPixels, 0, kNumBlackPixels, height()), |
| 141 gfx::Rect(0, 0, width(), kNumBlackPixels)); |
| 142 |
| 143 SkPaint paint; |
| 144 paint.setAlpha(alpha_); |
| 145 canvas->DrawImageInt( |
| 146 launcher_background, |
| 147 0, 0, launcher_background.width(), launcher_background.height(), |
| 148 0, 0, width(), height(), |
| 149 false, |
| 150 paint); |
| 151 canvas->FillRect(black_rect, SK_ColorBLACK); |
| 152 |
| 153 if (dimmed_) { |
| 154 gfx::ImageSkia background_image = |
| 155 *rb.GetImageSkiaNamed(IDR_AURA_LAUNCHER_DIMMING); |
| 156 if (SHELF_ALIGNMENT_BOTTOM != shelf_->GetAlignment()) |
| 157 background_image = gfx::ImageSkiaOperations::CreateRotatedImage( |
| 158 background_image, |
| 159 shelf_->shelf_layout_manager()->SelectValueForShelfAlignment( |
| 160 SkBitmapOperations::ROTATION_90_CW, |
| 161 SkBitmapOperations::ROTATION_90_CW, |
| 162 SkBitmapOperations::ROTATION_270_CW, |
| 163 SkBitmapOperations::ROTATION_180_CW)); |
| 164 |
| 165 SkPaint paint; |
| 166 paint.setAlpha(kDimAlpha); |
| 167 canvas->DrawImageInt( |
| 168 background_image, |
| 169 0, 0, background_image.width(), background_image.height(), |
| 170 0, 0, width(), height(), |
| 171 false, |
| 172 paint); |
| 173 } |
| 174 } |
| 175 |
| 176 void ShelfWidget::DelegateView::UpdateBackground(int alpha) { |
| 177 alpha_ = alpha; |
| 178 SchedulePaint(); |
| 179 } |
| 180 |
| 181 ShelfWidget::ShelfWidget( |
| 182 aura::Window* shelf_container, |
| 183 aura::Window* status_container, |
| 184 internal::WorkspaceController* workspace_controller) : |
| 185 launcher_(NULL), |
| 186 delegate_view_(new DelegateView(this)), |
| 187 background_animator_(delegate_view_, 0, kLauncherBackgroundAlpha), |
| 188 activating_as_fallback_(false), |
| 189 window_container_(shelf_container) { |
| 190 views::Widget::InitParams params( |
| 191 views::Widget::InitParams::TYPE_WINDOW_FRAMELESS); |
| 192 params.transparent = true; |
| 193 params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET; |
| 194 params.parent = shelf_container; |
| 195 params.delegate = delegate_view_; |
| 196 Init(params); |
| 197 |
| 198 // The shelf should not take focus when initially shown. |
| 199 set_focus_on_creation(false); |
| 200 SetContentsView(delegate_view_); |
| 201 |
| 202 status_area_widget_ = new internal::StatusAreaWidget(status_container); |
| 203 status_area_widget_->CreateTrayViews(); |
| 204 if (Shell::GetInstance()->delegate()->IsSessionStarted()) |
| 205 status_area_widget_->Show(); |
| 206 Shell::GetInstance()->focus_cycler()->AddWidget(status_area_widget_); |
| 207 |
| 208 shelf_layout_manager_ = new internal::ShelfLayoutManager(this); |
| 209 shelf_container->SetLayoutManager(shelf_layout_manager_); |
| 210 shelf_layout_manager_->set_workspace_controller(workspace_controller); |
| 211 workspace_controller->SetShelf(shelf_layout_manager_); |
| 212 |
| 213 status_container->SetLayoutManager( |
| 214 new internal::StatusAreaLayoutManager(this)); |
| 215 |
| 216 views::Widget::AddObserver(this); |
| 217 } |
| 218 |
| 219 ShelfWidget::~ShelfWidget() { |
| 220 RemoveObserver(this); |
| 221 |
| 222 if (window_container_) |
| 223 window_container_->SetLayoutManager(NULL); |
| 224 |
| 225 if (launcher_) { |
| 226 delete launcher_; |
| 227 } |
| 228 } |
| 229 |
| 230 void ShelfWidget::SetPaintsBackground( |
| 231 bool value, |
| 232 internal::BackgroundAnimator::ChangeType change_type) { |
| 233 background_animator_.SetPaintsBackground(value, change_type); |
| 234 } |
| 235 |
| 236 ShelfAlignment ShelfWidget::GetAlignment() const { |
| 237 return shelf_layout_manager_->GetAlignment(); |
| 238 } |
| 239 |
| 240 void ShelfWidget::SetAlignment(ShelfAlignment alignment) { |
| 241 shelf_layout_manager_->SetAlignment(alignment); |
| 242 shelf_layout_manager_->LayoutShelf(); |
| 243 } |
| 244 |
| 245 void ShelfWidget::SetDimsShelf(bool dimming) { |
| 246 delegate_view_->SetDimmed(dimming); |
| 247 } |
| 248 |
| 249 bool ShelfWidget::GetDimsShelf() const { |
| 250 return delegate_view_->GetDimmed(); |
| 251 } |
| 252 |
| 253 void ShelfWidget::CreateLauncher() { |
| 254 if (!launcher_) { |
| 255 // This needs to be called before launcher_model(). |
| 256 Shell::GetInstance()->GetLauncherDelegate(); |
| 257 launcher_ = new Launcher(Shell::GetInstance()->launcher_model(), |
| 258 Shell::GetInstance()->GetLauncherDelegate(), |
| 259 this); |
| 260 |
| 261 SetFocusCycler(Shell::GetInstance()->focus_cycler()); |
| 262 ShellDelegate* delegate = Shell::GetInstance()->delegate(); |
| 263 |
| 264 // Inform the root window controller (for PanelLayoutManager). |
| 265 internal::RootWindowController::ForWindow(window_container_)-> |
| 266 OnLauncherCreated(); |
| 267 |
| 268 if (delegate) |
| 269 launcher_->SetVisible(delegate->IsSessionStarted()); |
| 270 |
| 271 Show(); |
| 272 } |
| 273 } |
| 274 |
| 275 bool ShelfWidget::LauncherVisible() const { |
| 276 return launcher_ && launcher_->visible(); |
| 277 } |
| 278 |
| 279 void ShelfWidget::SetLauncherVisibility(bool visible) { |
| 280 if (launcher_) |
| 281 launcher_->SetVisible(visible); |
| 282 } |
| 283 |
| 284 void ShelfWidget::SetFocusCycler(internal::FocusCycler* focus_cycler) { |
| 285 delegate_view_->set_focus_cycler(focus_cycler); |
| 286 if (focus_cycler) |
| 287 focus_cycler->AddWidget(this); |
| 288 } |
| 289 |
| 290 internal::FocusCycler* ShelfWidget::GetFocusCycler() { |
| 291 return delegate_view_->focus_cycler(); |
| 292 } |
| 293 |
| 294 void ShelfWidget::OnWidgetActivationChanged(views::Widget* widget, |
| 295 bool active) { |
| 296 activating_as_fallback_ = false; |
| 297 if (active) { |
| 298 delegate_view_->SetPaneFocusAndFocusDefault(); |
| 299 } else { |
| 300 delegate_view_->GetFocusManager()->ClearFocus(); |
| 301 } |
| 302 } |
| 303 |
| 304 } // namespace ash |
| 305 |
OLD | NEW |