| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "ash/wm/overview/transparent_activate_window_button.h" | 5 #include "ash/wm/overview/transparent_activate_window_button.h" |
| 6 | 6 |
| 7 #include <vector> | 7 #include <vector> |
| 8 | 8 |
| 9 #include "ash/ash_switches.h" |
| 9 #include "ash/shell.h" | 10 #include "ash/shell.h" |
| 10 #include "ash/shell_window_ids.h" | 11 #include "ash/shell_window_ids.h" |
| 11 #include "ash/wm/overview/transparent_activate_window_button_delegate.h" | 12 #include "ash/wm/overview/transparent_activate_window_button_delegate.h" |
| 13 #include "base/command_line.h" |
| 12 #include "ui/events/event.h" | 14 #include "ui/events/event.h" |
| 13 #include "ui/events/gesture_event_details.h" | 15 #include "ui/events/gesture_event_details.h" |
| 14 #include "ui/gfx/display.h" | 16 #include "ui/gfx/display.h" |
| 15 #include "ui/gfx/screen.h" | 17 #include "ui/gfx/screen.h" |
| 16 #include "ui/views/controls/button/button.h" | 18 #include "ui/views/controls/button/button.h" |
| 17 #include "ui/views/controls/button/custom_button.h" | 19 #include "ui/views/controls/button/custom_button.h" |
| 18 #include "ui/views/widget/widget.h" | 20 #include "ui/views/widget/widget.h" |
| 19 | 21 |
| 20 namespace ash { | 22 namespace ash { |
| 21 | 23 |
| 22 namespace { | 24 namespace { |
| 23 const char kTransparentButtonName[] = "TransparentButton"; | 25 const char kTransparentButtonName[] = "TransparentButton"; |
| 24 | 26 |
| 27 // The default close window distance minimum. |
| 28 const int kDefaultCloseWindowDistanceMinimum = 100; |
| 29 |
| 30 // The minimum fling velocity which will cause a window to be closed. Unit is |
| 31 // pixels per second. |
| 32 const float kMinimumFlingVelocity = 4000.0f; |
| 33 |
| 34 // Initializes the event handler transparent window. |
| 35 views::Widget* InitEventHandler(aura::Window* root_window) { |
| 36 views::Widget* widget = new views::Widget; |
| 37 views::Widget::InitParams params; |
| 38 params.type = views::Widget::InitParams::TYPE_POPUP; |
| 39 params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET; |
| 40 params.opacity = views::Widget::InitParams::TRANSLUCENT_WINDOW; |
| 41 params.accept_events = true; |
| 42 params.parent = Shell::GetContainer(root_window, |
| 43 kShellWindowId_OverlayContainer); |
| 44 widget->set_focus_on_creation(false); |
| 45 widget->Init(params); |
| 46 widget->Show(); |
| 47 |
| 48 aura::Window* handler = widget->GetNativeWindow(); |
| 49 handler->parent()->StackChildAtBottom(handler); |
| 50 |
| 51 return widget; |
| 52 } |
| 53 |
| 54 } // namespace |
| 55 |
| 25 // Transparent button that handles events to activate or close windows in | 56 // Transparent button that handles events to activate or close windows in |
| 26 // overview mode. | 57 // overview mode. |
| 27 class TransparentButton | 58 class TransparentButton |
| 28 : public views::CustomButton, | 59 : public views::CustomButton, |
| 29 public views::ButtonListener { | 60 public views::ButtonListener { |
| 30 public: | 61 public: |
| 31 explicit TransparentButton(TransparentActivateWindowButtonDelegate* delegate); | 62 explicit TransparentButton(TransparentActivateWindowButtonDelegate* delegate); |
| 32 | 63 |
| 33 ~TransparentButton() override; | 64 ~TransparentButton() override; |
| 34 | 65 |
| 35 TransparentActivateWindowButtonDelegate* delegate() { return delegate_; } | 66 TransparentActivateWindowButtonDelegate* delegate() { return delegate_; } |
| 36 | 67 |
| 68 void set_close_window_distance_minimum(int distance) { |
| 69 close_window_distance_minimum_ = distance; |
| 70 } |
| 71 |
| 37 // views::View: | 72 // views::View: |
| 38 virtual void OnGestureEvent(ui::GestureEvent* event) override; | 73 virtual void OnGestureEvent(ui::GestureEvent* event) override; |
| 39 | 74 |
| 40 virtual const char* GetClassName() const override { | 75 virtual const char* GetClassName() const override { |
| 41 return kTransparentButtonName; | 76 return kTransparentButtonName; |
| 42 } | 77 } |
| 43 | 78 |
| 44 // views::ButtonListener: | 79 // views::ButtonListener: |
| 45 void ButtonPressed(views::Button* sender, const ui::Event& event) override; | 80 void ButtonPressed(views::Button* sender, const ui::Event& event) override; |
| 46 | 81 |
| 47 private: | 82 private: |
| 48 TransparentActivateWindowButtonDelegate* delegate_; | 83 TransparentActivateWindowButtonDelegate* delegate_; |
| 49 | 84 |
| 85 // True if the swipe to close feature is disabled. |
| 86 const bool swipe_to_close_disabled_; |
| 87 |
| 88 // The original X location for a tap down event. |original_x_| is in the local |
| 89 // coordinate space as |this|. |
| 90 float original_x_; |
| 91 |
| 92 // The velocity of the current fling gesture. A fling is active iff |
| 93 // |flint_velocity_x_| has a value greater than 0. |
| 94 float fling_velocity_x_; |
| 95 |
| 96 // The minimum distance, in pixels, for which a touch drag should cause a |
| 97 // window to be closed. |
| 98 int close_window_distance_minimum_; |
| 99 |
| 50 DISALLOW_COPY_AND_ASSIGN(TransparentButton); | 100 DISALLOW_COPY_AND_ASSIGN(TransparentButton); |
| 51 }; | 101 }; |
| 52 | 102 |
| 53 TransparentButton::TransparentButton( | 103 TransparentButton::TransparentButton( |
| 54 TransparentActivateWindowButtonDelegate* delegate) | 104 TransparentActivateWindowButtonDelegate* delegate) |
| 55 : CustomButton(this), | 105 : views::CustomButton(this), |
| 56 delegate_(delegate) { | 106 delegate_(delegate), |
| 107 swipe_to_close_disabled_(CommandLine::ForCurrentProcess()->HasSwitch( |
| 108 switches::kAshDisableSwipeToCloseInOverviewMode)), |
| 109 fling_velocity_x_(0.0f), |
| 110 close_window_distance_minimum_(kDefaultCloseWindowDistanceMinimum) { |
| 57 } | 111 } |
| 58 | 112 |
| 59 TransparentButton::~TransparentButton() { | 113 TransparentButton::~TransparentButton() { |
| 60 } | 114 } |
| 61 | 115 |
| 62 void TransparentButton::OnGestureEvent(ui::GestureEvent* event) { | 116 void TransparentButton::OnGestureEvent(ui::GestureEvent* event) { |
| 63 // TODO(tdanderson): Re-evaluate whether we want to set capture once | 117 // TODO(tdanderson): Re-evaluate whether we want to set capture once |
| 64 // the a fix has landed to avoid crashing when a window | 118 // the a fix has landed to avoid crashing when a window |
| 65 // having an active gesture sequence is destroyed as a | 119 // having an active gesture sequence is destroyed as a |
| 66 // result of a gesture in a separate window. Consider using | 120 // result of a gesture in a separate window. Consider using |
| 67 // a StaticWindowTargeter instead of a transparent overlay | 121 // a StaticWindowTargeter instead of a transparent overlay |
| 68 // widget to re-direct input events. | 122 // widget to re-direct input events. |
| 69 if (event->type() == ui::ET_GESTURE_TAP_DOWN) | 123 if (event->type() == ui::ET_GESTURE_TAP_DOWN) |
| 70 GetWidget()->SetCapture(this); | 124 GetWidget()->SetCapture(this); |
| 71 | 125 |
| 72 if (event->type() == ui::ET_GESTURE_TAP || | 126 if (event->type() == ui::ET_GESTURE_TAP || |
| 73 event->type() == ui::ET_GESTURE_END) { | 127 event->type() == ui::ET_GESTURE_END) { |
| 74 GetWidget()->ReleaseCapture(); | 128 GetWidget()->ReleaseCapture(); |
| 75 } | 129 } |
| 76 | 130 |
| 131 if (swipe_to_close_disabled_) { |
| 132 CustomButton::OnGestureEvent(event); |
| 133 return; |
| 134 } |
| 135 |
| 136 int delta_x = event->x() - original_x_; |
| 137 |
| 138 switch (event->type()) { |
| 139 case ui::ET_GESTURE_TAP_DOWN: |
| 140 original_x_ = event->x(); |
| 141 break; |
| 142 case ui::ET_GESTURE_SCROLL_BEGIN: |
| 143 case ui::ET_GESTURE_SCROLL_UPDATE: |
| 144 delegate_->Scroll(delta_x); |
| 145 break; |
| 146 case ui::ET_SCROLL_FLING_START: |
| 147 fling_velocity_x_ = fabs(event->details().velocity_x()); |
| 148 break; |
| 149 case ui::ET_GESTURE_END: |
| 150 if (abs(delta_x) > close_window_distance_minimum_ || |
| 151 fling_velocity_x_ > kMinimumFlingVelocity) { |
| 152 delegate_->Close(); |
| 153 } else { |
| 154 delegate_->CancelScroll(); |
| 155 } |
| 156 original_x_ = 0; |
| 157 fling_velocity_x_ = 0.0f; |
| 158 break; |
| 159 default: |
| 160 break; |
| 161 } |
| 77 CustomButton::OnGestureEvent(event); | 162 CustomButton::OnGestureEvent(event); |
| 78 event->StopPropagation(); | 163 event->StopPropagation(); |
| 79 } | 164 } |
| 80 | 165 |
| 81 void TransparentButton::ButtonPressed(views::Button* sender, | 166 void TransparentButton::ButtonPressed(views::Button* sender, |
| 82 const ui::Event& event) { | 167 const ui::Event& event) { |
| 83 delegate_->Select(); | 168 delegate_->Select(); |
| 84 } | 169 } |
| 85 | 170 |
| 86 // Initializes the event handler transparent window. | |
| 87 views::Widget* InitEventHandler(aura::Window* root_window) { | |
| 88 views::Widget* widget = new views::Widget; | |
| 89 views::Widget::InitParams params; | |
| 90 params.type = views::Widget::InitParams::TYPE_POPUP; | |
| 91 params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET; | |
| 92 params.opacity = views::Widget::InitParams::TRANSLUCENT_WINDOW; | |
| 93 params.accept_events = true; | |
| 94 params.parent = Shell::GetContainer(root_window, | |
| 95 kShellWindowId_OverlayContainer); | |
| 96 widget->set_focus_on_creation(false); | |
| 97 widget->Init(params); | |
| 98 widget->Show(); | |
| 99 | |
| 100 aura::Window* handler = widget->GetNativeWindow(); | |
| 101 handler->parent()->StackChildAtBottom(handler); | |
| 102 | |
| 103 return widget; | |
| 104 } | |
| 105 | |
| 106 } // namespace | |
| 107 | |
| 108 TransparentActivateWindowButton::TransparentActivateWindowButton( | 171 TransparentActivateWindowButton::TransparentActivateWindowButton( |
| 109 aura::Window* root_window, | 172 aura::Window* root_window, |
| 110 TransparentActivateWindowButtonDelegate* delegate) | 173 TransparentActivateWindowButtonDelegate* delegate) |
| 111 : event_handler_widget_(InitEventHandler(root_window)), | 174 : event_handler_widget_(InitEventHandler(root_window)), |
| 112 transparent_button_(new TransparentButton(delegate)) { | 175 transparent_button_(new TransparentButton(delegate)) { |
| 113 event_handler_widget_->SetContentsView(transparent_button_); | 176 event_handler_widget_->SetContentsView(transparent_button_); |
| 114 } | 177 } |
| 115 | 178 |
| 116 TransparentActivateWindowButton::~TransparentActivateWindowButton() { | 179 TransparentActivateWindowButton::~TransparentActivateWindowButton() { |
| 117 } | 180 } |
| 118 | 181 |
| 119 void TransparentActivateWindowButton::SetAccessibleName( | 182 void TransparentActivateWindowButton::SetAccessibleName( |
| 120 const base::string16& name) { | 183 const base::string16& name) { |
| 121 transparent_button_->SetAccessibleName(name); | 184 transparent_button_->SetAccessibleName(name); |
| 122 } | 185 } |
| 123 | 186 |
| 124 void TransparentActivateWindowButton::SetBounds(const gfx::Rect& bounds) { | 187 void TransparentActivateWindowButton::SetBounds(const gfx::Rect& bounds) { |
| 125 aura::Window* window = event_handler_widget_->GetNativeWindow(); | 188 aura::Window* window = event_handler_widget_->GetNativeWindow(); |
| 126 const gfx::Display& display = gfx::Screen::GetScreenFor( | 189 const gfx::Display& display = gfx::Screen::GetScreenFor( |
| 127 window)->GetDisplayNearestWindow(window); | 190 window)->GetDisplayNearestWindow(window); |
| 128 // Explicitly specify the display so that the window doesn't change root | 191 // Explicitly specify the display so that the window doesn't change root |
| 129 // windows and cause the z-order of the transparent overlays to be updated. | 192 // windows and cause the z-order of the transparent overlays to be updated. |
| 130 window->SetBoundsInScreen(bounds, display); | 193 window->SetBoundsInScreen(bounds, display); |
| 131 } | 194 } |
| 132 | 195 |
| 196 void TransparentActivateWindowButton::SetCloseWindowDistanceMinimum( |
| 197 int distance) { |
| 198 transparent_button_->set_close_window_distance_minimum(distance); |
| 199 } |
| 200 |
| 133 void TransparentActivateWindowButton::SendFocusAlert() const { | 201 void TransparentActivateWindowButton::SendFocusAlert() const { |
| 134 event_handler_widget_->GetContentsView()-> | 202 event_handler_widget_->GetContentsView()-> |
| 135 NotifyAccessibilityEvent(ui::AX_EVENT_FOCUS, true); | 203 NotifyAccessibilityEvent(ui::AX_EVENT_FOCUS, true); |
| 136 } | 204 } |
| 137 | 205 |
| 138 void TransparentActivateWindowButton::StackAbove( | 206 void TransparentActivateWindowButton::StackAbove( |
| 139 TransparentActivateWindowButton* activate_button) { | 207 TransparentActivateWindowButton* activate_button) { |
| 140 aura::Window* this_window = event_handler_widget_->GetNativeWindow(); | 208 aura::Window* this_window = event_handler_widget_->GetNativeWindow(); |
| 141 aura::Window* other_window = activate_button->event_handler_widget_-> | 209 aura::Window* other_window = activate_button->event_handler_widget_-> |
| 142 GetNativeWindow(); | 210 GetNativeWindow(); |
| 143 this_window->parent()->StackChildAbove(this_window, other_window); | 211 this_window->parent()->StackChildAbove(this_window, other_window); |
| 144 } | 212 } |
| 145 | 213 |
| 146 } // namespace ash | 214 } // namespace ash |
| OLD | NEW |