OLD | NEW |
(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/tray/system_tray_bubble_view.h" |
| 6 |
| 7 #include "ash/shell.h" |
| 8 #include "ash/shell_window_ids.h" |
| 9 #include "ash/system/tray/tray_constants.h" |
| 10 #include "ash/wm/shelf_layout_manager.h" |
| 11 #include "grit/ash_strings.h" |
| 12 #include "third_party/skia/include/core/SkCanvas.h" |
| 13 #include "third_party/skia/include/core/SkColor.h" |
| 14 #include "third_party/skia/include/core/SkPaint.h" |
| 15 #include "third_party/skia/include/core/SkPath.h" |
| 16 #include "third_party/skia/include/effects/SkBlurImageFilter.h" |
| 17 #include "ui/base/accessibility/accessible_view_state.h" |
| 18 #include "ui/base/l10n/l10n_util.h" |
| 19 #include "ui/gfx/canvas.h" |
| 20 #include "ui/gfx/screen.h" |
| 21 #include "ui/views/bubble/bubble_frame_view.h" |
| 22 #include "ui/views/layout/box_layout.h" |
| 23 |
| 24 namespace ash { |
| 25 |
| 26 namespace { |
| 27 |
| 28 const int kShadowThickness = 4; |
| 29 const int kBottomLineHeight = 1; |
| 30 const int kSystemTrayBubbleHorizontalInset = 1; |
| 31 const int kSystemTrayBubbleVerticalInset = 1; |
| 32 |
| 33 const int kArrowHeight = 10; |
| 34 const int kArrowWidth = 20; |
| 35 const int kArrowPaddingFromRight = 20; |
| 36 const int kArrowPaddingFromBottom = 17; |
| 37 |
| 38 const SkColor kShadowColor = SkColorSetARGB(0xff, 0, 0, 0); |
| 39 |
| 40 void DrawBlurredShadowAroundView(gfx::Canvas* canvas, |
| 41 int top, |
| 42 int bottom, |
| 43 int width, |
| 44 const gfx::Insets& inset) { |
| 45 SkPath path; |
| 46 path.incReserve(4); |
| 47 path.moveTo(SkIntToScalar(inset.left() + kShadowThickness), |
| 48 SkIntToScalar(top + kShadowThickness + 1)); |
| 49 path.lineTo(SkIntToScalar(inset.left() + kShadowThickness), |
| 50 SkIntToScalar(bottom)); |
| 51 path.lineTo(SkIntToScalar(width), |
| 52 SkIntToScalar(bottom)); |
| 53 path.lineTo(SkIntToScalar(width), |
| 54 SkIntToScalar(top + kShadowThickness + 1)); |
| 55 |
| 56 SkPaint paint; |
| 57 paint.setColor(kShadowColor); |
| 58 paint.setStyle(SkPaint::kStroke_Style); |
| 59 paint.setXfermodeMode(SkXfermode::kSrcOver_Mode); |
| 60 paint.setStrokeWidth(SkIntToScalar(3)); |
| 61 paint.setImageFilter(new SkBlurImageFilter( |
| 62 SkIntToScalar(3), SkIntToScalar(3)))->unref(); |
| 63 canvas->sk_canvas()->drawPath(path, paint); |
| 64 } |
| 65 |
| 66 class SystemTrayBubbleBorder : public views::BubbleBorder { |
| 67 public: |
| 68 SystemTrayBubbleBorder(views::View* owner, |
| 69 views::BubbleBorder::ArrowLocation arrow_location, |
| 70 int arrow_offset) |
| 71 : views::BubbleBorder(arrow_location, |
| 72 views::BubbleBorder::NO_SHADOW), |
| 73 owner_(owner), |
| 74 arrow_offset_(arrow_offset) { |
| 75 set_alignment(views::BubbleBorder::ALIGN_EDGE_TO_ANCHOR_EDGE); |
| 76 } |
| 77 |
| 78 virtual ~SystemTrayBubbleBorder() {} |
| 79 |
| 80 private: |
| 81 // Overridden from views::BubbleBorder. |
| 82 // Override views::BubbleBorder to set the bubble on top of the anchor when |
| 83 // it has no arrow. |
| 84 virtual gfx::Rect GetBounds(const gfx::Rect& position_relative_to, |
| 85 const gfx::Size& contents_size) const OVERRIDE { |
| 86 if (arrow_location() != NONE) { |
| 87 return views::BubbleBorder::GetBounds(position_relative_to, |
| 88 contents_size); |
| 89 } |
| 90 |
| 91 gfx::Size border_size(contents_size); |
| 92 gfx::Insets insets; |
| 93 GetInsets(&insets); |
| 94 border_size.Enlarge(insets.width(), insets.height()); |
| 95 |
| 96 const int kArrowOverlap = 3; |
| 97 int x = position_relative_to.x() + |
| 98 position_relative_to.width() / 2 - border_size.width() / 2; |
| 99 // Position the bubble on top of the anchor. |
| 100 int y = position_relative_to.y() + |
| 101 kArrowOverlap - border_size.height(); |
| 102 return gfx::Rect(x, y, border_size.width(), border_size.height()); |
| 103 } |
| 104 |
| 105 // Overridden from views::Border. |
| 106 virtual void Paint(const views::View& view, |
| 107 gfx::Canvas* canvas) const OVERRIDE { |
| 108 gfx::Insets inset; |
| 109 GetInsets(&inset); |
| 110 DrawBlurredShadowAroundView(canvas, 0, owner_->height(), owner_->width(), |
| 111 inset); |
| 112 |
| 113 // Draw the bottom line. |
| 114 int y = owner_->height() + 1; |
| 115 canvas->FillRect(gfx::Rect(inset.left(), y, owner_->width(), |
| 116 kBottomLineHeight), kBorderDarkColor); |
| 117 |
| 118 if (!Shell::GetInstance()->shelf()->IsVisible() || |
| 119 arrow_location() == views::BubbleBorder::NONE) |
| 120 return; |
| 121 |
| 122 // Draw the arrow after drawing child borders, so that the arrow can cover |
| 123 // the its overlap section with child border. |
| 124 SkPath path; |
| 125 path.incReserve(4); |
| 126 if (arrow_location() == views::BubbleBorder::BOTTOM_RIGHT) { |
| 127 int tip_x = base::i18n::IsRTL() ? arrow_offset_ : |
| 128 owner_->width() - arrow_offset_; |
| 129 tip_x = std::min(std::max(kArrowWidth / 2, tip_x), |
| 130 owner_->width() - kArrowWidth / 2); |
| 131 int left_base_x = tip_x - kArrowWidth / 2; |
| 132 int left_base_y = y; |
| 133 int tip_y = left_base_y + kArrowHeight; |
| 134 path.moveTo(SkIntToScalar(left_base_x), SkIntToScalar(left_base_y)); |
| 135 path.lineTo(SkIntToScalar(tip_x), SkIntToScalar(tip_y)); |
| 136 path.lineTo(SkIntToScalar(left_base_x + kArrowWidth), |
| 137 SkIntToScalar(left_base_y)); |
| 138 } else { |
| 139 int tip_y = y - arrow_offset_; |
| 140 tip_y = std::min(std::max(kArrowWidth / 2, tip_y), |
| 141 owner_->height() - kArrowWidth / 2); |
| 142 int top_base_y = tip_y - kArrowWidth / 2; |
| 143 int top_base_x, tip_x; |
| 144 if (arrow_location() == views::BubbleBorder::LEFT_BOTTOM) { |
| 145 top_base_x = inset.left() + kSystemTrayBubbleHorizontalInset; |
| 146 tip_x = top_base_x - kArrowHeight; |
| 147 } else { |
| 148 DCHECK(arrow_location() == views::BubbleBorder::RIGHT_BOTTOM); |
| 149 top_base_x = inset.left() + owner_->width() - |
| 150 kSystemTrayBubbleHorizontalInset; |
| 151 tip_x = top_base_x + kArrowHeight; |
| 152 } |
| 153 path.moveTo(SkIntToScalar(top_base_x), SkIntToScalar(top_base_y)); |
| 154 path.lineTo(SkIntToScalar(tip_x), SkIntToScalar(tip_y)); |
| 155 path.lineTo(SkIntToScalar(top_base_x), |
| 156 SkIntToScalar(top_base_y + kArrowWidth)); |
| 157 } |
| 158 |
| 159 SkPaint paint; |
| 160 paint.setStyle(SkPaint::kFill_Style); |
| 161 paint.setColor(kHeaderBackgroundColorDark); |
| 162 canvas->DrawPath(path, paint); |
| 163 |
| 164 // Now draw the arrow border. |
| 165 paint.setStyle(SkPaint::kStroke_Style); |
| 166 paint.setColor(kBorderDarkColor); |
| 167 canvas->DrawPath(path, paint); |
| 168 |
| 169 } |
| 170 |
| 171 views::View* owner_; |
| 172 const int arrow_offset_; |
| 173 |
| 174 DISALLOW_COPY_AND_ASSIGN(SystemTrayBubbleBorder); |
| 175 }; |
| 176 |
| 177 } // namespace |
| 178 |
| 179 namespace internal { |
| 180 |
| 181 SystemTrayBubbleView::SystemTrayBubbleView( |
| 182 views::View* anchor, |
| 183 views::BubbleBorder::ArrowLocation arrow_location, |
| 184 Host* host, |
| 185 bool can_activate) |
| 186 : views::BubbleDelegateView(anchor, arrow_location), |
| 187 host_(host), |
| 188 can_activate_(can_activate), |
| 189 max_height_(0), |
| 190 bubble_width_(kTrayPopupWidth) { |
| 191 set_margin(0); |
| 192 set_parent_window(ash::Shell::GetInstance()->GetContainer( |
| 193 ash::internal::kShellWindowId_SettingBubbleContainer)); |
| 194 set_notify_enter_exit_on_child(true); |
| 195 SetPaintToLayer(true); |
| 196 SetFillsBoundsOpaquely(true); |
| 197 } |
| 198 |
| 199 SystemTrayBubbleView::~SystemTrayBubbleView() { |
| 200 // Inform host items (models) that their views are being destroyed. |
| 201 if (host_) |
| 202 host_->BubbleViewDestroyed(); |
| 203 } |
| 204 |
| 205 void SystemTrayBubbleView::SetBubbleBorder(int arrow_offset) { |
| 206 SystemTrayBubbleBorder* bubble_border = new SystemTrayBubbleBorder( |
| 207 this, arrow_location(), arrow_offset); |
| 208 GetBubbleFrameView()->SetBubbleBorder(bubble_border); |
| 209 // Recalculate with new border. |
| 210 SizeToContents(); |
| 211 } |
| 212 |
| 213 void SystemTrayBubbleView::UpdateAnchor() { |
| 214 SizeToContents(); |
| 215 GetWidget()->GetRootView()->SchedulePaint(); |
| 216 } |
| 217 |
| 218 void SystemTrayBubbleView::SetMaxHeight(int height) { |
| 219 max_height_ = height; |
| 220 if (GetWidget()) |
| 221 SizeToContents(); |
| 222 } |
| 223 |
| 224 void SystemTrayBubbleView::Init() { |
| 225 views::BoxLayout* layout = |
| 226 new views::BoxLayout(views::BoxLayout::kVertical, 0, 0, 0); |
| 227 layout->set_spread_blank_space(true); |
| 228 SetLayoutManager(layout); |
| 229 set_background(NULL); |
| 230 } |
| 231 |
| 232 gfx::Rect SystemTrayBubbleView::GetAnchorRect() { |
| 233 gfx::Rect rect; |
| 234 if (host_) |
| 235 rect = host_->GetAnchorRect(); |
| 236 // TODO(jennyz): May need to add left/right alignment in the following code. |
| 237 if (rect.IsEmpty()) { |
| 238 rect = gfx::Screen::GetPrimaryMonitor().bounds(); |
| 239 rect = gfx::Rect( |
| 240 base::i18n::IsRTL() ? kPaddingFromRightEdgeOfScreenBottomAlignment : |
| 241 rect.width() - kPaddingFromRightEdgeOfScreenBottomAlignment, |
| 242 rect.height() - kPaddingFromBottomOfScreenBottomAlignment, |
| 243 0, 0); |
| 244 } |
| 245 return rect; |
| 246 } |
| 247 |
| 248 gfx::Rect SystemTrayBubbleView::GetBubbleBounds() { |
| 249 // Same as BubbleDelegateView implementation, but don't try mirroring. |
| 250 return GetBubbleFrameView()->GetUpdatedWindowBounds(GetAnchorRect(), |
| 251 GetPreferredSize(), false /*try_mirroring_arrow*/); |
| 252 } |
| 253 |
| 254 bool SystemTrayBubbleView::CanActivate() const { |
| 255 return can_activate_; |
| 256 } |
| 257 |
| 258 gfx::Size SystemTrayBubbleView::GetPreferredSize() { |
| 259 gfx::Size size = views::BubbleDelegateView::GetPreferredSize(); |
| 260 int height = size.height(); |
| 261 if (max_height_ != 0 && height > max_height_) |
| 262 height = max_height_; |
| 263 return gfx::Size(bubble_width_, height); |
| 264 } |
| 265 |
| 266 void SystemTrayBubbleView::OnMouseEntered(const views::MouseEvent& event) { |
| 267 if (host_) |
| 268 host_->OnMouseEnteredView(); |
| 269 } |
| 270 |
| 271 void SystemTrayBubbleView::OnMouseExited(const views::MouseEvent& event) { |
| 272 if (host_) |
| 273 host_->OnMouseExitedView(); |
| 274 } |
| 275 |
| 276 void SystemTrayBubbleView::GetAccessibleState(ui::AccessibleViewState* state) { |
| 277 if (can_activate_) { |
| 278 state->role = ui::AccessibilityTypes::ROLE_WINDOW; |
| 279 state->name = l10n_util::GetStringUTF16( |
| 280 IDS_ASH_STATUS_TRAY_ACCESSIBLE_NAME); |
| 281 } |
| 282 } |
| 283 |
| 284 void SystemTrayBubbleView::ChildPreferredSizeChanged(View* child) { |
| 285 SizeToContents(); |
| 286 } |
| 287 |
| 288 void SystemTrayBubbleView::ViewHierarchyChanged(bool is_add, |
| 289 views::View* parent, |
| 290 views::View* child) { |
| 291 if (is_add && child == this) { |
| 292 parent->SetPaintToLayer(true); |
| 293 parent->SetFillsBoundsOpaquely(true); |
| 294 parent->layer()->SetMasksToBounds(true); |
| 295 } |
| 296 } |
| 297 |
| 298 } // namespace internal |
| 299 } // namespace ash |
OLD | NEW |