Chromium Code Reviews| 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/views/controls/button/custom_button.h" | 16 #include "ui/views/controls/button/custom_button.h" |
| 15 #include "ui/views/widget/widget.h" | 17 #include "ui/views/widget/widget.h" |
| 16 | 18 |
| 17 namespace ash { | 19 namespace ash { |
| 18 | 20 |
| 19 namespace { | 21 namespace { |
| 20 const char kTransparentButtonName[] = "TransparentButton"; | 22 const char kTransparentButtonName[] = "TransparentButton"; |
| 21 | 23 |
| 24 // The default close window distance minimum. | |
| 25 const int kDefaultCloseWindowDistanceMinimum = 100; | |
|
flackr
2014/11/07 19:10:52
Looks like this will never actually be used since
bruthig
2015/01/02 16:49:17
That is correct. I'm not sure if you are suggesti
| |
| 26 | |
| 27 // The minimum fling velocity which will cause a window to be closed. Unit is | |
| 28 // pixels per second. | |
| 29 const float kMinimumFlingVelocity = 4000.0f; | |
|
jonross
2014/11/12 23:57:03
Won't be needed if we decide to just close on flin
bruthig
2015/01/02 16:49:17
Windows are closed too easily if using all flings.
| |
| 30 | |
| 22 // Initializes the event handler transparent window. | 31 // Initializes the event handler transparent window. |
| 23 views::Widget* InitEventHandler(aura::Window* root_window) { | 32 views::Widget* InitEventHandler(aura::Window* root_window) { |
| 24 views::Widget* widget = new views::Widget; | 33 views::Widget* widget = new views::Widget; |
| 25 views::Widget::InitParams params; | 34 views::Widget::InitParams params; |
| 26 params.type = views::Widget::InitParams::TYPE_POPUP; | 35 params.type = views::Widget::InitParams::TYPE_POPUP; |
| 27 params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET; | 36 params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET; |
| 28 params.opacity = views::Widget::InitParams::TRANSLUCENT_WINDOW; | 37 params.opacity = views::Widget::InitParams::TRANSLUCENT_WINDOW; |
| 29 params.accept_events = true; | 38 params.accept_events = true; |
| 30 params.parent = Shell::GetContainer(root_window, | 39 params.parent = Shell::GetContainer(root_window, |
| 31 ash::kShellWindowId_OverlayContainer); | 40 ash::kShellWindowId_OverlayContainer); |
| (...skipping 13 matching lines...) Expand all Loading... | |
| 45 // overview mode. | 54 // overview mode. |
| 46 class TransparentButton : public views::CustomButton { | 55 class TransparentButton : public views::CustomButton { |
| 47 public: | 56 public: |
| 48 TransparentButton(views::ButtonListener* listener, | 57 TransparentButton(views::ButtonListener* listener, |
| 49 TransparentActivateWindowButtonDelegate* delegate); | 58 TransparentActivateWindowButtonDelegate* delegate); |
| 50 | 59 |
| 51 virtual ~TransparentButton(); | 60 virtual ~TransparentButton(); |
| 52 | 61 |
| 53 TransparentActivateWindowButtonDelegate* delegate() { return delegate_; } | 62 TransparentActivateWindowButtonDelegate* delegate() { return delegate_; } |
| 54 | 63 |
| 64 void set_close_window_distance_minimum(int distance) { | |
| 65 close_window_distance_minimum_ = distance; | |
|
flackr
2014/11/07 19:10:52
I think setting the close distance to a proportion
bruthig
2015/01/02 16:49:17
Not 100% sure I understand but I will take a shot.
jonross
2015/01/06 15:02:27
Can the size of the WindowSelectionItem be accesse
| |
| 66 } | |
| 67 | |
| 55 // views::View: | 68 // views::View: |
| 56 virtual void OnGestureEvent(ui::GestureEvent* event) override; | 69 virtual void OnGestureEvent(ui::GestureEvent* event) override; |
| 57 virtual void OnMouseEvent(ui::MouseEvent* event) override; | 70 virtual void OnMouseEvent(ui::MouseEvent* event) override; |
| 58 | 71 |
| 59 virtual const char* GetClassName() const override { | 72 virtual const char* GetClassName() const override { |
| 60 return kTransparentButtonName; | 73 return kTransparentButtonName; |
| 61 } | 74 } |
| 62 | 75 |
| 63 private: | 76 private: |
| 64 TransparentActivateWindowButtonDelegate* delegate_; | 77 TransparentActivateWindowButtonDelegate* delegate_; |
| 65 | 78 |
| 79 // True if the swipe to close feature is disabled. | |
| 80 const bool swipe_to_close_disabled_; | |
|
flackr
2014/11/07 19:10:52
Follow the pattern used by kEnableTouchFeedback to
pkotwicz
2014/12/21 00:09:32
Drive by:
I dislike the pattern used with kEnable
bruthig
2015/01/02 16:49:17
This is a very good example of how statics are pro
jonross
2015/01/06 15:02:27
It does add a complexity, however you can have one
| |
| 81 | |
| 82 // The original X location for a tap down event. |original_x_| is in the local | |
| 83 // coordinate space as |this|. | |
| 84 float original_x_; | |
| 85 | |
| 86 // The velocity of the current fling gesture. A fling is active iff | |
| 87 // |flint_velocity_x_| has a value greater than 0. | |
| 88 float fling_velocity_x_; | |
| 89 | |
| 90 // The minimum distance, in pixels, for which a touch drag should cause a | |
| 91 // window to be closed. | |
| 92 int close_window_distance_minimum_; | |
| 93 | |
| 66 DISALLOW_COPY_AND_ASSIGN(TransparentButton); | 94 DISALLOW_COPY_AND_ASSIGN(TransparentButton); |
| 67 }; | 95 }; |
| 68 | 96 |
| 69 TransparentButton::TransparentButton( | 97 TransparentButton::TransparentButton( |
| 70 views::ButtonListener* listener, | 98 views::ButtonListener* listener, |
| 71 TransparentActivateWindowButtonDelegate* delegate) | 99 TransparentActivateWindowButtonDelegate* delegate) |
| 72 : CustomButton(listener), | 100 : CustomButton(listener), |
| 73 delegate_(delegate) { | 101 delegate_(delegate), |
| 102 swipe_to_close_disabled_(CommandLine::ForCurrentProcess()->HasSwitch( | |
| 103 switches::kAshDisableSwipeToCloseInOverviewMode)), | |
| 104 fling_velocity_x_(0.0f), | |
| 105 close_window_distance_minimum_(kDefaultCloseWindowDistanceMinimum) { | |
| 74 } | 106 } |
| 75 | 107 |
| 76 TransparentButton::~TransparentButton() { | 108 TransparentButton::~TransparentButton() { |
| 77 } | 109 } |
| 78 | 110 |
| 79 void TransparentButton::OnMouseEvent(ui::MouseEvent* event) { | 111 void TransparentButton::OnMouseEvent(ui::MouseEvent* event) { |
| 80 // TODO(tdanderson): Remove this work around when we get rid of the | 112 // TODO(tdanderson): Remove this work around when we get rid of the |
| 81 // TransparentActivateWindowButton. | 113 // TransparentActivateWindowButton. |
| 82 if (GetWidget()->HasCapture() && event->type() == ui::ET_MOUSE_PRESSED) { | 114 if (GetWidget()->HasCapture() && event->type() == ui::ET_MOUSE_PRESSED) { |
| 83 GetWidget()->ReleaseCapture(); | 115 GetWidget()->ReleaseCapture(); |
| 116 if (!swipe_to_close_disabled_) | |
| 117 delegate_->CancelScroll(); | |
| 84 event->StopPropagation(); | 118 event->StopPropagation(); |
| 85 return; | 119 return; |
| 86 } | 120 } |
| 87 CustomButton::OnMouseEvent(event); | 121 CustomButton::OnMouseEvent(event); |
| 88 } | 122 } |
| 89 | 123 |
| 90 void TransparentButton::OnGestureEvent(ui::GestureEvent* event) { | 124 void TransparentButton::OnGestureEvent(ui::GestureEvent* event) { |
| 91 // TODO(tdanderson): Re-evaluate whether we want to set capture once | 125 // TODO(tdanderson): Re-evaluate whether we want to set capture once |
| 92 // the a fix has landed to avoid crashing when a window | 126 // the a fix has landed to avoid crashing when a window |
| 93 // having an active gesture sequence is destroyed as a | 127 // having an active gesture sequence is destroyed as a |
| 94 // result of a gesture in a separate window. Consider using | 128 // result of a gesture in a separate window. Consider using |
| 95 // a StaticWindowTargeter instead of a transparent overlay | 129 // a StaticWindowTargeter instead of a transparent overlay |
| 96 // widget to re-direct input events. | 130 // widget to re-direct input events. |
| 97 if (event->type() == ui::ET_GESTURE_TAP_DOWN) | 131 if (event->type() == ui::ET_GESTURE_TAP_DOWN) |
| 98 GetWidget()->SetCapture(this); | 132 GetWidget()->SetCapture(this); |
| 99 | 133 |
| 100 if (event->type() == ui::ET_GESTURE_TAP || | 134 if (event->type() == ui::ET_GESTURE_TAP || |
| 101 event->type() == ui::ET_GESTURE_END) { | 135 event->type() == ui::ET_GESTURE_END) { |
| 102 GetWidget()->ReleaseCapture(); | 136 GetWidget()->ReleaseCapture(); |
| 103 } | 137 } |
| 104 | 138 |
| 139 if (swipe_to_close_disabled_) { | |
| 140 CustomButton::OnGestureEvent(event); | |
| 141 return; | |
| 142 } | |
| 143 | |
| 144 int delta_x = event->x() - original_x_; | |
| 145 | |
| 146 switch (event->type()) { | |
| 147 case ui::ET_GESTURE_TAP_DOWN: | |
| 148 original_x_ = event->x(); | |
| 149 break; | |
| 150 case ui::ET_GESTURE_SCROLL_BEGIN: | |
| 151 case ui::ET_GESTURE_SCROLL_UPDATE: | |
| 152 delegate_->Scroll(delta_x); | |
| 153 break; | |
| 154 case ui::ET_SCROLL_FLING_START: | |
| 155 case ui::ET_SCROLL_FLING_CANCEL: | |
|
flackr
2014/11/07 19:10:52
I don't think we need to worry about fling cancel
bruthig
2015/01/02 16:49:17
Done.
| |
| 156 // event->details().velocity_x() will be zero when event->type() is | |
| 157 // ui::ET_SCROLL_FLING_CANCEL. | |
| 158 fling_velocity_x_ = fabs(event->details().velocity_x()); | |
|
flackr
2014/11/07 19:10:52
Why do we need to save the fling velocity? If I re
jonross
2014/11/12 23:57:03
Currently used on line 162 to determine if fling i
bruthig
2015/01/02 16:49:17
It's easier IMO to only evaluate for one event typ
| |
| 159 break; | |
| 160 case ui::ET_GESTURE_END: | |
| 161 if (abs(delta_x) > close_window_distance_minimum_ || | |
| 162 fling_velocity_x_ > kMinimumFlingVelocity) { | |
| 163 delegate_->Close(); | |
| 164 } else { | |
| 165 delegate_->CancelScroll(); | |
| 166 } | |
| 167 original_x_ = 0; | |
| 168 fling_velocity_x_ = 0.0f; | |
| 169 break; | |
| 170 default: | |
| 171 break; | |
| 172 } | |
| 105 CustomButton::OnGestureEvent(event); | 173 CustomButton::OnGestureEvent(event); |
| 106 event->StopPropagation(); | 174 event->StopPropagation(); |
| 107 } | 175 } |
| 108 | 176 |
| 109 TransparentActivateWindowButton::TransparentActivateWindowButton( | 177 TransparentActivateWindowButton::TransparentActivateWindowButton( |
| 110 aura::Window* root_window, | 178 aura::Window* root_window, |
| 111 TransparentActivateWindowButtonDelegate* delegate) | 179 TransparentActivateWindowButtonDelegate* delegate) |
| 112 : event_handler_widget_(InitEventHandler(root_window)), | 180 : event_handler_widget_(InitEventHandler(root_window)), |
| 113 transparent_button_(new TransparentButton(this, delegate)) { | 181 transparent_button_(new TransparentButton(this, delegate)) { |
| 114 event_handler_widget_->SetContentsView(transparent_button_); | 182 event_handler_widget_->SetContentsView(transparent_button_); |
| 115 } | 183 } |
| 116 | 184 |
| 117 TransparentActivateWindowButton::~TransparentActivateWindowButton() { | 185 TransparentActivateWindowButton::~TransparentActivateWindowButton() { |
| 118 } | 186 } |
| 119 | 187 |
| 120 void TransparentActivateWindowButton::SetAccessibleName( | 188 void TransparentActivateWindowButton::SetAccessibleName( |
| 121 const base::string16& name) { | 189 const base::string16& name) { |
| 122 transparent_button_->SetAccessibleName(name); | 190 transparent_button_->SetAccessibleName(name); |
| 123 } | 191 } |
| 124 | 192 |
| 125 void TransparentActivateWindowButton::SetBounds(const gfx::Rect& bounds) { | 193 void TransparentActivateWindowButton::SetBounds(const gfx::Rect& bounds) { |
| 126 event_handler_widget_->SetBounds(bounds); | 194 event_handler_widget_->SetBounds(bounds); |
| 127 } | 195 } |
| 128 | 196 |
| 197 void TransparentActivateWindowButton::SetCloseWindowDistanceMinimum( | |
| 198 int distance) { | |
| 199 transparent_button_->set_close_window_distance_minimum(distance); | |
| 200 } | |
| 201 | |
| 129 void TransparentActivateWindowButton::SendFocusAlert() const { | 202 void TransparentActivateWindowButton::SendFocusAlert() const { |
| 130 event_handler_widget_->GetContentsView()-> | 203 event_handler_widget_->GetContentsView()-> |
| 131 NotifyAccessibilityEvent(ui::AX_EVENT_FOCUS, true); | 204 NotifyAccessibilityEvent(ui::AX_EVENT_FOCUS, true); |
| 132 } | 205 } |
| 133 | 206 |
| 134 void TransparentActivateWindowButton::StackAtBottom() { | 207 void TransparentActivateWindowButton::StackAtBottom() { |
| 135 aura::Window* handler = event_handler_widget_->GetNativeWindow(); | 208 aura::Window* handler = event_handler_widget_->GetNativeWindow(); |
| 136 handler->parent()->StackChildAtBottom(handler); | 209 handler->parent()->StackChildAtBottom(handler); |
| 137 } | 210 } |
| 138 | 211 |
| 139 void TransparentActivateWindowButton::ButtonPressed(views::Button* sender, | 212 void TransparentActivateWindowButton::ButtonPressed(views::Button* sender, |
| 140 const ui::Event& event) { | 213 const ui::Event& event) { |
| 141 transparent_button_->delegate()->Select(); | 214 transparent_button_->delegate()->Select(); |
| 142 } | 215 } |
| 143 | 216 |
| 144 } // namespace ash | 217 } // namespace ash |
| OLD | NEW |