OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2014 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/web_notification/popup_alignment_delegate.h" | |
6 | |
7 #include "ash/display/display_controller.h" | |
8 #include "ash/shelf/shelf_constants.h" | |
9 #include "ash/shelf/shelf_layout_manager.h" | |
10 #include "ash/shelf/shelf_types.h" | |
11 #include "ash/shelf/shelf_widget.h" | |
12 #include "ash/shell.h" | |
13 #include "base/i18n/rtl.h" | |
14 #include "ui/aura/window.h" | |
15 #include "ui/gfx/display.h" | |
16 #include "ui/gfx/geometry/rect.h" | |
17 #include "ui/gfx/screen.h" | |
18 #include "ui/message_center/message_center_style.h" | |
19 #include "ui/message_center/views/message_popup_collection.h" | |
20 | |
21 namespace ash { | |
22 | |
23 namespace { | |
24 | |
25 const int kToastMarginX = 3; | |
26 | |
27 // If there should be no margin for the first item, this value needs to be | |
28 // substracted to flush the message to the shelf (the width of the border + | |
29 // shadow). | |
30 const int kNoToastMarginBorderAndShadowOffset = 2; | |
31 | |
32 } | |
33 | |
34 PopupAlignmentDelegate::PopupAlignmentDelegate() | |
35 : display_id_(gfx::Display::kInvalidDisplayID), | |
36 screen_(NULL), | |
stevenjb
2014/07/02 21:59:23
root_window_(NULL)
Jun Mukai
2014/07/07 18:12:35
Done.
| |
37 shelf_(NULL), | |
38 system_tray_height_(0) { | |
39 } | |
40 | |
41 PopupAlignmentDelegate::~PopupAlignmentDelegate() { | |
42 if (screen_) | |
43 screen_->RemoveObserver(this); | |
44 Shell::GetInstance()->RemoveShellObserver(this); | |
45 if (shelf_) | |
46 shelf_->RemoveObserver(this); | |
47 shelf_ = NULL; | |
stevenjb
2014/07/02 21:59:23
nit: unnecessary (and inconsistent with screen_)
Jun Mukai
2014/07/07 18:12:35
Done.
| |
48 } | |
49 | |
50 void PopupAlignmentDelegate::StartObserving( | |
51 gfx::Screen* screen, const gfx::Display& display) { | |
stevenjb
2014/07/02 21:59:23
one line per arg
Jun Mukai
2014/07/07 18:12:35
Done.
| |
52 screen_ = screen; | |
53 display_id_ = display.id(); | |
54 UpdateShelf(); | |
55 screen->AddObserver(this); | |
56 Shell::GetInstance()->AddShellObserver(this); | |
57 if (system_tray_height_ > 0) | |
58 OnAutoHideStateChanged(shelf_->auto_hide_state()); | |
59 } | |
60 | |
61 void PopupAlignmentDelegate::SetSystemTrayHeight(int height) { | |
62 system_tray_height_ = height; | |
63 | |
64 // If the shelf is shown during auto-hide state, the distance from the edge | |
65 // should be reduced by the height of shelf's shown height. | |
66 if (shelf_ && shelf_->visibility_state() == SHELF_AUTO_HIDE && | |
67 shelf_->auto_hide_state() == SHELF_AUTO_HIDE_SHOWN) { | |
68 system_tray_height_ -= kShelfSize - ShelfLayoutManager::kAutoHideSize; | |
69 } | |
70 | |
71 if (system_tray_height_ > 0) | |
72 system_tray_height_ += message_center::kMarginBetweenItems; | |
73 else | |
74 system_tray_height_ = 0; | |
75 | |
76 if (!shelf_) | |
77 return; | |
78 | |
79 DoUpdateIfPossible(); | |
80 } | |
81 | |
82 int PopupAlignmentDelegate::GetToastOriginX( | |
83 const gfx::Rect& toast_bounds) const { | |
84 // In Ash, RTL UI language mirrors the whole ash layout, so the toast | |
85 // widgets should be at the bottom-left instead of bottom right. | |
86 if (base::i18n::IsRTL()) | |
87 return work_area_.x() + kToastMarginX; | |
88 | |
89 if (IsFromLeft()) | |
90 return work_area_.x() + kToastMarginX; | |
91 return work_area_.right() - kToastMarginX - toast_bounds.width(); | |
92 } | |
93 | |
94 int PopupAlignmentDelegate::GetBaseLine() const { | |
95 return IsTopDown() | |
96 ? work_area_.y() + kNoToastMarginBorderAndShadowOffset + | |
97 system_tray_height_ | |
98 : work_area_.bottom() - kNoToastMarginBorderAndShadowOffset - | |
99 system_tray_height_; | |
100 } | |
101 | |
102 int PopupAlignmentDelegate::GetWorkAreaBottom() const { | |
103 return work_area_.bottom() - system_tray_height_; | |
104 } | |
105 | |
106 bool PopupAlignmentDelegate::IsTopDown() const { | |
107 return GetAlignment() == SHELF_ALIGNMENT_TOP; | |
108 } | |
109 | |
110 bool PopupAlignmentDelegate::IsFromLeft() const { | |
111 return GetAlignment() == SHELF_ALIGNMENT_LEFT; | |
112 } | |
113 | |
114 ShelfAlignment PopupAlignmentDelegate::GetAlignment() const { | |
115 return shelf_ ? shelf_->GetAlignment() : SHELF_ALIGNMENT_BOTTOM; | |
116 } | |
117 | |
118 void PopupAlignmentDelegate::UpdateShelf() { | |
119 if (shelf_) | |
120 return; | |
121 | |
122 aura::Window* root_window = ash::Shell::GetInstance()->display_controller()-> | |
123 GetRootWindowForDisplayId(display_id_); | |
124 shelf_ = ShelfLayoutManager::ForShelf(root_window); | |
125 if (shelf_) | |
126 shelf_->AddObserver(this); | |
127 } | |
128 | |
129 void PopupAlignmentDelegate::OnDisplayWorkAreaInsetsChanged() { | |
130 UpdateShelf(); | |
131 | |
132 work_area_ = Shell::GetScreen()->GetDisplayNearestWindow( | |
133 shelf_->shelf_widget()->GetNativeView()).work_area(); | |
134 } | |
135 | |
136 void PopupAlignmentDelegate::OnAutoHideStateChanged( | |
137 ShelfAutoHideState new_state) { | |
138 work_area_ = Shell::GetScreen()->GetDisplayNearestWindow( | |
139 shelf_->shelf_widget()->GetNativeView()).work_area(); | |
140 int width = 0; | |
141 if ((shelf_->visibility_state() == SHELF_AUTO_HIDE) && | |
142 new_state == SHELF_AUTO_HIDE_SHOWN) { | |
143 // Since the work_area is already reduced by kAutoHideSize, the inset width | |
144 // should be just the difference. | |
145 width = kShelfSize - ShelfLayoutManager::kAutoHideSize; | |
146 } | |
147 work_area_.Inset(shelf_->SelectValueForShelfAlignment( | |
148 gfx::Insets(0, 0, width, 0), | |
149 gfx::Insets(0, width, 0, 0), | |
150 gfx::Insets(0, 0, 0, width), | |
151 gfx::Insets(width, 0, 0, 0))); | |
152 | |
153 DoUpdateIfPossible(); | |
154 } | |
155 | |
156 void PopupAlignmentDelegate::OnDisplayAdded( | |
157 const gfx::Display& new_display) { | |
158 } | |
159 | |
160 void PopupAlignmentDelegate::OnDisplayRemoved( | |
161 const gfx::Display& old_display) { | |
162 } | |
163 | |
164 void PopupAlignmentDelegate::OnDisplayMetricsChanged( | |
165 const gfx::Display& display, | |
166 uint32_t metrics) { | |
167 if (display.id() == display_id_ && shelf_) | |
168 OnAutoHideStateChanged(shelf_->auto_hide_state()); | |
169 } | |
170 | |
171 } // namespace ash | |
OLD | NEW |