Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / ash / system / status_area_widget_delegate.cc
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.
4
5 #include "ash/system/status_area_widget_delegate.h"
6
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"
21
22 namespace ash {
23
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);
30 }
31
32 StatusAreaWidgetDelegate::~StatusAreaWidgetDelegate() {
33 }
34
35 void StatusAreaWidgetDelegate::SetFocusCyclerForTesting(
36     const FocusCycler* focus_cycler) {
37   focus_cycler_for_testing_ = focus_cycler;
38 }
39
40 views::View* StatusAreaWidgetDelegate::GetDefaultFocusableChild() {
41   return child_at(0);
42 }
43
44 views::Widget* StatusAreaWidgetDelegate::GetWidget() {
45   return View::GetWidget();
46 }
47
48 const views::Widget* StatusAreaWidgetDelegate::GetWidget() const {
49   return View::GetWidget();
50 }
51
52 void StatusAreaWidgetDelegate::OnGestureEvent(ui::GestureEvent* event) {
53   if (gesture_handler_.ProcessGestureEvent(*event))
54     event->StopPropagation();
55   else
56     views::AccessiblePaneView::OnGestureEvent(event);
57 }
58
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();
65 }
66
67 void StatusAreaWidgetDelegate::DeleteDelegate() {
68 }
69
70 void StatusAreaWidgetDelegate::AddTray(views::View* tray) {
71   SetLayoutManager(NULL);  // Reset layout manager before adding a child.
72   AddChildView(tray);
73   // Set the layout manager with the new list of children.
74   UpdateLayout();
75 }
76
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);
82
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())
90         continue;
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);
97     }
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);
103     }
104   } else {
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())
112         continue;
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);
118     }
119   }
120   Layout();
121   UpdateWidgetSize();
122 }
123
124 void StatusAreaWidgetDelegate::ChildPreferredSizeChanged(View* child) {
125   // Need to resize the window when trays or items are added/removed.
126   UpdateWidgetSize();
127 }
128
129 void StatusAreaWidgetDelegate::ChildVisibilityChanged(View* child) {
130   UpdateLayout();
131 }
132
133 void StatusAreaWidgetDelegate::UpdateWidgetSize() {
134   if (GetWidget())
135     GetWidget()->SetSize(GetPreferredSize());
136 }
137
138 }  // namespace ash