submenu_button.cc 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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 "ui/accessibility/ax_enums.mojom.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_highlight.h"
  12. #include "ui/views/animation/ink_drop_host.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(std::move(callback), gfx::RemoveAccelerator(title)),
  20. background_color_(background_color) {
  21. #if BUILDFLAG(IS_LINUX)
  22. // Dont' use native style border.
  23. SetBorder(CreateDefaultBorder());
  24. #endif
  25. SetAccessibleRole(ax::mojom::Role::kPopUpButton);
  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. views::InkDropHost* ink_drop = views::InkDrop::Get(this);
  31. ink_drop->SetMode(views::InkDropHost::InkDropMode::ON);
  32. ink_drop->SetBaseColor(
  33. color_utils::BlendTowardMaxContrast(background_color_, 0x81));
  34. views::InkDrop::UseInkDropForFloodFillRipple(ink_drop, false, true);
  35. }
  36. SubmenuButton::~SubmenuButton() = default;
  37. void SubmenuButton::SetAcceleratorVisibility(bool visible) {
  38. if (visible == show_underline_)
  39. return;
  40. show_underline_ = visible;
  41. SchedulePaint();
  42. }
  43. void SubmenuButton::SetUnderlineColor(SkColor color) {
  44. underline_color_ = color;
  45. }
  46. void SubmenuButton::PaintButtonContents(gfx::Canvas* canvas) {
  47. views::MenuButton::PaintButtonContents(canvas);
  48. if (show_underline_ && (underline_start_ != underline_end_)) {
  49. float padding = (width() - text_width_) / 2;
  50. float underline_height = (height() + text_height_) / 2 - 2;
  51. canvas->DrawSharpLine(
  52. gfx::PointF(underline_start_ + padding, underline_height),
  53. gfx::PointF(underline_end_ + padding, underline_height),
  54. underline_color_);
  55. }
  56. }
  57. bool SubmenuButton::GetUnderlinePosition(const std::u16string& text,
  58. char16_t* accelerator,
  59. int* start,
  60. int* end) const {
  61. int pos, span;
  62. std::u16string trimmed =
  63. gfx::LocateAndRemoveAcceleratorChar(text, &pos, &span);
  64. if (pos > -1 && span != 0) {
  65. *accelerator = base::ToUpperASCII(trimmed[pos]);
  66. GetCharacterPosition(trimmed, pos, start);
  67. GetCharacterPosition(trimmed, pos + span, end);
  68. return true;
  69. }
  70. return false;
  71. }
  72. void SubmenuButton::GetCharacterPosition(const std::u16string& text,
  73. int index,
  74. int* pos) const {
  75. int height = 0;
  76. gfx::Canvas::SizeStringInt(text.substr(0, index), gfx::FontList(), pos,
  77. &height, 0, 0);
  78. }
  79. BEGIN_METADATA(SubmenuButton)
  80. END_METADATA
  81. } // namespace electron