summaryrefslogtreecommitdiffstats
path: root/ui/views/mus/input_method_mus.cc
blob: cee5ea671f75fb533180b4b4336adc23234a63ba (plain)
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
// Copyright (c) 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/mus/input_method_mus.h"

#include <utility>

#include "components/mus/public/cpp/window.h"
#include "mojo/converters/ime/ime_type_converters.h"
#include "ui/base/ime/text_input_client.h"
#include "ui/events/event.h"
#include "ui/mojo/ime/text_input_state.mojom.h"

namespace views {

////////////////////////////////////////////////////////////////////////////////
// InputMethodMUS, public:

InputMethodMUS::InputMethodMUS(ui::internal::InputMethodDelegate* delegate,
                               mus::Window* window)
    : window_(window) {
  SetDelegate(delegate);
}

InputMethodMUS::~InputMethodMUS() {}

////////////////////////////////////////////////////////////////////////////////
// InputMethodMUS, ui::InputMethod implementation:

void InputMethodMUS::OnFocus() {
  InputMethodBase::OnFocus();
  UpdateTextInputType();
}

void InputMethodMUS::OnBlur() {
  InputMethodBase::OnBlur();
  UpdateTextInputType();
}

bool InputMethodMUS::OnUntranslatedIMEMessage(const base::NativeEvent& event,
                                              NativeEventResult* result) {
  return false;
}

void InputMethodMUS::DispatchKeyEvent(ui::KeyEvent* event) {
  DCHECK(event->type() == ui::ET_KEY_PRESSED ||
         event->type() == ui::ET_KEY_RELEASED);

  // If no text input client, do nothing.
  if (!GetTextInputClient()) {
    ignore_result(DispatchKeyEventPostIME(event));
    return;
  }

  // Here is where we change the differ from our base class's logic. Instead of
  // always dispatching a key down event, and then sending a synthesized
  // character event, we instead check to see if this is a character event and
  // send out the key if it is. (We fallback to normal dispatch if it isn't.)
  if (event->is_char()) {
    GetTextInputClient()->InsertChar(*event);
    event->StopPropagation();
    return;
  }

  ignore_result(DispatchKeyEventPostIME(event));
}

void InputMethodMUS::OnTextInputTypeChanged(const ui::TextInputClient* client) {
  if (IsTextInputClientFocused(client))
    UpdateTextInputType();
  InputMethodBase::OnTextInputTypeChanged(client);
}

void InputMethodMUS::OnCaretBoundsChanged(const ui::TextInputClient* client) {}

void InputMethodMUS::CancelComposition(const ui::TextInputClient* client) {}

void InputMethodMUS::OnInputLocaleChanged() {}

std::string InputMethodMUS::GetInputLocale() {
  return "";
}

bool InputMethodMUS::IsCandidatePopupOpen() const {
  return false;
}

void InputMethodMUS::OnDidChangeFocusedClient(
    ui::TextInputClient* focused_before,
    ui::TextInputClient* focused) {
  InputMethodBase::OnDidChangeFocusedClient(focused_before, focused);
  UpdateTextInputType();
}

void InputMethodMUS::UpdateTextInputType() {
  ui::TextInputType type = GetTextInputType();
  mojo::TextInputStatePtr state = mojo::TextInputState::New();
  state->type = mojo::ConvertTo<mojo::TextInputType>(type);
  if (type != ui::TEXT_INPUT_TYPE_NONE)
    window_->SetImeVisibility(true, std::move(state));
  else
    window_->SetTextInputState(std::move(state));
}

}  // namespace views