Chromium Code Reviews| 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 #ifndef UI_VIEWS_CONTROLS_BUTTON_MENU_BUTTON_H_ | 5 #ifndef UI_VIEWS_CONTROLS_BUTTON_MENU_BUTTON_H_ |
| 6 #define UI_VIEWS_CONTROLS_BUTTON_MENU_BUTTON_H_ | 6 #define UI_VIEWS_CONTROLS_BUTTON_MENU_BUTTON_H_ |
| 7 | 7 |
| 8 #include <string> | 8 #include <string> |
| 9 | 9 |
| 10 #include "base/memory/weak_ptr.h" | |
| 10 #include "base/strings/string16.h" | 11 #include "base/strings/string16.h" |
| 11 #include "base/time/time.h" | 12 #include "base/time/time.h" |
| 12 #include "ui/views/background.h" | 13 #include "ui/views/background.h" |
| 13 #include "ui/views/controls/button/label_button.h" | 14 #include "ui/views/controls/button/label_button.h" |
| 14 | 15 |
| 15 namespace views { | 16 namespace views { |
| 16 | 17 |
| 17 class MenuButtonListener; | 18 class MenuButtonListener; |
| 18 | 19 |
| 19 //////////////////////////////////////////////////////////////////////////////// | 20 //////////////////////////////////////////////////////////////////////////////// |
| 20 // | 21 // |
| 21 // MenuButton | 22 // MenuButton |
| 22 // | 23 // |
| 23 // A button that shows a menu when the left mouse button is pushed | 24 // A button that shows a menu when the left mouse button is pushed |
| 24 // | 25 // |
| 25 //////////////////////////////////////////////////////////////////////////////// | 26 //////////////////////////////////////////////////////////////////////////////// |
| 26 class VIEWS_EXPORT MenuButton : public LabelButton { | 27 class VIEWS_EXPORT MenuButton : public LabelButton { |
| 27 public: | 28 public: |
| 29 // A scoped lock for keeping the MenuButton in STATE_PRESSED e.g., while a | |
| 30 // menu is running. These are cumulative. | |
| 31 class PressedLock { | |
|
sky
2014/09/19 21:21:59
How about some test coverage here?
Devlin
2014/09/19 22:37:48
Of course. Just wanted to make sure this approach
| |
| 32 public: | |
| 33 PressedLock(MenuButton* menu_button); | |
|
sky
2014/09/19 21:21:59
explicit.
Devlin
2014/09/19 22:37:49
Done.
| |
| 34 ~PressedLock(); | |
| 35 | |
| 36 private: | |
| 37 base::WeakPtr<MenuButton> menu_button_; | |
| 38 | |
| 39 DISALLOW_COPY_AND_ASSIGN(PressedLock); | |
| 40 }; | |
| 41 | |
| 28 static const char kViewClassName[]; | 42 static const char kViewClassName[]; |
| 29 | 43 |
| 30 // How much padding to put on the left and right of the menu marker. | 44 // How much padding to put on the left and right of the menu marker. |
| 31 static const int kMenuMarkerPaddingLeft; | 45 static const int kMenuMarkerPaddingLeft; |
| 32 static const int kMenuMarkerPaddingRight; | 46 static const int kMenuMarkerPaddingRight; |
| 33 | 47 |
| 34 // Create a Button. | 48 // Create a Button. |
| 35 MenuButton(ButtonListener* listener, | 49 MenuButton(ButtonListener* listener, |
| 36 const base::string16& text, | 50 const base::string16& text, |
| 37 MenuButtonListener* menu_button_listener, | 51 MenuButtonListener* menu_button_listener, |
| (...skipping 11 matching lines...) Expand all Loading... | |
| 49 | 63 |
| 50 // Activate the button (called when the button is pressed). | 64 // Activate the button (called when the button is pressed). |
| 51 virtual bool Activate(); | 65 virtual bool Activate(); |
| 52 | 66 |
| 53 // Overridden from View: | 67 // Overridden from View: |
| 54 virtual gfx::Size GetPreferredSize() const OVERRIDE; | 68 virtual gfx::Size GetPreferredSize() const OVERRIDE; |
| 55 virtual const char* GetClassName() const OVERRIDE; | 69 virtual const char* GetClassName() const OVERRIDE; |
| 56 virtual void OnPaint(gfx::Canvas* canvas) OVERRIDE; | 70 virtual void OnPaint(gfx::Canvas* canvas) OVERRIDE; |
| 57 virtual bool OnMousePressed(const ui::MouseEvent& event) OVERRIDE; | 71 virtual bool OnMousePressed(const ui::MouseEvent& event) OVERRIDE; |
| 58 virtual void OnMouseReleased(const ui::MouseEvent& event) OVERRIDE; | 72 virtual void OnMouseReleased(const ui::MouseEvent& event) OVERRIDE; |
| 73 virtual void OnMouseEntered(const ui::MouseEvent& event) OVERRIDE; | |
| 59 virtual void OnMouseExited(const ui::MouseEvent& event) OVERRIDE; | 74 virtual void OnMouseExited(const ui::MouseEvent& event) OVERRIDE; |
| 75 virtual void OnMouseMoved(const ui::MouseEvent& event) OVERRIDE; | |
| 60 virtual void OnGestureEvent(ui::GestureEvent* event) OVERRIDE; | 76 virtual void OnGestureEvent(ui::GestureEvent* event) OVERRIDE; |
| 61 virtual bool OnKeyPressed(const ui::KeyEvent& event) OVERRIDE; | 77 virtual bool OnKeyPressed(const ui::KeyEvent& event) OVERRIDE; |
| 62 virtual bool OnKeyReleased(const ui::KeyEvent& event) OVERRIDE; | 78 virtual bool OnKeyReleased(const ui::KeyEvent& event) OVERRIDE; |
| 63 virtual void GetAccessibleState(ui::AXViewState* state) OVERRIDE; | 79 virtual void GetAccessibleState(ui::AXViewState* state) OVERRIDE; |
| 64 | 80 |
| 65 protected: | 81 protected: |
| 66 // Paint the menu marker image. | 82 // Paint the menu marker image. |
| 67 void PaintMenuMarker(gfx::Canvas* canvas); | 83 void PaintMenuMarker(gfx::Canvas* canvas); |
| 68 | 84 |
| 69 // Overridden from LabelButton: | 85 // Overridden from LabelButton: |
| 70 virtual gfx::Rect GetChildAreaBounds() OVERRIDE; | 86 virtual gfx::Rect GetChildAreaBounds() OVERRIDE; |
| 71 | 87 |
| 72 // True if the menu is currently visible. | |
| 73 bool menu_visible_; | |
| 74 | |
| 75 // Offset of the associated menu position. | 88 // Offset of the associated menu position. |
| 76 gfx::Point menu_offset_; | 89 gfx::Point menu_offset_; |
| 77 | 90 |
| 78 private: | 91 private: |
| 92 friend class PressedLock; | |
| 93 | |
| 94 // Increment/decrement the number of "pressed" locks this button has, and | |
| 95 // set the state accordingly. | |
| 96 void PressedLocked(); | |
|
sky
2014/09/19 21:21:59
nit: IncrementPressedLocked, DecrementPressedLocke
Devlin
2014/09/19 22:37:48
Done.
| |
| 97 void PressedUnlocked(); | |
| 98 | |
| 79 // Compute the maximum X coordinate for the current screen. MenuButtons | 99 // Compute the maximum X coordinate for the current screen. MenuButtons |
| 80 // use this to make sure a menu is never shown off screen. | 100 // use this to make sure a menu is never shown off screen. |
| 81 int GetMaximumScreenXCoordinate(); | 101 int GetMaximumScreenXCoordinate(); |
| 82 | 102 |
| 83 // We use a time object in order to keep track of when the menu was closed. | 103 // We use a time object in order to keep track of when the menu was closed. |
| 84 // The time is used for simulating menu behavior for the menu button; that | 104 // The time is used for simulating menu behavior for the menu button; that |
| 85 // is, if the menu is shown and the button is pressed, we need to close the | 105 // is, if the menu is shown and the button is pressed, we need to close the |
| 86 // menu. There is no clean way to get the second click event because the | 106 // menu. There is no clean way to get the second click event because the |
| 87 // menu is displayed using a modal loop and, unlike regular menus in Windows, | 107 // menu is displayed using a modal loop and, unlike regular menus in Windows, |
| 88 // the button is not part of the displayed menu. | 108 // the button is not part of the displayed menu. |
| 89 base::TimeTicks menu_closed_time_; | 109 base::TimeTicks menu_closed_time_; |
| 90 | 110 |
| 91 // Our listener. Not owned. | 111 // Our listener. Not owned. |
| 92 MenuButtonListener* listener_; | 112 MenuButtonListener* listener_; |
| 93 | 113 |
| 94 // Whether or not we're showing a drop marker. | 114 // Whether or not we're showing a drop marker. |
| 95 bool show_menu_marker_; | 115 bool show_menu_marker_; |
| 96 | 116 |
| 97 // The down arrow used to differentiate the menu button from normal buttons. | 117 // The down arrow used to differentiate the menu button from normal buttons. |
| 98 const gfx::ImageSkia* menu_marker_; | 118 const gfx::ImageSkia* menu_marker_; |
| 99 | 119 |
| 100 // If non-null the destuctor sets this to true. This is set while the menu is | 120 // If non-null the destuctor sets this to true. This is set while the menu is |
| 101 // showing and used to detect if the menu was deleted while running. | 121 // showing and used to detect if the menu was deleted while running. |
| 102 bool* destroyed_flag_; | 122 bool* destroyed_flag_; |
| 103 | 123 |
| 124 // The current number of "pressed" locks this button has. | |
| 125 int pressed_lock_count_; | |
| 126 | |
| 127 base::WeakPtrFactory<MenuButton> weak_factory_; | |
| 128 | |
| 104 DISALLOW_COPY_AND_ASSIGN(MenuButton); | 129 DISALLOW_COPY_AND_ASSIGN(MenuButton); |
| 105 }; | 130 }; |
| 106 | 131 |
| 107 } // namespace views | 132 } // namespace views |
| 108 | 133 |
| 109 #endif // UI_VIEWS_CONTROLS_BUTTON_MENU_BUTTON_H_ | 134 #endif // UI_VIEWS_CONTROLS_BUTTON_MENU_BUTTON_H_ |
| OLD | NEW |