summaryrefslogtreecommitdiffstats
path: root/chrome/browser/chromeos/status/status_area_view.cc
blob: 74c4791ac3f1ec92462a32b787d7bfa3c86d723f (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
// 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.

#include "chrome/browser/chromeos/status/status_area_view.h"

#include <algorithm>

#include "base/bind.h"
#include "base/command_line.h"
#include "base/message_loop.h"
#include "chrome/browser/chromeos/view_ids.h"
#include "ui/gfx/canvas.h"
#include "ui/views/border.h"

#if defined(USE_ASH)
#include "ash/focus_cycler.h"
#include "ash/shell.h"
#endif

// Number of pixels to separate each icon.
const int kSeparation = 0;

StatusAreaView::StatusAreaView()
    : need_return_focus_(false),
      skip_next_focus_return_(true) {
  set_id(VIEW_ID_STATUS_AREA);
}

StatusAreaView::~StatusAreaView() {
}

void StatusAreaView::AddButton(StatusAreaButton* button, ButtonBorder border) {
  buttons_.push_back(button);
  if (border == HAS_BORDER)
    button->set_border(views::Border::CreateEmptyBorder(0, 1, 0, 0));
  AddChildView(button);
  UpdateButtonVisibility();
}

void StatusAreaView::RemoveButton(StatusAreaButton* button) {
  std::list<StatusAreaButton*>::iterator iter =
      std::find(buttons_.begin(), buttons_.end(), button);
  if (iter != buttons_.end()) {
    RemoveChildView(*iter);
    buttons_.erase(iter);
  }
  UpdateButtonVisibility();
}

// views::View* overrides.

gfx::Size StatusAreaView::GetPreferredSize() {
  int result_w = 0;
  int result_h = 0;

  for (int i = 0; i < child_count(); i++) {
    views::View* cur = child_at(i);
    gfx::Size cur_size = cur->GetPreferredSize();
    if (cur->visible() && !cur_size.IsEmpty()) {
      if (result_w == 0)
        result_w = kSeparation;

      // Add each width.
      result_w += cur_size.width() + kSeparation;
      // Use max height.
      result_h = std::max(result_h, cur_size.height());
    }
  }
  return gfx::Size(result_w, result_h);
}

void StatusAreaView::Layout() {
  int cur_x = kSeparation;
  for (int i = 0; i < child_count(); i++) {
    views::View* cur = child_at(i);
    gfx::Size cur_size = cur->GetPreferredSize();
    if (cur->visible() && !cur_size.IsEmpty()) {
      int cur_y = (height() - cur_size.height()) / 2;

      // Handle odd number of pixels.
      cur_y += (height() - cur_size.height()) % 2;

      // Put next in row horizontally, and center vertically.
      cur->SetBounds(cur_x, cur_y, cur_size.width(), cur_size.height());
      cur_x += cur_size.width() + kSeparation;
    }
  }
}

void StatusAreaView::PreferredSizeChanged() {
  if (GetWidget())
    GetWidget()->SetSize(GetPreferredSize());
  views::AccessiblePaneView::PreferredSizeChanged();
}

void StatusAreaView::ChildPreferredSizeChanged(View* child) {
  // When something like the clock menu button's size changes, we need to
  // relayout. Also mark that this view's size has changed. This will let
  // BrowserView know to relayout, which will reset the bounds of this view.
  Layout();
  PreferredSizeChanged();
}

bool StatusAreaView::CanActivate() const {
#if defined(USE_ASH)
  // We don't want mouse clicks to activate us, but we need to allow
  // activation when the user is using the keyboard, such as by the FocusCycler
  // or on the Login screen.
  ash::internal::FocusCycler* focus_cycler =
      ash::Shell::GetInstance()->focus_cycler();
  return focus_cycler->widget_activating() == GetWidget() ||
      need_return_focus_;
#else
  return false;
#endif
}

void StatusAreaView::DeleteDelegate() {
#if defined(USE_ASH)
  // If this is used as the content-view of the widget, then do nothing, since
  // deleting the widget will end up deleting this. But if this is used only as
  // the widget-delegate, then delete this now.
  if (!GetWidget()) {
    delete this;
    return;
  }
#endif
  WidgetDelegate::DeleteDelegate();
}

views::Widget* StatusAreaView::GetWidget() {
  return View::GetWidget();
}

const views::Widget* StatusAreaView::GetWidget() const {
  return View::GetWidget();
}

void StatusAreaView::MakeButtonsActive(bool active) {
  for (std::list<StatusAreaButton*>::iterator iter = buttons_.begin();
       iter != buttons_.end(); ++iter) {
    (*iter)->SetMenuActive(active);
  }
}

void StatusAreaView::UpdateButtonVisibility() {
  Layout();
  PreferredSizeChanged();
}

void StatusAreaView::UpdateButtonTextStyle() {
  for (std::list<StatusAreaButton*>::const_iterator it = buttons_.begin();
       it != buttons_.end(); ++it) {
    StatusAreaButton* button = *it;
    button->UpdateTextStyle();
  }
}

void StatusAreaView::TakeFocus(
    bool reverse,
    const ReturnFocusCallback& return_focus_cb) {
  SetPaneFocus(reverse ? GetLastFocusableChild() : GetFirstFocusableChild());
  need_return_focus_ = true;
  return_focus_cb_ = return_focus_cb;
  GetWidget()->AddObserver(this);
}

void StatusAreaView::ReturnFocus(bool reverse) {
  ClearFocus();
  return_focus_cb_.Run(reverse);
}

void StatusAreaView::ClearFocus() {
  GetWidget()->RemoveObserver(this);
  RemovePaneFocus();
  focus_manager_->ClearFocus();
  need_return_focus_ = false;
}

void StatusAreaView::OnDidChangeFocus(views::View* focused_before,
                                      views::View* focused_now) {
  views::AccessiblePaneView::OnDidChangeFocus(focused_before, focused_now);
  if (need_return_focus_ && !skip_next_focus_return_) {
    const views::View* first = GetFirstFocusableChild();
    const views::View* last = GetLastFocusableChild();
    const bool first_to_last = (focused_before == first && focused_now == last);
    const bool last_to_first = (focused_now == first && focused_before == last);

    if (first_to_last || last_to_first)
      ReturnFocus(first_to_last);
  }
  skip_next_focus_return_ = false;
}

void StatusAreaView::OnWidgetActivationChanged(views::Widget* widget,
                                               bool active) {
  if (!active)
    ClearFocus();
}

bool StatusAreaView::AcceleratorPressed(const ui::Accelerator& accelerator) {
  if (need_return_focus_ && accelerator.key_code() == ui::VKEY_ESCAPE) {
    // Override Escape handling to return focus back.
    ReturnFocus(false);
    return true;
  } else if (accelerator.key_code() == ui::VKEY_HOME ||
             accelerator.key_code() == ui::VKEY_END) {
    // Do not return focus if it wraps right after pressing Home/End.
    skip_next_focus_return_ = true;
  }
  return AccessiblePaneView::AcceleratorPressed(accelerator);
}