1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
|
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "ui/views/touchui/touch_selection_menu_runner_views.h"
#include "base/strings/utf_string_conversions.h"
#include "ui/aura/window.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/base/resource/resource_bundle.h"
#include "ui/gfx/canvas.h"
#include "ui/gfx/geometry/rect.h"
#include "ui/gfx/geometry/size.h"
#include "ui/gfx/text_utils.h"
#include "ui/strings/grit/ui_strings.h"
#include "ui/views/bubble/bubble_delegate.h"
#include "ui/views/controls/button/button.h"
#include "ui/views/controls/button/label_button.h"
#include "ui/views/layout/box_layout.h"
namespace views {
namespace {
const int kMenuCommands[] = {IDS_APP_CUT, IDS_APP_COPY, IDS_APP_PASTE};
const int kSpacingBetweenButtons = 2;
const int kButtonSeparatorColor = SkColorSetARGB(13, 0, 0, 0);
const int kMenuButtonMinHeight = 38;
const int kMenuButtonMinWidth = 63;
const int kMenuMargin = 1;
const char kEllipsesButtonText[] = "...";
const int kEllipsesButtonTag = -1;
} // namespace
// A bubble that contains actions available for the selected text. An object of
// this type, as a BubbleDelegateView, manages its own lifetime.
class TouchSelectionMenuRunnerViews::Menu : public BubbleDelegateView,
public ButtonListener {
public:
// Closes the menu. This will eventually self-destroy the object.
void Close();
// Returns a new instance of Menu if there is any command available;
// otherwise, returns |nullptr|.
static Menu* Create(TouchSelectionMenuRunnerViews* owner,
ui::TouchSelectionMenuClient* client,
const gfx::Rect& anchor_rect,
const gfx::Size& handle_image_size,
aura::Window* context);
private:
Menu(TouchSelectionMenuRunnerViews* owner,
ui::TouchSelectionMenuClient* client,
const gfx::Rect& anchor_rect,
const gfx::Size& handle_image_size,
aura::Window* context);
~Menu() override;
// Queries the |client_| for what commands to show in the menu and sizes the
// menu appropriately.
void CreateButtons();
// Helper method to create a single button.
Button* CreateButton(const base::string16& title, int tag);
// BubbleDelegateView:
void OnPaint(gfx::Canvas* canvas) override;
void WindowClosing() override;
// ButtonListener:
void ButtonPressed(Button* sender, const ui::Event& event) override;
TouchSelectionMenuRunnerViews* owner_;
ui::TouchSelectionMenuClient* const client_;
DISALLOW_COPY_AND_ASSIGN(Menu);
};
TouchSelectionMenuRunnerViews::Menu*
TouchSelectionMenuRunnerViews::Menu::Create(
TouchSelectionMenuRunnerViews* owner,
ui::TouchSelectionMenuClient* client,
const gfx::Rect& anchor_rect,
const gfx::Size& handle_image_size,
aura::Window* context) {
DCHECK(client);
for (size_t i = 0; i < arraysize(kMenuCommands); i++) {
if (client->IsCommandIdEnabled(kMenuCommands[i]))
return new Menu(owner, client, anchor_rect, handle_image_size, context);
}
// No command is available, so return |nullptr|.
return nullptr;
}
TouchSelectionMenuRunnerViews::Menu::Menu(TouchSelectionMenuRunnerViews* owner,
ui::TouchSelectionMenuClient* client,
const gfx::Rect& anchor_rect,
const gfx::Size& handle_image_size,
aura::Window* context)
: BubbleDelegateView(nullptr, BubbleBorder::BOTTOM_CENTER),
owner_(owner),
client_(client) {
DCHECK(owner_);
DCHECK(client_);
set_shadow(BubbleBorder::SMALL_SHADOW);
set_parent_window(context);
set_margins(gfx::Insets(kMenuMargin, kMenuMargin, kMenuMargin, kMenuMargin));
set_can_activate(false);
set_adjust_if_offscreen(true);
EnableCanvasFlippingForRTLUI(true);
SetLayoutManager(
new BoxLayout(BoxLayout::kHorizontal, 0, 0, kSpacingBetweenButtons));
CreateButtons();
// After buttons are created, check if there is enough room between handles to
// show the menu and adjust anchor rect properly if needed, just in case the
// menu is needed to be shown under the selection.
gfx::Rect adjusted_anchor_rect(anchor_rect);
int menu_width = GetPreferredSize().width();
// TODO(mfomitchev): This assumes that the handles are center-aligned to the
// |achor_rect| edges, which is not true. We should fix this, perhaps by
// passing down the cumulative width occupied by the handles within
// |anchor_rect| plus the handle image height instead of |handle_image_size|.
// Perhaps we should also allow for some minimum padding.
if (menu_width > anchor_rect.width() - handle_image_size.width())
adjusted_anchor_rect.Inset(0, 0, 0, -handle_image_size.height());
SetAnchorRect(adjusted_anchor_rect);
BubbleDelegateView::CreateBubble(this);
GetWidget()->Show();
}
TouchSelectionMenuRunnerViews::Menu::~Menu() {
}
void TouchSelectionMenuRunnerViews::Menu::CreateButtons() {
for (size_t i = 0; i < arraysize(kMenuCommands); i++) {
int command_id = kMenuCommands[i];
if (!client_->IsCommandIdEnabled(command_id))
continue;
Button* button =
CreateButton(l10n_util::GetStringUTF16(command_id), command_id);
AddChildView(button);
}
// Finally, add ellipses button.
AddChildView(
CreateButton(base::UTF8ToUTF16(kEllipsesButtonText), kEllipsesButtonTag));
Layout();
}
Button* TouchSelectionMenuRunnerViews::Menu::CreateButton(
const base::string16& title,
int tag) {
base::string16 label =
gfx::RemoveAcceleratorChar(title, '&', nullptr, nullptr);
LabelButton* button = new LabelButton(this, label);
button->SetMinSize(gfx::Size(kMenuButtonMinWidth, kMenuButtonMinHeight));
button->SetFocusable(true);
button->set_request_focus_on_press(false);
const gfx::FontList& font_list =
ui::ResourceBundle::GetSharedInstance().GetFontList(
ui::ResourceBundle::SmallFont);
button->SetFontList(font_list);
button->SetHorizontalAlignment(gfx::ALIGN_CENTER);
button->set_tag(tag);
return button;
}
void TouchSelectionMenuRunnerViews::Menu::Close() {
// Closing the widget will self-destroy this object.
Widget* widget = GetWidget();
if (widget && !widget->IsClosed())
widget->Close();
owner_ = nullptr;
}
void TouchSelectionMenuRunnerViews::Menu::OnPaint(gfx::Canvas* canvas) {
BubbleDelegateView::OnPaint(canvas);
// Draw separator bars.
for (int i = 0; i < child_count() - 1; ++i) {
View* child = child_at(i);
int x = child->bounds().right() + kSpacingBetweenButtons / 2;
canvas->FillRect(gfx::Rect(x, 0, 1, child->height()),
kButtonSeparatorColor);
}
}
void TouchSelectionMenuRunnerViews::Menu::WindowClosing() {
DCHECK_IMPLIES(owner_, owner_->menu_ == this);
BubbleDelegateView::WindowClosing();
if (owner_)
owner_->menu_ = nullptr;
}
void TouchSelectionMenuRunnerViews::Menu::ButtonPressed(
Button* sender,
const ui::Event& event) {
Close();
if (sender->tag() != kEllipsesButtonTag)
client_->ExecuteCommand(sender->tag(), event.flags());
else
client_->RunContextMenu();
}
TouchSelectionMenuRunnerViews::TouchSelectionMenuRunnerViews()
: menu_(nullptr) {
}
TouchSelectionMenuRunnerViews::~TouchSelectionMenuRunnerViews() {
CloseMenu();
}
gfx::Rect TouchSelectionMenuRunnerViews::GetAnchorRectForTest() const {
return menu_ ? menu_->GetAnchorRect() : gfx::Rect();
}
void TouchSelectionMenuRunnerViews::OpenMenu(
ui::TouchSelectionMenuClient* client,
const gfx::Rect& anchor_rect,
const gfx::Size& handle_image_size,
aura::Window* context) {
CloseMenu();
menu_ = Menu::Create(this, client, anchor_rect, handle_image_size, context);
}
void TouchSelectionMenuRunnerViews::CloseMenu() {
if (!menu_)
return;
// Closing the menu will eventually delete the object.
menu_->Close();
menu_ = nullptr;
}
bool TouchSelectionMenuRunnerViews::IsRunning() const {
return menu_ != nullptr;
}
} // namespace views
|