summaryrefslogtreecommitdiffstats
path: root/chrome/browser/ui/panels/docked_panel_collection.h
blob: 19c219bc1a29c770eb82c98ee6792fcff0e153f8 (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
// Copyright (c) 2012 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_DOCKED_PANEL_COLLECTION_H_
#define CHROME_BROWSER_UI_PANELS_DOCKED_PANEL_COLLECTION_H_

#include <list>
#include <set>

#include "base/macros.h"
#include "base/memory/weak_ptr.h"
#include "chrome/browser/ui/panels/display_settings_provider.h"
#include "chrome/browser/ui/panels/panel.h"
#include "chrome/browser/ui/panels/panel_collection.h"
#include "chrome/browser/ui/panels/panel_mouse_watcher_observer.h"
#include "ui/gfx/geometry/rect.h"

class PanelManager;

// This class manages a group of panels that could be docked to the bottom of
// screen.
class DockedPanelCollection :
    public PanelCollection,
    public PanelMouseWatcherObserver,
    public DisplaySettingsProvider::DesktopBarObserver {
 public:
  typedef std::list<Panel*> Panels;

  explicit DockedPanelCollection(PanelManager* panel_manager);
  ~DockedPanelCollection() override;

  // PanelCollection OVERRIDES:
  void OnDisplayChanged() override;

  // Rearranges the positions of the panels in the collection
  // and reduces their width when there is not enough room.
  // This is called when the display space has been changed, i.e. working
  // area being changed or a panel being closed.
  void RefreshLayout() override;

  // Adds a panel to the collection. The panel may be a newly created panel or
  // one that is transitioning from another grouping of panels.
  void AddPanel(Panel* panel, PositioningMask positioning_mask) override;
  void RemovePanel(Panel* pane, RemovalReason reasonl) override;
  void CloseAll() override;
  void ResizePanelWindow(Panel* panel,
                         const gfx::Size& preferred_window_size) override;
  panel::Resizability GetPanelResizability(const Panel* panel) const override;
  void OnPanelResizedByMouse(Panel* panel,
                             const gfx::Rect& new_bounds) override;
  void OnPanelAttentionStateChanged(Panel* panel) override;
  void OnPanelTitlebarClicked(Panel* panel,
                              panel::ClickModifier modifier) override;
  void ActivatePanel(Panel* panel) override;
  void MinimizePanel(Panel* panel) override;
  void RestorePanel(Panel* panel) override;
  void OnMinimizeButtonClicked(Panel* panel,
                               panel::ClickModifier modifier) override;
  void OnRestoreButtonClicked(Panel* panel,
                              panel::ClickModifier modifier) override;
  bool CanShowMinimizeButton(const Panel* panel) const override;
  bool CanShowRestoreButton(const Panel* panel) const override;
  bool IsPanelMinimized(const Panel* panel) const override;
  bool UsesAlwaysOnTopPanels() const override;
  void SavePanelPlacement(Panel* panel) override;
  void RestorePanelToSavedPlacement() override;
  void DiscardSavedPanelPlacement() override;
  void UpdatePanelOnCollectionChange(Panel* panel) override;
  void OnPanelExpansionStateChanged(Panel* panel) override;
  void OnPanelActiveStateChanged(Panel* panel) override;
  gfx::Rect GetInitialPanelBounds(
      const gfx::Rect& requested_bounds) const override;

  // Returns true if we should bring up the titlebars, given the current mouse
  // point.
  bool ShouldBringUpTitlebars(int mouse_x, int mouse_y) const;

  // Brings up or down the titlebars for all minimized panels.
  void BringUpOrDownTitlebars(bool bring_up);

  // Returns the bottom position for the panel per its expansion state. If auto-
  // hide bottom bar is present, we want to move the minimized panel to the
  // bottom of the screen, not the bottom of the work area.
  int GetBottomPositionForExpansionState(
      Panel::ExpansionState expansion_state) const;

  // Returns panel width to be used, taking into account possible "squeezing"
  // due to lack of space in the collection.
  int WidthToDisplayPanelInCollection(bool is_for_active_panel,
                                      double squeeze_factor,
                                      int full_width) const;

  bool HasPanel(Panel* panel) const;

  // num_panels() and panels() only includes panels in the collection that
  // do NOT have a temporary layout.
  int num_panels() const { return panels_.size(); }
  const Panels& panels() const { return panels_; }
  Panel* last_panel() const { return panels_.empty() ? NULL : panels_.back(); }

  gfx::Rect work_area() const { return work_area_; }

  int StartingRightPosition() const;

#ifdef UNIT_TEST
  int minimized_panel_count() const {return minimized_panel_count_; }
#endif

 private:
  friend class DockedPanelDragHandler;

  enum TitlebarAction {
    NO_ACTION,
    BRING_UP,
    BRING_DOWN
  };

  struct PanelPlacement {
    Panel* panel;
    // Used to remember the panel to the left of |panel|, if any, for use when
    // restoring the position of |panel|.
    Panel* left_panel;

    PanelPlacement() : panel(NULL), left_panel(NULL) { }
  };

  // Overridden from PanelMouseWatcherObserver:
  void OnMouseMove(const gfx::Point& mouse_position) override;

  // Overridden from DisplaySettingsProvider::DesktopBarObserver:
  void OnAutoHidingDesktopBarVisibilityChanged(
      DisplaySettingsProvider::DesktopBarAlignment alignment,
      DisplaySettingsProvider::DesktopBarVisibility visibility) override;
  void OnAutoHidingDesktopBarThicknessChanged(
      DisplaySettingsProvider::DesktopBarAlignment alignment,
      int thickness) override;

  // Schedules a layout refresh with a short delay to avoid too much flicker.
  void ScheduleLayoutRefresh();

  // Keep track of the minimized panels to control mouse watching.
  void UpdateMinimizedPanelCount();

  // Minimizes or restores all panels in the collection.
  void MinimizeAll();
  void RestoreAll();

  // Makes sure the panel's bounds reflect its expansion state and the
  // panel is aligned at the bottom of the screen. Does not touch the x
  // coordinate.
  void AdjustPanelBoundsPerExpansionState(Panel* panel,
      gfx::Rect* panel_bounds);

  // Does the real job of bringing up or down the titlebars.
  void DoBringUpOrDownTitlebars(bool bring_up);
  // The callback for a delyed task, checks if it still need to perform
  // the delayed action.
  void DelayedBringUpOrDownTitlebarsCheck();

  // Compute default bounds for a panel of |full_size| that would be used
  // when adding the panel to the collection.
  gfx::Point GetDefaultPositionForPanel(const gfx::Size& full_size) const;

  int GetRightMostAvailablePosition() const;

  PanelManager* panel_manager_;  // Weak, owns us.

  // All panels in the collection must fit within this area.
  gfx::Rect work_area_;

  Panels panels_;

  int minimized_panel_count_;
  bool are_titlebars_up_;

  bool minimizing_all_;  // True while minimizing all panels.

  // Delayed transitions support. Sometimes transitions between minimized and
  // title-only states are delayed, for better usability with Taskbars/Docks.
  TitlebarAction delayed_titlebar_action_;

  // Used to save the placement information for a panel.
  PanelPlacement saved_panel_placement_;

  static const int kPanelsHorizontalSpacing = 4;

  // Owned by MessageLoop after posting.
  base::WeakPtrFactory<DockedPanelCollection> titlebar_action_factory_;

  // Owned by MessageLoop after posting.
  base::WeakPtrFactory<DockedPanelCollection> refresh_action_factory_;

  DISALLOW_COPY_AND_ASSIGN(DockedPanelCollection);
};

#endif  // CHROME_BROWSER_UI_PANELS_DOCKED_PANEL_COLLECTION_H_