Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "ui/views/bubble/tray_bubble_view.h" | 5 #include "ui/views/bubble/tray_bubble_view.h" |
| 6 | 6 |
| 7 #include "third_party/skia/include/core/SkCanvas.h" | 7 #include "third_party/skia/include/core/SkCanvas.h" |
| 8 #include "third_party/skia/include/core/SkColor.h" | 8 #include "third_party/skia/include/core/SkColor.h" |
| 9 #include "third_party/skia/include/core/SkPaint.h" | 9 #include "third_party/skia/include/core/SkPaint.h" |
| 10 #include "third_party/skia/include/core/SkPath.h" | 10 #include "third_party/skia/include/core/SkPath.h" |
| (...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 110 } | 110 } |
| 111 | 111 |
| 112 private: | 112 private: |
| 113 views::View* owner_; | 113 views::View* owner_; |
| 114 views::View* anchor_; | 114 views::View* anchor_; |
| 115 const int tray_arrow_offset_; | 115 const int tray_arrow_offset_; |
| 116 | 116 |
| 117 DISALLOW_COPY_AND_ASSIGN(TrayBubbleBorder); | 117 DISALLOW_COPY_AND_ASSIGN(TrayBubbleBorder); |
| 118 }; | 118 }; |
| 119 | 119 |
| 120 // Custom background for TrayBubbleView. Fills in the top and bottom margins | 120 class TrayBubbleContentMask : public ui::Layer, public ui::LayerDelegate { |
|
sky
2012/11/07 00:37:05
This code won't work on windows non-aura.
bartfab (slow)
2012/11/07 12:12:16
I modified the CL to preserve the previous impleme
| |
| 121 // with appropriate background colors without overwriting the rounded corners. | |
| 122 class TrayBubbleBackground : public views::Background { | |
| 123 public: | 121 public: |
| 124 explicit TrayBubbleBackground(views::BubbleBorder* border, | 122 TrayBubbleContentMask(views::BubbleBorder* border) |
| 125 SkColor top_color, | 123 : Layer(ui::LAYER_TEXTURED), |
| 126 SkColor bottom_color) | 124 radius_(SkIntToScalar(border->GetBorderCornerRadius() - 1)) { |
| 127 : border_(border), | 125 set_delegate(this); |
| 128 top_color_(top_color), | |
| 129 bottom_color_(bottom_color), | |
| 130 radius_(SkIntToScalar(border->GetBorderCornerRadius() - 1)) { | |
| 131 } | 126 } |
| 132 | 127 |
| 133 SkScalar radius() const { return radius_; } | 128 // Overridden from LayerDelegate: |
| 134 | 129 virtual void OnPaintLayer(gfx::Canvas* canvas) OVERRIDE { |
| 135 // Overridden from Background: | |
| 136 virtual void Paint(gfx::Canvas* canvas, views::View* view) const OVERRIDE { | |
| 137 canvas->Save(); | |
| 138 | |
| 139 // Set a clip mask for the bubble's rounded corners. | |
| 140 gfx::Rect bounds(view->GetContentsBounds()); | |
| 141 const int border_thickness(border_->GetBorderThickness()); | |
| 142 bounds.Inset(-border_thickness, -border_thickness); | |
| 143 SkPath path; | 130 SkPath path; |
| 144 path.addRoundRect(gfx::RectToSkRect(bounds), radius_, radius_); | 131 path.addRoundRect(gfx::RectToSkRect(bounds()), radius_, radius_); |
|
sky
2012/11/07 00:37:05
Bounds() is in the coordinate system of the parent
bartfab (slow)
2012/11/07 12:12:16
Done.
bounds() actually worked here because the b
| |
| 145 canvas->ClipPath(path); | |
| 146 | |
| 147 // Paint the header and footer (assumes the bubble contents fill in their | |
| 148 // own backgrounds). | |
| 149 SkPaint paint; | 132 SkPaint paint; |
| 150 paint.setStyle(SkPaint::kFill_Style); | 133 paint.setStyle(SkPaint::kFill_Style); |
|
stevenjb
2012/11/06 21:43:26
What are we drawing here? I'm unfamiliar with what
bartfab (slow)
2012/11/06 23:09:14
Since this is a mask layer, only alpha matters. A
stevenjb
2012/11/06 23:17:45
I see. Might be worth commenting to that effect (o
bartfab (slow)
2012/11/07 12:12:16
I added an explicit setAlpha() call.
| |
| 134 canvas->DrawPath(path, paint); | |
| 135 } | |
| 151 | 136 |
| 152 gfx::Rect top_rect(bounds); | 137 virtual void OnDeviceScaleFactorChanged(float device_scale_factor) OVERRIDE { |
| 153 top_rect.set_height(radius_); | 138 } |
| 154 paint.setColor(top_color_); | |
| 155 canvas->DrawRect(top_rect, paint); | |
| 156 | 139 |
| 157 gfx::Rect bottom_rect(bounds); | 140 virtual base::Closure PrepareForLayerBoundsChange() OVERRIDE { |
| 158 bottom_rect.set_y(bounds.y() + (bounds.height() - radius_)); | 141 return base::Closure(); |
| 159 bottom_rect.set_height(radius_); | |
| 160 paint.setColor(bottom_color_); | |
| 161 canvas->DrawRect(bottom_rect, paint); | |
| 162 | |
| 163 canvas->Restore(); | |
| 164 } | 142 } |
| 165 | 143 |
| 166 private: | 144 private: |
| 167 views::BubbleBorder* border_; | |
| 168 SkColor top_color_; | |
| 169 SkColor bottom_color_; | |
| 170 SkScalar radius_; | 145 SkScalar radius_; |
| 171 | 146 |
| 172 DISALLOW_COPY_AND_ASSIGN(TrayBubbleBackground); | 147 DISALLOW_COPY_AND_ASSIGN(TrayBubbleContentMask); |
| 173 }; | 148 }; |
| 174 | 149 |
| 175 // Custom layout for the bubble-view. Does the default box-layout if there is | 150 // Custom layout for the bubble-view. Does the default box-layout if there is |
| 176 // enough height. Otherwise, makes sure the bottom rows are visible. | 151 // enough height. Otherwise, makes sure the bottom rows are visible. |
| 177 class BottomAlignedBoxLayout : public views::BoxLayout { | 152 class BottomAlignedBoxLayout : public views::BoxLayout { |
| 178 public: | 153 public: |
| 179 explicit BottomAlignedBoxLayout(TrayBubbleView* bubble_view) | 154 explicit BottomAlignedBoxLayout(TrayBubbleView* bubble_view) |
| 180 : views::BoxLayout(views::BoxLayout::kVertical, 0, 0, 0), | 155 : views::BoxLayout(views::BoxLayout::kVertical, 0, 0, 0), |
| 181 bubble_view_(bubble_view) { | 156 bubble_view_(bubble_view) { |
| 182 } | 157 } |
| (...skipping 22 matching lines...) Expand all Loading... | |
| 205 } | 180 } |
| 206 | 181 |
| 207 TrayBubbleView* bubble_view_; | 182 TrayBubbleView* bubble_view_; |
| 208 | 183 |
| 209 DISALLOW_COPY_AND_ASSIGN(BottomAlignedBoxLayout); | 184 DISALLOW_COPY_AND_ASSIGN(BottomAlignedBoxLayout); |
| 210 }; | 185 }; |
| 211 | 186 |
| 212 } // namespace internal | 187 } // namespace internal |
| 213 | 188 |
| 214 using internal::TrayBubbleBorder; | 189 using internal::TrayBubbleBorder; |
| 215 using internal::TrayBubbleBackground; | 190 using internal::TrayBubbleContentMask; |
| 216 using internal::BottomAlignedBoxLayout; | 191 using internal::BottomAlignedBoxLayout; |
| 217 | 192 |
| 218 // static | 193 // static |
| 219 const int TrayBubbleView::InitParams::kArrowDefaultOffset = -1; | 194 const int TrayBubbleView::InitParams::kArrowDefaultOffset = -1; |
| 220 | 195 |
| 221 TrayBubbleView::InitParams::InitParams(AnchorType anchor_type, | 196 TrayBubbleView::InitParams::InitParams(AnchorType anchor_type, |
| 222 AnchorAlignment anchor_alignment, | 197 AnchorAlignment anchor_alignment, |
| 223 int bubble_width) | 198 int bubble_width) |
| 224 : anchor_type(anchor_type), | 199 : anchor_type(anchor_type), |
| 225 anchor_alignment(anchor_alignment), | 200 anchor_alignment(anchor_alignment), |
| 226 bubble_width(bubble_width), | 201 bubble_width(bubble_width), |
| 227 max_height(0), | 202 max_height(0), |
| 228 can_activate(false), | 203 can_activate(false), |
| 229 close_on_deactivate(true), | 204 close_on_deactivate(true), |
| 230 top_color(SK_ColorBLACK), | |
| 231 arrow_color(SK_ColorBLACK), | 205 arrow_color(SK_ColorBLACK), |
| 232 arrow_location(views::BubbleBorder::NONE), | 206 arrow_location(views::BubbleBorder::NONE), |
| 233 arrow_offset(kArrowDefaultOffset), | 207 arrow_offset(kArrowDefaultOffset), |
| 234 shadow(views::BubbleBorder::BIG_SHADOW) { | 208 shadow(views::BubbleBorder::BIG_SHADOW) { |
| 235 } | 209 } |
| 236 | 210 |
| 237 // static | 211 // static |
| 238 TrayBubbleView* TrayBubbleView::Create(gfx::NativeView parent_window, | 212 TrayBubbleView* TrayBubbleView::Create(gfx::NativeView parent_window, |
| 239 views::View* anchor, | 213 views::View* anchor, |
| 240 Delegate* delegate, | 214 Delegate* delegate, |
| (...skipping 17 matching lines...) Expand all Loading... | |
| 258 } | 232 } |
| 259 | 233 |
| 260 TrayBubbleView::TrayBubbleView(gfx::NativeView parent_window, | 234 TrayBubbleView::TrayBubbleView(gfx::NativeView parent_window, |
| 261 views::View* anchor, | 235 views::View* anchor, |
| 262 Delegate* delegate, | 236 Delegate* delegate, |
| 263 const InitParams& init_params) | 237 const InitParams& init_params) |
| 264 : views::BubbleDelegateView(anchor, init_params.arrow_location), | 238 : views::BubbleDelegateView(anchor, init_params.arrow_location), |
| 265 params_(init_params), | 239 params_(init_params), |
| 266 delegate_(delegate), | 240 delegate_(delegate), |
| 267 bubble_border_(NULL), | 241 bubble_border_(NULL), |
| 268 bubble_background_(NULL), | |
| 269 is_gesture_dragging_(false) { | 242 is_gesture_dragging_(false) { |
| 270 set_parent_window(parent_window); | 243 set_parent_window(parent_window); |
| 271 set_notify_enter_exit_on_child(true); | 244 set_notify_enter_exit_on_child(true); |
| 272 set_close_on_deactivate(init_params.close_on_deactivate); | 245 set_close_on_deactivate(init_params.close_on_deactivate); |
| 246 set_margins(gfx::Insets()); | |
| 273 SetPaintToLayer(true); | 247 SetPaintToLayer(true); |
| 274 SetFillsBoundsOpaquely(true); | 248 SetFillsBoundsOpaquely(true); |
| 275 | 249 |
| 276 bubble_border_ = new TrayBubbleBorder(this, anchor_view(), params_); | 250 bubble_border_ = new TrayBubbleBorder(this, anchor_view(), params_); |
| 277 | 251 |
| 278 bubble_background_ = new TrayBubbleBackground( | 252 bubble_content_mask_.reset(new TrayBubbleContentMask(bubble_border_)); |
| 279 bubble_border_, init_params.top_color, init_params.arrow_color); | |
| 280 | |
| 281 // Inset the view on the top and bottom by the corner radius to avoid drawing | |
| 282 // over the the bubble corners. | |
| 283 const int radius = bubble_background_->radius(); | |
| 284 set_margins(gfx::Insets(radius, 0, radius, 0)); | |
| 285 } | 253 } |
| 286 | 254 |
| 287 TrayBubbleView::~TrayBubbleView() { | 255 TrayBubbleView::~TrayBubbleView() { |
| 288 // Inform host items (models) that their views are being destroyed. | 256 // Inform host items (models) that their views are being destroyed. |
| 289 if (delegate_) | 257 if (delegate_) |
| 290 delegate_->BubbleViewDestroyed(); | 258 delegate_->BubbleViewDestroyed(); |
| 291 } | 259 } |
| 292 | 260 |
| 293 void TrayBubbleView::InitializeAndShowBubble() { | 261 void TrayBubbleView::InitializeAndShowBubble() { |
| 294 // Must occur after call to BubbleDelegateView::CreateBubble(). | 262 // Must occur after call to BubbleDelegateView::CreateBubble(). |
| 295 SetAlignment(views::BubbleBorder::ALIGN_EDGE_TO_ANCHOR_EDGE); | 263 SetAlignment(views::BubbleBorder::ALIGN_EDGE_TO_ANCHOR_EDGE); |
| 296 bubble_border_->UpdateArrowOffset(); | 264 bubble_border_->UpdateArrowOffset(); |
| 297 | 265 |
| 266 layer()->parent()->SetMaskLayer(bubble_content_mask_.get()); | |
| 267 | |
| 298 Show(); | 268 Show(); |
| 299 UpdateBubble(); | 269 UpdateBubble(); |
| 300 } | 270 } |
| 301 | 271 |
| 302 void TrayBubbleView::UpdateBubble() { | 272 void TrayBubbleView::UpdateBubble() { |
| 303 SizeToContents(); | 273 SizeToContents(); |
| 274 bubble_content_mask_->SetBounds(GetContentsBounds()); | |
| 304 GetWidget()->GetRootView()->SchedulePaint(); | 275 GetWidget()->GetRootView()->SchedulePaint(); |
| 305 } | 276 } |
| 306 | 277 |
| 307 void TrayBubbleView::SetMaxHeight(int height) { | 278 void TrayBubbleView::SetMaxHeight(int height) { |
| 308 params_.max_height = height; | 279 params_.max_height = height; |
| 309 if (GetWidget()) | 280 if (GetWidget()) |
| 310 SizeToContents(); | 281 SizeToContents(); |
| 311 } | 282 } |
| 312 | 283 |
| 313 void TrayBubbleView::SetPaintArrow(bool paint_arrow) { | 284 void TrayBubbleView::SetPaintArrow(bool paint_arrow) { |
| (...skipping 15 matching lines...) Expand all Loading... | |
| 329 return gfx::Rect(); | 300 return gfx::Rect(); |
| 330 return delegate_->GetAnchorRect(anchor_widget(), | 301 return delegate_->GetAnchorRect(anchor_widget(), |
| 331 params_.anchor_type, | 302 params_.anchor_type, |
| 332 params_.anchor_alignment); | 303 params_.anchor_alignment); |
| 333 } | 304 } |
| 334 | 305 |
| 335 bool TrayBubbleView::CanActivate() const { | 306 bool TrayBubbleView::CanActivate() const { |
| 336 return params_.can_activate; | 307 return params_.can_activate; |
| 337 } | 308 } |
| 338 | 309 |
| 339 // Overridden to create BubbleFrameView and set a custom border and background. | 310 // Overridden to create BubbleFrameView and set a custom border. |
| 340 views::NonClientFrameView* TrayBubbleView::CreateNonClientFrameView( | 311 views::NonClientFrameView* TrayBubbleView::CreateNonClientFrameView( |
| 341 views::Widget* widget) { | 312 views::Widget* widget) { |
| 342 views::BubbleFrameView* bubble_frame_view = | 313 views::BubbleFrameView* bubble_frame_view = |
| 343 new views::BubbleFrameView(margins(), bubble_border_); | 314 new views::BubbleFrameView(margins(), bubble_border_); |
| 344 bubble_frame_view->set_background(bubble_background_); | |
| 345 return bubble_frame_view; | 315 return bubble_frame_view; |
| 346 } | 316 } |
| 347 | 317 |
| 348 bool TrayBubbleView::WidgetHasHitTestMask() const { | 318 bool TrayBubbleView::WidgetHasHitTestMask() const { |
| 349 return true; | 319 return true; |
| 350 } | 320 } |
| 351 | 321 |
| 352 void TrayBubbleView::GetWidgetHitTestMask(gfx::Path* mask) const { | 322 void TrayBubbleView::GetWidgetHitTestMask(gfx::Path* mask) const { |
| 353 DCHECK(mask); | 323 DCHECK(mask); |
| 354 mask->addRect(gfx::RectToSkRect(GetBubbleFrameView()->GetContentsBounds())); | 324 mask->addRect(gfx::RectToSkRect(GetBubbleFrameView()->GetContentsBounds())); |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 387 views::View* parent, | 357 views::View* parent, |
| 388 views::View* child) { | 358 views::View* child) { |
| 389 if (is_add && child == this) { | 359 if (is_add && child == this) { |
| 390 parent->SetPaintToLayer(true); | 360 parent->SetPaintToLayer(true); |
| 391 parent->SetFillsBoundsOpaquely(true); | 361 parent->SetFillsBoundsOpaquely(true); |
| 392 parent->layer()->SetMasksToBounds(true); | 362 parent->layer()->SetMasksToBounds(true); |
| 393 } | 363 } |
| 394 } | 364 } |
| 395 | 365 |
| 396 } // namespace views | 366 } // namespace views |
| OLD | NEW |