Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / ash / system / web_notification / ash_popup_alignment_delegate.cc
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/ash_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 AshPopupAlignmentDelegate::AshPopupAlignmentDelegate()
35     : display_id_(gfx::Display::kInvalidDisplayID),
36       screen_(NULL),
37       root_window_(NULL),
38       shelf_(NULL),
39       system_tray_height_(0) {
40 }
41
42 AshPopupAlignmentDelegate::~AshPopupAlignmentDelegate() {
43   if (screen_)
44     screen_->RemoveObserver(this);
45   Shell::GetInstance()->RemoveShellObserver(this);
46   if (shelf_)
47     shelf_->RemoveObserver(this);
48 }
49
50 void AshPopupAlignmentDelegate::StartObserving(gfx::Screen* screen,
51                                                const gfx::Display& display) {
52   screen_ = screen;
53   display_id_ = display.id();
54   root_window_ = ash::Shell::GetInstance()->display_controller()->
55       GetRootWindowForDisplayId(display_id_);
56   UpdateShelf();
57   screen->AddObserver(this);
58   Shell::GetInstance()->AddShellObserver(this);
59   if (system_tray_height_ > 0)
60     OnAutoHideStateChanged(shelf_->auto_hide_state());
61 }
62
63 void AshPopupAlignmentDelegate::SetSystemTrayHeight(int height) {
64   system_tray_height_ = height;
65
66   // If the shelf is shown during auto-hide state, the distance from the edge
67   // should be reduced by the height of shelf's shown height.
68   if (shelf_ && shelf_->visibility_state() == SHELF_AUTO_HIDE &&
69       shelf_->auto_hide_state() == SHELF_AUTO_HIDE_SHOWN) {
70     system_tray_height_ -= kShelfSize - ShelfLayoutManager::kAutoHideSize;
71   }
72
73   if (system_tray_height_ > 0)
74     system_tray_height_ += message_center::kMarginBetweenItems;
75   else
76     system_tray_height_ = 0;
77
78   if (!shelf_)
79     return;
80
81   DoUpdateIfPossible();
82 }
83
84 int AshPopupAlignmentDelegate::GetToastOriginX(
85     const gfx::Rect& toast_bounds) const {
86   // In Ash, RTL UI language mirrors the whole ash layout, so the toast
87   // widgets should be at the bottom-left instead of bottom right.
88   if (base::i18n::IsRTL())
89     return work_area_.x() + kToastMarginX;
90
91   if (IsFromLeft())
92     return work_area_.x() + kToastMarginX;
93   return work_area_.right() - kToastMarginX - toast_bounds.width();
94 }
95
96 int AshPopupAlignmentDelegate::GetBaseLine() const {
97   return IsTopDown()
98       ? work_area_.y() + kNoToastMarginBorderAndShadowOffset +
99         system_tray_height_
100       : work_area_.bottom() - kNoToastMarginBorderAndShadowOffset -
101         system_tray_height_;
102 }
103
104 int AshPopupAlignmentDelegate::GetWorkAreaBottom() const {
105   return work_area_.bottom() - system_tray_height_;
106 }
107
108 bool AshPopupAlignmentDelegate::IsTopDown() const {
109   return GetAlignment() == SHELF_ALIGNMENT_TOP;
110 }
111
112 bool AshPopupAlignmentDelegate::IsFromLeft() const {
113   return GetAlignment() == SHELF_ALIGNMENT_LEFT;
114 }
115
116 void AshPopupAlignmentDelegate::RecomputeAlignment(
117     const gfx::Display& display) {
118   // Nothing needs to be done.
119 }
120
121 ShelfAlignment AshPopupAlignmentDelegate::GetAlignment() const {
122   return shelf_ ? shelf_->GetAlignment() : SHELF_ALIGNMENT_BOTTOM;
123 }
124
125 void AshPopupAlignmentDelegate::UpdateShelf() {
126   if (shelf_)
127     return;
128
129   shelf_ = ShelfLayoutManager::ForShelf(root_window_);
130   if (shelf_)
131     shelf_->AddObserver(this);
132 }
133
134 void AshPopupAlignmentDelegate::OnDisplayWorkAreaInsetsChanged() {
135   UpdateShelf();
136
137   work_area_ = Shell::GetScreen()->GetDisplayNearestWindow(
138       shelf_->shelf_widget()->GetNativeView()).work_area();
139 }
140
141 void AshPopupAlignmentDelegate::OnAutoHideStateChanged(
142     ShelfAutoHideState new_state) {
143   work_area_ = Shell::GetScreen()->GetDisplayNearestWindow(
144       shelf_->shelf_widget()->GetNativeView()).work_area();
145   int width = 0;
146   if ((shelf_->visibility_state() == SHELF_AUTO_HIDE) &&
147       new_state == SHELF_AUTO_HIDE_SHOWN) {
148     // Since the work_area is already reduced by kAutoHideSize, the inset width
149     // should be just the difference.
150     width = kShelfSize - ShelfLayoutManager::kAutoHideSize;
151   }
152   work_area_.Inset(shelf_->SelectValueForShelfAlignment(
153       gfx::Insets(0, 0, width, 0),
154       gfx::Insets(0, width, 0, 0),
155       gfx::Insets(0, 0, 0, width),
156       gfx::Insets(width, 0, 0, 0)));
157
158   DoUpdateIfPossible();
159 }
160
161 void AshPopupAlignmentDelegate::OnDisplayAdded(
162     const gfx::Display& new_display) {
163 }
164
165 void AshPopupAlignmentDelegate::OnDisplayRemoved(
166     const gfx::Display& old_display) {
167 }
168
169 void AshPopupAlignmentDelegate::OnDisplayMetricsChanged(
170     const gfx::Display& display,
171     uint32_t metrics) {
172   if (display.id() == display_id_ && shelf_)
173     OnAutoHideStateChanged(shelf_->auto_hide_state());
174 }
175
176 }  // namespace ash