| 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/controls/menu/menu_scroll_view_container.h" | 5 #include "ui/views/controls/menu/menu_scroll_view_container.h" |
| 6 | 6 |
| 7 #include "third_party/skia/include/core/SkPaint.h" | 7 #include "third_party/skia/include/core/SkPaint.h" |
| 8 #include "third_party/skia/include/core/SkPath.h" | 8 #include "third_party/skia/include/core/SkPath.h" |
| 9 #include "ui/accessibility/ax_view_state.h" | 9 #include "ui/accessibility/ax_view_state.h" |
| 10 #include "ui/gfx/canvas.h" | 10 #include "ui/gfx/canvas.h" |
| 11 #include "ui/gfx/color_palette.h" |
| 11 #include "ui/views/border.h" | 12 #include "ui/views/border.h" |
| 12 #include "ui/views/bubble/bubble_border.h" | 13 #include "ui/views/bubble/bubble_border.h" |
| 13 #include "ui/views/controls/menu/menu_config.h" | 14 #include "ui/views/controls/menu/menu_config.h" |
| 14 #include "ui/views/controls/menu/menu_controller.h" | 15 #include "ui/views/controls/menu/menu_controller.h" |
| 15 #include "ui/views/controls/menu/menu_item_view.h" | 16 #include "ui/views/controls/menu/menu_item_view.h" |
| 16 #include "ui/views/controls/menu/submenu_view.h" | 17 #include "ui/views/controls/menu/submenu_view.h" |
| 17 #include "ui/views/round_rect_painter.h" | 18 #include "ui/views/round_rect_painter.h" |
| 18 | 19 |
| 19 using ui::NativeTheme; | 20 using ui::NativeTheme; |
| 20 | 21 |
| (...skipping 173 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 194 | 195 |
| 195 bool MenuScrollViewContainer::HasBubbleBorder() { | 196 bool MenuScrollViewContainer::HasBubbleBorder() { |
| 196 return arrow_ != BubbleBorder::NONE; | 197 return arrow_ != BubbleBorder::NONE; |
| 197 } | 198 } |
| 198 | 199 |
| 199 void MenuScrollViewContainer::SetBubbleArrowOffset(int offset) { | 200 void MenuScrollViewContainer::SetBubbleArrowOffset(int offset) { |
| 200 DCHECK(HasBubbleBorder()); | 201 DCHECK(HasBubbleBorder()); |
| 201 bubble_border_->set_arrow_offset(offset); | 202 bubble_border_->set_arrow_offset(offset); |
| 202 } | 203 } |
| 203 | 204 |
| 204 void MenuScrollViewContainer::OnPaintBackground(gfx::Canvas* canvas) { | 205 gfx::Size MenuScrollViewContainer::GetPreferredSize() const { |
| 205 if (background()) { | 206 gfx::Size prefsize = scroll_view_->GetContents()->GetPreferredSize(); |
| 206 View::OnPaintBackground(canvas); | 207 gfx::Insets insets = GetInsets(); |
| 207 return; | 208 prefsize.Enlarge(insets.width(), insets.height()); |
| 208 } | 209 return prefsize; |
| 209 | |
| 210 gfx::Rect bounds(0, 0, width(), height()); | |
| 211 NativeTheme::ExtraParams extra; | |
| 212 const MenuConfig& menu_config = content_view_->GetMenuItem()->GetMenuConfig(); | |
| 213 extra.menu_background.corner_radius = menu_config.corner_radius; | |
| 214 GetNativeTheme()->Paint(canvas->sk_canvas(), | |
| 215 NativeTheme::kMenuPopupBackground, NativeTheme::kNormal, bounds, extra); | |
| 216 } | 210 } |
| 217 | 211 |
| 218 void MenuScrollViewContainer::Layout() { | 212 void MenuScrollViewContainer::Layout() { |
| 219 gfx::Insets insets = GetInsets(); | 213 gfx::Insets insets = GetInsets(); |
| 220 int x = insets.left(); | 214 int x = insets.left(); |
| 221 int y = insets.top(); | 215 int y = insets.top(); |
| 222 int width = View::width() - insets.width(); | 216 int width = View::width() - insets.width(); |
| 223 int content_height = height() - insets.height(); | 217 int content_height = height() - insets.height(); |
| 224 if (!scroll_up_button_->visible()) { | 218 if (!scroll_up_button_->visible()) { |
| 225 scroll_view_->SetBounds(x, y, width, content_height); | 219 scroll_view_->SetBounds(x, y, width, content_height); |
| 226 scroll_view_->Layout(); | 220 scroll_view_->Layout(); |
| 227 return; | 221 return; |
| 228 } | 222 } |
| 229 | 223 |
| 230 gfx::Size pref = scroll_up_button_->GetPreferredSize(); | 224 gfx::Size pref = scroll_up_button_->GetPreferredSize(); |
| 231 scroll_up_button_->SetBounds(x, y, width, pref.height()); | 225 scroll_up_button_->SetBounds(x, y, width, pref.height()); |
| 232 content_height -= pref.height(); | 226 content_height -= pref.height(); |
| 233 | 227 |
| 234 const int scroll_view_y = y + pref.height(); | 228 const int scroll_view_y = y + pref.height(); |
| 235 | 229 |
| 236 pref = scroll_down_button_->GetPreferredSize(); | 230 pref = scroll_down_button_->GetPreferredSize(); |
| 237 scroll_down_button_->SetBounds(x, height() - pref.height() - insets.top(), | 231 scroll_down_button_->SetBounds(x, height() - pref.height() - insets.top(), |
| 238 width, pref.height()); | 232 width, pref.height()); |
| 239 content_height -= pref.height(); | 233 content_height -= pref.height(); |
| 240 | 234 |
| 241 scroll_view_->SetBounds(x, scroll_view_y, width, content_height); | 235 scroll_view_->SetBounds(x, scroll_view_y, width, content_height); |
| 242 scroll_view_->Layout(); | 236 scroll_view_->Layout(); |
| 243 } | 237 } |
| 244 | 238 |
| 245 gfx::Size MenuScrollViewContainer::GetPreferredSize() const { | 239 void MenuScrollViewContainer::OnNativeThemeChanged( |
| 246 gfx::Size prefsize = scroll_view_->GetContents()->GetPreferredSize(); | 240 const ui::NativeTheme* theme) { |
| 247 gfx::Insets insets = GetInsets(); | 241 if (arrow_ == BubbleBorder::NONE) |
| 248 prefsize.Enlarge(insets.width(), insets.height()); | 242 CreateDefaultBorder(); |
| 249 return prefsize; | 243 } |
| 244 |
| 245 void MenuScrollViewContainer::OnPaintBackground(gfx::Canvas* canvas) { |
| 246 if (background()) { |
| 247 View::OnPaintBackground(canvas); |
| 248 return; |
| 249 } |
| 250 |
| 251 gfx::Rect bounds(0, 0, width(), height()); |
| 252 NativeTheme::ExtraParams extra; |
| 253 const MenuConfig& menu_config = content_view_->GetMenuItem()->GetMenuConfig(); |
| 254 extra.menu_background.corner_radius = menu_config.corner_radius; |
| 255 GetNativeTheme()->Paint(canvas->sk_canvas(), |
| 256 NativeTheme::kMenuPopupBackground, NativeTheme::kNormal, bounds, extra); |
| 250 } | 257 } |
| 251 | 258 |
| 252 void MenuScrollViewContainer::GetAccessibleState( | 259 void MenuScrollViewContainer::GetAccessibleState( |
| 253 ui::AXViewState* state) { | 260 ui::AXViewState* state) { |
| 254 // Get the name from the submenu view. | 261 // Get the name from the submenu view. |
| 255 content_view_->GetAccessibleState(state); | 262 content_view_->GetAccessibleState(state); |
| 256 | 263 |
| 257 // Now change the role. | 264 // Now change the role. |
| 258 state->role = ui::AX_ROLE_MENU_BAR; | 265 state->role = ui::AX_ROLE_MENU_BAR; |
| 259 // Some AT (like NVDA) will not process focus events on menu item children | 266 // Some AT (like NVDA) will not process focus events on menu item children |
| 260 // unless a parent claims to be focused. | 267 // unless a parent claims to be focused. |
| 261 state->AddStateFlag(ui::AX_STATE_FOCUSED); | 268 state->AddStateFlag(ui::AX_STATE_FOCUSED); |
| 262 } | 269 } |
| 263 | 270 |
| 264 void MenuScrollViewContainer::OnBoundsChanged( | 271 void MenuScrollViewContainer::OnBoundsChanged( |
| 265 const gfx::Rect& previous_bounds) { | 272 const gfx::Rect& previous_bounds) { |
| 266 gfx::Size content_pref = scroll_view_->GetContents()->GetPreferredSize(); | 273 gfx::Size content_pref = scroll_view_->GetContents()->GetPreferredSize(); |
| 267 scroll_up_button_->SetVisible(content_pref.height() > height()); | 274 scroll_up_button_->SetVisible(content_pref.height() > height()); |
| 268 scroll_down_button_->SetVisible(content_pref.height() > height()); | 275 scroll_down_button_->SetVisible(content_pref.height() > height()); |
| 269 Layout(); | 276 Layout(); |
| 270 } | 277 } |
| 271 | 278 |
| 272 void MenuScrollViewContainer::CreateDefaultBorder() { | 279 void MenuScrollViewContainer::CreateDefaultBorder() { |
| 273 arrow_ = BubbleBorder::NONE; | 280 DCHECK_EQ(arrow_, BubbleBorder::NONE); |
| 274 bubble_border_ = NULL; | 281 bubble_border_ = nullptr; |
| 275 | 282 |
| 276 const MenuConfig& menu_config = | 283 const MenuConfig& menu_config = |
| 277 content_view_->GetMenuItem()->GetMenuConfig(); | 284 content_view_->GetMenuItem()->GetMenuConfig(); |
| 278 | 285 |
| 279 int padding = menu_config.use_outer_border && menu_config.corner_radius > 0 | 286 int padding = menu_config.use_outer_border && menu_config.corner_radius > 0 |
| 280 ? kBorderPaddingDueToRoundedCorners | 287 ? kBorderPaddingDueToRoundedCorners |
| 281 : 0; | 288 : 0; |
| 282 | 289 |
| 283 int top = menu_config.menu_vertical_border_size + padding; | 290 int top = menu_config.menu_vertical_border_size + padding; |
| 284 int left = menu_config.menu_horizontal_border_size + padding; | 291 int left = menu_config.menu_horizontal_border_size + padding; |
| 285 int bottom = menu_config.menu_vertical_border_size + padding; | 292 int bottom = menu_config.menu_vertical_border_size + padding; |
| 286 int right = menu_config.menu_horizontal_border_size + padding; | 293 int right = menu_config.menu_horizontal_border_size + padding; |
| 287 | 294 |
| 288 if (menu_config.use_outer_border) { | 295 if (menu_config.use_outer_border) { |
| 296 SkColor color = GetNativeTheme() |
| 297 ? GetNativeTheme()->GetSystemColor( |
| 298 ui::NativeTheme::kColorId_MenuBorderColor) |
| 299 : gfx::kPlaceholderColor; |
| 289 SetBorder(views::Border::CreateBorderPainter( | 300 SetBorder(views::Border::CreateBorderPainter( |
| 290 new views::RoundRectPainter( | 301 new views::RoundRectPainter(color, menu_config.corner_radius), |
| 291 menu_config.native_theme->GetSystemColor( | |
| 292 ui::NativeTheme::kColorId_MenuBorderColor), | |
| 293 menu_config.corner_radius), | |
| 294 gfx::Insets(top, left, bottom, right))); | 302 gfx::Insets(top, left, bottom, right))); |
| 295 } else { | 303 } else { |
| 296 SetBorder(Border::CreateEmptyBorder(top, left, bottom, right)); | 304 SetBorder(Border::CreateEmptyBorder(top, left, bottom, right)); |
| 297 } | 305 } |
| 298 } | 306 } |
| 299 | 307 |
| 300 void MenuScrollViewContainer::CreateBubbleBorder() { | 308 void MenuScrollViewContainer::CreateBubbleBorder() { |
| 301 bubble_border_ = new BubbleBorder(arrow_, | 309 bubble_border_ = new BubbleBorder(arrow_, |
| 302 BubbleBorder::SMALL_SHADOW, | 310 BubbleBorder::SMALL_SHADOW, |
| 303 SK_ColorWHITE); | 311 SK_ColorWHITE); |
| (...skipping 11 matching lines...) Expand all Loading... |
| 315 case MENU_ANCHOR_BUBBLE_ABOVE: | 323 case MENU_ANCHOR_BUBBLE_ABOVE: |
| 316 return BubbleBorder::BOTTOM_CENTER; | 324 return BubbleBorder::BOTTOM_CENTER; |
| 317 case MENU_ANCHOR_BUBBLE_BELOW: | 325 case MENU_ANCHOR_BUBBLE_BELOW: |
| 318 return BubbleBorder::TOP_CENTER; | 326 return BubbleBorder::TOP_CENTER; |
| 319 default: | 327 default: |
| 320 return BubbleBorder::NONE; | 328 return BubbleBorder::NONE; |
| 321 } | 329 } |
| 322 } | 330 } |
| 323 | 331 |
| 324 } // namespace views | 332 } // namespace views |
| OLD | NEW |