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

Side by Side Diff: ash/shelf/shelf_widget.cc

Issue 12313118: Refactor: Shelf Widget (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Created 7 years, 9 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
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/shelf/shelf_widget.h"
6
7 #include "ash/focus_cycler.h"
8 #include "ash/launcher/launcher_delegate.h"
9 #include "ash/launcher/launcher_model.h"
10 #include "ash/launcher/launcher_navigator.h"
11 #include "ash/launcher/launcher_view.h"
12 #include "ash/root_window_controller.h"
13 #include "ash/shelf/shelf_layout_manager.h"
14 #include "ash/shelf/shelf_widget.h"
15 #include "ash/shell.h"
16 #include "ash/shell_delegate.h"
17 #include "ash/shell_window_ids.h"
18 #include "ash/wm/property_util.h"
19 #include "ash/wm/status_area_layout_manager.h"
20 #include "ash/wm/window_properties.h"
21 #include "ash/wm/workspace_controller.h"
22 #include "grit/ash_resources.h"
23 #include "ui/aura/client/activation_client.h"
24 #include "ui/aura/root_window.h"
25 #include "ui/aura/window.h"
26 #include "ui/aura/window_observer.h"
27 #include "ui/base/resource/resource_bundle.h"
28 #include "ui/compositor/layer.h"
29 #include "ui/gfx/canvas.h"
30 #include "ui/gfx/image/image.h"
31 #include "ui/gfx/image/image_skia_operations.h"
32 #include "ui/gfx/skbitmap_operations.h"
33 #include "ui/views/accessible_pane_view.h"
34 #include "ui/views/widget/widget.h"
35 #include "ui/views/widget/widget_delegate.h"
36
37 namespace {
38 // Size of black border at bottom (or side) of launcher.
39 const int kNumBlackPixels = 3;
40 // Alpha to paint dimming image with.
41 const int kDimAlpha = 96;
42 }
43
44 namespace ash {
45
46 // The contents view of the Shelf. This view contains LauncherView and
47 // sizes it to the width of the shelf minus the size of the status area.
48 class ShelfWidget::DelegateView : public views::WidgetDelegate,
49 public views::AccessiblePaneView,
50 public internal::BackgroundAnimatorDelegate {
51 public:
52 explicit DelegateView(ShelfWidget* shelf);
53 virtual ~DelegateView();
54
55 void set_focus_cycler(internal::FocusCycler* focus_cycler) {
56 focus_cycler_ = focus_cycler;
57 }
58 internal::FocusCycler* focus_cycler() {
59 return focus_cycler_;
60 }
61
62 void SetDimmed(bool dimmed) {
Mr4D (OOO till 08-26) 2013/03/02 02:02:12 Comment? What is dimmed exactly?
Harry McCleave 2013/03/04 02:47:41 Done.
63 if (dimmed_ != dimmed) {
64 dimmed_ = dimmed;
65 SchedulePaint();
66 }
67 }
68 bool GetDimmed() const { return dimmed_; }
Mr4D (OOO till 08-26) 2013/03/02 02:02:12 newline
Harry McCleave 2013/03/04 02:47:41 Done.
69 // views::View overrides
70 virtual void OnPaintBackground(gfx::Canvas* canvas) OVERRIDE;
71
72 // views::WidgetDelegateView overrides:
73 virtual views::Widget* GetWidget() OVERRIDE {
74 return View::GetWidget();
75 }
76 virtual const views::Widget* GetWidget() const OVERRIDE {
Mr4D (OOO till 08-26) 2013/03/02 02:02:12 virtual + OVERRIDE + body might make clang barf.
Harry McCleave 2013/03/04 02:47:41 Done.
77 return View::GetWidget();
78 }
79 virtual bool CanActivate() const OVERRIDE {
80 // Allow to activate as fallback.
81 if (shelf_->activating_as_fallback_)
82 return true;
83 // Allow to activate from the focus cycler.
84 if (focus_cycler_ && focus_cycler_->widget_activating() == GetWidget())
85 return true;
86 // Disallow activating in other cases, especially when using mouse.
87 return false;
88 }
89
90 virtual void Layout() OVERRIDE {
91 for(int i = 0; i < this->child_count(); ++i) {
92 if (shelf_->shelf_layout_manager()->IsHorizontalAlignment())
93 child_at(i)->SetBounds(child_at(i)->x(),child_at(i)->y(),
94 child_at(i)->width(),height());
95 else
96 child_at(i)->SetBounds(child_at(i)->x(),child_at(i)->y(),
97 width(),child_at(i)->height());
98 }
99 }
100
101 // BackgroundAnimatorDelegate overrides:
102 virtual void UpdateBackground(int alpha) OVERRIDE;
103
104 private:
105 ShelfWidget* shelf_;
Mr4D (OOO till 08-26) 2013/03/02 02:02:12 Again - comments?
Harry McCleave 2013/03/04 02:47:41 Done.
106 internal::FocusCycler* focus_cycler_;
107 int alpha_;
108 bool dimmed_;
109
110 DISALLOW_COPY_AND_ASSIGN(DelegateView);
111 };
112
113 ShelfWidget::DelegateView::DelegateView(ShelfWidget* shelf)
114 : shelf_(shelf),
115 focus_cycler_(NULL),
116 alpha_(0),
117 dimmed_(false) {
118 }
119
120 ShelfWidget::DelegateView::~DelegateView() {
121 }
122
123 void ShelfWidget::DelegateView::OnPaintBackground(gfx::Canvas* canvas) {
124 ResourceBundle& rb = ResourceBundle::GetSharedInstance();
125 gfx::ImageSkia launcher_background =
126 *rb.GetImageSkiaNamed(IDR_AURA_LAUNCHER_BACKGROUND);
127 if (SHELF_ALIGNMENT_BOTTOM != shelf_->GetAlignment())
128 launcher_background = gfx::ImageSkiaOperations::CreateRotatedImage(
129 launcher_background,
130 shelf_->shelf_layout_manager()->SelectValueForShelfAlignment(
131 SkBitmapOperations::ROTATION_90_CW,
132 SkBitmapOperations::ROTATION_90_CW,
133 SkBitmapOperations::ROTATION_270_CW,
134 SkBitmapOperations::ROTATION_180_CW));
135
136 gfx::Rect black_rect =
137 shelf_->shelf_layout_manager()->SelectValueForShelfAlignment(
138 gfx::Rect(0, height() - kNumBlackPixels, width(), kNumBlackPixels),
139 gfx::Rect(0, 0, kNumBlackPixels, height()),
140 gfx::Rect(width() - kNumBlackPixels, 0, kNumBlackPixels, height()),
141 gfx::Rect(0, 0, width(), kNumBlackPixels));
142
143 SkPaint paint;
144 paint.setAlpha(alpha_);
145 canvas->DrawImageInt(
146 launcher_background,
147 0, 0, launcher_background.width(), launcher_background.height(),
148 0, 0, width(), height(),
149 false,
150 paint);
151 canvas->FillRect(black_rect, SK_ColorBLACK);
152
153 if (dimmed_) {
154 gfx::ImageSkia background_image =
155 *rb.GetImageSkiaNamed(IDR_AURA_LAUNCHER_DIMMING);
156 if (SHELF_ALIGNMENT_BOTTOM != shelf_->GetAlignment())
157 background_image = gfx::ImageSkiaOperations::CreateRotatedImage(
158 background_image,
159 shelf_->shelf_layout_manager()->SelectValueForShelfAlignment(
160 SkBitmapOperations::ROTATION_90_CW,
161 SkBitmapOperations::ROTATION_90_CW,
162 SkBitmapOperations::ROTATION_270_CW,
163 SkBitmapOperations::ROTATION_180_CW));
164
165 SkPaint paint;
166 paint.setAlpha(kDimAlpha);
167 canvas->DrawImageInt(
168 background_image,
169 0, 0, background_image.width(), background_image.height(),
170 0, 0, width(), height(),
171 false,
172 paint);
173 }
174 }
175
176 void ShelfWidget::DelegateView::UpdateBackground(int alpha) {
177 alpha_ = alpha;
178 SchedulePaint();
179 }
180
181 ShelfWidget::ShelfWidget(
182 aura::Window* shelf_container,
183 aura::Window* status_container,
184 internal::WorkspaceController* workspace_controller) :
185 launcher_(NULL),
186 delegate_view_(new DelegateView(this)),
187 background_animator_(delegate_view_, 0, kLauncherBackgroundAlpha),
188 activating_as_fallback_(false),
189 window_container_(shelf_container) {
190 views::Widget::InitParams params(
191 views::Widget::InitParams::TYPE_WINDOW_FRAMELESS);
192 params.transparent = true;
193 params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET;
194 params.parent = shelf_container;
195 params.delegate = delegate_view_;
196 Init(params);
197
198 // The shelf should not take focus when initially shown.
199 set_focus_on_creation(false);
200 SetContentsView(delegate_view_);
201
202 status_area_widget_ = new internal::StatusAreaWidget(status_container);
203 status_area_widget_->CreateTrayViews();
204 if (Shell::GetInstance()->delegate()->IsSessionStarted())
205 status_area_widget_->Show();
206 Shell::GetInstance()->focus_cycler()->AddWidget(status_area_widget_);
207
208 shelf_layout_manager_ = new internal::ShelfLayoutManager(this);
209 shelf_container->SetLayoutManager(shelf_layout_manager_);
210 shelf_layout_manager_->set_workspace_controller(workspace_controller);
211 workspace_controller->SetShelf(shelf_layout_manager_);
212
213 status_container->SetLayoutManager(
214 new internal::StatusAreaLayoutManager(this));
215
216 views::Widget::AddObserver(this);
217 }
218
219 ShelfWidget::~ShelfWidget() {
220 RemoveObserver(this);
221 }
222
223 void ShelfWidget::SetPaintsBackground(
224 bool value,
225 internal::BackgroundAnimator::ChangeType change_type) {
226 background_animator_.SetPaintsBackground(value, change_type);
227 }
228
229 ShelfAlignment ShelfWidget::GetAlignment() const {
230 return shelf_layout_manager_->GetAlignment();
231 }
232
233 void ShelfWidget::SetAlignment(ShelfAlignment alignment) {
234 shelf_layout_manager_->SetAlignment(alignment);
235 shelf_layout_manager_->LayoutShelf();
236 }
237
238 void ShelfWidget::SetDimsShelf(bool dimming) {
239 delegate_view_->SetDimmed(dimming);
240 }
241
242 bool ShelfWidget::GetDimsShelf() const {
243 return delegate_view_->GetDimmed();
244 }
245
246 void ShelfWidget::CreateLauncher() {
247 if (!launcher_.get()) {
248 // This needs to be called before launcher_model().
249 Shell::GetInstance()->GetLauncherDelegate();
250 launcher_.reset(new Launcher(Shell::GetInstance()->launcher_model(),
251 Shell::GetInstance()->GetLauncherDelegate(),
252 this));
253
254 SetFocusCycler(Shell::GetInstance()->focus_cycler());
255 ShellDelegate* delegate = Shell::GetInstance()->delegate();
256
257 // Inform the root window controller (for PanelLayoutManager).
258 internal::RootWindowController::ForWindow(window_container_)->
259 OnLauncherCreated();
260
261 if (delegate)
262 launcher_->SetVisible(delegate->IsSessionStarted());
263
264 Show();
265 }
266 }
267
268 bool ShelfWidget::LauncherVisible() const {
269 return launcher_.get() && launcher_->visible();
270 }
271
272 void ShelfWidget::SetLauncherVisibility(bool visible) {
273 if (launcher_.get())
274 launcher_->SetVisible(visible);
275 }
276
277 void ShelfWidget::SetFocusCycler(internal::FocusCycler* focus_cycler) {
278 delegate_view_->set_focus_cycler(focus_cycler);
279 if (focus_cycler)
280 focus_cycler->AddWidget(this);
281 }
282
283 internal::FocusCycler* ShelfWidget::GetFocusCycler() {
284 return delegate_view_->focus_cycler();
285 }
286
287 void ShelfWidget::OnWidgetActivationChanged(views::Widget* widget,
288 bool active) {
289 activating_as_fallback_ = false;
290 if (active) {
291 delegate_view_->SetPaneFocusAndFocusDefault();
292 } else {
Mr4D (OOO till 08-26) 2013/03/02 02:02:12 no brackets if only one line like here (if & else)
Harry McCleave 2013/03/04 02:47:41 Done.
293 delegate_view_->GetFocusManager()->ClearFocus();
294 }
295 }
296
297 } // namespace ash
298
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698