1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef UI_BASE_IME_INPUT_METHOD_CHROMEOS_H_
6 #define UI_BASE_IME_INPUT_METHOD_CHROMEOS_H_
11 #include "base/basictypes.h"
12 #include "base/compiler_specific.h"
13 #include "base/memory/scoped_ptr.h"
14 #include "base/memory/weak_ptr.h"
15 #include "ui/base/ime/chromeos/character_composer.h"
16 #include "ui/base/ime/chromeos/ime_bridge.h"
17 #include "ui/base/ime/composition_text.h"
18 #include "ui/base/ime/input_method_base.h"
22 // A ui::InputMethod implementation based on IBus.
23 class UI_BASE_IME_EXPORT InputMethodChromeOS
24 : public InputMethodBase
,
25 public chromeos::IMEInputContextHandlerInterface
{
27 explicit InputMethodChromeOS(internal::InputMethodDelegate
* delegate
);
28 ~InputMethodChromeOS() override
;
30 // Overridden from InputMethod:
31 void OnFocus() override
;
32 void OnBlur() override
;
33 bool OnUntranslatedIMEMessage(const base::NativeEvent
& event
,
34 NativeEventResult
* result
) override
;
35 bool DispatchKeyEvent(const ui::KeyEvent
& event
) override
;
36 void OnTextInputTypeChanged(const TextInputClient
* client
) override
;
37 void OnCaretBoundsChanged(const TextInputClient
* client
) override
;
38 void CancelComposition(const TextInputClient
* client
) override
;
39 void OnInputLocaleChanged() override
;
40 std::string
GetInputLocale() override
;
41 bool IsActive() override
;
42 bool IsCandidatePopupOpen() const override
;
45 // Converts |text| into CompositionText.
46 void ExtractCompositionText(const chromeos::CompositionText
& text
,
47 uint32 cursor_position
,
48 CompositionText
* out_composition
) const;
50 // Process a key returned from the input method.
51 virtual void ProcessKeyEventPostIME(const ui::KeyEvent
& event
,
54 // Resets context and abandon all pending results and key events.
58 class PendingKeyEvent
;
60 // Overridden from InputMethodBase:
61 void OnWillChangeFocusedClient(TextInputClient
* focused_before
,
62 TextInputClient
* focused
) override
;
63 void OnDidChangeFocusedClient(TextInputClient
* focused_before
,
64 TextInputClient
* focused
) override
;
66 // Asks the client to confirm current composition text.
67 void ConfirmCompositionText();
69 // Checks the availability of focused text input client and update focus
71 void UpdateContextFocusState();
73 // Processes a key event that was already filtered by the input method.
74 // A VKEY_PROCESSKEY may be dispatched to the focused View.
75 void ProcessFilteredKeyPressEvent(const ui::KeyEvent
& event
);
77 // Processes a key event that was not filtered by the input method.
78 void ProcessUnfilteredKeyPressEvent(const ui::KeyEvent
& event
);
80 // Sends input method result caused by the given key event to the focused text
82 void ProcessInputMethodResult(const ui::KeyEvent
& event
, bool filtered
);
84 // Checks if the pending input method result needs inserting into the focused
85 // text input client as a single character.
86 bool NeedInsertChar() const;
88 // Checks if there is pending input method result.
89 bool HasInputMethodResult() const;
91 // Sends a fake key event for IME composing without physical key events.
92 void SendFakeProcessKeyEvent(bool pressed
) const;
94 // Passes keyevent and executes character composition if necessary. Returns
95 // true if character composer comsumes key event.
96 bool ExecuteCharacterComposer(const ui::KeyEvent
& event
);
98 // chromeos::IMEInputContextHandlerInterface overrides:
99 void CommitText(const std::string
& text
) override
;
100 void UpdateCompositionText(const chromeos::CompositionText
& text
,
102 bool visible
) override
;
103 void DeleteSurroundingText(int32 offset
, uint32 length
) override
;
105 // Hides the composition text.
106 void HidePreeditText();
108 // Callback function for IMEEngineHandlerInterface::ProcessKeyEvent.
109 void ProcessKeyEventDone(const ui::KeyEvent
* event
, bool is_handled
);
111 // Returns whether an non-password input field is focused.
112 bool IsNonPasswordInputFieldFocused();
114 // Returns true if an text input field is focused.
115 bool IsInputFieldFocused();
117 // Pending composition text generated by the current pending key event.
118 // It'll be sent to the focused text input client as soon as we receive the
119 // processing result of the pending key event.
120 CompositionText composition_
;
122 // Pending result text generated by the current pending key event.
123 // It'll be sent to the focused text input client as soon as we receive the
124 // processing result of the pending key event.
125 base::string16 result_text_
;
127 base::string16 previous_surrounding_text_
;
128 gfx::Range previous_selection_range_
;
130 // Indicates if there is an ongoing composition text.
131 bool composing_text_
;
133 // Indicates if the composition text is changed or deleted.
134 bool composition_changed_
;
136 // An object to compose a character from a sequence of key presses
137 // including dead key etc.
138 CharacterComposer character_composer_
;
140 // Used for making callbacks.
141 base::WeakPtrFactory
<InputMethodChromeOS
> weak_ptr_factory_
;
143 DISALLOW_COPY_AND_ASSIGN(InputMethodChromeOS
);
148 #endif // UI_BASE_IME_INPUT_METHOD_CHROMEOS_H_