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/ash_switches.h"
9 #include "ash/focus_cycler.h"
10 #include "ash/shell.h"
11 #include "ash/shell_window_ids.h"
12 #include "ash/system/tray/tray_constants.h"
13 #include "base/strings/utf_string_conversions.h"
14 #include "ui/aura/window_event_dispatcher.h"
15 #include "ui/base/resource/resource_bundle.h"
16 #include "ui/gfx/canvas.h"
17 #include "ui/gfx/image/image.h"
18 #include "ui/views/accessible_pane_view.h"
19 #include "ui/views/layout/grid_layout.h"
20 #include "ui/views/widget/widget.h"
24 StatusAreaWidgetDelegate::StatusAreaWidgetDelegate()
25 : focus_cycler_for_testing_(NULL),
26 alignment_(SHELF_ALIGNMENT_BOTTOM) {
27 // Allow the launcher to surrender the focus to another window upon
28 // navigation completion by the user.
29 set_allow_deactivate_on_esc(true);
32 StatusAreaWidgetDelegate::~StatusAreaWidgetDelegate() {
35 void StatusAreaWidgetDelegate::SetFocusCyclerForTesting(
36 const FocusCycler* focus_cycler) {
37 focus_cycler_for_testing_ = focus_cycler;
40 views::View* StatusAreaWidgetDelegate::GetDefaultFocusableChild() {
44 views::Widget* StatusAreaWidgetDelegate::GetWidget() {
45 return View::GetWidget();
48 const views::Widget* StatusAreaWidgetDelegate::GetWidget() const {
49 return View::GetWidget();
52 void StatusAreaWidgetDelegate::OnGestureEvent(ui::GestureEvent* event) {
53 if (gesture_handler_.ProcessGestureEvent(*event))
54 event->StopPropagation();
56 views::AccessiblePaneView::OnGestureEvent(event);
59 bool StatusAreaWidgetDelegate::CanActivate() const {
60 // We don't want mouse clicks to activate us, but we need to allow
61 // activation when the user is using the keyboard (FocusCycler).
62 const FocusCycler* focus_cycler = focus_cycler_for_testing_ ?
63 focus_cycler_for_testing_ : Shell::GetInstance()->focus_cycler();
64 return focus_cycler->widget_activating() == GetWidget();
67 void StatusAreaWidgetDelegate::DeleteDelegate() {
70 void StatusAreaWidgetDelegate::AddTray(views::View* tray) {
71 SetLayoutManager(NULL); // Reset layout manager before adding a child.
73 // Set the layout manager with the new list of children.
77 void StatusAreaWidgetDelegate::UpdateLayout() {
78 // Use a grid layout so that the trays can be centered in each cell, and
79 // so that the widget gets laid out correctly when tray sizes change.
80 views::GridLayout* layout = new views::GridLayout(this);
81 SetLayoutManager(layout);
83 views::ColumnSet* columns = layout->AddColumnSet(0);
84 if (alignment_ == SHELF_ALIGNMENT_BOTTOM ||
85 alignment_ == SHELF_ALIGNMENT_TOP) {
86 bool is_first_visible_child = true;
87 for (int c = 0; c < child_count(); ++c) {
88 views::View* child = child_at(c);
89 if (!child->visible())
91 if (!is_first_visible_child)
92 columns->AddPaddingColumn(0, kTraySpacing);
93 is_first_visible_child = false;
94 columns->AddColumn(views::GridLayout::CENTER, views::GridLayout::FILL,
95 0, /* resize percent */
96 views::GridLayout::USE_PREF, 0, 0);
98 layout->StartRow(0, 0);
99 for (int c = child_count() - 1; c >= 0; --c) {
100 views::View* child = child_at(c);
101 if (child->visible())
102 layout->AddView(child);
105 columns->AddColumn(views::GridLayout::FILL, views::GridLayout::CENTER,
106 0, /* resize percent */
107 views::GridLayout::USE_PREF, 0, 0);
108 bool is_first_visible_child = true;
109 for (int c = child_count() - 1; c >= 0; --c) {
110 views::View* child = child_at(c);
111 if (!child->visible())
113 if (!is_first_visible_child)
114 layout->AddPaddingRow(0, kTraySpacing);
115 is_first_visible_child = false;
116 layout->StartRow(0, 0);
117 layout->AddView(child);
124 void StatusAreaWidgetDelegate::ChildPreferredSizeChanged(View* child) {
125 // Need to resize the window when trays or items are added/removed.
129 void StatusAreaWidgetDelegate::ChildVisibilityChanged(View* child) {
133 void StatusAreaWidgetDelegate::UpdateWidgetSize() {
135 GetWidget()->SetSize(GetPreferredSize());