| 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/common/workspace/phantom_window_controller.h" | |
| 6 | |
| 7 #include <math.h> | |
| 8 | |
| 9 #include "ash/wm/common/root_window_finder.h" | |
| 10 #include "ash/wm/common/wm_lookup.h" | |
| 11 #include "ash/wm/common/wm_root_window_controller.h" | |
| 12 #include "ash/wm/common/wm_shell_window_ids.h" | |
| 13 #include "ash/wm/common/wm_window.h" | |
| 14 #include "grit/ash_resources.h" | |
| 15 #include "ui/compositor/layer.h" | |
| 16 #include "ui/compositor/scoped_layer_animation_settings.h" | |
| 17 #include "ui/views/background.h" | |
| 18 #include "ui/views/painter.h" | |
| 19 #include "ui/views/view.h" | |
| 20 #include "ui/views/widget/widget.h" | |
| 21 | |
| 22 namespace ash { | |
| 23 namespace { | |
| 24 | |
| 25 // The duration of the show animation. | |
| 26 const int kAnimationDurationMs = 200; | |
| 27 | |
| 28 // The size of the phantom window at the beginning of the show animation in | |
| 29 // relation to the size of the phantom window at the end of the animation. | |
| 30 const float kStartBoundsRatio = 0.85f; | |
| 31 | |
| 32 // The amount of pixels that the phantom window's shadow should extend past | |
| 33 // the bounds passed into Show(). | |
| 34 const int kShadowThickness = 15; | |
| 35 | |
| 36 // The minimum size of a phantom window including the shadow. The minimum size | |
| 37 // is derived from the size of the IDR_AURA_PHANTOM_WINDOW image assets. | |
| 38 const int kMinSizeWithShadow = 100; | |
| 39 | |
| 40 // Adjusts the phantom window's bounds so that the bounds: | |
| 41 // - Include the size of the shadow. | |
| 42 // - Have a size equal to or larger than the minimum phantom window size. | |
| 43 gfx::Rect GetAdjustedBounds(const gfx::Rect& bounds) { | |
| 44 int x_inset = std::max( | |
| 45 static_cast<int>(ceil((kMinSizeWithShadow - bounds.width()) / 2.0f)), | |
| 46 kShadowThickness); | |
| 47 int y_inset = std::max( | |
| 48 static_cast<int>(ceil((kMinSizeWithShadow - bounds.height()) / 2.0f)), | |
| 49 kShadowThickness); | |
| 50 | |
| 51 gfx::Rect adjusted_bounds(bounds); | |
| 52 adjusted_bounds.Inset(-x_inset, -y_inset); | |
| 53 return adjusted_bounds; | |
| 54 } | |
| 55 | |
| 56 // Starts an animation of |widget| to |new_bounds_in_screen|. No-op if |widget| | |
| 57 // is NULL. | |
| 58 void AnimateToBounds(views::Widget* widget, | |
| 59 const gfx::Rect& new_bounds_in_screen) { | |
| 60 if (!widget) | |
| 61 return; | |
| 62 | |
| 63 ui::ScopedLayerAnimationSettings scoped_setter( | |
| 64 wm::WmLookup::Get() | |
| 65 ->GetWindowForWidget(widget) | |
| 66 ->GetLayer() | |
| 67 ->GetAnimator()); | |
| 68 scoped_setter.SetTweenType(gfx::Tween::EASE_IN); | |
| 69 scoped_setter.SetPreemptionStrategy( | |
| 70 ui::LayerAnimator::IMMEDIATELY_ANIMATE_TO_NEW_TARGET); | |
| 71 scoped_setter.SetTransitionDuration( | |
| 72 base::TimeDelta::FromMilliseconds(kAnimationDurationMs)); | |
| 73 widget->SetBounds(new_bounds_in_screen); | |
| 74 } | |
| 75 | |
| 76 } // namespace | |
| 77 | |
| 78 // PhantomWindowController ---------------------------------------------------- | |
| 79 | |
| 80 PhantomWindowController::PhantomWindowController(wm::WmWindow* window) | |
| 81 : window_(window) {} | |
| 82 | |
| 83 PhantomWindowController::~PhantomWindowController() {} | |
| 84 | |
| 85 void PhantomWindowController::Show(const gfx::Rect& bounds_in_screen) { | |
| 86 gfx::Rect adjusted_bounds_in_screen = GetAdjustedBounds(bounds_in_screen); | |
| 87 if (adjusted_bounds_in_screen == target_bounds_in_screen_) | |
| 88 return; | |
| 89 target_bounds_in_screen_ = adjusted_bounds_in_screen; | |
| 90 | |
| 91 gfx::Rect start_bounds_in_screen = target_bounds_in_screen_; | |
| 92 int start_width = std::max( | |
| 93 kMinSizeWithShadow, | |
| 94 static_cast<int>(start_bounds_in_screen.width() * kStartBoundsRatio)); | |
| 95 int start_height = std::max( | |
| 96 kMinSizeWithShadow, | |
| 97 static_cast<int>(start_bounds_in_screen.height() * kStartBoundsRatio)); | |
| 98 start_bounds_in_screen.Inset( | |
| 99 floor((start_bounds_in_screen.width() - start_width) / 2.0f), | |
| 100 floor((start_bounds_in_screen.height() - start_height) / 2.0f)); | |
| 101 phantom_widget_ = | |
| 102 CreatePhantomWidget(wm::GetRootWindowMatching(target_bounds_in_screen_), | |
| 103 start_bounds_in_screen); | |
| 104 | |
| 105 AnimateToBounds(phantom_widget_.get(), target_bounds_in_screen_); | |
| 106 } | |
| 107 | |
| 108 std::unique_ptr<views::Widget> PhantomWindowController::CreatePhantomWidget( | |
| 109 wm::WmWindow* root_window, | |
| 110 const gfx::Rect& bounds_in_screen) { | |
| 111 std::unique_ptr<views::Widget> phantom_widget(new views::Widget); | |
| 112 views::Widget::InitParams params(views::Widget::InitParams::TYPE_POPUP); | |
| 113 params.opacity = views::Widget::InitParams::TRANSLUCENT_WINDOW; | |
| 114 // PhantomWindowController is used by FrameMaximizeButton to highlight the | |
| 115 // launcher button. Put the phantom in the same window as the launcher so that | |
| 116 // the phantom is visible. | |
| 117 params.keep_on_top = true; | |
| 118 params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET; | |
| 119 params.name = "PhantomWindow"; | |
| 120 root_window->GetRootWindowController()->ConfigureWidgetInitParamsForContainer( | |
| 121 phantom_widget.get(), kShellWindowId_ShelfContainer, ¶ms); | |
| 122 phantom_widget->set_focus_on_creation(false); | |
| 123 phantom_widget->Init(params); | |
| 124 phantom_widget->SetVisibilityChangedAnimationsEnabled(false); | |
| 125 wm::WmWindow* phantom_widget_window = | |
| 126 wm::WmLookup::Get()->GetWindowForWidget(phantom_widget.get()); | |
| 127 phantom_widget_window->SetShellWindowId(kShellWindowId_PhantomWindow); | |
| 128 phantom_widget->SetBounds(bounds_in_screen); | |
| 129 // TODO(sky): I suspect this is never true, verify that. | |
| 130 if (phantom_widget_window->GetParent() == window_->GetParent()) { | |
| 131 phantom_widget_window->GetParent()->StackChildAbove(phantom_widget_window, | |
| 132 window_); | |
| 133 } | |
| 134 | |
| 135 const int kImages[] = IMAGE_GRID(IDR_AURA_PHANTOM_WINDOW); | |
| 136 views::Painter* background_painter = | |
| 137 views::Painter::CreateImageGridPainter(kImages); | |
| 138 views::View* content_view = new views::View; | |
| 139 content_view->set_background( | |
| 140 views::Background::CreateBackgroundPainter(true, background_painter)); | |
| 141 phantom_widget->SetContentsView(content_view); | |
| 142 | |
| 143 // Show the widget after all the setups. | |
| 144 phantom_widget->Show(); | |
| 145 | |
| 146 // Fade the window in. | |
| 147 ui::Layer* widget_layer = phantom_widget_window->GetLayer(); | |
| 148 widget_layer->SetOpacity(0); | |
| 149 ui::ScopedLayerAnimationSettings scoped_setter(widget_layer->GetAnimator()); | |
| 150 scoped_setter.SetTransitionDuration( | |
| 151 base::TimeDelta::FromMilliseconds(kAnimationDurationMs)); | |
| 152 widget_layer->SetOpacity(1); | |
| 153 | |
| 154 return phantom_widget; | |
| 155 } | |
| 156 | |
| 157 } // namespace ash | |
| OLD | NEW |