[content shell] implement testRunner.overridePreference
[chromium-blink-merge.git] / ash / system / status_area_widget_delegate.cc
blobf82ec13219132ab3cc1c318dbac6f091dc91c418
1 // Copyright (c) 2012 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.
5 #include "ash/system/status_area_widget_delegate.h"
7 #include "ash/ash_export.h"
8 #include "ash/focus_cycler.h"
9 #include "ash/shell.h"
10 #include "ash/shell_window_ids.h"
11 #include "base/utf_string_conversions.h"
12 #include "ui/aura/root_window.h"
13 #include "ui/base/resource/resource_bundle.h"
14 #include "ui/gfx/canvas.h"
15 #include "ui/gfx/image/image.h"
16 #include "ui/views/accessible_pane_view.h"
17 #include "ui/views/layout/grid_layout.h"
18 #include "ui/views/widget/widget.h"
20 namespace {
22 int kTraySpacing = 8;
24 } // namespace
26 namespace ash {
27 namespace internal {
29 StatusAreaWidgetDelegate::StatusAreaWidgetDelegate()
30 : focus_cycler_for_testing_(NULL),
31 alignment_(SHELF_ALIGNMENT_BOTTOM) {
34 StatusAreaWidgetDelegate::~StatusAreaWidgetDelegate() {
37 void StatusAreaWidgetDelegate::SetFocusCyclerForTesting(
38 const FocusCycler* focus_cycler) {
39 focus_cycler_for_testing_ = focus_cycler;
42 views::View* StatusAreaWidgetDelegate::GetDefaultFocusableChild() {
43 return child_at(0);
46 bool StatusAreaWidgetDelegate::AcceleratorPressed(
47 const ui::Accelerator& accelerator) {
48 if (accelerator.key_code() == ui::VKEY_ESCAPE) {
49 RemovePaneFocus();
50 GetFocusManager()->ClearFocus();
51 return true;
53 return false;
56 views::Widget* StatusAreaWidgetDelegate::GetWidget() {
57 return View::GetWidget();
60 const views::Widget* StatusAreaWidgetDelegate::GetWidget() const {
61 return View::GetWidget();
64 void StatusAreaWidgetDelegate::OnGestureEvent(ui::GestureEvent* event) {
65 if (gesture_handler_.ProcessGestureEvent(*event))
66 event->StopPropagation();
67 else
68 views::AccessiblePaneView::OnGestureEvent(event);
71 bool StatusAreaWidgetDelegate::CanActivate() const {
72 // We don't want mouse clicks to activate us, but we need to allow
73 // activation when the user is using the keyboard (FocusCycler).
74 const FocusCycler* focus_cycler = focus_cycler_for_testing_ ?
75 focus_cycler_for_testing_ : Shell::GetInstance()->focus_cycler();
76 return focus_cycler->widget_activating() == GetWidget();
79 void StatusAreaWidgetDelegate::DeleteDelegate() {
82 void StatusAreaWidgetDelegate::AddTray(views::View* tray) {
83 SetLayoutManager(NULL); // Reset layout manager before adding a child.
84 AddChildView(tray);
85 // Set the layout manager with the new list of children.
86 UpdateLayout();
89 void StatusAreaWidgetDelegate::UpdateLayout() {
90 // Use a grid layout so that the trays can be centered in each cell, and
91 // so that the widget gets laid out correctly when tray sizes change.
92 views::GridLayout* layout = new views::GridLayout(this);
93 SetLayoutManager(layout);
95 views::ColumnSet* columns = layout->AddColumnSet(0);
96 if (alignment_ == SHELF_ALIGNMENT_BOTTOM) {
97 for (int c = 0; c < child_count(); ++c) {
98 if (c != 0)
99 columns->AddPaddingColumn(0, kTraySpacing);
100 columns->AddColumn(views::GridLayout::CENTER, views::GridLayout::CENTER,
101 0, /* resize percent */
102 views::GridLayout::USE_PREF, 0, 0);
104 layout->StartRow(0, 0);
105 for (int c = child_count() - 1; c >= 0; --c)
106 layout->AddView(child_at(c));
107 } else {
108 columns->AddColumn(views::GridLayout::CENTER, views::GridLayout::CENTER,
109 0, /* resize percent */
110 views::GridLayout::USE_PREF, 0, 0);
111 for (int c = child_count() - 1; c >= 0; --c) {
112 if (c != child_count() - 1)
113 layout->AddPaddingRow(0, kTraySpacing);
114 layout->StartRow(0, 0);
115 layout->AddView(child_at(c));
118 Layout();
119 UpdateWidgetSize();
122 void StatusAreaWidgetDelegate::ChildPreferredSizeChanged(View* child) {
123 // Need to resize the window when trays or items are added/removed.
124 UpdateWidgetSize();
127 void StatusAreaWidgetDelegate::UpdateWidgetSize() {
128 if (GetWidget())
129 GetWidget()->SetSize(GetPreferredSize());
132 } // namespace internal
133 } // namespace ash