summaryrefslogtreecommitdiffstats
path: root/ash/wm/shelf_layout_manager.h
blob: fe38d57813af66b8505f8f065a39968d6761f0cd (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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
// 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 ASH_WM_SHELF_LAYOUT_MANAGER_H_
#define ASH_WM_SHELF_LAYOUT_MANAGER_H_
#pragma once

#include "ash/ash_export.h"
#include "ash/launcher/launcher.h"
#include "ash/wm/shelf_auto_hide_behavior.h"
#include "base/basictypes.h"
#include "base/compiler_specific.h"
#include "base/timer.h"
#include "ui/aura/layout_manager.h"
#include "ui/gfx/insets.h"
#include "ui/gfx/rect.h"

namespace views {
class Widget;
}

namespace ash {
namespace internal {

class ShelfLayoutManagerTest;
class WorkspaceManager;

// ShelfLayoutManager is the layout manager responsible for the launcher and
// status widgets. The launcher is given the total available width and told the
// width of the status area. This allows the launcher to draw the background and
// layout to the status area.
// To respond to bounds changes in the status area StatusAreaLayoutManager works
// closely with ShelfLayoutManager.
class ASH_EXPORT ShelfLayoutManager : public aura::LayoutManager {
 public:
  enum VisibilityState {
    // Completely visible.
    VISIBLE,

    // A couple of pixels are reserved at the bottom for the shelf.
    AUTO_HIDE,

    // Nothing is shown. Used for fullscreen windows.
    HIDDEN,
  };

  enum AutoHideState {
    AUTO_HIDE_SHOWN,
    AUTO_HIDE_HIDDEN,
  };

  // We reserve a small area at the bottom of the workspace area to ensure that
  // the bottom-of-window resize handle can be hit.
  // TODO(jamescook): Some day we may want the workspace area to be an even
  // multiple of the size of the grid (currently 8 pixels), which will require
  // removing this and finding a way for hover and click events to pass through
  // the invisible parts of the launcher.
  static const int kWorkspaceAreaBottomInset;

  // Height of the shelf when auto-hidden.
  static const int kAutoHideHeight;

  explicit ShelfLayoutManager(views::Widget* status);
  virtual ~ShelfLayoutManager();

  // Sets the ShelfAutoHideBehavior. See enum description for details.
  void SetAutoHideBehavior(ShelfAutoHideBehavior behavior);
  ShelfAutoHideBehavior auto_hide_behavior() const {
    return auto_hide_behavior_;
  }

  void set_workspace_manager(WorkspaceManager* manager) {
    workspace_manager_ = manager;
  }

  views::Widget* launcher_widget() {
    return launcher_ ? launcher_->widget() : NULL;
  }
  const views::Widget* launcher_widget() const {
    return launcher_ ? launcher_->widget() : NULL;
  }
  views::Widget* status() { return status_; }

  bool in_layout() const { return in_layout_; }

  // See description above field.
  int shelf_height() const { return shelf_height_; }

  // Returns whether the shelf and its contents (launcher, status) are visible
  // on the screen.
  bool IsVisible() const;

  // Returns the bounds the specified window should be when maximized.
  gfx::Rect GetMaximizedWindowBounds(aura::Window* window) const;
  gfx::Rect GetUnmaximizedWorkAreaBounds(aura::Window* window) const;

  // The launcher is typically created after the layout manager.
  void SetLauncher(Launcher* launcher);

  // Stops any animations and sets the bounds of the launcher and status
  // widgets.
  void LayoutShelf();

  // Updates the visibility state.
  void UpdateVisibilityState();

  // Invoked by the shelf/launcher when the auto-hide state may have changed.
  void UpdateAutoHideState();

  VisibilityState visibility_state() const { return state_.visibility_state; }
  AutoHideState auto_hide_state() const { return state_.auto_hide_state; }

  // Sets whether any windows overlap the shelf. If a window overlaps the shelf
  // the shelf renders slightly differently.
  void SetWindowOverlapsShelf(bool value);

  // Overridden from aura::LayoutManager:
  virtual void OnWindowResized() OVERRIDE;
  virtual void OnWindowAddedToLayout(aura::Window* child) OVERRIDE;
  virtual void OnWillRemoveWindowFromLayout(aura::Window* child) OVERRIDE;
  virtual void OnChildWindowVisibilityChanged(aura::Window* child,
                                              bool visible) OVERRIDE;
  virtual void SetChildBounds(aura::Window* child,
                              const gfx::Rect& requested_bounds) OVERRIDE;

 private:
  class AutoHideEventFilter;
  friend class ShelfLayoutManagerTest;

  struct TargetBounds {
    TargetBounds() : opacity(0.0f) {}

    float opacity;
    gfx::Rect launcher_bounds;
    gfx::Rect status_bounds;
    gfx::Insets work_area_insets;
  };

  struct State {
    State() : visibility_state(VISIBLE), auto_hide_state(AUTO_HIDE_HIDDEN) {}

    // Returns true if the two states are considered equal. As
    // |auto_hide_state| only matters if |visibility_state| is |AUTO_HIDE|,
    // Equals() ignores the |auto_hide_state| as appropriate.
    bool Equals(const State& other) const {
      return other.visibility_state == visibility_state &&
          (visibility_state != AUTO_HIDE ||
           other.auto_hide_state == auto_hide_state);
    }

    VisibilityState visibility_state;
    AutoHideState auto_hide_state;
  };

  // Sets the visibility of the shelf to |state|.
  void SetState(VisibilityState visibility_state);

  // Stops any animations.
  void StopAnimating();

  // Calculates the target bounds assuming visibility of |visible|.
  void CalculateTargetBounds(const State& state,
                             TargetBounds* target_bounds) const;

  // Updates the background of the shelf.
  void UpdateShelfBackground(BackgroundAnimator::ChangeType type);

  // Returns whether the launcher should draw a background.
  bool GetLauncherPaintsBackground() const;

  // Updates the auto hide state immediately.
  void UpdateAutoHideStateNow();

  // Returns the AutoHideState. This value is determined from the launcher and
  // tray.
  AutoHideState CalculateAutoHideState(VisibilityState visibility_state) const;

  // Updates the hit test bounds override for launcher and status area.
  void UpdateHitTestBounds();

  // True when inside LayoutShelf method. Used to prevent calling LayoutShelf
  // again from SetChildBounds().
  bool in_layout_;

  // See description above setter.
  ShelfAutoHideBehavior auto_hide_behavior_;

  // Current state.
  State state_;

  // Height of the shelf (max of launcher and status).
  int shelf_height_;

  Launcher* launcher_;
  views::Widget* status_;

  WorkspaceManager* workspace_manager_;

  // Do any windows overlap the shelf? This is maintained by WorkspaceManager.
  bool window_overlaps_shelf_;

  base::OneShotTimer<ShelfLayoutManager> auto_hide_timer_;

  // EventFilter used to detect when user moves the mouse over the launcher to
  // trigger showing the launcher.
  scoped_ptr<AutoHideEventFilter> event_filter_;

  DISALLOW_COPY_AND_ASSIGN(ShelfLayoutManager);
};

}  // namespace internal
}  // namespace ash

#endif  // ASH_WM_SHELF_LAYOUT_MANAGER_H_