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
|
// Copyright 2018 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "ui/views/controls/views_text_services_context_menu.h"
#import <Cocoa/Cocoa.h>
#include "ui/base/l10n/l10n_util.h"
#include "ui/base/resource/resource_bundle.h"
#include "ui/gfx/decorated_text.h"
#import "ui/gfx/decorated_text_mac.h"
#include "ui/menus/cocoa/text_services_context_menu.h"
#include "ui/menus/simple_menu_model.h"
#include "ui/resources/grit/ui_resources.h"
#include "ui/strings/grit/ui_strings.h"
#include "ui/views/controls/textfield/textfield.h"
#include "ui/views/controls/views_text_services_context_menu_base.h"
#include "ui/views/widget/widget.h"
namespace views {
namespace {
// This class serves as a bridge to TextServicesContextMenu to add and handle
// text service items in the context menu. The items include Speech, Look Up
// and BiDi.
class ViewsTextServicesContextMenuMac
: public ViewsTextServicesContextMenuBase,
public ui::TextServicesContextMenu::Delegate {
public:
ViewsTextServicesContextMenuMac(ui::SimpleMenuModel* menu, Textfield* client);
ViewsTextServicesContextMenuMac(const ViewsTextServicesContextMenuMac&) =
delete;
ViewsTextServicesContextMenuMac& operator=(
const ViewsTextServicesContextMenuMac&) = delete;
~ViewsTextServicesContextMenuMac() override = default;
// ViewsTextServicesContextMenu:
bool IsCommandIdChecked(int command_id) const override;
bool IsCommandIdEnabled(int command_id) const override;
void ExecuteCommand(int command_id, int event_flags) override;
bool SupportsCommand(int command_id) const override;
// TextServicesContextMenu::Delegate:
std::u16string_view GetSelectedText() const override;
bool IsTextDirectionEnabled(
base::i18n::TextDirection direction) const override;
bool IsTextDirectionChecked(
base::i18n::TextDirection direction) const override;
void UpdateTextDirection(base::i18n::TextDirection direction) override;
private:
// Handler for the "Look Up" menu item.
void LookUpInDictionary();
ui::TextServicesContextMenu text_services_menu_{this};
};
ViewsTextServicesContextMenuMac::ViewsTextServicesContextMenuMac(
ui::SimpleMenuModel* menu,
Textfield* client)
: ViewsTextServicesContextMenuBase(menu, client) {
// Insert the "Look up" item in the first position.
const std::u16string_view text = GetSelectedText();
if (!text.empty()) {
menu->InsertSeparatorAt(0, ui::NORMAL_SEPARATOR);
menu->InsertItemAt(0, IDS_CONTENT_CONTEXT_LOOK_UP,
l10n_util::GetStringFUTF16(IDS_CONTENT_CONTEXT_LOOK_UP,
std::u16string(text)));
text_services_menu_.AppendToContextMenu(menu);
}
text_services_menu_.AppendEditableItems(menu);
}
bool ViewsTextServicesContextMenuMac::IsCommandIdChecked(int command_id) const {
return text_services_menu_.SupportsCommand(command_id)
? text_services_menu_.IsCommandIdChecked(command_id)
: ViewsTextServicesContextMenuBase::IsCommandIdChecked(command_id);
}
bool ViewsTextServicesContextMenuMac::IsCommandIdEnabled(int command_id) const {
if (text_services_menu_.SupportsCommand(command_id)) {
return text_services_menu_.IsCommandIdEnabled(command_id);
}
return (command_id == IDS_CONTENT_CONTEXT_LOOK_UP) ||
ViewsTextServicesContextMenuBase::IsCommandIdEnabled(command_id);
}
void ViewsTextServicesContextMenuMac::ExecuteCommand(int command_id,
int event_flags) {
if (text_services_menu_.SupportsCommand(command_id)) {
text_services_menu_.ExecuteCommand(command_id, event_flags);
} else if (command_id == IDS_CONTENT_CONTEXT_LOOK_UP) {
LookUpInDictionary();
} else {
ViewsTextServicesContextMenuBase::ExecuteCommand(command_id, event_flags);
}
}
bool ViewsTextServicesContextMenuMac::SupportsCommand(int command_id) const {
return text_services_menu_.SupportsCommand(command_id) ||
command_id == IDS_CONTENT_CONTEXT_LOOK_UP ||
ViewsTextServicesContextMenuBase::SupportsCommand(command_id);
}
std::u16string_view ViewsTextServicesContextMenuMac::GetSelectedText() const {
return (client()->GetTextInputType() == ui::TEXT_INPUT_TYPE_PASSWORD)
? std::u16string_view()
: client()->GetSelectedText();
}
bool ViewsTextServicesContextMenuMac::IsTextDirectionEnabled(
base::i18n::TextDirection direction) const {
if (client()->force_text_directionality()) {
return false;
}
return direction != base::i18n::UNKNOWN_DIRECTION;
}
bool ViewsTextServicesContextMenuMac::IsTextDirectionChecked(
base::i18n::TextDirection direction) const {
if (client()->force_text_directionality()) {
return direction == base::i18n::UNKNOWN_DIRECTION;
}
return IsTextDirectionEnabled(direction) &&
client()->GetTextDirection() == direction;
}
void ViewsTextServicesContextMenuMac::UpdateTextDirection(
base::i18n::TextDirection direction) {
DCHECK(IsTextDirectionEnabled(direction));
client()->ChangeTextDirectionAndLayoutAlignment(direction);
}
void ViewsTextServicesContextMenuMac::LookUpInDictionary() {
gfx::DecoratedText text;
gfx::Rect rect;
if (client()->GetWordLookupDataFromSelection(&text, &rect)) {
Widget* widget = client()->GetWidget();
// We only care about the baseline of the glyph, not the space it occupies.
gfx::Point baseline_point = rect.origin();
views::View::ConvertPointToTarget(client(), widget->GetRootView(),
&baseline_point);
NSView* view = widget->GetNativeView().GetNativeNSView();
NSPoint lookup_point = NSMakePoint(
baseline_point.x(), NSHeight([view frame]) - baseline_point.y());
[view showDefinitionForAttributedString:
gfx::GetAttributedStringFromDecoratedText(text)
atPoint:lookup_point];
}
}
} // namespace
// static
std::unique_ptr<ViewsTextServicesContextMenu>
ViewsTextServicesContextMenu::Create(ui::SimpleMenuModel* menu,
Textfield* client) {
return std::make_unique<ViewsTextServicesContextMenuMac>(menu, client);
}
} // namespace views
|