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 // Abandons all pending key events. It usually happends when we lose keyboard
95 // focus, the text input type is changed or we are destroyed.
96 void AbandonAllPendingKeyEvents();
98 // Passes keyevent and executes character composition if necessary. Returns
99 // true if character composer comsumes key event.
100 bool ExecuteCharacterComposer(const ui::KeyEvent
& event
);
102 // chromeos::IMEInputContextHandlerInterface overrides:
103 void CommitText(const std::string
& text
) override
;
104 void UpdateCompositionText(const chromeos::CompositionText
& text
,
106 bool visible
) override
;
107 void DeleteSurroundingText(int32 offset
, uint32 length
) override
;
109 // Hides the composition text.
110 void HidePreeditText();
112 // Callback function for IMEEngineHandlerInterface::ProcessKeyEvent.
113 void ProcessKeyEventDone(uint32 id
, ui::KeyEvent
* event
, bool is_handled
);
115 // Returns whether an non-password input field is focused.
116 bool IsNonPasswordInputFieldFocused();
118 // Returns true if an text input field is focused.
119 bool IsInputFieldFocused();
121 // All pending key events. Note: we do not own these object, we just save
122 // pointers to these object so that we can abandon them when necessary.
123 // They will be deleted in ProcessKeyEventDone().
124 std::set
<uint32
> pending_key_events_
;
126 // Pending composition text generated by the current pending key event.
127 // It'll be sent to the focused text input client as soon as we receive the
128 // processing result of the pending key event.
129 CompositionText composition_
;
131 // Pending result text generated by the current pending key event.
132 // It'll be sent to the focused text input client as soon as we receive the
133 // processing result of the pending key event.
134 base::string16 result_text_
;
136 base::string16 previous_surrounding_text_
;
137 gfx::Range previous_selection_range_
;
139 // Indicates if there is an ongoing composition text.
140 bool composing_text_
;
142 // Indicates if the composition text is changed or deleted.
143 bool composition_changed_
;
145 // The latest id of key event.
146 uint32 current_keyevent_id_
;
148 // An object to compose a character from a sequence of key presses
149 // including dead key etc.
150 CharacterComposer character_composer_
;
152 // Used for making callbacks.
153 base::WeakPtrFactory
<InputMethodChromeOS
> weak_ptr_factory_
;
155 DISALLOW_COPY_AND_ASSIGN(InputMethodChromeOS
);
160 #endif // UI_BASE_IME_INPUT_METHOD_CHROMEOS_H_