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/workspace/frame_maximize_button.h" |
| 6 |
| 7 #include "ash/shell.h" |
| 8 #include "ash/wm/property_util.h" |
| 9 #include "ash/launcher/launcher.h" |
| 10 #include "ash/wm/workspace/phantom_window_controller.h" |
| 11 #include "ash/wm/workspace/workspace_window_resizer.h" |
| 12 #include "grit/ui_resources.h" |
| 13 #include "ui/base/resource/resource_bundle.h" |
| 14 #include "ui/gfx/image/image.h" |
| 15 #include "ui/gfx/screen.h" |
| 16 #include "ui/views/widget/widget.h" |
| 17 |
| 18 namespace ash { |
| 19 |
| 20 FrameMaximizeButton::FrameMaximizeButton(views::ButtonListener* listener) |
| 21 : ImageButton(listener), |
| 22 is_snap_enabled_(false), |
| 23 exceeded_drag_threshold_(false), |
| 24 snap_type_(SNAP_NONE) { |
| 25 // TODO(sky): nuke this. It's temporary while we don't have good images. |
| 26 SetImageAlignment(ALIGN_LEFT, ALIGN_BOTTOM); |
| 27 } |
| 28 |
| 29 FrameMaximizeButton::~FrameMaximizeButton() { |
| 30 } |
| 31 |
| 32 bool FrameMaximizeButton::OnMousePressed(const views::MouseEvent& event) { |
| 33 is_snap_enabled_ = event.IsLeftMouseButton(); |
| 34 if (is_snap_enabled_) { |
| 35 snap_type_ = SNAP_NONE; |
| 36 press_location_ = event.location(); |
| 37 exceeded_drag_threshold_ = false; |
| 38 } |
| 39 ImageButton::OnMousePressed(event); |
| 40 return true; |
| 41 } |
| 42 |
| 43 void FrameMaximizeButton::OnMouseEntered(const views::MouseEvent& event) { |
| 44 ImageButton::OnMouseEntered(event); |
| 45 } |
| 46 |
| 47 void FrameMaximizeButton::OnMouseExited(const views::MouseEvent& event) { |
| 48 ImageButton::OnMouseExited(event); |
| 49 } |
| 50 |
| 51 bool FrameMaximizeButton::OnMouseDragged(const views::MouseEvent& event) { |
| 52 if (is_snap_enabled_) { |
| 53 int delta_x = event.location().x() - press_location_.x(); |
| 54 int delta_y = event.location().y() - press_location_.y(); |
| 55 if (!exceeded_drag_threshold_) { |
| 56 exceeded_drag_threshold_ = |
| 57 views::View::ExceededDragThreshold(delta_x, delta_y); |
| 58 } |
| 59 if (exceeded_drag_threshold_) |
| 60 UpdateSnap(delta_x, delta_y); |
| 61 } |
| 62 return ImageButton::OnMouseDragged(event); |
| 63 } |
| 64 |
| 65 void FrameMaximizeButton::OnMouseReleased(const views::MouseEvent& event) { |
| 66 bool should_snap = is_snap_enabled_; |
| 67 is_snap_enabled_ = false; |
| 68 if (should_snap && snap_type_ != SNAP_NONE) { |
| 69 SetState(BS_NORMAL); |
| 70 phantom_window_.reset(); |
| 71 Snap(); |
| 72 } else { |
| 73 ImageButton::OnMouseReleased(event); |
| 74 } |
| 75 } |
| 76 |
| 77 void FrameMaximizeButton::OnMouseCaptureLost() { |
| 78 is_snap_enabled_ = false; |
| 79 phantom_window_.reset(); |
| 80 SchedulePaint(); |
| 81 ImageButton::OnMouseCaptureLost(); |
| 82 } |
| 83 |
| 84 SkBitmap FrameMaximizeButton::GetImageToPaint() { |
| 85 if (is_snap_enabled_) { |
| 86 int id = 0; |
| 87 if (GetWidget()->IsMaximized()) { |
| 88 switch (snap_type_) { |
| 89 case SNAP_LEFT: |
| 90 id = IDR_AURA_WINDOW_MAXIMIZED_RESTORE_SNAP_LEFT_P; |
| 91 break; |
| 92 case SNAP_RIGHT: |
| 93 id = IDR_AURA_WINDOW_MAXIMIZED_RESTORE_SNAP_RIGHT_P; |
| 94 break; |
| 95 case SNAP_MAXIMIZE: |
| 96 case SNAP_NONE: |
| 97 id = IDR_AURA_WINDOW_MAXIMIZED_RESTORE_SNAP_P; |
| 98 break; |
| 99 case SNAP_MINIMIZE: |
| 100 id = IDR_AURA_WINDOW_MAXIMIZED_RESTORE_SNAP_MINIMIZE_P; |
| 101 break; |
| 102 default: |
| 103 NOTREACHED(); |
| 104 } |
| 105 } else { |
| 106 switch (snap_type_) { |
| 107 case SNAP_LEFT: |
| 108 id = IDR_AURA_WINDOW_MAXIMIZED_SNAP_LEFT_P; |
| 109 break; |
| 110 case SNAP_RIGHT: |
| 111 id = IDR_AURA_WINDOW_MAXIMIZED_SNAP_RIGHT_P; |
| 112 break; |
| 113 case SNAP_MAXIMIZE: |
| 114 case SNAP_NONE: |
| 115 id = IDR_AURA_WINDOW_MAXIMIZED_SNAP_P; |
| 116 break; |
| 117 case SNAP_MINIMIZE: |
| 118 id = IDR_AURA_WINDOW_MAXIMIZED_SNAP_MINIMIZE_P; |
| 119 break; |
| 120 default: |
| 121 NOTREACHED(); |
| 122 } |
| 123 } |
| 124 return *ResourceBundle::GetSharedInstance().GetImageNamed(id).ToSkBitmap(); |
| 125 } |
| 126 return ImageButton::GetImageToPaint(); |
| 127 } |
| 128 |
| 129 void FrameMaximizeButton::UpdateSnap(int delta_x, int delta_y) { |
| 130 SnapType type = SnapTypeForDelta(delta_x, delta_y); |
| 131 if (type == snap_type_) |
| 132 return; |
| 133 |
| 134 SchedulePaint(); |
| 135 |
| 136 if (type == SNAP_NONE) { |
| 137 phantom_window_.reset(); |
| 138 snap_type_ = SNAP_NONE; |
| 139 return; |
| 140 } |
| 141 |
| 142 snap_type_ = type; |
| 143 if (!phantom_window_.get()) { |
| 144 phantom_window_.reset(new internal::PhantomWindowController( |
| 145 GetWidget()->GetNativeWindow(), |
| 146 internal::PhantomWindowController::TYPE_EDGE, 0)); |
| 147 } |
| 148 phantom_window_->Show(BoundsForType(snap_type_)); |
| 149 } |
| 150 |
| 151 FrameMaximizeButton::SnapType FrameMaximizeButton::SnapTypeForDelta( |
| 152 int delta_x, |
| 153 int delta_y) const { |
| 154 if (!views::View::ExceededDragThreshold(delta_x, delta_y)) |
| 155 return GetWidget()->IsMaximized() ? SNAP_NONE : SNAP_MAXIMIZE; |
| 156 else if (delta_x < 0 && delta_y > delta_x && delta_y < -delta_x) |
| 157 return SNAP_LEFT; |
| 158 else if (delta_x > 0 && delta_y > -delta_x && delta_y < delta_x) |
| 159 return SNAP_RIGHT; |
| 160 else if (delta_y > 0) |
| 161 return SNAP_MINIMIZE; |
| 162 return GetWidget()->IsMaximized() ? SNAP_NONE : SNAP_MAXIMIZE; |
| 163 } |
| 164 |
| 165 gfx::Rect FrameMaximizeButton::BoundsForType(SnapType type) const { |
| 166 aura::Window* window = GetWidget()->GetNativeWindow(); |
| 167 int grid_size = Shell::GetInstance()->GetGridSize(); |
| 168 switch (type) { |
| 169 case SNAP_LEFT: |
| 170 return internal::WorkspaceWindowResizer::GetBoundsForWindowAlongEdge( |
| 171 window, internal::WorkspaceWindowResizer::LEFT_EDGE, grid_size); |
| 172 case SNAP_RIGHT: |
| 173 return internal::WorkspaceWindowResizer::GetBoundsForWindowAlongEdge( |
| 174 window, internal::WorkspaceWindowResizer::RIGHT_EDGE, grid_size); |
| 175 case SNAP_MAXIMIZE: |
| 176 return gfx::Screen::GetMonitorWorkAreaNearestWindow(window); |
| 177 case SNAP_MINIMIZE: { |
| 178 Launcher* launcher = Shell::GetInstance()->launcher(); |
| 179 gfx::Rect item_rect(launcher->GetScreenBoundsOfItemIconForWindow(window)); |
| 180 if (!item_rect.IsEmpty()) { |
| 181 // PhantomWindowController insets slightly, outset it so the phantom |
| 182 // doesn't appear inset. |
| 183 item_rect.Inset(-8, -8); |
| 184 return item_rect; |
| 185 } |
| 186 return launcher->widget()->GetWindowScreenBounds(); |
| 187 } |
| 188 default: |
| 189 NOTREACHED(); |
| 190 } |
| 191 return gfx::Rect(); |
| 192 } |
| 193 |
| 194 void FrameMaximizeButton::Snap() { |
| 195 switch (snap_type_) { |
| 196 case SNAP_LEFT: |
| 197 case SNAP_RIGHT: |
| 198 if (GetWidget()->IsMaximized()) { |
| 199 ash::SetRestoreBounds(GetWidget()->GetNativeWindow(), |
| 200 BoundsForType(snap_type_)); |
| 201 GetWidget()->Restore(); |
| 202 } else { |
| 203 GetWidget()->SetBounds(BoundsForType(snap_type_)); |
| 204 } |
| 205 break; |
| 206 case SNAP_MAXIMIZE: |
| 207 GetWidget()->Maximize(); |
| 208 break; |
| 209 case SNAP_MINIMIZE: |
| 210 GetWidget()->Minimize(); |
| 211 break; |
| 212 default: |
| 213 NOTREACHED(); |
| 214 } |
| 215 } |
| 216 |
| 217 } // namespace ash |
OLD | NEW |