Skip to content

Commit

Permalink
respect engine's switch behavior on focus out (#206)
Browse files Browse the repository at this point in the history
  • Loading branch information
eagleoflqj authored Nov 28, 2024
1 parent 45060dc commit 209d290
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 13 deletions.
2 changes: 1 addition & 1 deletion fcitx5
2 changes: 1 addition & 1 deletion macosfrontend/macosfrontend-public.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,4 @@ uint16_t fcitx_string_to_osx_keycode(const char *) noexcept;
ICUUID create_input_context(const char *appId, id client) noexcept;
void destroy_input_context(ICUUID uuid) noexcept;
void focus_in(ICUUID uuid) noexcept;
void focus_out(ICUUID uuid) noexcept;
std::string focus_out(ICUUID uuid) noexcept;
26 changes: 22 additions & 4 deletions macosfrontend/macosfrontend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include <fcitx-utils/event.h>
#include <fcitx/addonmanager.h>
#include <fcitx/inputcontext.h>
#include <fcitx/inputmethodengine.h>
#include <fcitx/inputpanel.h>
#include <nlohmann/json.hpp>

Expand Down Expand Up @@ -174,12 +175,28 @@ void MacosFrontend::focusIn(ICUUID uuid) {
useAppDefaultIM(program);
}

void MacosFrontend::focusOut(ICUUID uuid) {
std::string MacosFrontend::focusOut(ICUUID uuid) {
auto *ic = findIC(uuid);
if (!ic)
return;
return "{}";

// Fake a switch input method event to call engine's deactivate method and
// maybe commit and clear preedit synchronously.
ic->isSyncEvent = true;
InputContextEvent event(ic, EventType::InputContextSwitchInputMethod);
auto engine = instance_->inputMethodEngine(ic);
auto entry = instance_->inputMethodEntry(ic);
engine->deactivate(*entry, event);
// At this stage panel is still shown. If removed, a following backspace
// will commit a BS character in VSCode.
ic->setDummyPreedit(false);
auto state = ic->getState(false);
ic->isSyncEvent = false;

FCITX_INFO() << "Focus out " << ic->program();
ic->focusOut();

return state;
}

MacosInputContext::MacosInputContext(MacosFrontend *frontend,
Expand Down Expand Up @@ -306,6 +323,7 @@ void focus_in(ICUUID uuid) noexcept {
with_fcitx([=](Fcitx &fcitx) { return fcitx.frontend()->focusIn(uuid); });
}

void focus_out(ICUUID uuid) noexcept {
with_fcitx([=](Fcitx &fcitx) { return fcitx.frontend()->focusOut(uuid); });
std::string focus_out(ICUUID uuid) noexcept {
return with_fcitx(
[=](Fcitx &fcitx) { return fcitx.frontend()->focusOut(uuid); });
}
2 changes: 1 addition & 1 deletion macosfrontend/macosfrontend.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ class MacosFrontend : public AddonInstance {
void destroyInputContext(ICUUID);
std::string keyEvent(ICUUID, const Key &key, bool isRelease);
void focusIn(ICUUID);
void focusOut(ICUUID);
std::string focusOut(ICUUID);

private:
Instance *instance_;
Expand Down
21 changes: 15 additions & 6 deletions src/controller.swift
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,7 @@ class FcitxInputController: IMKInputController {
uuid = create_input_context(appId, client)
}

func processKey(_ unicode: UInt32, _ modsVal: UInt32, _ code: UInt16, _ isRelease: Bool) -> Bool {
guard let client = client as? IMKTextInput else {
return false
}
let res = String(process_key(uuid, unicode, modsVal, code, isRelease))
func processRes(_ client: IMKTextInput, _ res: String) -> Bool {
do {
if let data = res.data(using: .utf8) {
let json = try JSON(data: data)
Expand All @@ -66,6 +62,14 @@ class FcitxInputController: IMKInputController {
return false
}

func processKey(_ unicode: UInt32, _ modsVal: UInt32, _ code: UInt16, _ isRelease: Bool) -> Bool {
guard let client = client as? IMKTextInput else {
return false
}
let res = String(process_key(uuid, unicode, modsVal, code, isRelease))
return processRes(client, res)
}

// Default behavior is to recognize keyDown only
override func recognizedEvents(_ sender: Any!) -> Int {
let events: NSEvent.EventTypeMask = [.keyDown, .flagsChanged]
Expand Down Expand Up @@ -130,7 +134,12 @@ class FcitxInputController: IMKInputController {
}

override func deactivateServer(_ client: Any!) {
focus_out(uuid)
guard let client = client as? IMKTextInput else {
return
}
let res = String(focus_out(uuid))
// Maybe commit and clear preedit synchronously if user switches to ABC by Ctrl+Space.
let _ = processRes(client, res)
}

override func menu() -> NSMenu! {
Expand Down

0 comments on commit 209d290

Please sign in to comment.