balloon_view.h revision 4a5e2dc747d50c653511c68ccb2cfbfb740bd5a7
1// Copyright (c) 2010 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 "app/animation_delegate.h"
12#include "base/basictypes.h"
13#include "base/scoped_ptr.h"
14#include "base/task.h"
15#include "chrome/browser/notifications/balloon.h"
16#include "chrome/browser/views/notifications/balloon_view_host.h"
17#include "chrome/common/notification_registrar.h"
18#include "chrome/common/notification_service.h"
19#include "gfx/path.h"
20#include "gfx/point.h"
21#include "gfx/rect.h"
22#include "gfx/size.h"
23#include "views/controls/button/menu_button.h"
24#include "views/controls/label.h"
25#include "views/controls/menu/view_menu_delegate.h"
26#include "views/view.h"
27#include "views/widget/widget_delegate.h"
28
29namespace views {
30class ButtonListener;
31class ImageButton;
32class ImagePainter;
33class TextButton;
34class WidgetWin;
35class Menu2;
36}  // namespace views
37
38class BalloonCollection;
39class NotificationDetails;
40class NotificationOptionsMenuModel;
41class NotificationSource;
42class SlideAnimation;
43
44// A balloon view is the UI component for a desktop notification toasts.
45// It draws a border, and within the border an HTML renderer.
46class BalloonViewImpl : public BalloonView,
47                        public views::View,
48                        public views::ViewMenuDelegate,
49                        public views::WidgetDelegate,
50                        public views::ButtonListener,
51                        public NotificationObserver,
52                        public AnimationDelegate {
53 public:
54  explicit BalloonViewImpl(BalloonCollection* collection);
55  ~BalloonViewImpl();
56
57  // BalloonView interface.
58  virtual void Show(Balloon* balloon);
59  virtual void Update();
60  virtual void RepositionToBalloon();
61  virtual void Close(bool by_user);
62  virtual gfx::Size GetSize() const;
63  virtual BalloonHost* GetHost() const { return html_contents_.get(); }
64
65 private:
66  // views::View interface.
67  virtual void Paint(gfx::Canvas* canvas);
68  virtual void DidChangeBounds(const gfx::Rect& previous,
69                               const gfx::Rect& current);
70  virtual gfx::Size GetPreferredSize() {
71    return gfx::Size(1000, 1000);
72  }
73
74  // views::ViewMenuDelegate interface.
75  void RunMenu(views::View* source, const gfx::Point& pt);
76
77  // views::WidgetDelegate interface.
78  void DisplayChanged();
79  void WorkAreaChanged();
80
81  // views::ButtonListener interface.
82  virtual void ButtonPressed(views::Button* sender, const views::Event&);
83
84  // NotificationObserver interface.
85  virtual void Observe(NotificationType type,
86                       const NotificationSource& source,
87                       const NotificationDetails& details);
88
89  // AnimationDelegate interface.
90  virtual void AnimationProgressed(const Animation* animation);
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<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