Skip to content

Commit

Permalink
Add support for compose string (#58)
Browse files Browse the repository at this point in the history
Fix #54
  • Loading branch information
wengxt authored May 29, 2024
1 parent 1cbeda5 commit 64a4c4a
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 17 deletions.
21 changes: 21 additions & 0 deletions src/unix/fcitx5/fcitx_key_event_handler.cc
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,15 @@

#include "unix/fcitx5/fcitx_key_event_handler.h"

#include <fcitx-utils/charutils.h>
#include <fcitx-utils/key.h>
#include <fcitx-utils/utf8.h>

#include <cstdint>
#include <map>
#include <memory>
#include <set>
#include <string>

#include "absl/log/check.h"
#include "absl/log/log.h"
Expand Down Expand Up @@ -124,6 +127,24 @@ bool KeyEventHandler::GetKeyEvent(
return ProcessModifiers(is_key_up, keyval, key);
}

bool KeyEventHandler::GetKeyEvent(
const std::string &composeString,
mozc::config::Config::PreeditMethod preedit_method, bool layout_is_jp,
mozc::commands::KeyEvent *key) {
key->Clear();
auto length = utf8::length(composeString);
if (length == 1) {
auto chr = utf8::getChar(composeString);
// For ascii key & yen, use the regular key event conversion.
if ((chr >= 0x20 && chr <= 0x7e) || chr == 0xa5) {
return GetKeyEvent(static_cast<KeySym>(chr), 0, KeyStates(),
preedit_method, layout_is_jp, false, key);
}
}
key->set_key_string(composeString);
return true;
}

void KeyEventHandler::Clear() {
is_non_modifier_key_pressed_ = false;
currently_pressed_modifiers_.clear();
Expand Down
4 changes: 4 additions & 0 deletions src/unix/fcitx5/fcitx_key_event_handler.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ class KeyEventHandler {
bool layout_is_jp, bool is_key_up,
mozc::commands::KeyEvent *key);

bool GetKeyEvent(const std::string &composeString,
mozc::config::Config::PreeditMethod preedit_method,
bool layout_is_jp, mozc::commands::KeyEvent *key);

// Clears states.
void Clear();

Expand Down
55 changes: 42 additions & 13 deletions src/unix/fcitx5/mozc_state.cc
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
#include <fcitx-utils/keysym.h>
#include <fcitx-utils/log.h>
#include <fcitx-utils/stringutils.h>
#include <fcitx-utils/utf8.h>
#include <fcitx/candidatelist.h>
#include <fcitx/event.h>
#include <fcitx/inputpanel.h>
Expand All @@ -42,6 +43,7 @@

#include <cstdint>
#include <memory>
#include <optional>
#include <string>
#include <utility>

Expand Down Expand Up @@ -109,10 +111,10 @@ void MozcState::UpdatePreeditMethod() {
}
}

bool MozcState::TrySendKeyEvent(
InputContext* ic, KeySym sym, uint32_t keycode, KeyStates state,
mozc::commands::CompositionMode composition_mode, bool layout_is_jp,
bool is_key_up, mozc::commands::Output* out, std::string* out_error) const {
bool MozcState::TrySendKeyEvent(InputContext* ic,
const mozc::commands::KeyEvent& event,
mozc::commands::Output* out,
std::string* out_error) const {
DCHECK(out);
DCHECK(out_error);

Expand All @@ -125,13 +127,7 @@ bool MozcState::TrySendKeyEvent(
return false;
}

mozc::commands::KeyEvent event;
if (!handler_->GetKeyEvent(sym, keycode, state, preedit_method_, layout_is_jp,
is_key_up, &event)) {
return false;
}

if ((composition_mode == mozc::commands::DIRECT) &&
if ((composition_mode_ == mozc::commands::DIRECT) &&
!client->IsDirectModeCommand(event)) {
MOZC_VLOG(1) << "In DIRECT mode. Not consumed.";
return false; // not consumed.
Expand Down Expand Up @@ -232,11 +228,42 @@ bool MozcState::ProcessKeyEvent(KeySym sym, uint32_t keycode, KeyStates state,
}
}

mozc::commands::KeyEvent event;
std::optional<std::string> compose;
do {
if (!is_key_up &&
!state.testAny(KeyStates{KeyState::Ctrl, KeyState::Super})) {
compose = engine_->instance()->processComposeString(ic_, sym);
if (!compose) {
return true;
}
if (!compose->empty()) {
auto length = utf8::lengthValidated(*compose);
if (length == utf8::INVALID_LENGTH) {
return true;
}
if (!handler_->GetKeyEvent(*compose, preedit_method_, layout_is_jp,
&event)) {
return false;
}
break;
}
}
if (!handler_->GetKeyEvent(sym, keycode, state, preedit_method_,
layout_is_jp, is_key_up, &event)) {
return false;
}
} while (false);

std::string error;
mozc::commands::Output raw_response;
if (!TrySendKeyEvent(ic_, sym, keycode, state, composition_mode_,
layout_is_jp, is_key_up, &raw_response, &error)) {
if (!TrySendKeyEvent(ic_, event, &raw_response, &error)) {
// TODO(yusukes): Show |error|.
if (compose && !compose->empty()) {
ic_->commitString(*compose);
Reset();
return true;
}
return false; // not consumed.
}

Expand Down Expand Up @@ -274,6 +301,7 @@ void MozcState::Reset() {
}
ClearAll(); // just in case.
DrawAll();
engine_->instance()->resetCompose(ic_);
}

bool MozcState::Paging(bool prev) {
Expand Down Expand Up @@ -314,6 +342,7 @@ void MozcState::FocusOut(const InputContextEvent& event) {
}
ClearAll(); // just in case.
DrawAll();
engine_->instance()->resetCompose(ic_);
}

bool MozcState::ParseResponse(const mozc::commands::Output& raw_response) {
Expand Down
5 changes: 1 addition & 4 deletions src/unix/fcitx5/mozc_state.h
Original file line number Diff line number Diff line change
Expand Up @@ -112,10 +112,7 @@ class MozcState : public InputContextProperty {
// response is stored on 'out' (and 'out_error' is not modified). If the IPC
// fails, returns false and the error message is stored on 'out_error'. In
// this case, 'out' is not modified.
bool TrySendKeyEvent(InputContext *ic, KeySym sym, uint32_t keycode,
KeyStates state,
mozc::commands::CompositionMode composition_mode,
bool layout_is_jp, bool is_key_up,
bool TrySendKeyEvent(InputContext *ic, const mozc::commands::KeyEvent &event,
mozc::commands::Output *out,
std::string *out_error) const;

Expand Down

0 comments on commit 64a4c4a

Please sign in to comment.