Skip to content

Commit 4ab5870

Browse files
committed
Reduce the requirements of unnecessary key event forwarding.
In a previous fix of keyboard engine, update preedit to empty is commonly done to ensure there is nothing in the preedit. This introduces some empty update preedit for keyboard engine and uses the code path for key event order fix. This makes almost all key event need to be re-forwarded, which is unnecssary. Add a new helper function to check if the strict order matters: 1. all pending events are update preedit. 2. preedit is empty. If above condition satisfies and we don't need to need forward the key, allow it to be handled out of order. Attempt Fix #702
1 parent df632d7 commit 4ab5870

File tree

3 files changed

+57
-3
lines changed

3 files changed

+57
-3
lines changed

src/lib/fcitx/inputcontext.cpp

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,27 @@ bool InputContext::hasPendingEvents() const {
329329
return !d->blockedEvents_.empty();
330330
}
331331

332+
bool InputContext::hasPendingEventsStrictOrder() const {
333+
FCITX_D();
334+
if (d->blockedEvents_.empty()) {
335+
return false;
336+
}
337+
338+
// Check we only have update preedit.
339+
if (std::any_of(d->blockedEvents_.begin(), d->blockedEvents_.end(),
340+
[](const auto &event) {
341+
return event->type() !=
342+
EventType::InputContextUpdatePreedit;
343+
})) {
344+
return false;
345+
}
346+
347+
// Check whether the preedit is non-empty.
348+
// If key event may produce anything, it still may trigger the clear
349+
// preedit. In that case, preedit order does matter.
350+
return !inputPanel().clientPreedit().toString().empty();
351+
}
352+
332353
void InputContext::commitString(const std::string &text) {
333354
FCITX_D();
334355
if (auto *instance = d->manager_.instance()) {
@@ -367,11 +388,21 @@ InputPanel &InputContext::inputPanel() {
367388
return d->inputPanel_;
368389
}
369390

391+
const InputPanel &InputContext::inputPanel() const {
392+
FCITX_D();
393+
return d->inputPanel_;
394+
}
395+
370396
StatusArea &InputContext::statusArea() {
371397
FCITX_D();
372398
return d->statusArea_;
373399
}
374400

401+
const StatusArea &InputContext::statusArea() const {
402+
FCITX_D();
403+
return d->statusArea_;
404+
}
405+
375406
void InputContext::updateClientSideUIImpl() {}
376407

377408
InputContextEventBlocker::InputContextEventBlocker(InputContext *inputContext)

src/lib/fcitx/inputcontext.h

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,16 @@ class FCITXCORE_EXPORT InputContext : public TrackableObject<InputContext> {
207207
void setBlockEventToClient(bool block);
208208
bool hasPendingEvents() const;
209209

210+
/**
211+
* Has pending event that need to use key order fix.
212+
*
213+
* If pending event only have preedit, then it's generally fine to not use
214+
* key forward.
215+
*
216+
* @since 5.0.21
217+
*/
218+
bool hasPendingEventsStrictOrder() const;
219+
210220
/// Returns the input context property by name.
211221
InputContextProperty *property(const std::string &name);
212222

@@ -216,9 +226,23 @@ class FCITXCORE_EXPORT InputContext : public TrackableObject<InputContext> {
216226
/// Returns the associated input panel.
217227
InputPanel &inputPanel();
218228

229+
/**
230+
* Returns the associated input panel.
231+
*
232+
* @since 5.0.22
233+
*/
234+
const InputPanel &inputPanel() const;
235+
219236
/// Returns the associated StatusArea.
220237
StatusArea &statusArea();
221238

239+
/**
240+
* Returns the associated StatusArea.
241+
*
242+
* @since 5.0.22
243+
*/
244+
const StatusArea &statusArea() const;
245+
222246
/**
223247
* Helper function to return the input context property in specific type.
224248
*

src/lib/fcitx/instance.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1443,9 +1443,8 @@ bool Instance::postEvent(Event &event) const {
14431443
: XKB_KEY_DOWN);
14441444
} while (0);
14451445
#endif
1446-
if (ic->hasPendingEvents() &&
1447-
ic->capabilityFlags().test(CapabilityFlag::KeyEventOrderFix) &&
1448-
!keyEvent.accepted()) {
1446+
if (ic->capabilityFlags().test(CapabilityFlag::KeyEventOrderFix) &&
1447+
!keyEvent.accepted() && ic->hasPendingEventsStrictOrder()) {
14491448
// Re-forward the event to ensure we got delivered later than
14501449
// commit.
14511450
keyEvent.filterAndAccept();

0 commit comments

Comments
 (0)