submenu_button.cc 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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 "shell/browser/ui/views/submenu_button.h"
  5. #include "base/strings/string_util.h"
  6. #include "base/strings/utf_string_conversions.h"
  7. #include "ui/accessibility/ax_enums.mojom.h"
  8. #include "ui/gfx/canvas.h"
  9. #include "ui/gfx/color_utils.h"
  10. #include "ui/gfx/text_utils.h"
  11. #include "ui/views/animation/flood_fill_ink_drop_ripple.h"
  12. #include "ui/views/animation/ink_drop_host_view.h"
  13. #include "ui/views/animation/ink_drop_impl.h"
  14. #include "ui/views/controls/button/label_button_border.h"
  15. namespace electron {
  16. SubmenuButton::SubmenuButton(PressedCallback callback,
  17. const std::u16string& title,
  18. const SkColor& background_color)
  19. : views::MenuButton(callback, gfx::RemoveAccelerator(title)),
  20. background_color_(background_color) {
  21. #if defined(OS_LINUX)
  22. // Dont' use native style border.
  23. SetBorder(CreateDefaultBorder());
  24. #endif
  25. if (GetUnderlinePosition(title, &accelerator_, &underline_start_,
  26. &underline_end_))
  27. gfx::Canvas::SizeStringInt(GetText(), gfx::FontList(), &text_width_,
  28. &text_height_, 0, 0);
  29. views::InkDropHost* ink_drop = views::InkDrop::Get(this);
  30. ink_drop->SetMode(views::InkDropHost::InkDropMode::ON);
  31. ink_drop->SetBaseColor(
  32. color_utils::BlendTowardMaxContrast(background_color_, 0x81));
  33. views::InkDrop::UseInkDropForFloodFillRipple(ink_drop, false, true);
  34. }
  35. SubmenuButton::~SubmenuButton() = default;
  36. void SubmenuButton::SetAcceleratorVisibility(bool visible) {
  37. if (visible == show_underline_)
  38. return;
  39. show_underline_ = visible;
  40. SchedulePaint();
  41. }
  42. void SubmenuButton::SetUnderlineColor(SkColor color) {
  43. underline_color_ = color;
  44. }
  45. void SubmenuButton::GetAccessibleNodeData(ui::AXNodeData* node_data) {
  46. node_data->SetName(GetAccessibleName());
  47. node_data->role = ax::mojom::Role::kPopUpButton;
  48. }
  49. void SubmenuButton::PaintButtonContents(gfx::Canvas* canvas) {
  50. views::MenuButton::PaintButtonContents(canvas);
  51. if (show_underline_ && (underline_start_ != underline_end_)) {
  52. float padding = (width() - text_width_) / 2;
  53. float underline_height = (height() + text_height_) / 2 - 2;
  54. canvas->DrawSharpLine(
  55. gfx::PointF(underline_start_ + padding, underline_height),
  56. gfx::PointF(underline_end_ + padding, underline_height),
  57. underline_color_);
  58. }
  59. }
  60. bool SubmenuButton::GetUnderlinePosition(const std::u16string& text,
  61. char16_t* accelerator,
  62. int* start,
  63. int* end) const {
  64. int pos, span;
  65. std::u16string trimmed =
  66. gfx::LocateAndRemoveAcceleratorChar(text, &pos, &span);
  67. if (pos > -1 && span != 0) {
  68. *accelerator = base::ToUpperASCII(trimmed[pos]);
  69. GetCharacterPosition(trimmed, pos, start);
  70. GetCharacterPosition(trimmed, pos + span, end);
  71. return true;
  72. }
  73. return false;
  74. }
  75. void SubmenuButton::GetCharacterPosition(const std::u16string& text,
  76. int index,
  77. int* pos) const {
  78. int height = 0;
  79. gfx::Canvas::SizeStringInt(text.substr(0, index), gfx::FontList(), pos,
  80. &height, 0, 0);
  81. }
  82. } // namespace electron