summaryrefslogtreecommitdiffstats
path: root/chrome/browser/ui/panels/panel_browser_frame_view.h
blob: d883caf4486e68ee7d798ef5b986f49465d5e405 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CHROME_BROWSER_UI_PANELS_PANEL_BROWSER_FRAME_VIEW_H_
#define CHROME_BROWSER_UI_PANELS_PANEL_BROWSER_FRAME_VIEW_H_
#pragma once

#include "base/gtest_prod_util.h"
#include "base/memory/scoped_ptr.h"
#include "base/message_loop.h"
#include "chrome/browser/extensions/extension_uninstall_dialog.h"
#include "chrome/browser/ui/views/frame/browser_non_client_frame_view.h"
#include "chrome/browser/ui/views/tab_icon_view.h"
#include "ui/base/models/simple_menu_model.h"
#include "views/controls/button/button.h"
#include "views/controls/menu/menu_item_view.h"
#include "views/controls/menu/menu_model_adapter.h"
#include "views/controls/menu/view_menu_delegate.h"

class Extension;
class PanelBrowserView;
namespace views {
class ImageButton;
class Label;
class MenuButton;
}

class PanelBrowserFrameView : public BrowserNonClientFrameView,
                              public views::ButtonListener,
                              public views::ViewMenuDelegate,
                              public ui::SimpleMenuModel::Delegate,
                              public TabIconView::TabIconViewModel,
                              public ExtensionUninstallDialog::Delegate {
 public:
  PanelBrowserFrameView(BrowserFrame* frame, PanelBrowserView* browser_view);
  virtual ~PanelBrowserFrameView();

  void UpdateTitleBar();
  void OnFocusChanged(bool focused);

  // Returns the height of the entire nonclient top border, including the window
  // frame, any title area, and any connected client edge.
  int NonClientTopBorderHeight() const;

 protected:
  // Overridden from BrowserNonClientFrameView:
  virtual gfx::Rect GetBoundsForTabStrip(views::View* tabstrip) const OVERRIDE;
  virtual int GetHorizontalTabStripVerticalOffset(bool restored) const OVERRIDE;
  virtual void UpdateThrobber(bool running) OVERRIDE;

  // Overridden from views::NonClientFrameView:
  virtual gfx::Rect GetBoundsForClientView() const OVERRIDE;
  virtual gfx::Rect GetWindowBoundsForClientBounds(
      const gfx::Rect& client_bounds) const OVERRIDE;
  virtual int NonClientHitTest(const gfx::Point& point) OVERRIDE;
  virtual void GetWindowMask(const gfx::Size& size, gfx::Path* window_mask)
      OVERRIDE;
  virtual void EnableClose(bool enable) OVERRIDE;
  virtual void ResetWindowControls() OVERRIDE;
  virtual void UpdateWindowIcon() OVERRIDE;

  // Overridden from views::View:
  virtual void OnPaint(gfx::Canvas* canvas) OVERRIDE;
  virtual void OnThemeChanged() OVERRIDE;
  virtual gfx::Size GetMinimumSize() OVERRIDE;
  virtual void Layout() OVERRIDE;
  virtual void GetAccessibleState(ui::AccessibleViewState* state) OVERRIDE;
  virtual bool OnMousePressed(const views::MouseEvent& event) OVERRIDE;
  virtual bool OnMouseDragged(const views::MouseEvent& event) OVERRIDE;
  virtual void OnMouseReleased(const views::MouseEvent& event) OVERRIDE;
  virtual void OnMouseCaptureLost() OVERRIDE;

  // Overridden from views::ButtonListener:
  virtual void ButtonPressed(views::Button* sender, const views::Event& event)
      OVERRIDE;

  // Overridden from views::ViewMenuDelegate:
  virtual void RunMenu(View* source, const gfx::Point& pt) OVERRIDE;

  // Overridden from ui::SimpleMenuModel::Delegate:
  virtual bool IsCommandIdChecked(int command_id) const OVERRIDE;
  virtual bool IsCommandIdEnabled(int command_id) const OVERRIDE;
  virtual bool GetAcceleratorForCommandId(
      int command_id, ui::Accelerator* accelerator) OVERRIDE;
  virtual void ExecuteCommand(int command_id) OVERRIDE;

  // Overridden from TabIconView::TabIconViewModel:
  virtual bool ShouldTabIconViewAnimate() const OVERRIDE;
  virtual SkBitmap GetFaviconForTabIconView() OVERRIDE;

  // ExtensionUninstallDialog::Delegate:
  virtual void ExtensionDialogAccepted() OVERRIDE;
  virtual void ExtensionDialogCanceled() OVERRIDE;

 private:
  friend class PanelBrowserViewTest;
  FRIEND_TEST_ALL_PREFIXES(PanelBrowserViewTest, CreatePanel);
  FRIEND_TEST_ALL_PREFIXES(PanelBrowserViewTest, ShowOrHideSettingsButton);

  enum PaintState {
    NOT_PAINTED,
    PAINT_AS_INACTIVE,
    PAINT_AS_ACTIVE,
    PAINT_FOR_ATTENTION
  };

  enum {
    COMMAND_NAME = 0,
    COMMAND_CONFIGURE,
    COMMAND_DISABLE,
    COMMAND_UNINSTALL,
    COMMAND_MANAGE
  };

  class MouseWatcher : public MessageLoopForUI::Observer {
   public:
    explicit MouseWatcher(PanelBrowserFrameView* view);
    virtual ~MouseWatcher();

    virtual bool IsCursorInViewBounds() const;

  #if defined(OS_WIN)
    virtual void WillProcessMessage(const MSG& msg) OVERRIDE { }
    virtual void DidProcessMessage(const MSG& msg) OVERRIDE;
  #else
    virtual void WillProcessEvent(GdkEvent* event) OVERRIDE { }
    virtual void DidProcessEvent(GdkEvent* event) OVERRIDE;
  #endif

   private:
    void HandleGlobalMouseMoveEvent();

    PanelBrowserFrameView* view_;
    bool is_mouse_within_;

    DISALLOW_COPY_AND_ASSIGN(MouseWatcher);
  };

  // Returns the thickness of the entire nonclient left, right, and bottom
  // borders, including both the window frame and any client edge.
  int NonClientBorderThickness() const;

  // Update control styles to indicate if the title bar is active or not.
  void UpdateControlStyles(PaintState paint_state);

  // Custom draw the frame.
  void PaintFrameBorder(gfx::Canvas* canvas);
  void PaintClientEdge(gfx::Canvas* canvas);

  // Called by MouseWatcher to notify if the mouse enters or leaves the window.
  void OnMouseEnterOrLeaveWindow(bool mouse_entered);

  // Retrieves the drawing metrics based on the current painting state.
  SkColor GetTitleColor(PaintState paint_state) const;
  gfx::Font* GetTitleFont(PaintState paint_state) const;
  SkBitmap* GetFrameTheme(PaintState paint_state) const;

  // Make settings button visible if either of the conditions is met:
  // 1) The panel is active, i.e. having focus.
  // 2) The mouse is over the panel.
  void UpdateSettingsButtonVisibility(bool active, bool cursor_in_view);

  const Extension* GetExtension() const;

  void EnsureSettingsMenuCreated();

#ifdef UNIT_TEST
  void set_mouse_watcher(MouseWatcher* mouse_watcher) {
    mouse_watcher_.reset(mouse_watcher);
  }
#endif

  // The frame that hosts this view. This is a weak reference such that frame_
  // will always be valid in the lifetime of this view.
  BrowserFrame* frame_;

  // The client view hosted within this non-client frame view that is
  // guaranteed to be freed before the client view.
  // (see comments about view hierarchies in non_client_view.h)
  PanelBrowserView* browser_view_;

  PaintState paint_state_;
  views::MenuButton* settings_button_;
  bool is_settings_button_visible_;
  views::ImageButton* close_button_;
  TabIconView* title_icon_;
  views::Label* title_label_;
  gfx::Rect client_view_bounds_;
  scoped_ptr<MouseWatcher> mouse_watcher_;
  ui::SimpleMenuModel settings_menu_contents_;
  views::MenuModelAdapter settings_menu_adapter_;
  views::MenuItemView settings_menu_;
  scoped_ptr<ExtensionUninstallDialog> extension_uninstall_dialog_;

  DISALLOW_COPY_AND_ASSIGN(PanelBrowserFrameView);
};

#endif  // CHROME_BROWSER_UI_PANELS_PANEL_BROWSER_FRAME_VIEW_H_