• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (c) 2011 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 // Draws the view for the balloons.
6 
7 #ifndef CHROME_BROWSER_UI_VIEWS_NOTIFICATIONS_BALLOON_VIEW_H_
8 #define CHROME_BROWSER_UI_VIEWS_NOTIFICATIONS_BALLOON_VIEW_H_
9 #pragma once
10 
11 #include "base/basictypes.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "base/task.h"
14 #include "chrome/browser/notifications/balloon.h"
15 #include "chrome/browser/ui/views/notifications/balloon_view_host.h"
16 #include "content/common/notification_registrar.h"
17 #include "ui/base/animation/animation_delegate.h"
18 #include "ui/gfx/path.h"
19 #include "ui/gfx/point.h"
20 #include "ui/gfx/rect.h"
21 #include "ui/gfx/size.h"
22 #include "views/controls/button/menu_button.h"
23 #include "views/controls/label.h"
24 #include "views/controls/menu/view_menu_delegate.h"
25 #include "views/view.h"
26 #include "views/widget/widget_delegate.h"
27 
28 namespace views {
29 class ButtonListener;
30 class ImageButton;
31 class ImagePainter;
32 class TextButton;
33 class WidgetWin;
34 class Menu2;
35 }  // namespace views
36 
37 class BalloonCollection;
38 class NotificationDetails;
39 class NotificationOptionsMenuModel;
40 class NotificationSource;
41 
42 namespace ui {
43 class SlideAnimation;
44 }
45 
46 // A balloon view is the UI component for a desktop notification toasts.
47 // It draws a border, and within the border an HTML renderer.
48 class BalloonViewImpl : public BalloonView,
49                         public views::View,
50                         public views::ViewMenuDelegate,
51                         public views::WidgetDelegate,
52                         public views::ButtonListener,
53                         public NotificationObserver,
54                         public ui::AnimationDelegate {
55  public:
56   explicit BalloonViewImpl(BalloonCollection* collection);
57   ~BalloonViewImpl();
58 
59   // BalloonView interface.
60   virtual void Show(Balloon* balloon) OVERRIDE;
61   virtual void Update() OVERRIDE;
62   virtual void RepositionToBalloon() OVERRIDE;
63   virtual void Close(bool by_user) OVERRIDE;
64   virtual gfx::Size GetSize() const OVERRIDE;
65   virtual BalloonHost* GetHost() const OVERRIDE;
66 
67  private:
68   // views::View interface.
69   virtual void OnPaint(gfx::Canvas* canvas) OVERRIDE;
70   virtual void OnBoundsChanged(const gfx::Rect& previous_bounds) OVERRIDE;
71   virtual gfx::Size GetPreferredSize() OVERRIDE;
72 
73   // views::ViewMenuDelegate interface.
74   virtual void RunMenu(views::View* source, const gfx::Point& pt) OVERRIDE;
75 
76   // views::WidgetDelegate interface.
77   virtual void OnDisplayChanged() OVERRIDE;
78   virtual void OnWorkAreaChanged() OVERRIDE;
79 
80   // views::ButtonListener interface.
81   virtual void ButtonPressed(
82       views::Button* sender, const views::Event&) OVERRIDE;
83 
84   // NotificationObserver interface.
85   virtual void Observe(NotificationType type,
86                        const NotificationSource& source,
87                        const NotificationDetails& details) OVERRIDE;
88 
89   // ui::AnimationDelegate interface.
90   virtual void AnimationProgressed(const ui::Animation* animation) OVERRIDE;
91 
92   // Launches the options menu at screen coordinates |pt|.
93   void RunOptionsMenu(const gfx::Point& pt);
94 
95   // Initializes the options menu.
96   void CreateOptionsMenu();
97 
98   // Masks the contents to fit within the frame.
99   void GetContentsMask(const gfx::Rect& contents_rect, gfx::Path* path) const;
100 
101   // Masks the frame for the rounded corners of the shadow-bubble.
102   void GetFrameMask(const gfx::Rect&, gfx::Path* path) const;
103 
104   // Adjust the contents window size to be appropriate for the frame.
105   void SizeContentsWindow();
106 
107   // Do the delayed close work.
108   void DelayedClose(bool by_user);
109 
110   // The height of the balloon's shelf.
111   // The shelf is where is close button is located.
112   int GetShelfHeight() const;
113 
114   // The height of the part of the frame around the balloon.
115   int GetBalloonFrameHeight() const;
116 
117   int GetTotalWidth() const;
118   int GetTotalHeight() const;
119 
120   gfx::Rect GetCloseButtonBounds() const;
121   gfx::Rect GetOptionsButtonBounds() const;
122   gfx::Rect GetLabelBounds() const;
123 
124   // Where the balloon contents should be placed with respect to the top left
125   // of the frame.
126   gfx::Point GetContentsOffset() const;
127 
128   // Where the balloon contents should be in screen coordinates.
129   gfx::Rect GetContentsRectangle() const;
130 
131   // Non-owned pointer to the balloon which owns this object.
132   Balloon* balloon_;
133 
134   // Non-owned pointer to the balloon collection this is a part of.
135   BalloonCollection* collection_;
136 
137   // The window that contains the frame of the notification.
138   // Pointer owned by the View subclass.
139   views::Widget* frame_container_;
140 
141   // The window that contains the contents of the notification.
142   // Pointer owned by the View subclass.
143   views::Widget* html_container_;
144 
145   // The renderer of the HTML contents.
146   scoped_ptr<BalloonViewHost> html_contents_;
147 
148   // The following factory is used to call methods at a later time.
149   ScopedRunnableMethodFactory<BalloonViewImpl> method_factory_;
150 
151   // Pointer to sub-view is owned by the View sub-class.
152   views::ImageButton* close_button_;
153 
154   // Pointer to sub-view is owned by View class.
155   views::Label* source_label_;
156 
157   // An animation to move the balloon on the screen as its position changes.
158   scoped_ptr<ui::SlideAnimation> animation_;
159   gfx::Rect anim_frame_start_;
160   gfx::Rect anim_frame_end_;
161 
162   // The options menu.
163   scoped_ptr<NotificationOptionsMenuModel> options_menu_model_;
164   scoped_ptr<views::Menu2> options_menu_menu_;
165   views::MenuButton* options_menu_button_;
166 
167   NotificationRegistrar notification_registrar_;
168 
169   DISALLOW_COPY_AND_ASSIGN(BalloonViewImpl);
170 };
171 
172 #endif  // CHROME_BROWSER_UI_VIEWS_NOTIFICATIONS_BALLOON_VIEW_H_
173