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.
5 #include "atom/browser/ui/views/submenu_button.h"
7 #include "base/strings/string_util.h"
8 #include "base/strings/utf_string_conversions.h"
9 #include "ui/gfx/canvas.h"
10 #include "ui/gfx/color_utils.h"
11 #include "ui/gfx/text_utils.h"
12 #include "ui/views/animation/flood_fill_ink_drop_ripple.h"
13 #include "ui/views/animation/ink_drop_impl.h"
14 #include "ui/views/animation/ink_drop_host_view.h"
15 #include "ui/views/controls/button/label_button_border.h"
21 // Filter out the "&" in menu label.
22 base::string16 FilterAccelerator(const base::string16& label) {
24 base::RemoveChars(label, base::ASCIIToUTF16("&").c_str(), &out);
30 SubmenuButton::SubmenuButton(const base::string16& title,
31 views::MenuButtonListener* menu_button_listener,
32 const SkColor& background_color)
33 : views::MenuButton(FilterAccelerator(title),
34 menu_button_listener, false),
36 show_underline_(false),
41 underline_color_(SK_ColorBLACK),
42 background_color_(background_color) {
44 // Dont' use native style border.
45 SetBorder(std::move(CreateDefaultBorder()));
48 if (GetUnderlinePosition(title, &accelerator_, &underline_start_,
50 gfx::Canvas::SizeStringInt(GetText(), GetFontList(), &text_width_,
53 SetInkDropMode(InkDropMode::ON);
54 set_ink_drop_base_color(
55 color_utils::BlendTowardOppositeLuma(background_color_, 0x61));
58 SubmenuButton::~SubmenuButton() {
61 std::unique_ptr<views::InkDropRipple> SubmenuButton::CreateInkDropRipple()
63 std::unique_ptr<views::InkDropRipple> ripple(
64 new views::FloodFillInkDropRipple(
66 GetInkDropCenterBasedOnLastEvent(),
67 GetInkDropBaseColor(),
68 ink_drop_visible_opacity()));
72 std::unique_ptr<views::InkDrop> SubmenuButton::CreateInkDrop() {
73 std::unique_ptr<views::InkDropImpl> ink_drop =
74 CustomButton::CreateDefaultInkDropImpl();
75 ink_drop->SetShowHighlightOnHover(false);
76 return std::move(ink_drop);
79 void SubmenuButton::SetAcceleratorVisibility(bool visible) {
80 if (visible == show_underline_)
83 show_underline_ = visible;
87 void SubmenuButton::SetUnderlineColor(SkColor color) {
88 underline_color_ = color;
91 void SubmenuButton::OnPaint(gfx::Canvas* canvas) {
92 views::MenuButton::OnPaint(canvas);
94 if (show_underline_ && (underline_start_ != underline_end_)) {
95 int padding = (width() - text_width_) / 2;
96 int underline_height = (height() + text_height_) / 2 - 2;
97 canvas->DrawLine(gfx::Point(underline_start_ + padding, underline_height),
98 gfx::Point(underline_end_ + padding, underline_height),
103 bool SubmenuButton::GetUnderlinePosition(const base::string16& text,
104 base::char16* accelerator,
105 int* start, int* end) {
107 base::string16 trimmed = gfx::RemoveAcceleratorChar(text, '&', &pos, &span);
108 if (pos > -1 && span != 0) {
109 *accelerator = base::ToUpperASCII(trimmed[pos]);
110 GetCharacterPosition(trimmed, pos, start);
111 GetCharacterPosition(trimmed, pos + span, end);
118 void SubmenuButton::GetCharacterPosition(
119 const base::string16& text, int index, int* pos) {
121 gfx::Canvas::SizeStringInt(text.substr(0, index), GetFontList(), pos, &height,