OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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/workspace/frame_maximize_button.h" | 5 #include "ash/wm/workspace/frame_maximize_button.h" |
6 | 6 |
7 #include "ash/shell.h" | 7 #include "ash/shell.h" |
8 #include "ash/wm/property_util.h" | 8 #include "ash/wm/property_util.h" |
9 #include "ash/launcher/launcher.h" | 9 #include "ash/launcher/launcher.h" |
10 #include "ash/wm/workspace/phantom_window_controller.h" | 10 #include "ash/wm/workspace/phantom_window_controller.h" |
11 #include "ash/wm/workspace/workspace_window_resizer.h" | 11 #include "ash/wm/workspace/workspace_window_resizer.h" |
12 #include "grit/ui_resources.h" | 12 #include "grit/ui_resources.h" |
| 13 #include "ui/aura/event.h" |
| 14 #include "ui/aura/event_filter.h" |
13 #include "ui/base/resource/resource_bundle.h" | 15 #include "ui/base/resource/resource_bundle.h" |
14 #include "ui/gfx/image/image.h" | 16 #include "ui/gfx/image/image.h" |
15 #include "ui/gfx/screen.h" | 17 #include "ui/gfx/screen.h" |
16 #include "ui/views/widget/widget.h" | 18 #include "ui/views/widget/widget.h" |
17 | 19 |
18 namespace ash { | 20 namespace ash { |
19 | 21 |
| 22 // EscapeEventFilter is installed on the RootWindow to track when the escape key |
| 23 // is pressed. We use an EventFilter for this as the FrameMaximizeButton |
| 24 // normally does not get focus. |
| 25 class FrameMaximizeButton::EscapeEventFilter : public aura::EventFilter { |
| 26 public: |
| 27 explicit EscapeEventFilter(FrameMaximizeButton* button); |
| 28 virtual ~EscapeEventFilter(); |
| 29 |
| 30 // EventFilter overrides: |
| 31 virtual bool PreHandleKeyEvent(aura::Window* target, |
| 32 aura::KeyEvent* event) OVERRIDE; |
| 33 virtual bool PreHandleMouseEvent(aura::Window* target, |
| 34 aura::MouseEvent* event) OVERRIDE; |
| 35 virtual ui::TouchStatus PreHandleTouchEvent( |
| 36 aura::Window* target, |
| 37 aura::TouchEvent* event) OVERRIDE; |
| 38 virtual ui::GestureStatus PreHandleGestureEvent( |
| 39 aura::Window* target, |
| 40 aura::GestureEvent* event) OVERRIDE; |
| 41 |
| 42 private: |
| 43 FrameMaximizeButton* button_; |
| 44 |
| 45 DISALLOW_COPY_AND_ASSIGN(EscapeEventFilter); |
| 46 }; |
| 47 |
| 48 FrameMaximizeButton::EscapeEventFilter::EscapeEventFilter( |
| 49 FrameMaximizeButton* button) |
| 50 : button_(button) { |
| 51 Shell::GetInstance()->AddRootWindowEventFilter(this); |
| 52 } |
| 53 |
| 54 FrameMaximizeButton::EscapeEventFilter::~EscapeEventFilter() { |
| 55 Shell::GetInstance()->RemoveRootWindowEventFilter(this); |
| 56 } |
| 57 |
| 58 bool FrameMaximizeButton::EscapeEventFilter::PreHandleKeyEvent( |
| 59 aura::Window* target, |
| 60 aura::KeyEvent* event) { |
| 61 if (event->type() == ui::ET_KEY_PRESSED && |
| 62 event->key_code() == ui::VKEY_ESCAPE) { |
| 63 button_->Cancel(); |
| 64 } |
| 65 return false; |
| 66 } |
| 67 |
| 68 bool FrameMaximizeButton::EscapeEventFilter::PreHandleMouseEvent( |
| 69 aura::Window* target, |
| 70 aura::MouseEvent* event) { |
| 71 return false; |
| 72 } |
| 73 |
| 74 ui::TouchStatus FrameMaximizeButton::EscapeEventFilter::PreHandleTouchEvent( |
| 75 aura::Window* target, |
| 76 aura::TouchEvent* event) { |
| 77 return ui::TOUCH_STATUS_UNKNOWN; |
| 78 } |
| 79 |
| 80 ui::GestureStatus FrameMaximizeButton::EscapeEventFilter::PreHandleGestureEvent( |
| 81 aura::Window* target, |
| 82 aura::GestureEvent* event) { |
| 83 return ui::GESTURE_STATUS_UNKNOWN; |
| 84 } |
| 85 |
| 86 // FrameMaximizeButton --------------------------------------------------------- |
| 87 |
20 FrameMaximizeButton::FrameMaximizeButton(views::ButtonListener* listener) | 88 FrameMaximizeButton::FrameMaximizeButton(views::ButtonListener* listener) |
21 : ImageButton(listener), | 89 : ImageButton(listener), |
22 is_snap_enabled_(false), | 90 is_snap_enabled_(false), |
23 exceeded_drag_threshold_(false), | 91 exceeded_drag_threshold_(false), |
24 snap_type_(SNAP_NONE) { | 92 snap_type_(SNAP_NONE) { |
25 // TODO(sky): nuke this. It's temporary while we don't have good images. | 93 // TODO(sky): nuke this. It's temporary while we don't have good images. |
26 SetImageAlignment(ALIGN_LEFT, ALIGN_BOTTOM); | 94 SetImageAlignment(ALIGN_LEFT, ALIGN_BOTTOM); |
27 } | 95 } |
28 | 96 |
29 FrameMaximizeButton::~FrameMaximizeButton() { | 97 FrameMaximizeButton::~FrameMaximizeButton() { |
30 } | 98 } |
31 | 99 |
32 bool FrameMaximizeButton::OnMousePressed(const views::MouseEvent& event) { | 100 bool FrameMaximizeButton::OnMousePressed(const views::MouseEvent& event) { |
33 is_snap_enabled_ = event.IsLeftMouseButton(); | 101 is_snap_enabled_ = event.IsLeftMouseButton(); |
34 if (is_snap_enabled_) { | 102 if (is_snap_enabled_) { |
| 103 InstallEventFilter(); |
35 snap_type_ = SNAP_NONE; | 104 snap_type_ = SNAP_NONE; |
36 press_location_ = event.location(); | 105 press_location_ = event.location(); |
37 exceeded_drag_threshold_ = false; | 106 exceeded_drag_threshold_ = false; |
38 } | 107 } |
39 ImageButton::OnMousePressed(event); | 108 ImageButton::OnMousePressed(event); |
40 return true; | 109 return true; |
41 } | 110 } |
42 | 111 |
43 void FrameMaximizeButton::OnMouseEntered(const views::MouseEvent& event) { | 112 void FrameMaximizeButton::OnMouseEntered(const views::MouseEvent& event) { |
44 ImageButton::OnMouseEntered(event); | 113 ImageButton::OnMouseEntered(event); |
(...skipping 11 matching lines...) Expand all Loading... |
56 exceeded_drag_threshold_ = | 125 exceeded_drag_threshold_ = |
57 views::View::ExceededDragThreshold(delta_x, delta_y); | 126 views::View::ExceededDragThreshold(delta_x, delta_y); |
58 } | 127 } |
59 if (exceeded_drag_threshold_) | 128 if (exceeded_drag_threshold_) |
60 UpdateSnap(delta_x, delta_y); | 129 UpdateSnap(delta_x, delta_y); |
61 } | 130 } |
62 return ImageButton::OnMouseDragged(event); | 131 return ImageButton::OnMouseDragged(event); |
63 } | 132 } |
64 | 133 |
65 void FrameMaximizeButton::OnMouseReleased(const views::MouseEvent& event) { | 134 void FrameMaximizeButton::OnMouseReleased(const views::MouseEvent& event) { |
| 135 UninstallEventFilter(); |
66 bool should_snap = is_snap_enabled_; | 136 bool should_snap = is_snap_enabled_; |
67 is_snap_enabled_ = false; | 137 is_snap_enabled_ = false; |
68 if (should_snap && snap_type_ != SNAP_NONE) { | 138 if (should_snap && snap_type_ != SNAP_NONE) { |
69 SetState(BS_NORMAL); | 139 SetState(BS_NORMAL); |
70 phantom_window_.reset(); | 140 phantom_window_.reset(); |
71 Snap(); | 141 Snap(); |
72 } else { | 142 } else { |
73 ImageButton::OnMouseReleased(event); | 143 ImageButton::OnMouseReleased(event); |
74 } | 144 } |
75 } | 145 } |
76 | 146 |
77 void FrameMaximizeButton::OnMouseCaptureLost() { | 147 void FrameMaximizeButton::OnMouseCaptureLost() { |
78 is_snap_enabled_ = false; | 148 Cancel(); |
79 phantom_window_.reset(); | |
80 SchedulePaint(); | |
81 ImageButton::OnMouseCaptureLost(); | 149 ImageButton::OnMouseCaptureLost(); |
82 } | 150 } |
83 | 151 |
84 SkBitmap FrameMaximizeButton::GetImageToPaint() { | 152 SkBitmap FrameMaximizeButton::GetImageToPaint() { |
85 if (is_snap_enabled_) { | 153 if (is_snap_enabled_) { |
86 int id = 0; | 154 int id = 0; |
87 if (GetWidget()->IsMaximized()) { | 155 if (GetWidget()->IsMaximized()) { |
88 switch (snap_type_) { | 156 switch (snap_type_) { |
89 case SNAP_LEFT: | 157 case SNAP_LEFT: |
90 id = IDR_AURA_WINDOW_MAXIMIZED_RESTORE_SNAP_LEFT_P; | 158 id = IDR_AURA_WINDOW_MAXIMIZED_RESTORE_SNAP_LEFT_P; |
(...skipping 28 matching lines...) Expand all Loading... |
119 break; | 187 break; |
120 default: | 188 default: |
121 NOTREACHED(); | 189 NOTREACHED(); |
122 } | 190 } |
123 } | 191 } |
124 return *ResourceBundle::GetSharedInstance().GetImageNamed(id).ToSkBitmap(); | 192 return *ResourceBundle::GetSharedInstance().GetImageNamed(id).ToSkBitmap(); |
125 } | 193 } |
126 return ImageButton::GetImageToPaint(); | 194 return ImageButton::GetImageToPaint(); |
127 } | 195 } |
128 | 196 |
| 197 void FrameMaximizeButton::Cancel() { |
| 198 UninstallEventFilter(); |
| 199 is_snap_enabled_ = false; |
| 200 phantom_window_.reset(); |
| 201 SchedulePaint(); |
| 202 } |
| 203 |
| 204 void FrameMaximizeButton::InstallEventFilter() { |
| 205 if (escape_event_filter_.get()) |
| 206 return; |
| 207 |
| 208 escape_event_filter_.reset(new EscapeEventFilter(this)); |
| 209 } |
| 210 |
| 211 void FrameMaximizeButton::UninstallEventFilter() { |
| 212 escape_event_filter_.reset(NULL); |
| 213 } |
| 214 |
129 void FrameMaximizeButton::UpdateSnap(int delta_x, int delta_y) { | 215 void FrameMaximizeButton::UpdateSnap(int delta_x, int delta_y) { |
130 SnapType type = SnapTypeForDelta(delta_x, delta_y); | 216 SnapType type = SnapTypeForDelta(delta_x, delta_y); |
131 if (type == snap_type_) | 217 if (type == snap_type_) |
132 return; | 218 return; |
133 | 219 |
134 SchedulePaint(); | 220 SchedulePaint(); |
135 | 221 |
136 if (type == SNAP_NONE) { | 222 if (type == SNAP_NONE) { |
137 phantom_window_.reset(); | 223 phantom_window_.reset(); |
138 snap_type_ = SNAP_NONE; | 224 snap_type_ = SNAP_NONE; |
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
208 break; | 294 break; |
209 case SNAP_MINIMIZE: | 295 case SNAP_MINIMIZE: |
210 GetWidget()->Minimize(); | 296 GetWidget()->Minimize(); |
211 break; | 297 break; |
212 default: | 298 default: |
213 NOTREACHED(); | 299 NOTREACHED(); |
214 } | 300 } |
215 } | 301 } |
216 | 302 |
217 } // namespace ash | 303 } // namespace ash |
OLD | NEW |