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 CHROME_BROWSER_UI_VIEWS_PROFILES_AVATAR_MENU_BUBBLE_VIEW_H_ 6 #define CHROME_BROWSER_UI_VIEWS_PROFILES_AVATAR_MENU_BUBBLE_VIEW_H_ 7 8 #include <vector> 9 10 #include "base/basictypes.h" 11 #include "base/compiler_specific.h" 12 #include "base/gtest_prod_util.h" 13 #include "chrome/browser/profiles/avatar_menu_observer.h" 14 #include "ui/views/bubble/bubble_delegate.h" 15 #include "ui/views/controls/button/button.h" 16 #include "ui/views/controls/link_listener.h" 17 18 class AvatarMenu; 19 class Browser; 20 class ProfileItemView; 21 22 namespace content { 23 class WebContents; 24 } 25 26 namespace views { 27 class CustomButton; 28 class ImageView; 29 class Label; 30 class Link; 31 class Separator; 32 } 33 34 // This bubble view is displayed when the user clicks on the avatar button. 35 // It displays a list of profiles and allows users to switch between profiles. 36 class AvatarMenuBubbleView : public views::BubbleDelegateView, 37 public views::ButtonListener, 38 public views::LinkListener, 39 public AvatarMenuObserver { 40 public: 41 // Helper function to show the bubble and ensure that it doesn't reshow. 42 // Normally this bubble is shown when there's a mouse down event on a button. 43 // If the bubble is already showing when the user clicks on the button then 44 // this will cause two things to happen: 45 // - (1) the button will show a new instance of the bubble 46 // - (2) the old instance of the bubble will get a deactivate event and 47 // close 48 // To prevent this reshow this function checks if an instance of the bubble 49 // is already showing and do nothing. This means that (1) will do nothing 50 // and (2) will correctly hide the old bubble instance. 51 static void ShowBubble(views::View* anchor_view, 52 views::BubbleBorder::Arrow arrow, 53 views::BubbleBorder::ArrowPaintType arrow_paint_type, 54 views::BubbleBorder::BubbleAlignment border_alignment, 55 const gfx::Rect& anchor_rect, 56 Browser* browser); 57 static bool IsShowing(); 58 static void Hide(); 59 60 virtual ~AvatarMenuBubbleView(); 61 62 // views::View implementation. 63 virtual gfx::Size GetPreferredSize() const OVERRIDE; 64 virtual void Layout() OVERRIDE; 65 virtual bool AcceleratorPressed(const ui::Accelerator& accelerator) OVERRIDE; 66 67 // views::ButtonListener implementation. 68 virtual void ButtonPressed(views::Button* sender, 69 const ui::Event& event) OVERRIDE; 70 71 // views::LinkListener implementation. 72 virtual void LinkClicked(views::Link* source, int event_flags) OVERRIDE; 73 74 // BubbleDelegate implementation. 75 virtual gfx::Rect GetAnchorRect() const OVERRIDE; 76 virtual void Init() OVERRIDE; 77 virtual void WindowClosing() OVERRIDE; 78 79 // AvatarMenuObserver implementation. 80 virtual void OnAvatarMenuChanged( 81 AvatarMenu* avatar_menu) OVERRIDE; 82 83 // We normally close the bubble any time it becomes inactive but this can lead 84 // to flaky tests where unexpected UI events are triggering this behavior. 85 // Tests should call this with "false" for more consistent operation. 86 static void clear_close_on_deactivate_for_testing() { 87 close_on_deactivate_for_testing_ = false; 88 } 89 90 private: 91 AvatarMenuBubbleView(views::View* anchor_view, 92 views::BubbleBorder::Arrow arrow, 93 const gfx::Rect& anchor_rect, 94 Browser* browser); 95 96 // Sets the colors on all the |item_views_|. Called after the 97 // BubbleDelegateView is created and has loaded the colors from the 98 // NativeTheme. 99 void SetBackgroundColors(); 100 101 // Create the menu contents for a normal profile. 102 void InitMenuContents(AvatarMenu* avatar_menu); 103 104 // Create the supervised user specific contents of the menu. 105 void InitSupervisedUserContents(AvatarMenu* avatar_menu); 106 107 scoped_ptr<AvatarMenu> avatar_menu_; 108 gfx::Rect anchor_rect_; 109 Browser* browser_; 110 std::vector<ProfileItemView*> item_views_; 111 112 // Used to separate the link entry in the avatar menu from the other entries. 113 views::Separator* separator_; 114 115 // This will be non-NULL if and only if 116 // avatar_menu_->ShouldShowAddNewProfileLink() returns true. See 117 // OnAvatarMenuChanged(). 118 views::View* buttons_view_; 119 120 // This will be non-NULL if and only if |expanded_| is false and 121 // avatar_menu_->GetSupervisedUserInformation() returns a non-empty string. 122 // See OnAvatarMenuChanged(). 123 views::Label* supervised_user_info_; 124 views::ImageView* icon_view_; 125 views::Separator* separator_switch_users_; 126 views::Link* switch_profile_link_; 127 128 static AvatarMenuBubbleView* avatar_bubble_; 129 static bool close_on_deactivate_for_testing_; 130 131 // Is set to true if the supervised user has clicked on Switch Users. 132 bool expanded_; 133 134 DISALLOW_COPY_AND_ASSIGN(AvatarMenuBubbleView); 135 }; 136 137 #endif // CHROME_BROWSER_UI_VIEWS_PROFILES_AVATAR_MENU_BUBBLE_VIEW_H_ 138