Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(29)

Side by Side Diff: ash/system/status_area_widget_delegate.cc

Issue 2109193002: mash: Convert StatusAreaWidgetDelegate to wm common types (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: rebase Created 4 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « ash/system/status_area_widget_delegate.h ('k') | ash/wm/gestures/shelf_gesture_handler.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
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/common/ash_switches.h"
9 #include "ash/common/focus_cycler.h"
10 #include "ash/common/material_design/material_design_controller.h"
11 #include "ash/common/shelf/shelf_constants.h"
12 #include "ash/common/shelf/wm_shelf_util.h"
13 #include "ash/common/shell_window_ids.h"
14 #include "ash/common/system/tray/tray_constants.h"
15 #include "ash/common/wm_shell.h"
16 #include "base/strings/utf_string_conversions.h"
17 #include "ui/compositor/layer.h"
18 #include "ui/compositor/scoped_layer_animation_settings.h"
19 #include "ui/gfx/animation/tween.h"
20 #include "ui/gfx/canvas.h"
21 #include "ui/gfx/image/image.h"
22 #include "ui/views/accessible_pane_view.h"
23 #include "ui/views/border.h"
24 #include "ui/views/layout/grid_layout.h"
25 #include "ui/views/widget/widget.h"
26
27 namespace {
28
29 const int kAnimationDurationMs = 250;
30
31 class StatusAreaWidgetDelegateAnimationSettings
32 : public ui::ScopedLayerAnimationSettings {
33 public:
34 explicit StatusAreaWidgetDelegateAnimationSettings(ui::Layer* layer)
35 : ui::ScopedLayerAnimationSettings(layer->GetAnimator()) {
36 SetTransitionDuration(
37 base::TimeDelta::FromMilliseconds(kAnimationDurationMs));
38 SetPreemptionStrategy(ui::LayerAnimator::IMMEDIATELY_ANIMATE_TO_NEW_TARGET);
39 SetTweenType(gfx::Tween::EASE_IN_OUT);
40 }
41
42 ~StatusAreaWidgetDelegateAnimationSettings() override {}
43
44 private:
45 DISALLOW_COPY_AND_ASSIGN(StatusAreaWidgetDelegateAnimationSettings);
46 };
47
48 } // namespace
49
50 namespace ash {
51
52 StatusAreaWidgetDelegate::StatusAreaWidgetDelegate()
53 : focus_cycler_for_testing_(nullptr), alignment_(SHELF_ALIGNMENT_BOTTOM) {
54 // Allow the launcher to surrender the focus to another window upon
55 // navigation completion by the user.
56 set_allow_deactivate_on_esc(true);
57 SetPaintToLayer(true);
58 layer()->SetFillsBoundsOpaquely(false);
59 }
60
61 StatusAreaWidgetDelegate::~StatusAreaWidgetDelegate() {}
62
63 void StatusAreaWidgetDelegate::SetFocusCyclerForTesting(
64 const FocusCycler* focus_cycler) {
65 focus_cycler_for_testing_ = focus_cycler;
66 }
67
68 views::View* StatusAreaWidgetDelegate::GetDefaultFocusableChild() {
69 return child_at(0);
70 }
71
72 views::Widget* StatusAreaWidgetDelegate::GetWidget() {
73 return View::GetWidget();
74 }
75
76 const views::Widget* StatusAreaWidgetDelegate::GetWidget() const {
77 return View::GetWidget();
78 }
79
80 void StatusAreaWidgetDelegate::OnGestureEvent(ui::GestureEvent* event) {
81 aura::Window* target_window = static_cast<views::View*>(event->target())
82 ->GetWidget()
83 ->GetNativeWindow();
84 if (gesture_handler_.ProcessGestureEvent(*event, target_window))
85 event->StopPropagation();
86 else
87 views::AccessiblePaneView::OnGestureEvent(event);
88 }
89
90 bool StatusAreaWidgetDelegate::CanActivate() const {
91 // We don't want mouse clicks to activate us, but we need to allow
92 // activation when the user is using the keyboard (FocusCycler).
93 const FocusCycler* focus_cycler = focus_cycler_for_testing_
94 ? focus_cycler_for_testing_
95 : WmShell::Get()->focus_cycler();
96 return focus_cycler->widget_activating() == GetWidget();
97 }
98
99 void StatusAreaWidgetDelegate::DeleteDelegate() {}
100
101 void StatusAreaWidgetDelegate::AddTray(views::View* tray) {
102 SetLayoutManager(NULL); // Reset layout manager before adding a child.
103 AddChildView(tray);
104 // Set the layout manager with the new list of children.
105 UpdateLayout();
106 }
107
108 void StatusAreaWidgetDelegate::UpdateLayout() {
109 // Use a grid layout so that the trays can be centered in each cell, and
110 // so that the widget gets laid out correctly when tray sizes change.
111 views::GridLayout* layout = new views::GridLayout(this);
112 SetLayoutManager(layout);
113
114 // Update tray border based on layout.
115 bool is_child_on_edge = true;
116 for (int c = 0; c < child_count(); ++c) {
117 views::View* child = child_at(c);
118 if (!child->visible())
119 continue;
120 SetBorderOnChild(child, is_child_on_edge);
121 is_child_on_edge = false;
122 }
123
124 views::ColumnSet* columns = layout->AddColumnSet(0);
125
126 if (IsHorizontalAlignment(alignment_)) {
127 bool is_first_visible_child = true;
128 for (int c = child_count() - 1; c >= 0; --c) {
129 views::View* child = child_at(c);
130 if (!child->visible())
131 continue;
132 if (!is_first_visible_child)
133 columns->AddPaddingColumn(0, GetTrayConstant(TRAY_SPACING));
134 is_first_visible_child = false;
135 columns->AddColumn(views::GridLayout::CENTER, views::GridLayout::FILL,
136 0, /* resize percent */
137 views::GridLayout::USE_PREF, 0, 0);
138 }
139 layout->StartRow(0, 0);
140 for (int c = child_count() - 1; c >= 0; --c) {
141 views::View* child = child_at(c);
142 if (child->visible())
143 layout->AddView(child);
144 }
145 } else {
146 columns->AddColumn(views::GridLayout::FILL, views::GridLayout::CENTER,
147 0, /* resize percent */
148 views::GridLayout::USE_PREF, 0, 0);
149 bool is_first_visible_child = true;
150 for (int c = child_count() - 1; c >= 0; --c) {
151 views::View* child = child_at(c);
152 if (!child->visible())
153 continue;
154 if (!is_first_visible_child)
155 layout->AddPaddingRow(0, GetTrayConstant(TRAY_SPACING));
156 is_first_visible_child = false;
157 layout->StartRow(0, 0);
158 layout->AddView(child);
159 }
160 }
161
162 layer()->GetAnimator()->StopAnimating();
163 StatusAreaWidgetDelegateAnimationSettings settings(layer());
164
165 Layout();
166 UpdateWidgetSize();
167 }
168
169 void StatusAreaWidgetDelegate::ChildPreferredSizeChanged(View* child) {
170 // Need to resize the window when trays or items are added/removed.
171 StatusAreaWidgetDelegateAnimationSettings settings(layer());
172 UpdateWidgetSize();
173 }
174
175 void StatusAreaWidgetDelegate::ChildVisibilityChanged(View* child) {
176 UpdateLayout();
177 }
178
179 void StatusAreaWidgetDelegate::UpdateWidgetSize() {
180 if (GetWidget())
181 GetWidget()->SetSize(GetPreferredSize());
182 }
183
184 void StatusAreaWidgetDelegate::SetBorderOnChild(views::View* child,
185 bool extend_border_to_edge) {
186 const int shelf_size = GetShelfConstant(SHELF_SIZE);
187 const int item_height = GetTrayConstant(TRAY_ITEM_HEIGHT_LEGACY);
188 int top_edge, left_edge, bottom_edge, right_edge;
189
190 // Tray views are laid out right-to-left or bottom-to-top.
191 if (MaterialDesignController::IsShelfMaterial()) {
192 const bool horizontal_alignment = IsHorizontalAlignment(alignment_);
193 const int padding = (shelf_size - item_height) / 2;
194 const int extended_padding =
195 GetTrayConstant(TRAY_PADDING_FROM_EDGE_OF_SHELF);
196
197 top_edge = horizontal_alignment ? padding : 0;
198 left_edge = horizontal_alignment ? 0 : padding;
199 bottom_edge = horizontal_alignment
200 ? padding
201 : (extend_border_to_edge ? extended_padding : 0);
202 right_edge = horizontal_alignment
203 ? (extend_border_to_edge ? extended_padding : 0)
204 : padding;
205 } else {
206 bool on_edge = (child == child_at(0));
207 if (IsHorizontalAlignment(alignment_)) {
208 top_edge = kShelfItemInset;
209 left_edge = 0;
210 bottom_edge = shelf_size - kShelfItemInset - item_height;
211 right_edge =
212 on_edge ? GetTrayConstant(TRAY_PADDING_FROM_EDGE_OF_SHELF) : 0;
213 } else if (alignment_ == SHELF_ALIGNMENT_LEFT) {
214 top_edge = 0;
215 left_edge = shelf_size - kShelfItemInset - item_height;
216 bottom_edge =
217 on_edge ? GetTrayConstant(TRAY_PADDING_FROM_EDGE_OF_SHELF) : 0;
218 right_edge = kShelfItemInset;
219 } else { // SHELF_ALIGNMENT_RIGHT
220 top_edge = 0;
221 left_edge = kShelfItemInset;
222 bottom_edge =
223 on_edge ? GetTrayConstant(TRAY_PADDING_FROM_EDGE_OF_SHELF) : 0;
224 right_edge = shelf_size - kShelfItemInset - item_height;
225 }
226 }
227 child->SetBorder(views::Border::CreateEmptyBorder(top_edge, left_edge,
228 bottom_edge, right_edge));
229 }
230
231 } // namespace ash
OLDNEW
« no previous file with comments | « ash/system/status_area_widget_delegate.h ('k') | ash/wm/gestures/shelf_gesture_handler.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698