File: ime_controller_client_impl.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (251 lines) | stat: -rw-r--r-- 8,606 bytes parent folder | download | duplicates (6)
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
250
251
// Copyright 2017 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/ui/ash/input_method/ime_controller_client_impl.h"

#include <memory>
#include <vector>

#include "base/check_op.h"
#include "base/strings/utf_string_conversions.h"
#include "ui/base/ime/ash/extension_ime_util.h"
#include "ui/base/ime/ash/ime_bridge.h"
#include "ui/base/ime/ash/ime_keyboard.h"
#include "ui/base/ime/ash/input_method_descriptor.h"
#include "ui/base/ime/ash/input_method_util.h"

namespace {

using ::ash::input_method::InputMethodDescriptor;
using ::ash::input_method::InputMethodManager;
using ::ash::input_method::InputMethodUtil;
using ::ui::ime::InputMethodMenuManager;

ImeControllerClientImpl* g_ime_controller_client_instance = nullptr;

}  // namespace

ImeControllerClientImpl::ImeControllerClientImpl(InputMethodManager* manager)
    : input_method_manager_(manager) {
  DCHECK(input_method_manager_);
  input_method_manager_->AddObserver(this);
  input_method_manager_->AddImeMenuObserver(this);
  if (input_method_manager_->GetImeKeyboard()) {
    observation_.Observe(input_method_manager_->GetImeKeyboard());
  }
  InputMethodMenuManager::GetInstance()->AddObserver(this);

  // This does not need to send the initial state to ash because that happens
  // via observers when the InputMethodManager initializes its list of IMEs.

  DCHECK(!g_ime_controller_client_instance);
  g_ime_controller_client_instance = this;
}

ImeControllerClientImpl::~ImeControllerClientImpl() {
  ime_controller_->SetClient(nullptr);
  DCHECK_EQ(this, g_ime_controller_client_instance);
  g_ime_controller_client_instance = nullptr;

  InputMethodMenuManager::GetInstance()->RemoveObserver(this);
  input_method_manager_->RemoveImeMenuObserver(this);
  input_method_manager_->RemoveObserver(this);
}

void ImeControllerClientImpl::Init() {
  // Connect to the controller in ash.
  ime_controller_ = ash::ImeController::Get();
  DCHECK(ime_controller_);
  InitAndSetClient();
}

// static
ImeControllerClientImpl* ImeControllerClientImpl::Get() {
  return g_ime_controller_client_instance;
}

void ImeControllerClientImpl::SetImesManagedByPolicy(bool managed) {
  ime_controller_->SetImesManagedByPolicy(managed);
}

// ash::mojom::ImeControllerClient:
void ImeControllerClientImpl::SwitchToNextIme() {
  InputMethodManager::State* state =
      input_method_manager_->GetActiveIMEState().get();
  if (state) {
    state->SwitchToNextInputMethod();
  }
}

void ImeControllerClientImpl::SwitchToLastUsedIme() {
  InputMethodManager::State* state =
      input_method_manager_->GetActiveIMEState().get();
  if (state) {
    state->SwitchToLastUsedInputMethod();
  }
}

void ImeControllerClientImpl::SwitchImeById(const std::string& id,
                                            bool show_message) {
  InputMethodManager::State* state =
      input_method_manager_->GetActiveIMEState().get();
  if (state) {
    state->ChangeInputMethod(id, show_message);
  }
}

void ImeControllerClientImpl::ActivateImeMenuItem(const std::string& key) {
  input_method_manager_->ActivateInputMethodMenuItem(key);
}

void ImeControllerClientImpl::SetCapsLockEnabled(bool caps_enabled) {
  ash::input_method::ImeKeyboard* keyboard =
      InputMethodManager::Get()->GetImeKeyboard();
  if (keyboard) {
    keyboard->SetCapsLockEnabled(caps_enabled);
  }
}

void ImeControllerClientImpl::OverrideKeyboardKeyset(
    ash::input_method::ImeKeyset keyset,
    OverrideKeyboardKeysetCallback callback) {
  input_method_manager_->OverrideKeyboardKeyset(keyset);
  std::move(callback).Run();
}

void ImeControllerClientImpl::ShowModeIndicator() {
  // Get the short name of the changed input method (e.g. US, JA, etc.)
  const InputMethodDescriptor descriptor =
      input_method_manager_->GetActiveIMEState()->GetCurrentInputMethod();
  const std::u16string short_name = descriptor.GetIndicator();

  ash::IMECandidateWindowHandlerInterface* cw_handler =
      ash::IMEBridge::Get()->GetCandidateWindowHandler();
  if (!cw_handler) {
    return;
  }

  gfx::Rect anchor_bounds = cw_handler->GetCursorBounds();
  if (anchor_bounds == gfx::Rect()) {
    // TODO(shuchen): Show the mode indicator in the right bottom of the
    // display when the launch bar is hidden and the focus is out.  To
    // implement it, we should consider to use message center or system
    // notification.  Note, launch bar can be vertical and can be placed
    // right/left side of display.
    return;
  }

  // Call to Ash to show the mode indicator view with the given anchor
  // bounds and short name.
  ime_controller_->ShowModeIndicator(anchor_bounds, short_name);
}

// ash::input_method::InputMethodManager::Observer:
void ImeControllerClientImpl::InputMethodChanged(InputMethodManager* manager,
                                                 Profile* profile,
                                                 bool show_message) {
  RefreshIme();
  if (show_message) {
    ShowModeIndicator();
  }
}

// ash::input_method::InputMethodManager::ImeMenuObserver:
void ImeControllerClientImpl::ImeMenuActivationChanged(bool is_active) {
  ime_controller_->ShowImeMenuOnShelf(is_active);
}

void ImeControllerClientImpl::ImeMenuListChanged() {
  RefreshIme();
}

void ImeControllerClientImpl::ImeMenuItemsChanged(
    const std::string& engine_id,
    const std::vector<InputMethodManager::MenuItem>& items) {}

// ui::ime::InputMethodMenuManager::Observer:
void ImeControllerClientImpl::InputMethodMenuItemChanged(
    InputMethodMenuManager* manager) {
  RefreshIme();
}

// ash::input_method::ImeKeyboard::Observer:
void ImeControllerClientImpl::OnCapsLockChanged(bool enabled) {
  ime_controller_->UpdateCapsLockState(enabled);
}

void ImeControllerClientImpl::OnLayoutChanging(const std::string& layout_name) {
  ime_controller_->OnKeyboardLayoutNameChanged(layout_name);
}

void ImeControllerClientImpl::InitAndSetClient() {
  ime_controller_->SetClient(this);

  // Now that the client is set, flush state from observed objects to
  // the ImeController, now that it will hear it.
  input_method_manager_->NotifyObserversImeExtraInputStateChange();
  if (const ash::input_method::ImeKeyboard* keyboard =
          input_method_manager_->GetImeKeyboard()) {
    ime_controller_->OnKeyboardLayoutNameChanged(
        keyboard->GetCurrentKeyboardLayoutName());
  }
}

ash::ImeInfo ImeControllerClientImpl::GetAshImeInfo(
    const InputMethodDescriptor& ime) const {
  InputMethodUtil* util = input_method_manager_->GetInputMethodUtil();
  ash::ImeInfo info;
  info.id = ime.id();
  info.name = util->GetInputMethodLongName(ime);
  info.short_name = ime.GetIndicator();
  info.third_party = ash::extension_ime_util::IsExtensionIME(ime.id());
  return info;
}

void ImeControllerClientImpl::RefreshIme() {
  InputMethodManager::State* state =
      input_method_manager_->GetActiveIMEState().get();
  if (!state) {
    const std::string empty_ime_id;
    ime_controller_->RefreshIme(empty_ime_id, std::vector<ash::ImeInfo>(),
                                std::vector<ash::ImeMenuItem>());
    return;
  }

  const std::string current_ime_id = state->GetCurrentInputMethod().id();

  std::vector<ash::ImeInfo> available_imes;
  std::vector<InputMethodDescriptor> enabled_ime_descriptors =
      state->GetEnabledInputMethodsSortedByLocalizedDisplayNames();
  for (const InputMethodDescriptor& descriptor : enabled_ime_descriptors) {
    ash::ImeInfo info = GetAshImeInfo(descriptor);
    available_imes.push_back(std::move(info));
  }

  std::vector<ash::ImeMenuItem> ash_menu_items;
  ui::ime::InputMethodMenuItemList menu_list =
      ui::ime::InputMethodMenuManager::GetInstance()
          ->GetCurrentInputMethodMenuItemList();
  for (const ui::ime::InputMethodMenuItem& menu_item : menu_list) {
    ash::ImeMenuItem ash_item;
    ash_item.key = menu_item.key;
    ash_item.label = base::UTF8ToUTF16(menu_item.label);
    ash_item.checked = menu_item.is_selection_item_checked;
    ash_menu_items.push_back(std::move(ash_item));
  }
  ime_controller_->RefreshIme(current_ime_id, std::move(available_imes),
                              std::move(ash_menu_items));
}

void ImeControllerClientImpl::OnExtraInputEnabledStateChange(
    bool is_extra_input_options_enabled,
    bool is_emoji_enabled,
    bool is_handwriting_enabled,
    bool is_voice_enabled) {
  if (ime_controller_) {
    ime_controller_->SetExtraInputOptionsEnabledState(
        is_extra_input_options_enabled, is_emoji_enabled,
        is_handwriting_enabled, is_voice_enabled);
  }
}