submenu_button.cc 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. // Copyright (c) 2014 GitHub, Inc.
  2. // Use of this source code is governed by the MIT license that can be
  3. // found in the LICENSE file.
  4. #include "atom/browser/ui/views/submenu_button.h"
  5. #include "base/strings/string_util.h"
  6. #include "base/strings/utf_string_conversions.h"
  7. #include "ui/gfx/canvas.h"
  8. #include "ui/gfx/color_utils.h"
  9. #include "ui/gfx/text_utils.h"
  10. #include "ui/views/animation/flood_fill_ink_drop_ripple.h"
  11. #include "ui/views/animation/ink_drop_host_view.h"
  12. #include "ui/views/animation/ink_drop_impl.h"
  13. #include "ui/views/controls/button/label_button_border.h"
  14. namespace atom {
  15. SubmenuButton::SubmenuButton(const base::string16& title,
  16. views::MenuButtonListener* menu_button_listener,
  17. const SkColor& background_color)
  18. : views::MenuButton(gfx::RemoveAcceleratorChar(title, '&', NULL, NULL),
  19. menu_button_listener,
  20. false),
  21. background_color_(background_color) {
  22. #if defined(OS_LINUX)
  23. // Dont' use native style border.
  24. SetBorder(CreateDefaultBorder());
  25. #endif
  26. if (GetUnderlinePosition(title, &accelerator_, &underline_start_,
  27. &underline_end_))
  28. gfx::Canvas::SizeStringInt(GetText(), gfx::FontList(), &text_width_,
  29. &text_height_, 0, 0);
  30. SetInkDropMode(InkDropMode::ON);
  31. set_ink_drop_base_color(
  32. color_utils::BlendTowardOppositeLuma(background_color_, 0x61));
  33. }
  34. SubmenuButton::~SubmenuButton() {}
  35. std::unique_ptr<views::InkDropRipple> SubmenuButton::CreateInkDropRipple()
  36. const {
  37. std::unique_ptr<views::InkDropRipple> ripple(
  38. new views::FloodFillInkDropRipple(
  39. size(), GetInkDropCenterBasedOnLastEvent(), GetInkDropBaseColor(),
  40. ink_drop_visible_opacity()));
  41. return ripple;
  42. }
  43. std::unique_ptr<views::InkDrop> SubmenuButton::CreateInkDrop() {
  44. std::unique_ptr<views::InkDropImpl> ink_drop =
  45. views::Button::CreateDefaultInkDropImpl();
  46. ink_drop->SetShowHighlightOnHover(false);
  47. return std::move(ink_drop);
  48. }
  49. void SubmenuButton::SetAcceleratorVisibility(bool visible) {
  50. if (visible == show_underline_)
  51. return;
  52. show_underline_ = visible;
  53. SchedulePaint();
  54. }
  55. void SubmenuButton::SetUnderlineColor(SkColor color) {
  56. underline_color_ = color;
  57. }
  58. void SubmenuButton::PaintButtonContents(gfx::Canvas* canvas) {
  59. views::MenuButton::PaintButtonContents(canvas);
  60. if (show_underline_ && (underline_start_ != underline_end_)) {
  61. int padding = (width() - text_width_) / 2;
  62. int underline_height = (height() + text_height_) / 2 - 2;
  63. canvas->DrawLine(gfx::Point(underline_start_ + padding, underline_height),
  64. gfx::Point(underline_end_ + padding, underline_height),
  65. underline_color_);
  66. }
  67. }
  68. bool SubmenuButton::GetUnderlinePosition(const base::string16& text,
  69. base::char16* accelerator,
  70. int* start,
  71. int* end) const {
  72. int pos, span;
  73. base::string16 trimmed = gfx::RemoveAcceleratorChar(text, '&', &pos, &span);
  74. if (pos > -1 && span != 0) {
  75. *accelerator = base::ToUpperASCII(trimmed[pos]);
  76. GetCharacterPosition(trimmed, pos, start);
  77. GetCharacterPosition(trimmed, pos + span, end);
  78. return true;
  79. }
  80. return false;
  81. }
  82. void SubmenuButton::GetCharacterPosition(const base::string16& text,
  83. int index,
  84. int* pos) const {
  85. int height = 0;
  86. gfx::Canvas::SizeStringInt(text.substr(0, index), gfx::FontList(), pos,
  87. &height, 0, 0);
  88. }
  89. } // namespace atom