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 3 // found in the LICENSE file. 4 5 #ifndef ASH_FRAME_CAPTION_BUTTONS_FRAME_SIZE_BUTTON_H_ 6 #define ASH_FRAME_CAPTION_BUTTONS_FRAME_SIZE_BUTTON_H_ 7 8 #include "ash/ash_export.h" 9 #include "ash/frame/caption_buttons/frame_caption_button.h" 10 #include "ash/frame/caption_buttons/frame_size_button_delegate.h" 11 #include "base/timer/timer.h" 12 13 namespace views { 14 class Widget; 15 } 16 17 namespace ash { 18 class FrameSizeButtonDelegate; 19 class PhantomWindowController; 20 21 // The maximize/restore button. 22 // When the mouse is pressed over the size button or the size button is touched: 23 // - The minimize and close buttons are set to snap left and snap right 24 // respectively. 25 // - The size button stays pressed while the mouse is over the buttons to snap 26 // left and to snap right. The button underneath the mouse is hovered. 27 // When the drag terminates, the action for the button underneath the mouse 28 // is executed. For the sake of simplicity, the size button is the event 29 // handler for a click starting on the size button and the entire drag. 30 class ASH_EXPORT FrameSizeButton : public FrameCaptionButton { 31 public: 32 FrameSizeButton(views::ButtonListener* listener, 33 views::Widget* frame, 34 FrameSizeButtonDelegate* delegate); 35 36 virtual ~FrameSizeButton(); 37 38 // views::CustomButton overrides: 39 virtual bool OnMousePressed(const ui::MouseEvent& event) OVERRIDE; 40 virtual bool OnMouseDragged(const ui::MouseEvent& event) OVERRIDE; 41 virtual void OnMouseReleased(const ui::MouseEvent& event) OVERRIDE; 42 virtual void OnMouseCaptureLost() OVERRIDE; 43 virtual void OnMouseMoved(const ui::MouseEvent& event) OVERRIDE; 44 virtual void OnGestureEvent(ui::GestureEvent* event) OVERRIDE; 45 set_delay_to_set_buttons_to_snap_mode(int delay_ms)46 void set_delay_to_set_buttons_to_snap_mode(int delay_ms) { 47 set_buttons_to_snap_mode_delay_ms_ = delay_ms; 48 } 49 50 private: 51 enum SnapType { 52 SNAP_LEFT, 53 SNAP_RIGHT, 54 SNAP_NONE 55 }; 56 57 // Starts |set_buttons_to_snap_mode_timer_|. 58 void StartSetButtonsToSnapModeTimer(const ui::LocatedEvent& event); 59 60 // Animates the buttons adjacent to the size button to snap left and right. 61 void AnimateButtonsToSnapMode(); 62 63 // Sets the buttons adjacent to the size button to snap left and right. 64 // Passing in ANIMATE_NO progresses the animation (if any) to the end. 65 void SetButtonsToSnapMode(FrameSizeButtonDelegate::Animate animate); 66 67 // Updates |snap_type_|, whether the size button is pressed and whether any 68 // other buttons are hovered. 69 void UpdateSnapType(const ui::LocatedEvent& event); 70 71 // Returns the button which should be hovered (if any) while in "snap mode" 72 // for |event_location_in_screen|. 73 const FrameCaptionButton* GetButtonToHover( 74 const gfx::Point& event_location_in_screen) const; 75 76 // Snaps |frame_| according to |snap_type_|. Returns true if |frame_| was 77 // snapped. 78 bool CommitSnap(const ui::LocatedEvent& event); 79 80 // Sets the buttons adjacent to the size button to minimize and close again. 81 // Clears any state set while snapping was enabled. |animate| indicates 82 // whether the buttons should animate back to their original icons. 83 void SetButtonsToNormalMode(FrameSizeButtonDelegate::Animate animate); 84 85 // Widget that the size button acts on. 86 views::Widget* frame_; 87 88 // Not owned. 89 FrameSizeButtonDelegate* delegate_; 90 91 // Location of the event which started |set_buttons_to_snap_mode_timer_| in 92 // view coordinates. 93 gfx::Point set_buttons_to_snap_mode_timer_event_location_; 94 95 // The delay between the user pressing the size button and the buttons 96 // adjacent to the size button morphing into buttons for snapping left and 97 // right. 98 int set_buttons_to_snap_mode_delay_ms_; 99 100 base::OneShotTimer<FrameSizeButton> set_buttons_to_snap_mode_timer_; 101 102 // Whether the buttons adjacent to the size button snap the window left and 103 // right. 104 bool in_snap_mode_; 105 106 // The action to execute when the drag/click is ended. If 107 // |snap_type_| == SNAP_NONE, the size button's default action is run when the 108 // drag/click is ended. 109 SnapType snap_type_; 110 111 // Displays a preview of how the window's bounds will change as a result of 112 // snapping the window left or right. The preview is only visible if the snap 113 // left or snap right button is pressed. 114 scoped_ptr<PhantomWindowController> phantom_window_controller_; 115 116 DISALLOW_COPY_AND_ASSIGN(FrameSizeButton); 117 }; 118 119 } // namespace ash 120 121 #endif // ASH_FRAME_CAPTION_BUTTONS_FRAME_SIZE_BUTTON_H_ 122