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"
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"
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() {
46 bool StatusAreaWidgetDelegate::AcceleratorPressed(
47 const ui::Accelerator
& accelerator
) {
48 if (accelerator
.key_code() == ui::VKEY_ESCAPE
) {
50 GetFocusManager()->ClearFocus();
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();
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.
85 // Set the layout manager with the new list of children.
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
) {
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
));
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
));
122 void StatusAreaWidgetDelegate::ChildPreferredSizeChanged(View
* child
) {
123 // Need to resize the window when trays or items are added/removed.
127 void StatusAreaWidgetDelegate::UpdateWidgetSize() {
129 GetWidget()->SetSize(GetPreferredSize());
132 } // namespace internal